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