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