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