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