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