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