]> git.mxchange.org Git - friendica.git/blob - view/theme/frost-mobile/js/main.js
Merge pull request #1 from friendica/develop
[friendica.git] / view / theme / frost-mobile / js / main.js
1
2         function openClose(listID) {
3 /*              if(document.getElementById(theID).style.display == "block") {
4                         document.getElementById(theID).style.display = "none"
5                 }
6                 else {
7                         document.getElementById(theID).style.display = "block"
8                 }*/
9                 listID = "#" + listID.replace(/:/g, "\\:");
10                 listID = listID.replace(/\./g, "\\.");
11                 listID = listID.replace(/@/g, "\\@");
12
13                 if($(listID).is(":visible")) {
14                         $(listID).hide();
15                         $(listID+"-wrapper").show();
16                 }
17                 else {
18                         $(listID).show();
19                         $(listID+"-wrapper").hide();
20                 }
21         }
22
23         function openMenu(theID) {
24                 document.getElementById(theID).style.display = "block"
25         }
26
27         function closeMenu(theID) {
28                 document.getElementById(theID).style.display = "none"
29         }
30
31
32
33         var src = null;
34         var prev = null;
35         var livetime = null;
36         var stopped = false;
37         var totStopped = false;
38         var timer = null;
39         var pr = 0;
40         var liking = 0;
41         var in_progress = false;
42         var langSelect = false;
43         var commentBusy = false;
44         var last_popup_menu = null;
45         var last_popup_button = null;
46
47         $(function() {
48                 $.ajaxSetup({cache: false});
49
50                 //collapseHeight();
51
52                 /* setup tooltips *//*
53                 $("a,.tt").each(function(){
54                         var e = $(this);
55                         var pos="bottom";
56                         if (e.hasClass("tttop")) pos="top";
57                         if (e.hasClass("ttbottom")) pos="bottom";
58                         if (e.hasClass("ttleft")) pos="left";
59                         if (e.hasClass("ttright")) pos="right";
60                         e.tipTip({defaultPosition: pos, edgeOffset: 8});
61                 });*/
62
63
64
65                 /* setup onoff widgets */
66                 $(".onoff input").each(function(){
67                         val = $(this).val();
68                         id = $(this).attr("id");
69                         $("#"+id+"_onoff ."+ (val==0?"on":"off")).addClass("hidden");
70
71                 });
72                 $(".onoff > a").click(function(event){
73                         event.preventDefault();
74                         var input = $(this).siblings("input");
75                         var val = 1-input.val();
76                         var id = input.attr("id");
77                         $("#"+id+"_onoff ."+ (val==0?"on":"off")).addClass("hidden");
78                         $("#"+id+"_onoff ."+ (val==1?"on":"off")).removeClass("hidden");
79                         input.val(val);
80                 });
81
82                 /* popup menus */
83                 function close_last_popup_menu(e) {
84
85                         if( last_popup_menu ) {
86                                 if( '#' + last_popup_menu.attr('id') !== $(e.target).attr('rel')) {
87                                         last_popup_menu.hide();
88                                         if (last_popup_menu.attr('id') == "nav-notifications-menu" ) $('.main-container').show();
89                                         last_popup_button.removeClass("selected");
90                                         last_popup_menu = null;
91                                         last_popup_button = null;
92                                 }
93                         }
94                 }
95                 $('img[rel^=#]').click(function(e){
96
97                         close_last_popup_menu(e);
98                         menu = $( $(this).attr('rel') );
99                         e.preventDefault();
100                         e.stopPropagation();
101
102                         if (menu.attr('popup')=="false") return false;
103
104 //                      $(this).parent().toggleClass("selected");
105 //                      menu.toggle();
106
107                         if (menu.css("display") == "none") {
108                                 $(this).parent().addClass("selected");
109                                 menu.show();
110                                 if (menu.attr('id') == "nav-notifications-menu" ) $('.main-container').hide();
111                                 last_popup_menu = menu;
112                                 last_popup_button = $(this).parent();
113                         } else {
114                                 $(this).parent().removeClass("selected");
115                                 menu.hide();
116                                 if (menu.attr('id') == "nav-notifications-menu" ) $('.main-container').show();
117                                 last_popup_menu = null;
118                                 last_popup_button = null;
119                         }
120                         return false;
121                 });
122                 $('html').click(function(e) {
123                         close_last_popup_menu(e);
124                 });
125
126                 // fancyboxes
127                 /*$("a.popupbox").colorbox({
128                         'inline' : true,
129                         'transition' : 'none'
130                 });*/
131
132
133                 /* notifications template */
134                 var notifications_tpl= unescape($("#nav-notifications-template[rel=template]").html());
135                 var notifications_all = unescape($('<div>').append( $("#nav-notifications-see-all").clone() ).html()); //outerHtml hack
136                 var notifications_mark = unescape($('<div>').append( $("#nav-notifications-mark-all").clone() ).html()); //outerHtml hack
137                 var notifications_empty = unescape($("#nav-notifications-menu").html());
138
139                 /* nav update event  */
140                 $('nav').bind('nav-update', function(e,data){;
141                         var invalid = $(data).find('invalid').text();
142                         if(invalid == 1) { window.location.href=window.location.href }
143
144                         var net = $(data).find('net').text();
145                         if(net == 0) { net = ''; $('#net-update').removeClass('show') } else { $('#net-update').addClass('show') }
146                         $('#net-update').html(net);
147
148                         var home = $(data).find('home').text();
149                         if(home == 0) { home = '';  $('#home-update').removeClass('show') } else { $('#home-update').addClass('show') }
150                         $('#home-update').html(home);
151
152                         var intro = $(data).find('intro').text();
153                         if(intro == 0) { intro = '';  $('#intro-update').removeClass('show') } else { $('#intro-update').addClass('show') }
154                         $('#intro-update').html(intro);
155
156                         var mail = $(data).find('mail').text();
157                         if(mail == 0) { mail = '';  $('#mail-update').removeClass('show') } else { $('#mail-update').addClass('show') }
158                         $('#mail-update').html(mail);
159
160                         var intro = $(data).find('intro').text();
161                         if(intro == 0) { intro = '';  $('#intro-update-li').removeClass('show') } else { $('#intro-update-li').addClass('show') }
162                         $('#intro-update-li').html(intro);
163
164                         var mail = $(data).find('mail').text();
165                         if(mail == 0) { mail = '';  $('#mail-update-li').removeClass('show') } else { $('#mail-update-li').addClass('show') }
166                         $('#mail-update-li').html(mail);
167
168                         var eNotif = $(data).find('notif')
169
170                         if (eNotif.children("note").length==0){
171                                 $("#nav-notifications-menu").html(notifications_empty);
172                         } else {
173                                 nnm = $("#nav-notifications-menu");
174                                 nnm.html(notifications_all + notifications_mark);
175                                 //nnm.attr('popup','true');
176                                 eNotif.children("note").each(function(){
177                                         e = $(this);
178                                         text = e.text().format("<span class='contactname'>"+e.attr('name')+"</span>");
179                                         html = notifications_tpl.format(e.attr('href'),e.attr('photo'), text, e.attr('date'), e.attr('seen'));
180                                         nnm.append(html);
181                                 });
182
183                                 $("img[data-src]", nnm).each(function(i, el){
184                                         // Add src attribute for images with a data-src attribute
185                                         // However, don't bother if the data-src attribute is empty, because
186                                         // an empty "src" tag for an image will cause some browsers
187                                         // to prefetch the root page of the Friendica hub, which will
188                                         // unnecessarily load an entire profile/ or network/ page
189                                         if($(el).data("src") != '') $(el).attr('src', $(el).data("src"));
190                                 });
191                         }
192                         notif = eNotif.attr('count');
193                         if (notif>0){
194                                 $("#nav-notifications-linkmenu").addClass("on");
195                         } else {
196                                 $("#nav-notifications-linkmenu").removeClass("on");
197                         }
198                         if(notif == 0) { notif = ''; $('#notify-update').removeClass('show') } else { $('#notify-update').addClass('show') }
199                         $('#notify-update').html(notif);
200
201                         var eSysmsg = $(data).find('sysmsgs');
202                         eSysmsg.children("notice").each(function(){
203                                 text = $(this).text();
204                                 $.jGrowl(text, { sticky: false, theme: 'notice', life: 1000 });
205                         });
206                         eSysmsg.children("info").each(function(){
207                                 text = $(this).text();
208                                 $.jGrowl(text, { sticky: false, theme: 'info', life: 1000 });
209                         });
210
211                 });
212
213
214                 NavUpdate();
215                 // Allow folks to stop the ajax page updates with the pause/break key
216 /*              $(document).keydown(function(event) {
217                         if(event.keyCode == '8') {
218                                 var target = event.target || event.srcElement;
219                                 if (!/input|textarea/i.test(target.nodeName)) {
220                                         return false;
221                                 }
222                         }
223                         if(event.keyCode == '19' || (event.ctrlKey && event.which == '32')) {
224                                 event.preventDefault();
225                                 if(stopped == false) {
226                                         stopped = true;
227                                         if (event.ctrlKey) {
228                                                 totStopped = true;
229                                         }
230                                         $('#pause').html('<img src="images/pause.gif" alt="pause" style="border: 1px solid black;" />');
231                                 } else {
232                                         unpause();
233                                 }
234                         } else {
235                                 if (!totStopped) {
236                                         unpause();
237                                 }
238                         }
239                 });*/
240
241
242         });
243
244         function NavUpdate() {
245
246                 if(! stopped) {
247                         var pingCmd = 'ping' + ((localUser != 0) ? '?f=&uid=' + localUser : '');
248                         $.get(pingCmd,function(data) {
249                                 $(data).find('result').each(function() {
250                                         // send nav-update event
251                                         $('nav').trigger('nav-update', this);
252
253
254                                         // start live update
255
256
257
258                                         if($('#live-network').length)   { src = 'network'; liveUpdate(); }
259                                         if($('#live-profile').length)   { src = 'profile'; liveUpdate(); }
260                                         if($('#live-community').length) { src = 'community'; liveUpdate(); }
261                                         if($('#live-notes').length)     { src = 'notes'; liveUpdate(); }
262                                         if($('#live-display').length) { src = 'display'; liveUpdate(); }
263                                         /*if($('#live-display').length) {
264                                                 if(liking) {
265                                                         liking = 0;
266                                                         window.location.href=window.location.href
267                                                 }
268                                         }*/
269                                         if($('#live-photos').length) {
270                                                 if(liking) {
271                                                         liking = 0;
272                                                         window.location.href=window.location.href
273                                                 }
274                                         }
275
276
277
278
279                                 });
280                         }) ;
281                 }
282                 timer = setTimeout(NavUpdate,updateInterval);
283         }
284
285         function liveUpdate() {
286                 if((src == null) || (stopped) || (typeof profile_uid == 'undefined') || (! profile_uid)) { $('.like-rotator').hide(); return; }
287                 if(($('.comment-edit-text-full').length) || (in_progress)) {
288                         if(livetime) {
289                                 clearTimeout(livetime);
290                         }
291                         livetime = setTimeout(liveUpdate, 5000);
292                         return;
293                 }
294                 if(livetime != null)
295                         livetime = null;
296
297                 prev = 'live-' + src;
298
299                 in_progress = true;
300                 var udargs = ((netargs.length) ? '/' + netargs : '');
301                 var update_url = 'update_' + src + udargs + '&p=' + profile_uid + '&page=' + profile_page;
302
303                 $.get(update_url,function(data) {
304                         in_progress = false;
305                         //                      $('.collapsed-comments',data).each(function() {
306                         //      var ident = $(this).attr('id');
307                         //      var is_hidden = $('#' + ident).is(':hidden');
308                         //      if($('#' + ident).length) {
309                         //              $('#' + ident).replaceWith($(this));
310                         //              if(is_hidden)
311                         //                      $('#' + ident).hide();
312                         //      }
313                         //});
314
315                         // add a new thread
316
317                         $('.toplevel_item',data).each(function() {
318                                 var ident = $(this).attr('id');
319
320                                 if($('#' + ident).length == 0 && profile_page == 1) {
321                                         $('img',this).each(function() {
322                                                 $(this).attr('src',$(this).attr('dst'));
323                                         });
324                                         $('#' + prev).after($(this));
325                                 }
326                                 else {
327                                         // Find out if the hidden comments are open, so we can keep it that way
328                                         // if a new comment has been posted
329                                         var id = $('.hide-comments-total', this).attr('id');
330                                         if(typeof id != 'undefined') {
331                                                 id = id.split('-')[3];
332                                                 var commentsOpen = $("#collapsed-comments-" + id).is(":visible");
333                                         }
334
335                                         $('img',this).each(function() {
336                                                 $(this).attr('src',$(this).attr('dst'));
337                                         });
338                                         //vScroll = $(document).scrollTop();
339                                         $('html').height($('html').height());
340                                         $('#' + ident).replaceWith($(this));
341
342                                         if(typeof id != 'undefined') {
343                                                 if(commentsOpen) showHideComments(id);
344                                         }
345                                         $('html').height('auto');
346                                         //$(document).scrollTop(vScroll);
347                                 }
348                                 prev = ident;
349                         });
350
351
352                         collapseHeight();
353
354                         // reset vars for inserting individual items
355
356                         /*prev = 'live-' + src;
357
358                         $('.wall-item-outside-wrapper',data).each(function() {
359                                 var ident = $(this).attr('id');
360
361                                 if($('#' + ident).length == 0 && prev != 'live-' + src) {
362                                                 $('img',this).each(function() {
363                                                         $(this).attr('src',$(this).attr('dst'));
364                                                 });
365                                                 $('#' + prev).after($(this));
366                                 }
367                                 else {
368                                         $('#' + ident + ' ' + '.wall-item-ago').replaceWith($(this).find('.wall-item-ago'));
369                                         if($('#' + ident + ' ' + '.comment-edit-text-empty').length)
370                                                 $('#' + ident + ' ' + '.wall-item-comment-wrapper').replaceWith($(this).find('.wall-item-comment-wrapper'));
371                                         $('#' + ident + ' ' + '.hide-comments-total').replaceWith($(this).find('.hide-comments-total'));
372                                         $('#' + ident + ' ' + '.wall-item-like').replaceWith($(this).find('.wall-item-like'));
373                                         $('#' + ident + ' ' + '.wall-item-dislike').replaceWith($(this).find('.wall-item-dislike'));
374                                         $('#' + ident + ' ' + '.my-comment-photo').each(function() {
375                                                 $(this).attr('src',$(this).attr('dst'));
376                                         });
377                                 }
378                                 prev = ident;
379                         });*/
380
381                         $('.like-rotator').hide();
382                         if(commentBusy) {
383                                 commentBusy = false;
384                                 $('body').css('cursor', 'auto');
385                         }
386                         /* autocomplete @nicknames */
387                         $(".comment-edit-form  textarea").editor_autocomplete(baseurl+"/acl");
388
389                         // setup videos, since VideoJS won't take care of any loaded via AJAX
390                         if(typeof videojs != 'undefined') videojs.autoSetup();
391                 });
392         }
393
394         function collapseHeight(elems) {
395                 var elemName = '.wall-item-body:not(.divmore)';
396                 if(typeof elems != 'undefined') {
397                         elemName = elems + ' ' + elemName;
398                 }
399                 $(elemName).each(function() {
400                         $('html').height($('html').height());
401                         $(this).readmore({maxheight:350 , moreLink:'<a href="#" class="showmore">+ Show more</a>', lessLink:'<a href="#" class="showmore">+ Show less</a>'});
402                         $(this).addClass('divmore');
403                         $('html').height('auto');
404                 });
405         }
406
407 /*      function imgbright(node) {
408                 $(node).removeClass("drophide").addClass("drop");
409         }
410
411         function imgdull(node) {
412                 $(node).removeClass("drop").addClass("drophide");
413         }*/
414
415         // Since our ajax calls are asynchronous, we will give a few
416         // seconds for the first ajax call (setting like/dislike), then
417         // run the updater to pick up any changes and display on the page.
418         // The updater will turn any rotators off when it's done.
419         // This function will have returned long before any of these
420         // events have completed and therefore there won't be any
421         // visible feedback that anything changed without all this
422         // trickery. This still could cause confusion if the "like" ajax call
423         // is delayed and NavUpdate runs before it completes.
424
425         function dolike(ident,verb) {
426                 unpause();
427                 $('#like-rotator-' + ident.toString()).show();
428                 $.get('like/' + ident.toString() + '?verb=' + verb, NavUpdate );
429 //              if(timer) clearTimeout(timer);
430 //              timer = setTimeout(NavUpdate,3000);
431                 liking = 1;
432         }
433
434         function dostar(ident) {
435                 ident = ident.toString();
436                 //$('#like-rotator-' + ident).show();
437                 $.get('starred/' + ident, function(data) {
438                         if(data.match(/1/)) {
439                                 $('#starred-' + ident).addClass('starred');
440                                 $('#starred-' + ident).removeClass('unstarred');
441                                 $('#star-' + ident).addClass('hidden');
442                                 $('#unstar-' + ident).removeClass('hidden');
443                         }
444                         else {
445                                 $('#starred-' + ident).addClass('unstarred');
446                                 $('#starred-' + ident).removeClass('starred');
447                                 $('#star-' + ident).removeClass('hidden');
448                                 $('#unstar-' + ident).addClass('hidden');
449                         }
450                         //$('#like-rotator-' + ident).hide();
451                 });
452         }
453
454         function getPosition(e) {
455                 var cursor = {x:0, y:0};
456                 if ( e.pageX || e.pageY  ) {
457                         cursor.x = e.pageX;
458                         cursor.y = e.pageY;
459                 }
460                 else {
461                         if( e.clientX || e.clientY ) {
462                                 cursor.x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
463                                 cursor.y = e.clientY + (document.documentElement.scrollTop  || document.body.scrollTop)  - document.documentElement.clientTop;
464                         }
465                         else {
466                                 if( e.x || e.y ) {
467                                         cursor.x = e.x;
468                                         cursor.y = e.y;
469                                 }
470                         }
471                 }
472                 return cursor;
473         }
474
475         var lockvisible = false;
476
477         function lockview(event,id) {
478                 event = event || window.event;
479                 cursor = getPosition(event);
480                 if(lockvisible) {
481                         lockviewhide();
482                 }
483                 else {
484                         lockvisible = true;
485                         $.get('lockview/' + id, function(data) {
486                                 $('#panel').html(data);
487                                 $('#panel').css({ 'left': 10 , 'top': cursor.y + 20});
488                                 $('#panel').show();
489                         });
490                 }
491         }
492
493         function lockviewhide() {
494                 lockvisible = false;
495                 $('#panel').hide();
496         }
497
498         function post_comment(id) {
499                 unpause();
500                 commentBusy = true;
501                 $('body').css('cursor', 'wait');
502                 $("#comment-preview-inp-" + id).val("0");
503                 $.post(
504              "item",
505              $("#comment-edit-form-" + id).serialize(),
506                         function(data) {
507                                 if(data.success) {
508                                         $("#comment-edit-wrapper-" + id).hide();
509                                         $("#comment-edit-text-" + id).val('');
510                                 var tarea = document.getElementById("comment-edit-text-" + id);
511                                         if(tarea)
512                                                 commentClose(tarea,id);
513                                         if(timer) clearTimeout(timer);
514                                         timer = setTimeout(NavUpdate,10);
515                                 }
516                                 if(data.reload) {
517                                         window.location.href=data.reload;
518                                 }
519                         },
520                         "json"
521          );
522          return false;
523         }
524
525
526         function preview_comment(id) {
527                 $("#comment-preview-inp-" + id).val("1");
528                 $("#comment-edit-preview-" + id).show();
529                 $.post(
530              "item",
531              $("#comment-edit-form-" + id).serialize(),
532                         function(data) {
533                                 if(data.preview) {
534
535                                         $("#comment-edit-preview-" + id).html(data.preview);
536                                         $("#comment-edit-preview-" + id + " a").click(function() { return false; });
537                                 }
538                         },
539                         "json"
540          );
541          return true;
542         }
543
544
545         function showHideComments(id) {
546                 if( $("#collapsed-comments-" + id).is(":visible")) {
547                         $("#collapsed-comments-" + id).hide();
548                         $("#hide-comments-" + id).html(window.showMore);
549                 }
550                 else {
551                         $("#collapsed-comments-" + id).show();
552                         $("#hide-comments-" + id).html(window.showFewer);
553                         collapseHeight("#collapsed-comments-" + id);
554                 }
555         }
556
557
558         function preview_post() {
559                 $("#jot-preview").val("1");
560                 $("#jot-preview-content").show();
561                 $.post(
562                         "item",
563                         $("#profile-jot-form").serialize(),
564                         function(data) {
565                                 if(data.preview) {
566                                         $("#jot-preview-content").html(data.preview);
567                                         $("#jot-preview-content" + " a").click(function() { return false; });
568                                 }
569                         },
570                         "json"
571                 );
572                 $("#jot-preview").val("0");
573                 return true;
574         }
575
576
577         function unpause() {
578                 // unpause auto reloads if they are currently stopped
579                 totStopped = false;
580                 stopped = false;
581             $('#pause').html('');
582         }
583
584
585     function bin2hex(s){
586         // Converts the binary representation of data to hex
587         //
588         // version: 812.316
589         // discuss at: http://phpjs.org/functions/bin2hex
590         // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
591         // +   bugfixed by: Onno Marsman
592         // +   bugfixed by: Linuxworld
593         // *     example 1: bin2hex('Kev');
594         // *     returns 1: '4b6576'
595         // *     example 2: bin2hex(String.fromCharCode(0x00));
596         // *     returns 2: '00'
597         var v,i, f = 0, a = [];
598         s += '';
599         f = s.length;
600
601         for (i = 0; i<f; i++) {
602             a[i] = s.charCodeAt(i).toString(16).replace(/^([\da-f])$/,"0$1");
603         }
604
605         return a.join('');
606     }
607
608         function groupChangeMember(gid, cid, sec_token) {
609                 $('body .fakelink').css('cursor', 'wait');
610                 $.get('group/' + gid + '/' + cid + "?t=" + sec_token, function(data) {
611                                 $('#group-update-wrapper').html(data);
612                                 $('body .fakelink').css('cursor', 'auto');
613                 });
614         }
615
616         function profChangeMember(gid,cid) {
617                 $('body .fakelink').css('cursor', 'wait');
618                 $.get('profperm/' + gid + '/' + cid, function(data) {
619                                 $('#prof-update-wrapper').html(data);
620                                 $('body .fakelink').css('cursor', 'auto');
621                 });
622         }
623
624         function contactgroupChangeMember(gid,cid) {
625                 $('body').css('cursor', 'wait');
626                 $.get('contactgroup/' + gid + '/' + cid, function(data) {
627                                 $('body').css('cursor', 'auto');
628                 });
629         }
630
631
632 function checkboxhighlight(box) {
633   if($(box).is(':checked')) {
634         $(box).addClass('checkeditem');
635   }
636   else {
637         $(box).removeClass('checkeditem');
638   }
639 }
640
641 function notifyMarkAll() {
642         $.get('notify/mark/all', function(data) {
643                 if(timer) clearTimeout(timer);
644                 timer = setTimeout(NavUpdate,1000);
645         });
646 }
647
648 /**
649  * sprintf in javascript
650  *      "{0} and {1}".format('zero','uno');
651  **/
652 String.prototype.format = function() {
653     var formatted = this;
654     for (var i = 0; i < arguments.length; i++) {
655         var regexp = new RegExp('\\{'+i+'\\}', 'gi');
656         formatted = formatted.replace(regexp, arguments[i]);
657     }
658     return formatted;
659 };
660 // Array Remove
661 Array.prototype.remove = function(item) {
662   to=undefined; from=this.indexOf(item);
663   var rest = this.slice((to || from) + 1 || this.length);
664   this.length = from < 0 ? this.length + from : from;
665   return this.push.apply(this, rest);
666 };
667
668 function previewTheme(elm) {
669         theme = $(elm).val();
670         $.getJSON('pretheme?f=&theme=' + theme,function(data) {
671                         $('#theme-preview').html('<div id="theme-desc">' + data.desc + '</div><div id="theme-version">' + data.version + '</div><div id="theme-credits">' + data.credits + '</div>');
672         });
673
674 }