Merge pull request #5128 from tobiasd/20180525-5067
[friendica.git/.git] / view / theme / frost / js / theme.js
1 $(document).ready(function() {
2
3         window.navMenuTimeout = {
4                 '#network-menu-list-timeout': null,
5                 '#contacts-menu-list-timeout': null,
6                 '#system-menu-list-timeout': null,
7                 '#network-menu-list-opening': false,
8                 '#contacts-menu-list-opening': false,
9                 '#system-menu-list-opening': false,
10                 '#network-menu-list-closing': false,
11                 '#contacts-menu-list-closing': false,
12                 '#system-menu-list-closing': false
13         };
14
15         /* enable editor on focus and click */
16         $("#profile-jot-text").focus(enableOnUser);
17         $("#profile-jot-text").click(enableOnUser);
18
19         $('.nav-menu-list, .nav-menu-icon').hover(function() {
20                 showNavMenu($(this).attr('point'));
21         }, function() {
22                 hideNavMenu($(this).attr('point'));
23         });
24
25         $('.group-edit-icon').hover(
26                 function() {
27                         $(this).addClass('icon'); $(this).removeClass('iconspacer');},
28                 function() {
29                         $(this).removeClass('icon'); $(this).addClass('iconspacer');}
30                 );
31
32         $('.sidebar-group-element').hover(
33                 function() {
34                         id = $(this).attr('id');
35                         $('#edit-' + id).addClass('icon'); $('#edit-' + id).removeClass('iconspacer');},
36
37                 function() {
38                         id = $(this).attr('id');
39                         $('#edit-' + id).removeClass('icon');$('#edit-' + id).addClass('iconspacer');}
40                 );
41
42
43         $('.savedsearchdrop').hover(
44                 function() {
45                         $(this).addClass('drop'); $(this).addClass('icon'); $(this).removeClass('iconspacer');},
46                 function() {
47                         $(this).removeClass('drop'); $(this).removeClass('icon'); $(this).addClass('iconspacer');}
48         );
49
50         $('.savedsearchterm').hover(
51                 function() {
52                         id = $(this).attr('id');
53                         $('#drop-' + id).addClass('icon');      $('#drop-' + id).addClass('drophide'); $('#drop-' + id).removeClass('iconspacer');},
54
55                 function() {
56                         id = $(this).attr('id');
57                         $('#drop-' + id).removeClass('icon');$('#drop-' + id).removeClass('drophide'); $('#drop-' + id).addClass('iconspacer');}
58         );
59
60         $('#id_share').change(function() {
61
62                 if ($('#id_share').is(':checked')) {
63                         $('#acl-wrapper').show();
64                 } else {
65                         $('#acl-wrapper').hide();
66                 }
67         }).trigger('change');
68
69         if (typeof window.AjaxUpload != "undefined") {
70                 var uploader = new window.AjaxUpload(
71                         window.imageUploadButton,
72                         { action: 'wall_upload/'+window.nickname,
73                                 name: 'userfile',
74                                 onSubmit: function(file,ext) { $('#profile-rotator').show(); },
75                                 onComplete: function(file,response) {
76                                         addeditortext(window.jotId, response);
77                                         $('#profile-rotator').hide();
78                                 }
79                         }
80                 );
81
82                 if ($('#wall-file-upload').length) {
83                         var file_uploader = new window.AjaxUpload(
84                                 'wall-file-upload',
85                                 { action: 'wall_attach/'+window.nickname,
86                                         name: 'userfile',
87                                         onSubmit: function(file,ext) { $('#profile-rotator').show(); },
88                                         onComplete: function(file,response) {
89                                                 addeditortext(window.jotId, response);
90                                                 $('#profile-rotator').hide();
91                                         }
92                                 }
93                         );
94                 }
95         }
96
97
98         if (typeof window.aclInit !="undefined" && typeof acl=="undefined") {
99                 acl = new ACL(
100                         baseurl+"/acl",
101                         [ window.allowCID,window.allowGID,window.denyCID,window.denyGID ]
102                 );
103         }
104
105
106         if (window.aclType == "settings-head" || window.aclType == "photos_head" || window.aclType == "event_head") {
107                 $('#contact_allow, #contact_deny, #group_allow, #group_deny').change(function() {
108                         var selstr;
109                         $('#contact_allow option:selected, #contact_deny option:selected, #group_allow option:selected, #group_deny option:selected').each( function() {
110                                 selstr = $(this).text();
111                                 $('#jot-perms-icon').removeClass('unlock').addClass('lock');
112                                 $('#jot-public').hide();
113                         });
114                         if (selstr == null) {
115                                 $('#jot-perms-icon').removeClass('lock').addClass('unlock');
116                                 $('#jot-public').show();
117                         }
118
119                 }).trigger('change');
120         }
121
122         if (window.aclType == "event_head") {
123                 $('#events-calendar').fullCalendar({
124                         events: baseurl + window.eventModuleUrl +'/json/',
125                         header: {
126                                 left: 'prev,next today',
127                                 center: 'title',
128                                 right: 'month,agendaWeek,agendaDay'
129                         },
130                         timeFormat: 'H(:mm)',
131                         eventClick: function(calEvent, jsEvent, view) {
132                                 showEvent(calEvent.id);
133                         },
134                         loading: function(isLoading, view) {
135                                 if (!isLoading) {
136                                         $('td.fc-day').dblclick(function() { window.location.href='/events/new?start='+$(this).data('date'); });
137                                 }
138                         },
139
140                         eventRender: function(event, element, view) {
141                                 //console.log(view.name);
142                                 if (event.item['author-name']==null) return;
143                                 switch(view.name) {
144                                         case "month":
145                                         element.find(".fc-title").html(
146                                                 "<img src='{0}' style='height:10px;width:10px'>{1} : {2}".format(
147                                                         event.item['author-avatar'],
148                                                         event.item['author-name'],
149                                                         event.title
150                                         ));
151                                         break;
152                                         case "agendaWeek":
153                                         element.find(".fc-title").html(
154                                                 "<img src='{0}' style='height:12px; width:12px'>{1}<p>{2}</p><p>{3}</p>".format(
155                                                         event.item['author-avatar'],
156                                                         event.item['author-name'],
157                                                         event.item.desc,
158                                                         event.item.location
159                                         ));
160                                         break;
161                                         case "agendaDay":
162                                         element.find(".fc-title").html(
163                                                 "<img src='{0}' style='height:24px;width:24px'>{1}<p>{2}</p><p>{3}</p>".format(
164                                                         event.item['author-avatar'],
165                                                         event.item['author-name'],
166                                                         event.item.desc,
167                                                         event.item.location
168                                         ));
169                                         break;
170                                 }
171                         }
172
173                 });
174
175                 // center on date
176                 var args=location.href.replace(baseurl,"").split("/");
177                 if (args.length>=5 && window.eventModeParams == 2) {
178                         $("#events-calendar").fullCalendar('gotoDate',args[3] , args[4]-1);
179                 } else if (args.length>=4 && window.eventModeParams == 1) {
180                         $("#events-calendar").fullCalendar('gotoDate',args[2] , args[3]-1);
181                 }
182
183                 // show event popup
184                 var hash = location.hash.split("-")
185                 if (hash.length==2 && hash[0]=="#link") showEvent(hash[1]);
186         }
187
188
189         switch(window.autocompleteType) {
190                 case 'msg-header':
191                         $("#recip").name_autocomplete(baseurl + '/acl', '', false, function(data) {
192                                         $("#recip-complete").val(data.id);
193                         });
194                         break;
195                 case 'contacts-head':
196                         $("#contacts-search").contact_autocomplete(baseurl + '/acl', 'a', true);
197
198
199                         $("#contacts-search").keyup(function(event) {
200                                 if (event.keyCode == 13) {
201                                         $("#contacts-search").click();
202                                 }
203                         });
204                         $(".autocomplete-w1 .selected").keyup(function(event) {
205                                 if (event.keyCode == 13) {
206                                         $("#contacts-search").click();
207                                 }
208                         });
209                         break;
210                 case 'display-head':
211                         $(".comment-wwedit-wrapper textarea").editor_autocomplete(baseurl+"/acl");
212                         break;
213                 default:
214                         break;
215         }
216
217         // Add Colorbox for viewing Network page images
218         //var cBoxClasses = new Array();
219         $(".wall-item-body a img").each(function() {
220                 var aElem = $(this).parent();
221                 var imgHref = aElem.attr("href");
222
223                 // We need to make sure we only put a Colorbox on links to Friendica images
224                 // We'll try to do this by looking for links of the form
225                 // .../photo/ab803d8eg08daf85023adfec08 (with nothing more following), in hopes
226                 // that that will be unique enough
227                 if (imgHref.match(/\/photo\/[a-fA-F0-9]+(-[0-9]\.[\w]+?)?$/)) {
228
229                         // Add a unique class to all the images of a certain post, to allow scrolling through
230                         var cBoxClass = $(this).closest(".wall-item-body").attr("id") + "-lightbox";
231                         $(this).addClass(cBoxClass);
232
233                         aElem.colorbox({
234                                 maxHeight: '90%',
235                                 photo: true, // Colorbox doesn't recognize a URL that don't end in .jpg, etc. as a photo
236                                 rel: cBoxClass //$(this).attr("class").match(/wall-item-body-[\d]+-lightbox/)[0]
237                         });
238                 }
239         });
240 });
241
242
243 // update pending count //
244 $(function() {
245
246         $("nav").bind('nav-update',  function(e,data) {
247                 var elm = $('#pending-update');
248                 var register = $(data).find('register').text();
249                 if (register=="0") { register=""; elm.hide();} else { elm.show(); }
250                 elm.html(register);
251         });
252 });
253
254
255 $(function() {
256
257         $("#cnftheme").click(function() {
258                 $.colorbox({
259                         width: 800,
260                         height: '90%',
261                         href: baseurl + "/admin/themes/" + $("#id_theme :selected").val(),
262                         onComplete: function() {
263                                 $("div#fancybox-content form").submit(function(e) {
264                                         var url = $(this).attr('action');
265                                         // can't get .serialize() to work...
266                                         var data={};
267                                         $(this).find("input").each(function() {
268                                                 data[$(this).attr('name')] = $(this).val();
269                                         });
270                                         $(this).find("select").each(function() {
271                                                 data[$(this).attr('name')] = $(this).children(":selected").val();
272                                         });
273                                         console.log(":)", url, data);
274
275                                         $.post(url, data, function(data) {
276                                                 if (timer) {
277                                                         clearTimeout(timer);
278                                                 }
279                                                 NavUpdate();
280                                                 $.colorbox.close();
281                                         })
282
283                                         return false;
284                                 });
285
286                         }
287                 });
288                 return false;
289         });
290 });
291
292
293 function homeRedirect() {
294         $('html').fadeOut('slow', function() {
295                 window.location = baseurl + "/login";
296         });
297 }
298
299
300 if (typeof window.photoEdit != 'undefined') {
301
302         $(document).keydown(function(event) {
303
304                         if (window.prevLink != '') { if (event.ctrlKey && event.keyCode == 37) { event.preventDefault(); window.location.href = window.prevLink; }}
305                         if (window.nextLink != '') { if (event.ctrlKey && event.keyCode == 39) { event.preventDefault(); window.location.href = window.nextLink; }}
306
307         });
308 }
309
310 function showEvent(eventid) {
311         $.get(
312                 baseurl + window.eventModuleUrl + '/?id=' + eventid,
313                 function(data) {
314                         $.colorbox({html:data});
315                         $.colorbox.resize();
316                 }
317         );
318 }
319
320 function doEventPreview() {
321         $('#event-edit-preview').val(1);
322         $.post('events',$('#event-edit-form').serialize(), function(data) {
323                 $.colorbox({ html: data });
324         });
325         $('#event-edit-preview').val(0);
326 }
327
328 function initCrop() {
329         function onEndCrop( coords, dimensions ) {
330                 $PR( 'x1' ).value = coords.x1;
331                 $PR( 'y1' ).value = coords.y1;
332                 $PR( 'x2' ).value = coords.x2;
333                 $PR( 'y2' ).value = coords.y2;
334                 $PR( 'width' ).value = dimensions.width;
335                 $PR( 'height' ).value = dimensions.height;
336         }
337
338         Event.observe( window, 'load', function() {
339                 new Cropper.ImgWithPreview(
340                 'croppa',
341                 {
342                         previewWrap: 'previewWrap',
343                         minWidth: 175,
344                         minHeight: 175,
345                         maxWidth: 640,
346                         maxHeight: 640,
347                         ratioDim: { x: 100, y:100 },
348                         displayOnInit: true,
349                         onEndCrop: onEndCrop
350                 });
351         });
352 }
353
354 function showNavMenu(menuID) {
355
356         if (window.navMenuTimeout[menuID + '-closing']) {
357                 window.navMenuTimeout[menuID + '-closing'] = false;
358                 clearTimeout(window.navMenuTimeout[menuID + '-timeout']);
359         } else {
360                 window.navMenuTimeout[menuID + '-opening'] = true;
361
362                 window.navMenuTimeout[menuID + '-timeout'] = setTimeout( function () {
363                         $(menuID).slideDown('fast').show();
364                         window.navMenuTimeout[menuID + '-opening'] = false;
365                 }, 200);
366         }
367 }
368
369 function hideNavMenu(menuID) {
370
371         if (window.navMenuTimeout[menuID + '-opening']) {
372                 window.navMenuTimeout[menuID + '-opening'] = false;
373                 clearTimeout(window.navMenuTimeout[menuID + '-timeout']);
374         } else {
375                 window.navMenuTimeout[menuID + '-closing'] = true;
376
377                 window.navMenuTimeout[menuID + '-timeout'] = setTimeout( function () {
378                         $(menuID).slideUp('fast');
379                         window.navMenuTimeout[menuID + '-closing'] = false;
380                 }, 500);
381         }
382 }
383
384
385
386 /*
387  * Editor
388  */
389
390 var editor = false;
391 var textlen = 0;
392
393 function initEditor(callback) {
394         if(editor == false) {
395                 $("#profile-jot-text-loading").show();
396
397                 $("#profile-jot-text-loading").hide();
398                 $("#profile-jot-text").css({ 'height': 200, 'color': '#000' });
399                 $("#profile-jot-text").editor_autocomplete(baseurl+"/acl");
400                 $(".jothidden").show();
401                 // setup acl popup
402                 $("a#jot-perms-icon").colorbox({
403                         'inline' : true,
404                         'transition' : 'elastic'
405                 });
406
407                 editor = true;
408         }
409         if (typeof callback != "undefined") {
410                 callback();
411         }
412 }
413
414 function enableOnUser() {
415         if (editor) {
416                 return;
417         }
418         $(this).val("");
419         initEditor();
420 }
421
422 function msgInitEditor() {
423         $("#prvmail-text").editor_autocomplete(baseurl+"/acl");
424 }
425
426 /*
427  * Jot
428  */
429
430 function addeditortext(textElem, data) {
431         var currentText = $(textElem).val();
432         $(textElem).val(currentText + data);
433 }
434
435 function jotVideoURL() {
436         reply = prompt(window.vidURL);
437         if (reply && reply.length) {
438                 addeditortext("#profile-jot-text", '[video]' + reply + '[/video]');
439         }
440 }
441
442 function jotAudioURL() {
443         reply = prompt(window.audURL);
444         if (reply && reply.length) {
445                 addeditortext("#profile-jot-text", '[audio]' + reply + '[/audio]');
446         }
447 }
448
449
450 function jotGetLocation() {
451         reply = prompt(window.whereAreU, $('#jot-location').val());
452         if (reply && reply.length) {
453                 $('#jot-location').val(reply);
454         }
455 }
456
457 function jotShare(id) {
458         if ($('#jot-popup').length != 0) $('#jot-popup').show();
459
460         $('#like-rotator-' + id).show();
461         $.get('share/' + id, function(data) {
462                 if (!editor) $("#profile-jot-text").val("");
463                 initEditor(function() {
464                         addeditortext("#profile-jot-text", data);
465                         $('#like-rotator-' + id).hide();
466                         $(window).scrollTop(0);
467                 });
468
469         });
470 }
471
472 function jotClearLocation() {
473         $('#jot-coord').val('');
474         $('#profile-nolocation-wrapper').hide();
475 }
476
477
478 function jotGetLink() {
479         reply = prompt(window.linkURL);
480         if (reply && reply.length) {
481                 reply = bin2hex(reply);
482                 $('#profile-rotator').show();
483                 $.get('parse_url?binurl=' + reply, function(data) {
484                         addeditortext(window.jotId, data);
485                         $('#profile-rotator').hide();
486                 });
487         }
488 }
489
490
491 function linkdropper(event) {
492         var linkFound = event.dataTransfer.types.contains("text/uri-list");
493         if (linkFound)
494                 event.preventDefault();
495 }
496
497
498 function linkdrop(event) {
499         var reply = event.dataTransfer.getData("text/uri-list");
500         event.preventDefault();
501         if (reply && reply.length) {
502                 reply = bin2hex(reply);
503                 $('#profile-rotator').show();
504                 $.get('parse_url?binurl=' + reply, function(data) {
505                         addeditortext(window.jotId, data);
506                         $('#profile-rotator').hide();
507                 });
508         }
509 }
510
511
512 if (typeof window.geoTag === 'function') window.geoTag();
513
514
515 /*
516  * Items
517  */
518
519 function confirmDelete() { return confirm(window.delItem); }
520
521 function deleteCheckedItems(delID) {
522         if (confirm(window.delItems)) {
523                 var checkedstr = '';
524
525                 $(delID).hide();
526                 $(delID + '-rotator').show();
527                 $('.item-select').each( function() {
528                         if ($(this).is(':checked')) {
529                                 if (checkedstr.length != 0) {
530                                         checkedstr = checkedstr + ',' + $(this).val();
531                                 } else {
532                                         checkedstr = $(this).val();
533                                 }
534                         }
535                 });
536                 $.post('item', { dropitems: checkedstr }, function(data) {
537                         window.location.reload();
538                 });
539         }
540 }
541
542 function itemTag(id) {
543         reply = prompt(window.term);
544         if (reply && reply.length) {
545                 reply = reply.replace('#','');
546                 if (reply.length) {
547
548                         commentBusy = true;
549                         $('body').css('cursor', 'wait');
550
551                         $.get('tagger/' + id + '?term=' + reply, NavUpdate);
552                         liking = 1;
553                 }
554         }
555 }
556
557 function itemFiler(id) {
558
559         var bordercolor = $("input").css("border-color");
560
561         $.get('filer/', function(data) {
562                 $.colorbox({html:data});
563                 $.colorbox.resize();
564                 $("#id_term").keypress(function() {
565                         $(this).css("border-color",bordercolor);
566                 })
567                 $("#select_term").change(function() {
568                         $("#id_term").css("border-color",bordercolor);
569                 })
570
571                 $("#filer_save").click(function(e) {
572                         e.preventDefault();
573                         reply = $("#id_term").val();
574                         if (reply && reply.length) {
575                                 commentBusy = true;
576                                 $('body').css('cursor', 'wait');
577                                 $.get('filer/' + id + '?term=' + reply, NavUpdate);
578                                 liking = 1;
579                                 $.colorbox.close();
580                         } else {
581                                 $("#id_term").css("border-color","#FF0000");
582                         }
583                         return false;
584                 });
585         });
586
587 }
588
589
590 /*
591  * Comments
592  */
593
594 function insertFormatting(BBcode, id) {
595         var tmpStr = $("#comment-edit-text-" + id).val();
596         if (tmpStr == "") {
597                 $("#comment-edit-text-" + id).addClass("comment-edit-text-full");
598                 $("#comment-edit-text-" + id).removeClass("comment-edit-text-empty");
599                 openMenu("comment-edit-submit-wrapper-" + id);
600         }
601
602         textarea = document.getElementById("comment-edit-text-" +id);
603         if (document.selection) {
604                 textarea.focus();
605                 selected = document.selection.createRange();
606                 selected.text = "["+BBcode+"]" + selected.text + "[/"+BBcode+"]";
607         } else if (textarea.selectionStart || textarea.selectionStart == "0") {
608                 var start = textarea.selectionStart;
609                 var end = textarea.selectionEnd;
610                 textarea.value = textarea.value.substring(0, start) + "["+BBcode+"]" + textarea.value.substring(start, end) + "[/"+BBcode+"]" + textarea.value.substring(end, textarea.value.length);
611         }
612
613         return true;
614 }
615
616 function cmtBbOpen(id) {
617         $("#comment-edit-bb-" + id).show();
618 }
619 function cmtBbClose(id) {
620         $("#comment-edit-bb-" + id).hide();
621 }
622
623 function commentOpen(obj,id) {
624         if (obj.value == "") {
625                 $("#comment-edit-text-" + id).addClass("comment-edit-text-full");
626                 $("#comment-edit-text-" + id).removeClass("comment-edit-text-empty");
627                 $("#mod-cmnt-wrap-" + id).show();
628                 openMenu("comment-edit-submit-wrapper-" + id);
629         }
630 }
631 function commentClose(obj,id) {
632         if (obj.value == "") {
633                 $("#comment-edit-text-" + id).removeClass("comment-edit-text-full");
634                 $("#comment-edit-text-" + id).addClass("comment-edit-text-empty");
635                 $("#mod-cmnt-wrap-" + id).hide();
636                 closeMenu("comment-edit-submit-wrapper-" + id);
637         }
638 }
639
640
641 function commentInsert(obj,id) {
642         var tmpStr = $("#comment-edit-text-" + id).val();
643         if (tmpStr == "") {
644                 $("#comment-edit-text-" + id).addClass("comment-edit-text-full");
645                 $("#comment-edit-text-" + id).removeClass("comment-edit-text-empty");
646                 openMenu("comment-edit-submit-wrapper-" + id);
647         }
648         var ins = $(obj).html();
649         ins = ins.replace("&lt;","<");
650         ins = ins.replace("&gt;",">");
651         ins = ins.replace("&amp;","&");
652         ins = ins.replace("&quot;",'"');
653         $("#comment-edit-text-" + id).val(tmpStr + ins);
654 }
655
656 function qCommentInsert(obj,id) {
657         var tmpStr = $("#comment-edit-text-" + id).val();
658         if (tmpStr == "") {
659                 $("#comment-edit-text-" + id).addClass("comment-edit-text-full");
660                 $("#comment-edit-text-" + id).removeClass("comment-edit-text-empty");
661                 openMenu("comment-edit-submit-wrapper-" + id);
662         }
663         var ins = $(obj).val();
664         ins = ins.replace("&lt;","<");
665         ins = ins.replace("&gt;",">");
666         ins = ins.replace("&amp;","&");
667         ins = ins.replace("&quot;",'"');
668         $("#comment-edit-text-" + id).val(tmpStr + ins);
669         $(obj).val("");
670 }