Merge pull request #3120 from tobiasd/20170128-editor
[friendica.git/.git] / view / theme / frost-mobile / js / theme.js
1 $(document).ready(function() {
2
3         /* enable editor on focus and click */
4         $("#profile-jot-text").focus(enableOnUser);
5         $("#profile-jot-text").click(enableOnUser);
6
7         $('#event-share-checkbox').change(function() {
8                 if ($('#event-share-checkbox').is(':checked')) {
9                         $('#acl-wrapper').show();
10                 }
11                 else {
12                         $('#acl-wrapper').hide();
13                 }
14         }).trigger('change');
15
16         $(".popupbox").click(function () {
17                 var parent = $( $(this).attr('href') ).parent();
18                 if (parent.css('display') == 'none') {
19                         parent.show();
20                 } else {
21                         parent.hide();
22                 }
23                 return false;
24         });
25
26
27
28         if (typeof window.AjaxUpload != "undefined") {
29                 var uploader = new window.AjaxUpload(
30                         window.imageUploadButton,
31                         { action: 'wall_upload/' + window.nickname,
32                                 name: 'userfile',
33                                 onSubmit: function(file,ext) { $('#profile-rotator').show(); },
34                                 onComplete: function(file,response) {
35                                         addeditortext(window.jotId, response);
36                                         $('#profile-rotator').hide();
37                                 }
38                         }
39                 );
40
41                 if ($('#wall-file-upload').length) {
42                         var file_uploader = new window.AjaxUpload(
43                                 'wall-file-upload',
44                                 { action: 'wall_attach/' + window.nickname,
45                                         name: 'userfile',
46                                         onSubmit: function(file,ext) { $('#profile-rotator').show(); },
47                                         onComplete: function(file,response) {
48                                                 addeditortext(window.jotId, response);
49                                                 $('#profile-rotator').hide();
50                                         }
51                                 }
52                         );
53                 }
54         }
55
56
57         if (typeof window.aclInit !="undefined" && typeof acl=="undefined") {
58                 acl = new ACL(
59                         baseurl+"/acl",
60                         [ window.allowCID,window.allowGID,window.denyCID,window.denyGID ]
61                 );
62         }
63
64         switch(window.autocompleteType) {
65                 case 'msg-header':
66                         $("#recip").name_autocomplete(baseurl + '/acl', '', false, function(data) {
67                                         $("#recip-complete").val(data.id);
68                         });
69                         break;
70                 case 'contacts-head':
71                         $("#contacts-search").contact_autocomplete(baseurl + '/acl', 'a', true);
72
73
74                         $("#contacts-search").keyup(function(event) {
75                                 if (event.keyCode == 13) {
76                                         $("#contacts-search").click();
77                                 }
78                         });
79                         $(".autocomplete-w1 .selected").keyup(function(event) {
80                                 if (event.keyCode == 13) {
81                                         $("#contacts-search").click();
82                                 }
83                         });
84                         break;
85                 case 'display-head':
86                         $(".comment-wwedit-wrapper textarea").editor_autocomplete(baseurl+"/acl");
87                         break;
88                 default:
89                         break;
90         }
91
92         if (window.aclType == "settings-head" || window.aclType == "photos_head" || window.aclType == "event_head") {
93                 $('#contact_allow, #contact_deny, #group_allow, #group_deny').change(function() {
94                         var selstr;
95                         $('#contact_allow option:selected, #contact_deny option:selected, #group_allow option:selected, #group_deny option:selected').each( function() {
96                                 selstr = $(this).text();
97                                 $('#jot-perms-icon').removeClass('unlock').addClass('lock');
98                                 $('#jot-public').hide();
99                         });
100                         if (selstr == null) {
101                                 $('#jot-perms-icon').removeClass('lock').addClass('unlock');
102                                 $('#jot-public').show();
103                         }
104
105                 }).trigger('change');
106         }
107
108         if (window.aclType == "event_head") {
109                 $('#events-calendar').fullCalendar({
110                         events: baseurl + window.eventModuleUrl +'/json/',
111                         header: {
112                                 left: 'prev,next today',
113                                 center: 'title',
114                                 right: 'month,agendaWeek,agendaDay'
115                         },
116                         timeFormat: 'H(:mm)',
117                         eventClick: function(calEvent, jsEvent, view) {
118                                 showEvent(calEvent.id);
119                         },
120
121                         eventRender: function(event, element, view) {
122                                 //console.log(view.name);
123                                 if (event.item['author-name']==null) return;
124                                 switch(view.name) {
125                                         case "month":
126                                         element.find(".fc-title").html(
127                                                 "<img src='{0}' style='height:10px;width:10px'>{1} : {2}".format(
128                                                         event.item['author-avatar'],
129                                                         event.item['author-name'],
130                                                         event.title
131                                         ));
132                                         break;
133                                         case "agendaWeek":
134                                         element.find(".fc-title").html(
135                                                 "<img src='{0}' style='height:12px; width:12px'>{1}<p>{2}</p><p>{3}</p>".format(
136                                                         event.item['author-avatar'],
137                                                         event.item['author-name'],
138                                                         event.item.desc,
139                                                         event.item.location
140                                         ));
141                                         break;
142                                         case "agendaDay":
143                                         element.find(".fc-title").html(
144                                                 "<img src='{0}' style='height:24px;width:24px'>{1}<p>{2}</p><p>{3}</p>".format(
145                                                         event.item['author-avatar'],
146                                                         event.item['author-name'],
147                                                         event.item.desc,
148                                                         event.item.location
149                                         ));
150                                         break;
151                                 }
152                         }
153
154                 });
155
156                 // center on date
157                 var args=location.href.replace(baseurl,"").split("/");
158                 if (args.length>=5 && window.eventModeParams == 2) {
159                         $("#events-calendar").fullCalendar('gotoDate',args[3] , args[4]-1);
160                 } else if (args.length>=4 && window.eventModeParams == 1) {
161                         $("#events-calendar").fullCalendar('gotoDate',args[2] , args[3]-1);
162                 }
163
164                 // show event popup
165                 var hash = location.hash.split("-")
166                 if (hash.length==2 && hash[0]=="#link") showEvent(hash[1]);
167         }
168
169 });
170
171 // update pending count //
172 $(function() {
173
174         $("nav").bind('nav-update',  function(e,data) {
175                 var elm = $('#pending-update');
176                 var register = $(data).find('register').text();
177                 if (register=="0") { register=""; elm.hide();} else { elm.show(); }
178                 elm.html(register);
179         });
180 });
181
182
183
184 function homeRedirect() {
185         $('html').fadeOut('slow', function() {
186                 window.location = baseurl + "/login";
187         });
188 }
189
190
191 function initCrop() {
192         function onEndCrop( coords, dimensions ) {
193                 $PR( 'x1' ).value = coords.x1;
194                 $PR( 'y1' ).value = coords.y1;
195                 $PR( 'x2' ).value = coords.x2;
196                 $PR( 'y2' ).value = coords.y2;
197                 $PR( 'width' ).value = dimensions.width;
198                 $PR( 'height' ).value = dimensions.height;
199         }
200
201         Event.observe( window, 'load', function() {
202                 new Cropper.ImgWithPreview(
203                 'croppa',
204                 {
205                         previewWrap: 'previewWrap',
206                         minWidth: 175,
207                         minHeight: 175,
208                         maxWidth: 640,
209                         maxHeight: 640,
210                         ratioDim: { x: 100, y:100 },
211                         displayOnInit: true,
212                         onEndCrop: onEndCrop
213                 });
214         });
215 }
216
217
218 function showEvent(eventid) {
219 }
220
221 /*
222  * Editor
223  */
224 var editor = false;
225 var textlen = 0;
226
227 function initEditor(callback){
228         if (editor == false) {
229                 $("#profile-jot-text").css({ 'height': 200, 'color': '#000' });
230                 $("#profile-jot-text").editor_autocomplete(baseurl+"/acl");
231                 $("a#jot-perms-icon, a#settings-default-perms-menu").click(function () {
232                         var parent = $("#profile-jot-acl-wrapper").parent();
233                         if (parent.css('display') == 'none') {
234                                 parent.show();
235                         } else {
236                                 parent.hide();
237                         }
238                         return false;
239                 });
240                 $(".jothidden").show();
241
242                 editor = true;
243         }
244         if (typeof callback != "undefined") {
245                 callback();
246         }
247 }
248
249 function enableOnUser(){
250         if (editor) {
251                 return;
252         }
253         $(this).val("");
254         initEditor();
255 }
256
257 /*
258  * Jot
259  */
260
261 function addeditortext(textElem, data) {
262         var currentText = $(textElem).val();
263         $(textElem).val(currentText + data);
264 }
265
266 function jotVideoURL() {
267         reply = prompt(window.vidURL);
268         if (reply && reply.length) {
269                 addeditortext("#profile-jot-text", '[video]' + reply + '[/video]');
270         }
271 }
272
273 function jotAudioURL() {
274         reply = prompt(window.audURL);
275         if (reply && reply.length) {
276                 addeditortext("#profile-jot-text", '[audio]' + reply + '[/audio]');
277         }
278 }
279
280
281 function jotGetLocation() {
282         reply = prompt(window.whereAreU, $('#jot-location').val());
283         if (reply && reply.length) {
284                 $('#jot-location').val(reply);
285         }
286 }
287
288 function jotShare(id) {
289         if ($('#jot-popup').length != 0) $('#jot-popup').show();
290
291         $('#like-rotator-' + id).show();
292         $.get('share/' + id, function(data) {
293                 if (!editor) $("#profile-jot-text").val("");
294                 initEditor(function() {
295                         addeditortext("#profile-jot-text", data);
296                         $('#like-rotator-' + id).hide();
297                         $(window).scrollTop(0);
298                 });
299
300         });
301 }
302
303 function jotGetLink() {
304         reply = prompt(window.linkURL);
305         if (reply && reply.length) {
306                 reply = bin2hex(reply);
307                 $('#profile-rotator').show();
308                 $.get('parse_url?binurl=' + reply, function(data) {
309                         addeditortext(window.jotId, data);
310                         $('#profile-rotator').hide();
311                 });
312         }
313 }
314
315 function jotClearLocation() {
316         $('#jot-coord').val('');
317         $('#profile-nolocation-wrapper').hide();
318 }
319
320 if (typeof window.geoTag === 'function') window.geoTag();
321
322
323
324 /*
325  * Items
326  */
327
328 function confirmDelete() { return confirm(window.delItem); }
329
330 function itemTag(id) {
331         reply = prompt(window.term);
332         if (reply && reply.length) {
333                 reply = reply.replace('#','');
334                 if (reply.length) {
335
336                         commentBusy = true;
337                         $('body').css('cursor', 'wait');
338
339                         $.get('tagger/' + id + '?term=' + reply, NavUpdate);
340                         /*if (timer) clearTimeout(timer);
341                         timer = setTimeout(NavUpdate,3000);*/
342                         liking = 1;
343                 }
344         }
345 }
346
347 function itemFiler(id) {
348         $.get('filer/', function(data) {
349
350                 var promptText = $('#id_term_label', data).text();
351
352                 reply = prompt(promptText);
353                 if (reply && reply.length) {
354                         commentBusy = true;
355                         $('body').css('cursor', 'wait');
356                         $.get('filer/' + id + '?term=' + reply, NavUpdate);
357                         liking = 1;
358                 }
359         });
360 }
361
362 /*
363  * Comments
364  */
365 function commentOpen(obj,id) {
366         if (obj.value == "") {
367                 $("#comment-edit-text-" + id).addClass("comment-edit-text-full");
368                 $("#comment-edit-text-" + id).removeClass("comment-edit-text-empty");
369                 $("#mod-cmnt-wrap-" + id).show();
370                 openMenu("comment-edit-submit-wrapper-" + id);
371         }
372 }
373 function commentClose(obj,id) {
374         if (obj.value == "") {
375                 $("#comment-edit-text-" + id).removeClass("comment-edit-text-full");
376                 $("#comment-edit-text-" + id).addClass("comment-edit-text-empty");
377                 $("#mod-cmnt-wrap-" + id).hide();
378                 closeMenu("comment-edit-submit-wrapper-" + id);
379         }
380 }
381
382
383 function commentInsert(obj,id) {
384         var tmpStr = $("#comment-edit-text-" + id).val();
385         if (tmpStr == "") {
386                 $("#comment-edit-text-" + id).addClass("comment-edit-text-full");
387                 $("#comment-edit-text-" + id).removeClass("comment-edit-text-empty");
388                 openMenu("comment-edit-submit-wrapper-" + id);
389         }
390         var ins = $(obj).html();
391         ins = ins.replace("&lt;","<");
392         ins = ins.replace("&gt;",">");
393         ins = ins.replace("&amp;","&");
394         ins = ins.replace("&quot;",'"');
395         $("#comment-edit-text-" + id).val(tmpStr + ins);
396 }
397
398 function qCommentInsert(obj,id) {
399         var tmpStr = $("#comment-edit-text-" + id).val();
400         if (tmpStr == "") {
401                 $("#comment-edit-text-" + id).addClass("comment-edit-text-full");
402                 $("#comment-edit-text-" + id).removeClass("comment-edit-text-empty");
403                 openMenu("comment-edit-submit-wrapper-" + id);
404         }
405         var ins = $(obj).val();
406         ins = ins.replace("&lt;","<");
407         ins = ins.replace("&gt;",">");
408         ins = ins.replace("&amp;","&");
409         ins = ins.replace("&quot;",'"');
410         $("#comment-edit-text-" + id).val(tmpStr + ins);
411         $(obj).val("");
412 }
413
414 function insertFormatting(BBcode, id) {
415         var tmpStr = $("#comment-edit-text-" + id).val();
416         if (tmpStr == "") {
417                 $("#comment-edit-text-" + id).addClass("comment-edit-text-full");
418                 $("#comment-edit-text-" + id).removeClass("comment-edit-text-empty");
419                 openMenu("comment-edit-submit-wrapper-" + id);
420         }
421
422         textarea = document.getElementById("comment-edit-text-" +id);
423         if (document.selection) {
424                 textarea.focus();
425                 selected = document.selection.createRange();
426                 if (BBcode == "url") {
427                         selected.text = "["+BBcode+"=http://]" +  selected.text + "[/"+BBcode+"]";
428                 } else {
429                         selected.text = "["+BBcode+"]" + selected.text + "[/"+BBcode+"]";
430                 }
431         } else if (textarea.selectionStart || textarea.selectionStart == "0") {
432                 var start = textarea.selectionStart;
433                 var end = textarea.selectionEnd;
434                 if (BBcode == "url") {
435                         textarea.value = textarea.value.substring(0, start) + "["+BBcode+"=http://]" + textarea.value.substring(start, end) + "[/"+BBcode+"]" + textarea.value.substring(end, textarea.value.length);
436                 } else {
437                         textarea.value = textarea.value.substring(0, start) + "["+BBcode+"]" + textarea.value.substring(start, end) + "[/"+BBcode+"]" + textarea.value.substring(end, textarea.value.length);
438                 }
439         }
440
441         return true;
442 }
443
444 function cmtBbOpen(id) {
445         $(".comment-edit-bb-" + id).show();
446 }
447 function cmtBbClose(id) {
448         $(".comment-edit-bb-" + id).hide();
449 }