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