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