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