]> git.mxchange.org Git - friendica.git/blob - js/main.js
Merge remote-tracking branch 'friendika/master'
[friendica.git] / 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         var src = null;
21         var prev = null;
22         var livetime = null;
23         var msie = false;
24         var stopped = false;
25         var timer = null;
26         var pr = 0;
27         var liking = 0;
28         var in_progress = false;
29         var langSelect = false;
30         var commentBusy = false;
31
32         $(function() {
33                 $.ajaxSetup({cache: false});
34
35                 msie = $.browser.msie ;
36                 
37                 /* setup tooltips */
38                 $("a,.tt").each(function(){
39                         var e = $(this);
40                         var pos="bottom";
41                         if (e.hasClass("tttop")) pos="top";
42                         if (e.hasClass("ttbottom")) pos="bottom";
43                         if (e.hasClass("ttleft")) pos="left";
44                         if (e.hasClass("ttright")) pos="right";
45                         e.tipTip({defaultPosition: pos});
46                 });
47                 
48                 
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                         //console.log(id);
66                 });
67                 
68                 /* setup field_richtext */
69                 setupFieldRichtext();
70
71                 /* popup menus */
72                 $('a[rel^=#]').click(function(e){
73                         menu = $( $(this).attr('rel') );
74                         e.preventDefault();
75                         e.stopPropagation();
76                         if (menu.attr('popup')=="false") return false;
77                         $(this).parent().toggleClass("selected");
78                         menu.toggle();
79                         return false;
80                 });
81                 
82                 
83
84                 /* notifications template */
85                 var notifications_tpl= unescape($("#nav-notifications-template[rel=template]").html());
86                 var notifications_empty = unescape($("#nav-notifications-menu").html());
87                 
88                 /* nav update event  */
89                 $('nav').bind('nav-update', function(e,data){;
90                         var net = $(data).find('net').text();
91                         if(net == 0) { net = ''; $('#net-update').removeClass('show') } else { $('#net-update').addClass('show') }
92                         $('#net-update').html(net);
93
94                         var home = $(data).find('home').text();
95                         if(home == 0) { home = '';  $('#home-update').removeClass('show') } else { $('#home-update').addClass('show') }
96                         $('#home-update').html(home);
97
98                         var eNotif = $(data).find('notif')
99                         notif = eNotif.attr('count');
100                         if (notif>0){
101                                 $("#nav-notifications-linkmenu").addClass("on");
102                                 nnm = $("#nav-notifications-menu");
103                                 nnm.html("");
104                                 //nnm.attr('popup','true');
105                                 eNotif.children("note").each(function(){
106                                         e = $(this);
107                                         text = e.text().format("<span class='contactname'>"+e.attr('name')+"</span>");
108                                         html = notifications_tpl.format(e.attr('href'),e.attr('photo'), text, e.attr('date'));
109                                         nnm.append(html);
110                                 });
111                         } else {
112                                 $("#nav-notifications-linkmenu").removeClass("on");
113                                 $("#nav-notifications-menu").html(notifications_empty);
114                         }
115                         if(notif == 0) { notif = ''; $('#notify-update').removeClass('show') } else { $('#notify-update').addClass('show') }
116                         $('#notify-update').html(notif);
117                         
118                         var eSysmsg = $(data).find('sysmsgs');
119                         eSysmsg.children("notice").each(function(){
120                                 text = $(this).text();
121                                 $.jGrowl(text, { sticky: true, theme: 'notice' });
122                         });
123                         eSysmsg.children("info").each(function(){
124                                 text = $(this).text();
125                                 $.jGrowl(text, { sticky: false, theme: 'info' });
126                         });
127                         
128                 });
129                 
130                 
131                 NavUpdate(); 
132                 // Allow folks to stop the ajax page updates with the pause/break key
133                 $(document).keypress(function(event) {
134                         if(event.keyCode == '19') {
135                                 event.preventDefault();
136                                 if(stopped == false) {
137                                         stopped = true;
138                                         $('#pause').html('<img src="images/pause.gif" alt="pause" style="border: 1px solid black;" />');
139                                 }
140                                 else {
141                                         stopped = false;
142                                         $('#pause').html('');
143                                 }
144                         }
145                         else {
146                                 // any key to resume
147                                 if(stopped == true) {
148                                         stopped = false;
149                                         $('#pause').html('');
150                                 }
151                         }
152
153                 });                                     
154         });
155
156         function NavUpdate() {
157
158                 if($('#live-network').length)   { src = 'network'; liveUpdate(); }
159                 if($('#live-profile').length)   { src = 'profile'; liveUpdate(); }
160                 if($('#live-community').length) { src = 'community'; liveUpdate(); }
161                 if($('#live-notes').length)     { src = 'notes'; liveUpdate(); }
162                 if($('#live-display').length) { 
163                         if(liking) {
164                                 liking = 0;
165                                 window.location.href=window.location.href 
166                         }
167                 }
168                 if($('#live-photos').length)  { 
169                         if(liking) {
170                                 liking = 0;
171                                 window.location.href=window.location.href 
172                         }
173                 }
174
175                 if(! stopped) {
176                         $.get("ping",function(data) {
177                                 $(data).find('result').each(function() {
178                                         // send nav-update event
179                                         $('nav').trigger('nav-update', this);
180                                 });
181                         }) ;
182                 }
183                 timer = setTimeout(NavUpdate,30000);
184         }
185
186         function liveUpdate() {
187                 if((src == null) || (stopped) || (! profile_uid)) { $('.like-rotator').hide(); return; }
188                 if(($('.comment-edit-text-full').length) || (in_progress)) {
189                         livetime = setTimeout(liveUpdate, 10000);
190                         return;
191                 }
192                 prev = 'live-' + src;
193
194                 in_progress = true;
195                 var udargs = ((netargs.length) ? '/' + netargs : '');
196                 var update_url = 'update_' + src + udargs + '&p=' + profile_uid + '&page=' + profile_page + '&msie=' + ((msie) ? 1 : 0);
197
198                 $.get(update_url,function(data) {
199                         in_progress = false;
200                         $('.ccollapse-wrapper',data).each(function() {
201                                 var ident = $(this).attr('id');
202                                 var is_hidden = $('#' + ident).is(':hidden');
203                                 if($('#' + ident).length) {
204                                         $('#' + ident).replaceWith($(this));
205                                         if(is_hidden)
206                                                 $('#' + ident).hide();
207                                 }
208                         });
209                         $('.wall-item-outside-wrapper',data).each(function() {
210                                 var ident = $(this).attr('id');
211                                 if($('#' + ident).length == 0) {
212                                         $('img',this).each(function() {
213                                                 $(this).attr('src',$(this).attr('dst'));
214                                         });
215                                         $('#' + prev).after($(this));
216                                 }
217                                 else { 
218
219                                         $('#' + ident + ' ' + '.wall-item-ago').replaceWith($(this).find('.wall-item-ago')); 
220                                         if($('#' + ident + ' ' + '.comment-edit-text-empty').length)
221                                                 $('#' + ident + ' ' + '.wall-item-comment-wrapper').replaceWith($(this).find('.wall-item-comment-wrapper'));
222                                         $('#' + ident + ' ' + '.wall-item-like').replaceWith($(this).find('.wall-item-like'));
223                                         $('#' + ident + ' ' + '.wall-item-dislike').replaceWith($(this).find('.wall-item-dislike'));
224                                         $('#' + ident + ' ' + '.my-comment-photo').each(function() {
225                                                 $(this).attr('src',$(this).attr('dst'));
226                                         });
227                                 }
228                                 prev = ident; 
229                         });
230                         $('.like-rotator').hide();
231                         if(commentBusy) {
232                                 commentBusy = false;
233                                 $('body').css('cursor', 'auto');
234                         }
235                 });
236         }
237
238         function imgbright(node) {
239                 $(node).removeClass("drophide").addClass("drop");
240         }
241
242         function imgdull(node) {
243                 $(node).removeClass("drop").addClass("drophide");
244         }
245
246         // Since our ajax calls are asynchronous, we will give a few 
247         // seconds for the first ajax call (setting like/dislike), then 
248         // run the updater to pick up any changes and display on the page.
249         // The updater will turn any rotators off when it's done. 
250         // This function will have returned long before any of these
251         // events have completed and therefore there won't be any
252         // visible feedback that anything changed without all this
253         // trickery. This still could cause confusion if the "like" ajax call
254         // is delayed and NavUpdate runs before it completes.
255
256         function dolike(ident,verb) {
257                 $('#like-rotator-' + ident.toString()).show();
258                 $.get('like/' + ident.toString() + '?verb=' + verb );
259                 if(timer) clearTimeout(timer);
260                 timer = setTimeout(NavUpdate,3000);
261                 liking = 1;
262         }
263
264         function dostar(ident) {
265                 ident = ident.toString();
266                 $('#like-rotator-' + ident).show();
267                 $.get('starred/' + ident, function(data) {
268                         if(data.match(/1/)) {
269                                 $('#starred-' + ident).addClass('starred');
270                                 $('#starred-' + ident).removeClass('unstarred');
271                                 $('#star-' + ident).addClass('hidden');
272                                 $('#unstar-' + ident).removeClass('hidden');
273                         }
274                         else {                  
275                                 $('#starred-' + ident).addClass('unstarred');
276                                 $('#starred-' + ident).removeClass('starred');
277                                 $('#star-' + ident).removeClass('hidden');
278                                 $('#unstar-' + ident).addClass('hidden');
279                         }
280                         $('#like-rotator-' + ident).hide();     
281                 });
282         }
283
284         function getPosition(e) {
285                 var cursor = {x:0, y:0};
286                 if ( e.pageX || e.pageY  ) {
287                         cursor.x = e.pageX;
288                         cursor.y = e.pageY;
289                 }
290                 else {
291                         if( e.clientX || e.clientY ) {
292                                 cursor.x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
293                                 cursor.y = e.clientY + (document.documentElement.scrollTop  || document.body.scrollTop)  - document.documentElement.clientTop;
294                         }
295                         else {
296                                 if( e.x || e.y ) {
297                                         cursor.x = e.x;
298                                         cursor.y = e.y;
299                                 }
300                         }
301                 }
302                 return cursor;
303         }
304
305         var lockvisible = false;
306
307         function lockview(event,id) {
308                 event = event || window.event;
309                 cursor = getPosition(event);
310                 if(lockvisible) {
311                         lockviewhide();
312                 }
313                 else {
314                         lockvisible = true;
315                         $.get('lockview/' + id, function(data) {
316                                 $('#panel').html(data);
317                                 $('#panel').css({ 'left': cursor.x + 5 , 'top': cursor.y + 5});
318                                 $('#panel').show();
319                         });
320                 }
321         }
322
323         function lockviewhide() {
324                 lockvisible = false;
325                 $('#panel').hide();
326         }
327
328         function post_comment(id) {
329                 commentBusy = true;
330                 $('body').css('cursor', 'wait');
331                 $.post(  
332              "item",  
333              $("#comment-edit-form-" + id).serialize(),
334                         function(data) {
335                                 if(data.success) {
336                                         $("#comment-edit-wrapper-" + id).hide();
337                                         $("#comment-edit-text-" + id).val('');
338                                 var tarea = document.getElementById("comment-edit-text-" + id);
339                                         if(tarea)
340                                                 commentClose(tarea,id);
341                                         if(timer) clearTimeout(timer);
342                                         timer = setTimeout(NavUpdate,10);
343                                 }
344                                 if(data.reload) {
345                                         window.location.href=data.reload;
346                                 }
347                         },
348                         "json"  
349          );  
350          return false;  
351         }
352
353
354     function bin2hex(s){  
355         // Converts the binary representation of data to hex    
356         //   
357         // version: 812.316  
358         // discuss at: http://phpjs.org/functions/bin2hex  
359         // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)  
360         // +   bugfixed by: Onno Marsman  
361         // +   bugfixed by: Linuxworld  
362         // *     example 1: bin2hex('Kev');  
363         // *     returns 1: '4b6576'  
364         // *     example 2: bin2hex(String.fromCharCode(0x00));  
365         // *     returns 2: '00'  
366         var v,i, f = 0, a = [];  
367         s += '';  
368         f = s.length;  
369           
370         for (i = 0; i<f; i++) {  
371             a[i] = s.charCodeAt(i).toString(16).replace(/^([\da-f])$/,"0$1");  
372         }  
373           
374         return a.join('');  
375     }  
376
377         function groupChangeMember(gid,cid) {
378                 $('body .fakelink').css('cursor', 'wait');
379                 $.get('group/' + gid + '/' + cid, function(data) {
380                                 $('#group-update-wrapper').html(data);
381                                 $('body .fakelink').css('cursor', 'auto');                              
382                 });
383         }
384
385         function profChangeMember(gid,cid) {
386                 $('body .fakelink').css('cursor', 'wait');
387                 $.get('profperm/' + gid + '/' + cid, function(data) {
388                                 $('#prof-update-wrapper').html(data);
389                                 $('body .fakelink').css('cursor', 'auto');                              
390                 });
391         }
392
393         function contactgroupChangeMember(gid,cid) {
394                 $('body').css('cursor', 'wait');
395                 $.get('contactgroup/' + gid + '/' + cid, function(data) {
396                                 $('body').css('cursor', 'auto');
397                 });
398         }
399
400
401 function checkboxhighlight(box) {
402   if($(box).is(':checked')) {
403         $(box).addClass('checkeditem');
404   }
405   else {
406         $(box).removeClass('checkeditem');
407   }
408 }
409
410 function setupFieldRichtext(){
411         tinyMCE.init({
412                 theme : "advanced",
413                 mode : "specific_textareas",
414                 editor_selector: "fieldRichtext",
415                 plugins : "bbcode,paste",
416                 theme_advanced_buttons1 : "bold,italic,underline,undo,redo,link,unlink,image,forecolor,formatselect,code",
417                 theme_advanced_buttons2 : "",
418                 theme_advanced_buttons3 : "",
419                 theme_advanced_toolbar_location : "top",
420                 theme_advanced_toolbar_align : "center",
421                 theme_advanced_blockformats : "blockquote,code",
422                 paste_text_sticky : true,
423                 entity_encoding : "raw",
424                 add_unload_trigger : false,
425                 remove_linebreaks : false,
426                 force_p_newlines : false,
427                 force_br_newlines : true,
428                 forced_root_block : '',
429                 convert_urls: false,
430                 content_css: baseurl+"/view/custom_tinymce.css",
431                 theme_advanced_path : false,
432         });
433 }
434
435 /** 
436  * sprintf in javascript 
437  *      "{0} and {1}".format('zero','uno'); 
438  **/
439 String.prototype.format = function() {
440     var formatted = this;
441     for (var i = 0; i < arguments.length; i++) {
442         var regexp = new RegExp('\\{'+i+'\\}', 'gi');
443         formatted = formatted.replace(regexp, arguments[i]);
444     }
445     return formatted;
446 };
447 // Array Remove
448 Array.prototype.remove = function(item) {
449   to=undefined; from=this.indexOf(item);
450   var rest = this.slice((to || from) + 1 || this.length);
451   this.length = from < 0 ? this.length + from : from;
452   return this.push.apply(this, rest);
453 };
454