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