]> git.mxchange.org Git - friendica.git/blob - js/main.js
Solved a problem with author links that are different from what is provided in the...
[friendica.git] / js / main.js
1   function resizeIframe(obj) {
2     obj.style.height = 0;
3     obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
4   }
5
6   function openClose(theID) {
7     if(document.getElementById(theID).style.display == "block") {
8       document.getElementById(theID).style.display = "none"
9     }
10     else {
11       document.getElementById(theID).style.display = "block"
12     }
13   }
14
15   function openMenu(theID) {
16       document.getElementById(theID).style.display = "block"
17   }
18
19   function closeMenu(theID) {
20       document.getElementById(theID).style.display = "none"
21   }
22
23
24
25         var src = null;
26         var prev = null;
27         var livetime = null;
28         var force_update = false;
29         var stopped = false;
30         var totStopped = false;
31         var timer = null;
32         var pr = 0;
33         var liking = 0;
34         var in_progress = false;
35         var langSelect = false;
36         var commentBusy = false;
37         var last_popup_menu = null;
38         var last_popup_button = null;
39
40         $(function() {
41                 $.ajaxSetup({cache: false});
42
43                 /* setup tooltips *//*
44                 $("a,.tt").each(function(){
45                         var e = $(this);
46                         var pos="bottom";
47                         if (e.hasClass("tttop")) pos="top";
48                         if (e.hasClass("ttbottom")) pos="bottom";
49                         if (e.hasClass("ttleft")) pos="left";
50                         if (e.hasClass("ttright")) pos="right";
51                         e.tipTip({defaultPosition: pos, edgeOffset: 8});
52                 });*/
53
54                 /* setup comment textarea buttons */
55                 /* comment textarea buttons needs some "data-*" attributes to work:
56                  *              data-role="insert-formatting" : to mark the element as a formatting button
57                  *              data-comment="<string>" : string for "Comment", used by insertFormatting() function
58                  *              data-bbcode="<string>" : name of the bbcode element to insert. insertFormatting() will insert it as "[name][/name]"
59                  *              data-id="<string>" : id of the comment, used to find other comment-related element, like the textarea
60                  * */
61                 $('body').on('click','[data-role="insert-formatting"]', function(e) {
62                         e.preventDefault();
63                         var o = $(this);
64                         var comment = o.data('comment');
65                         var bbcode  = o.data('bbcode');
66                         var id = o.data('id');
67                         if (bbcode=="img") {
68                                 Dialog.doImageBrowser("comment", id);
69                                 return;
70                         }
71                         insertFormatting(comment, bbcode, id);
72                 });
73
74                 /* event from comment textarea button popups */
75                 /* insert returned bbcode at cursor position or replace selected text */
76                 $("body").on("fbrowser.image.comment", function(e, filename, bbcode, id) {
77                         console.log("on", id);
78                         $.colorbox.close();
79                         var textarea = document.getElementById("comment-edit-text-" +id);
80                         var start = textarea.selectionStart;
81                         var end = textarea.selectionEnd;
82                         textarea.value = textarea.value.substring(0, start) + bbcode + textarea.value.substring(end, textarea.value.length);
83                 });
84
85
86
87                 /* setup onoff widgets */
88                 $(".onoff input").each(function(){
89                         val = $(this).val();
90                         id = $(this).attr("id");
91                         $("#"+id+"_onoff ."+ (val==0?"on":"off")).addClass("hidden");
92
93                 });
94                 $(".onoff > a").click(function(event){
95                         event.preventDefault();
96                         var input = $(this).siblings("input");
97                         var val = 1-input.val();
98                         var id = input.attr("id");
99                         $("#"+id+"_onoff ."+ (val==0?"on":"off")).addClass("hidden");
100                         $("#"+id+"_onoff ."+ (val==1?"on":"off")).removeClass("hidden");
101                         input.val(val);
102                         //console.log(id);
103                 });
104
105                 /* setup field_richtext */
106                 setupFieldRichtext();
107
108                 /* popup menus */
109                 function close_last_popup_menu() {
110                         if(last_popup_menu) {
111                                 last_popup_menu.hide();
112                                 last_popup_button.removeClass("selected");
113                                 last_popup_menu = null;
114                                 last_popup_button = null;
115                         }
116                 }
117                 $('a[rel^=#]').click(function(e){
118                         e.preventDefault();
119                         var parent = $(this).parent();
120                         var isSelected = (last_popup_button && parent.attr('id') == last_popup_button.attr('id'));
121                         close_last_popup_menu();
122                         if(isSelected) return false;
123                         menu = $( $(this).attr('rel') );
124                         e.preventDefault();
125                         e.stopPropagation();
126                         if (menu.attr('popup')=="false") return false;
127                         parent.toggleClass("selected");
128                         menu.toggle();
129                         if (menu.css("display") == "none") {
130                                 last_popup_menu = null;
131                                 last_popup_button = null;
132                         } else {
133                                 last_popup_menu = menu;
134                                 last_popup_button = parent;
135                         }
136                         return false;
137                 });
138                 $('html').click(function() {
139                         close_last_popup_menu();
140                 });
141
142                 // fancyboxes
143                 $("a.popupbox").colorbox({
144                         'inline' : true,
145                         'transition' : 'elastic'
146                 });
147
148
149                 /* notifications template */
150                 var notifications_tpl= unescape($("#nav-notifications-template[rel=template]").html());
151                 var notifications_all = unescape($('<div>').append( $("#nav-notifications-see-all").clone() ).html()); //outerHtml hack
152                 var notifications_mark = unescape($('<div>').append( $("#nav-notifications-mark-all").clone() ).html()); //outerHtml hack
153                 var notifications_empty = unescape($("#nav-notifications-menu").html());
154
155                 /* nav update event  */
156                 $('nav').bind('nav-update', function(e,data){
157                         var invalid = $(data).find('invalid').text();
158                         if(invalid == 1) { window.location.href=window.location.href }
159
160                         var net = $(data).find('net').text();
161                         if(net == 0) { net = ''; $('#net-update').removeClass('show') } else { $('#net-update').addClass('show') }
162                         $('#net-update').html(net);
163
164                         var home = $(data).find('home').text();
165                         if(home == 0) { home = '';  $('#home-update').removeClass('show') } else { $('#home-update').addClass('show') }
166                         $('#home-update').html(home);
167
168                         var intro = $(data).find('intro').text();
169                         if(intro == 0) { intro = '';  $('#intro-update').removeClass('show') } else { $('#intro-update').addClass('show') }
170                         $('#intro-update').html(intro);
171
172                         var mail = $(data).find('mail').text();
173                         if(mail == 0) { mail = '';  $('#mail-update').removeClass('show') } else { $('#mail-update').addClass('show') }
174                         $('#mail-update').html(mail);
175
176                         var intro = $(data).find('intro').text();
177                         if(intro == 0) { intro = '';  $('#intro-update-li').removeClass('show') } else { $('#intro-update-li').addClass('show') }
178                         $('#intro-update-li').html(intro);
179
180                         var mail = $(data).find('mail').text();
181                         if(mail == 0) { mail = '';  $('#mail-update-li').removeClass('show') } else { $('#mail-update-li').addClass('show') }
182                         $('#mail-update-li').html(mail);
183
184
185                         var allevents = $(data).find('all-events').text();
186                         if(allevents == 0) { allevents = ''; $('#allevents-update').removeClass('show') } else { $('#allevents-update').addClass('show') }
187                         $('#allevents-update').html(allevents);
188
189                         var alleventstoday = $(data).find('all-events-today').text();
190                         if(alleventstoday == 0) { $('#allevents-update').removeClass('notif-allevents-today') } else { $('#allevents-update').addClass('notif-allevents-today') }
191
192                         var events = $(data).find('events').text();
193                         if(events == 0) { events = ''; $('#events-update').removeClass('show') } else { $('#events-update').addClass('show') }
194                         $('#events-update').html(events);
195
196                         var eventstoday = $(data).find('events-today').text();
197                         if(eventstoday == 0) { $('#events-update').removeClass('notif-events-today') } else { $('#events-update').addClass('notif-events-today') }
198
199                         var birthdays = $(data).find('birthdays').text();
200                         if(birthdays == 0) {birthdays = ''; $('#birthdays-update').removeClass('show') } else { $('#birthdays-update').addClass('show') }
201                         $('#birthdays-update').html(birthdays);
202
203                         var birthdaystoday = $(data).find('birthdays-today').text();
204                         if(birthdaystoday == 0) { $('#birthdays-update').removeClass('notif-birthdays-today') } else { $('#birthdays-update').addClass('notif-birthdays-today') }
205
206                         $(".sidebar-group-li .notify").removeClass("show");
207                         $(data).find("group").each(function() {
208                                 var gid = this.id;
209                                 var gcount = this.innerHTML;
210                                 $(".group-"+gid+" .notify").addClass("show").text(gcount);
211                         });
212
213                         $(".forum-widget-entry .notify").removeClass("show");
214                         $(data).find("forum").each(function() {
215                                 var fid = this.id;
216                                 var fcount = this.innerHTML;
217                                 $(".forum-"+fid+" .notify").addClass("show").text(fcount);
218                         });
219
220
221                         var eNotif = $(data).find('notif')
222
223                         if (eNotif.children("note").length==0){
224                                 $("#nav-notifications-menu").html(notifications_empty);
225                         } else {
226                                 nnm = $("#nav-notifications-menu");
227                                 nnm.html(notifications_all + notifications_mark);
228                                 //nnm.attr('popup','true');
229
230                                 var notification_lastitem = parseInt(localStorage.getItem("notification-lastitem"));
231                                 var notification_id = 0;
232                                 eNotif.children("note").each(function(){
233                                         e = $(this);
234                                         var text = e.text().format("<span class='contactname'>"+e.attr('name')+"</span>");
235                                         var seenclass = (e.attr('seen')==1)?"notify-seen":"notify-unseen";
236                                         var html = notifications_tpl.format(e.attr('href'),
237                                                 e.attr('photo'),                    // {0}
238                                                 text,                               // {1}
239                                                 e.attr('date'),                     // {2}
240                                                 seenclass,                          // {3}
241                                                 new Date(e.attr('timestamp')*1000)  // {4}
242                                         );
243                                         nnm.append(html);
244                                 });
245                                 $(eNotif.children("note").get().reverse()).each(function(){
246                                         e = $(this);
247                                         notification_id = parseInt(e.attr('timestamp'));
248                                         if (notification_lastitem!== null && notification_id > notification_lastitem) {
249                                                 if (getNotificationPermission()==="granted") {
250                                                         var notification = new Notification(document.title, {
251                                                                                           body: e.text().replace('&rarr; ','').format(e.attr('name')),
252                                                                                           icon: e.attr('photo'),
253                                                                                          });
254                                                         notification['url'] = e.attr('href');
255                                                         notification.addEventListener("click", function(ev){
256                                                                 window.location = ev.target.url;
257                                                         });
258                                                 }
259                                         }
260
261                                 });
262                                 notification_lastitem = notification_id;
263                                 localStorage.setItem("notification-lastitem", notification_lastitem)
264
265                                 $("img[data-src]", nnm).each(function(i, el){
266                                         // Add src attribute for images with a data-src attribute
267                                         // However, don't bother if the data-src attribute is empty, because
268                                         // an empty "src" tag for an image will cause some browsers
269                                         // to prefetch the root page of the Friendica hub, which will
270                                         // unnecessarily load an entire profile/ or network/ page
271                                         if($(el).data("src") != '') $(el).attr('src', $(el).data("src"));
272                                 });
273                         }
274
275                         notif = eNotif.attr('count');
276                         if (notif>0){
277                                 $("#nav-notifications-linkmenu").addClass("on");
278                         } else {
279                                 $("#nav-notifications-linkmenu").removeClass("on");
280                         }
281                         if(notif == 0) { notif = ''; $('#notify-update').removeClass('show') } else { $('#notify-update').addClass('show') }
282                         $('#notify-update').html(notif);
283
284                         var eSysmsg = $(data).find('sysmsgs');
285                         eSysmsg.children("notice").each(function(){
286                                 text = $(this).text();
287                                 $.jGrowl(text, { sticky: true, theme: 'notice' });
288                         });
289                         eSysmsg.children("info").each(function(){
290                                 text = $(this).text();
291                                 $.jGrowl(text, { sticky: false, theme: 'info', life: 5000 });
292                         });
293
294                 });
295
296                 NavUpdate();
297                 // Allow folks to stop the ajax page updates with the pause/break key
298                 $(document).keydown(function(event) {
299                         if(event.keyCode == '8') {
300                                 var target = event.target || event.srcElement;
301                                 if (!/input|textarea/i.test(target.nodeName)) {
302                                         return false;
303                                 }
304                         }
305                         if(event.keyCode == '19' || (event.ctrlKey && event.which == '32')) {
306                                 event.preventDefault();
307                                 if(stopped == false) {
308                                         stopped = true;
309                                         if (event.ctrlKey) {
310                                                 totStopped = true;
311                                         }
312                                         $('#pause').html('<img src="images/pause.gif" alt="pause" style="border: 1px solid black;" />');
313                                 } else {
314                                         unpause();
315                                 }
316                         } else {
317                                 if (!totStopped) {
318                                         unpause();
319                                 }
320                         }
321                 });
322
323
324         });
325
326         function NavUpdate() {
327
328                 if(! stopped) {
329                         var pingCmd = 'ping' + ((localUser != 0) ? '?f=&uid=' + localUser : '');
330                         $.get(pingCmd,function(data) {
331                                 $(data).find('result').each(function() {
332                                         // send nav-update event
333                                         $('nav').trigger('nav-update', this);
334
335
336                                         // start live update
337
338                                         if($('#live-network').length)   { src = 'network'; liveUpdate(); }
339                                         if($('#live-profile').length)   { src = 'profile'; liveUpdate(); }
340                                         if($('#live-community').length) { src = 'community'; liveUpdate(); }
341                                         if($('#live-notes').length)     { src = 'notes'; liveUpdate(); }
342                                         if($('#live-display').length)     { src = 'display'; liveUpdate(); }
343 /*                                      if($('#live-display').length) {
344                                                 if(liking) {
345                                                         liking = 0;
346                                                         window.location.href=window.location.href
347                                                 }
348                                         }*/
349                                         if($('#live-photos').length) {
350                                                 if(liking) {
351                                                         liking = 0;
352                                                         window.location.href=window.location.href
353                                                 }
354                                         }
355
356
357
358
359                                 });
360                         }) ;
361                 }
362                 timer = setTimeout(NavUpdate,updateInterval);
363         }
364
365         function liveUpdate() {
366                 if((src == null) || (stopped) || (! profile_uid)) { $('.like-rotator').hide(); return; }
367                 if(($('.comment-edit-text-full').length) || (in_progress)) {
368                         if(livetime) {
369                                 clearTimeout(livetime);
370                         }
371                         livetime = setTimeout(liveUpdate, 5000);
372                         return;
373                 }
374                 if(livetime != null)
375                         livetime = null;
376
377                 prev = 'live-' + src;
378
379                 in_progress = true;
380
381                 if ($(document).scrollTop() == 0)
382                         force_update = true;
383
384                 var udargs = ((netargs.length) ? '/' + netargs : '');
385                 var update_url = 'update_' + src + udargs + '&p=' + profile_uid + '&page=' + profile_page + '&force=' + ((force_update) ? 1 : 0);
386
387                 $.get(update_url,function(data) {
388                         in_progress = false;
389                         force_update = false;
390                         //                      $('.collapsed-comments',data).each(function() {
391                         //      var ident = $(this).attr('id');
392                         //      var is_hidden = $('#' + ident).is(':hidden');
393                         //      if($('#' + ident).length) {
394                         //              $('#' + ident).replaceWith($(this));
395                         //              if(is_hidden)
396                         //                      $('#' + ident).hide();
397                         //      }
398                         //});
399
400                         // add a new thread
401                         $('.toplevel_item',data).each(function() {
402                                 var ident = $(this).attr('id');
403
404                                 if($('#' + ident).length == 0 && profile_page == 1) {
405                                         $('img',this).each(function() {
406                                                 $(this).attr('src',$(this).attr('dst'));
407                                         });
408                                         $('#' + prev).after($(this));
409                                 }
410                                 else {
411                                         // Find out if the hidden comments are open, so we can keep it that way
412                                         // if a new comment has been posted
413                                         var id = $('.hide-comments-total', this).attr('id');
414                                         if(typeof id != 'undefined') {
415                                                 id = id.split('-')[3];
416                                                 var commentsOpen = $("#collapsed-comments-" + id).is(":visible");
417                                         }
418
419                                         $('img',this).each(function() {
420                                                 $(this).attr('src',$(this).attr('dst'));
421                                         });
422                                         //vScroll = $(document).scrollTop();
423                                         $('html').height($('html').height());
424                                         $('#' + ident).replaceWith($(this));
425
426                                         if(typeof id != 'undefined') {
427                                                 if(commentsOpen) showHideComments(id);
428                                         }
429                                         $('html').height('auto');
430                                         //$(document).scrollTop(vScroll);
431                                 }
432                                 prev = ident;
433                         });
434
435                         // reset vars for inserting individual items
436
437                         /*                      prev = 'live-' + src;
438
439                         $('.wall-item-outside-wrapper',data).each(function() {
440                                 var ident = $(this).attr('id');
441
442                                 if($('#' + ident).length == 0 && prev != 'live-' + src) {
443                                                 $('img',this).each(function() {
444                                                         $(this).attr('src',$(this).attr('dst'));
445                                                 });
446                                                 $('#' + prev).after($(this));
447                                 }
448                                 else {
449                                         $('#' + ident + ' ' + '.wall-item-ago').replaceWith($(this).find('.wall-item-ago'));
450                                         if($('#' + ident + ' ' + '.comment-edit-text-empty').length)
451                                                 $('#' + ident + ' ' + '.wall-item-comment-wrapper').replaceWith($(this).find('.wall-item-comment-wrapper'));
452                                         $('#' + ident + ' ' + '.hide-comments-total').replaceWith($(this).find('.hide-comments-total'));
453                                         $('#' + ident + ' ' + '.wall-item-like').replaceWith($(this).find('.wall-item-like'));
454                                         $('#' + ident + ' ' + '.wall-item-dislike').replaceWith($(this).find('.wall-item-dislike'));
455                                         $('#' + ident + ' ' + '.my-comment-photo').each(function() {
456                                                 $(this).attr('src',$(this).attr('dst'));
457                                         });
458                                 }
459                                 prev = ident;
460                         });
461                         */
462                         $('.like-rotator').hide();
463                         if(commentBusy) {
464                                 commentBusy = false;
465                                 $('body').css('cursor', 'auto');
466                         }
467                         /* autocomplete @nicknames */
468                         $(".comment-edit-form  textarea").contact_autocomplete(baseurl+"/acl");
469
470                         // setup videos, since VideoJS won't take care of any loaded via AJAX
471                         if(typeof videojs != 'undefined') videojs.autoSetup();
472                 });
473         }
474
475         function imgbright(node) {
476                 $(node).removeClass("drophide").addClass("drop");
477         }
478
479         function imgdull(node) {
480                 $(node).removeClass("drop").addClass("drophide");
481         }
482
483         // Since our ajax calls are asynchronous, we will give a few
484         // seconds for the first ajax call (setting like/dislike), then
485         // run the updater to pick up any changes and display on the page.
486         // The updater will turn any rotators off when it's done.
487         // This function will have returned long before any of these
488         // events have completed and therefore there won't be any
489         // visible feedback that anything changed without all this
490         // trickery. This still could cause confusion if the "like" ajax call
491         // is delayed and NavUpdate runs before it completes.
492
493         function dolike(ident,verb) {
494                 unpause();
495                 $('#like-rotator-' + ident.toString()).show();
496                 $.get('like/' + ident.toString() + '?verb=' + verb, NavUpdate );
497                 liking = 1;
498                 force_update = true;
499         }
500
501         function dosubthread(ident) {
502                 unpause();
503                 $('#like-rotator-' + ident.toString()).show();
504                 $.get('subthread/' + ident.toString(), NavUpdate );
505                 liking = 1;
506         }
507
508
509         function dostar(ident) {
510                 ident = ident.toString();
511                 $('#like-rotator-' + ident).show();
512                 $.get('starred/' + ident, function(data) {
513                         if(data.match(/1/)) {
514                                 $('#starred-' + ident).addClass('starred');
515                                 $('#starred-' + ident).removeClass('unstarred');
516                                 $('#star-' + ident).addClass('hidden');
517                                 $('#unstar-' + ident).removeClass('hidden');
518                         }
519                         else {
520                                 $('#starred-' + ident).addClass('unstarred');
521                                 $('#starred-' + ident).removeClass('starred');
522                                 $('#star-' + ident).removeClass('hidden');
523                                 $('#unstar-' + ident).addClass('hidden');
524                         }
525                         $('#like-rotator-' + ident).hide();
526                 });
527         }
528
529         function doignore(ident) {
530                 ident = ident.toString();
531                 $('#like-rotator-' + ident).show();
532                 $.get('ignored/' + ident, function(data) {
533                         if(data.match(/1/)) {
534                                 $('#ignored-' + ident).addClass('ignored');
535                                 $('#ignored-' + ident).removeClass('unignored');
536                                 $('#ignore-' + ident).addClass('hidden');
537                                 $('#unignore-' + ident).removeClass('hidden');
538                         }
539                         else {
540                                 $('#ignored-' + ident).addClass('unignored');
541                                 $('#ignored-' + ident).removeClass('ignored');
542                                 $('#ignore-' + ident).removeClass('hidden');
543                                 $('#unignore-' + ident).addClass('hidden');
544                         }
545                         $('#like-rotator-' + ident).hide();
546                 });
547         }
548
549         function getPosition(e) {
550                 var cursor = {x:0, y:0};
551                 if ( e.pageX || e.pageY  ) {
552                         cursor.x = e.pageX;
553                         cursor.y = e.pageY;
554                 }
555                 else {
556                         if( e.clientX || e.clientY ) {
557                                 cursor.x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
558                                 cursor.y = e.clientY + (document.documentElement.scrollTop  || document.body.scrollTop)  - document.documentElement.clientTop;
559                         }
560                         else {
561                                 if( e.x || e.y ) {
562                                         cursor.x = e.x;
563                                         cursor.y = e.y;
564                                 }
565                         }
566                 }
567                 return cursor;
568         }
569
570         var lockvisible = false;
571
572         function lockview(event,id) {
573                 event = event || window.event;
574                 cursor = getPosition(event);
575                 if(lockvisible) {
576                         lockviewhide();
577                 }
578                 else {
579                         lockvisible = true;
580                         $.get('lockview/' + id, function(data) {
581                                 $('#panel').html(data);
582                                 $('#panel').css({ 'left': cursor.x + 5 , 'top': cursor.y + 5});
583                                 $('#panel').show();
584                         });
585                 }
586         }
587
588         function lockviewhide() {
589                 lockvisible = false;
590                 $('#panel').hide();
591         }
592
593         function post_comment(id) {
594                 unpause();
595                 commentBusy = true;
596                 $('body').css('cursor', 'wait');
597                 $("#comment-preview-inp-" + id).val("0");
598                 $.post(
599                         "item",
600                         $("#comment-edit-form-" + id).serialize(),
601                         function(data) {
602                                 if(data.success) {
603                                         $("#comment-edit-wrapper-" + id).hide();
604                                         $("#comment-edit-text-" + id).val('');
605                                         var tarea = document.getElementById("comment-edit-text-" + id);
606                                         if(tarea)
607                                                 commentClose(tarea,id);
608                                         if(timer) clearTimeout(timer);
609                                         timer = setTimeout(NavUpdate,10);
610                                         force_update = true;
611                                 }
612                                 if(data.reload) {
613                                         window.location.href=data.reload;
614                                 }
615                         },
616                         "json"
617                 );
618                 return false;
619         }
620
621
622         function preview_comment(id) {
623                 $("#comment-preview-inp-" + id).val("1");
624                 $("#comment-edit-preview-" + id).show();
625                 $.post(
626                         "item",
627                         $("#comment-edit-form-" + id).serialize(),
628                         function(data) {
629                                 if(data.preview) {
630                                         $("#comment-edit-preview-" + id).html(data.preview);
631                                         $("#comment-edit-preview-" + id + " a").click(function() { return false; });
632                                 }
633                         },
634                         "json"
635                 );
636                 return true;
637         }
638
639
640
641         function showHideComments(id) {
642                 if( $("#collapsed-comments-" + id).is(":visible")) {
643                         $("#collapsed-comments-" + id).hide();
644                         $("#hide-comments-" + id).html(window.showMore);
645                 }
646                 else {
647                         $("#collapsed-comments-" + id).show();
648                         $("#hide-comments-" + id).html(window.showFewer);
649                 }
650         }
651
652
653
654         function preview_post() {
655                 $("#jot-preview").val("1");
656                 $("#jot-preview-content").show();
657                 tinyMCE.triggerSave();
658                 $.post(
659                         "item",
660                         $("#profile-jot-form").serialize(),
661                         function(data) {
662                                 if(data.preview) {
663                                         $("#jot-preview-content").html(data.preview);
664                                         $("#jot-preview-content" + " a").click(function() { return false; });
665                                 }
666                         },
667                         "json"
668                 );
669                 $("#jot-preview").val("0");
670                 return true;
671         }
672
673
674         function unpause() {
675                 // unpause auto reloads if they are currently stopped
676                 totStopped = false;
677                 stopped = false;
678                 $('#pause').html('');
679         }
680
681
682     function bin2hex(s){
683         // Converts the binary representation of data to hex
684         //
685         // version: 812.316
686         // discuss at: http://phpjs.org/functions/bin2hex
687         // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
688         // +   bugfixed by: Onno Marsman
689         // +   bugfixed by: Linuxworld
690         // *     example 1: bin2hex('Kev');
691         // *     returns 1: '4b6576'
692         // *     example 2: bin2hex(String.fromCharCode(0x00));
693         // *     returns 2: '00'
694         var v,i, f = 0, a = [];
695         s += '';
696         f = s.length;
697
698         for (i = 0; i<f; i++) {
699             a[i] = s.charCodeAt(i).toString(16).replace(/^([\da-f])$/,"0$1");
700         }
701
702         return a.join('');
703     }
704
705         function groupChangeMember(gid, cid, sec_token) {
706                 $('body .fakelink').css('cursor', 'wait');
707                 $.get('group/' + gid + '/' + cid + "?t=" + sec_token, function(data) {
708                                 $('#group-update-wrapper').html(data);
709                                 $('body .fakelink').css('cursor', 'auto');
710                 });
711         }
712
713         function profChangeMember(gid,cid) {
714                 $('body .fakelink').css('cursor', 'wait');
715                 $.get('profperm/' + gid + '/' + cid, function(data) {
716                                 $('#prof-update-wrapper').html(data);
717                                 $('body .fakelink').css('cursor', 'auto');
718                 });
719         }
720
721         function contactgroupChangeMember(gid,cid) {
722                 $('body').css('cursor', 'wait');
723                 $.get('contactgroup/' + gid + '/' + cid, function(data) {
724                                 $('body').css('cursor', 'auto');
725                 });
726         }
727
728
729 function checkboxhighlight(box) {
730   if($(box).is(':checked')) {
731         $(box).addClass('checkeditem');
732   }
733   else {
734         $(box).removeClass('checkeditem');
735   }
736 }
737
738 function notifyMarkAll() {
739         $.get('notify/mark/all', function(data) {
740                 if(timer) clearTimeout(timer);
741                 timer = setTimeout(NavUpdate,1000);
742                 force_update = true;
743         });
744 }
745
746
747 // code from http://www.tinymce.com/wiki.php/How-to_implement_a_custom_file_browser
748 function fcFileBrowser (field_name, url, type, win) {
749     /* TODO: If you work with sessions in PHP and your client doesn't accept cookies you might need to carry
750        the session name and session ID in the request string (can look like this: "?PHPSESSID=88p0n70s9dsknra96qhuk6etm5").
751        These lines of code extract the necessary parameters and add them back to the filebrowser URL again. */
752
753
754     var cmsURL = baseurl+"/fbrowser/"+type+"/";
755
756     tinyMCE.activeEditor.windowManager.open({
757         file : cmsURL,
758         title : 'File Browser',
759         width : 420,  // Your dimensions may differ - toy around with them!
760         height : 400,
761         resizable : "yes",
762         inline : "yes",  // This parameter only has an effect if you use the inlinepopups plugin!
763         close_previous : "no"
764     }, {
765         window : win,
766         input : field_name
767     });
768     return false;
769   }
770
771 function setupFieldRichtext(){
772         tinyMCE.init({
773                 theme : "advanced",
774                 mode : "specific_textareas",
775                 editor_selector: "fieldRichtext",
776                 plugins : "bbcode,paste, inlinepopups",
777                 theme_advanced_buttons1 : "bold,italic,underline,undo,redo,link,unlink,image,forecolor,formatselect,code",
778                 theme_advanced_buttons2 : "",
779                 theme_advanced_buttons3 : "",
780                 theme_advanced_toolbar_location : "top",
781                 theme_advanced_toolbar_align : "center",
782                 theme_advanced_blockformats : "blockquote,code",
783                 theme_advanced_resizing : true,
784                 paste_text_sticky : true,
785                 entity_encoding : "raw",
786                 add_unload_trigger : false,
787                 remove_linebreaks : false,
788                 //force_p_newlines : false,
789                 //force_br_newlines : true,
790                 forced_root_block : 'div',
791                 convert_urls: false,
792                 content_css: baseurl+"/view/custom_tinymce.css",
793                 theme_advanced_path : false,
794                 file_browser_callback : "fcFileBrowser",
795         });
796 }
797
798
799 /**
800  * sprintf in javascript
801  *      "{0} and {1}".format('zero','uno');
802  **/
803 String.prototype.format = function() {
804     var formatted = this;
805     for (var i = 0; i < arguments.length; i++) {
806         var regexp = new RegExp('\\{'+i+'\\}', 'gi');
807         formatted = formatted.replace(regexp, arguments[i]);
808     }
809     return formatted;
810 };
811 // Array Remove
812 Array.prototype.remove = function(item) {
813   to=undefined; from=this.indexOf(item);
814   var rest = this.slice((to || from) + 1 || this.length);
815   this.length = from < 0 ? this.length + from : from;
816   return this.push.apply(this, rest);
817 };
818
819 function previewTheme(elm) {
820         theme = $(elm).val();
821         $.getJSON('pretheme?f=&theme=' + theme,function(data) {
822                         $('#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>');
823         });
824
825 }
826
827 // notification permission settings in localstorage
828 // set by settings page
829 function getNotificationPermission() {
830         if (window["Notification"] === undefined) {
831                 return null;
832         }
833     if (Notification.permission === 'granted') {
834         var val = localStorage.getItem('notification-permissions');
835                 if (val === null) return 'denied';
836                 return val;
837     } else {
838         return Notification.permission;
839     }
840 }
841
842 /**
843  * Show a dialog loaded from an url
844  * By defaults this load the url in an iframe in colorbox
845  * Themes can overwrite `show()` function to personalize it
846  */
847 var Dialog = {
848         /**
849          * Show the dialog
850          *
851          * @param string url
852          * @return object colorbox
853          */
854         show : function (url) {
855                 var size = Dialog._get_size();
856                 return $.colorbox({href: url, iframe:true,innerWidth: size.width+'px',innerHeight: size.height+'px'})
857         },
858
859         /**
860          * Show the Image browser dialog
861          *
862          * @param string name
863          * @param string id (optional)
864          * @return object
865          *
866          * The name will be used to build the event name
867          * fired by image browser dialog when the user select
868          * an image. The optional id will be passed as argument
869          * to the event handler
870          */
871         doImageBrowser : function (name, id) {
872                 var url = Dialog._get_url("image",name,id);
873                 return Dialog.show(url);
874         },
875
876         /**
877          * Show the File browser dialog
878          *
879          * @param string name
880          * @param string id (optional)
881          * @return object
882          *
883          * The name will be used to build the event name
884          * fired by file browser dialog when the user select
885          * a file. The optional id will be passed as argument
886          * to the event handler
887          */
888         doFileBrowser : function (name, id) {
889                 var url = Dialog._get_url("file",name,id);
890                 return Dialog.show(url);
891         },
892
893         _get_url : function(type, name, id) {
894                 var hash = name;
895                 if (id !== undefined) hash = hash + "-" + id;
896                 return baseurl + "/fbrowser/"+type+"/?mode=minimal#"+hash;
897         },
898
899         _get_size: function() {
900                 return {
901                         width: window.innerWidth-50,
902                         height: window.innerHeight-100
903                 };
904         }
905 }