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