Check for existing element in openMenu()
[friendica.git/.git] / view / theme / frost-mobile / js / theme.js
1 $(document).ready(function() {
2
3         /* enable tinymce 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+'?nomce=1',
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+'?nomce=1',
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
223 /*
224  * TinyMCE/Editor
225  */
226
227 var editor=false;
228 var textlen = 0;
229 var plaintext = 'none';//window.editSelect;
230 //var ispublic = window.isPublic;
231
232 function initEditor(cb) {
233         if (editor==false) {
234                 if (plaintext == 'none') {
235                         $("#profile-jot-text").css({ 'height': 200, 'color': '#000' });
236                         $("#profile-jot-text").editor_autocomplete(baseurl+"/acl");
237                         editor = true;
238
239                         $("a#jot-perms-icon, a#settings-default-perms-menu").click(function () {
240                                 var parent = $("#profile-jot-acl-wrapper").parent();
241                                 if (parent.css('display') == 'none') {
242                                         parent.show();
243                                 } else {
244                                         parent.hide();
245                                 }
246
247                                 return false;
248                         });
249                         $(".jothidden").show();
250                         if (typeof cb!="undefined") {
251                                 cb();
252                         }
253                         return;
254                 }
255         } else {
256                 if (typeof cb!="undefined") cb();
257         }
258 }
259
260 function enableOnUser() {
261         if (editor) {
262                 return;
263         }
264         $(this).val("");
265         initEditor();
266 }
267
268 /*
269  * Jot
270  */
271
272 function addeditortext(textElem, data) {
273         if (window.editSelect == 'none') {
274                 var currentText = $(textElem).val();
275                 $(textElem).val(currentText + data);
276         }
277 }
278
279 function jotVideoURL() {
280         reply = prompt(window.vidURL);
281         if (reply && reply.length) {
282                 addeditortext("#profile-jot-text", '[video]' + reply + '[/video]');
283         }
284 }
285
286 function jotAudioURL() {
287         reply = prompt(window.audURL);
288         if (reply && reply.length) {
289                 addeditortext("#profile-jot-text", '[audio]' + reply + '[/audio]');
290         }
291 }
292
293
294 function jotGetLocation() {
295         reply = prompt(window.whereAreU, $('#jot-location').val());
296         if (reply && reply.length) {
297                 $('#jot-location').val(reply);
298         }
299 }
300
301 function jotShare(id) {
302         if ($('#jot-popup').length != 0) $('#jot-popup').show();
303
304         $('#like-rotator-' + id).show();
305         $.get('share/' + id, function(data) {
306                 if (!editor) $("#profile-jot-text").val("");
307                 initEditor(function() {
308                         addeditortext("#profile-jot-text", data);
309                         $('#like-rotator-' + id).hide();
310                         $(window).scrollTop(0);
311                 });
312
313         });
314 }
315
316 function jotGetLink() {
317         reply = prompt(window.linkURL);
318         if (reply && reply.length) {
319                 reply = bin2hex(reply);
320                 $('#profile-rotator').show();
321                 $.get('parse_url?binurl=' + reply, function(data) {
322                         addeditortext(window.jotId, data);
323                         $('#profile-rotator').hide();
324                 });
325         }
326 }
327
328 function jotClearLocation() {
329         $('#jot-coord').val('');
330         $('#profile-nolocation-wrapper').hide();
331 }
332
333 if (typeof window.geoTag === 'function') window.geoTag();
334
335
336
337 /*
338  * Items
339  */
340
341 function confirmDelete() { return confirm(window.delItem); }
342
343 function itemTag(id) {
344         reply = prompt(window.term);
345         if (reply && reply.length) {
346                 reply = reply.replace('#','');
347                 if (reply.length) {
348
349                         commentBusy = true;
350                         $('body').css('cursor', 'wait');
351
352                         $.get('tagger/' + id + '?term=' + reply, NavUpdate);
353                         /*if (timer) clearTimeout(timer);
354                         timer = setTimeout(NavUpdate,3000);*/
355                         liking = 1;
356                 }
357         }
358 }
359
360 function itemFiler(id) {
361         $.get('filer/', function(data) {
362
363                 var promptText = $('#id_term_label', data).text();
364
365                 reply = prompt(promptText);
366                 if (reply && reply.length) {
367                         commentBusy = true;
368                         $('body').css('cursor', 'wait');
369                         $.get('filer/' + id + '?term=' + reply, NavUpdate);
370                         liking = 1;
371                 }
372         });
373 }
374
375 /*
376  * Comments
377  */
378 function commentOpen(obj,id) {
379         if (obj.value == "") {
380                 $("#comment-edit-text-" + id).addClass("comment-edit-text-full");
381                 $("#comment-edit-text-" + id).removeClass("comment-edit-text-empty");
382                 $("#mod-cmnt-wrap-" + id).show();
383                 openMenu("comment-edit-submit-wrapper-" + id);
384         }
385 }
386 function commentClose(obj,id) {
387         if (obj.value == "") {
388                 $("#comment-edit-text-" + id).removeClass("comment-edit-text-full");
389                 $("#comment-edit-text-" + id).addClass("comment-edit-text-empty");
390                 $("#mod-cmnt-wrap-" + id).hide();
391                 closeMenu("comment-edit-submit-wrapper-" + id);
392         }
393 }
394
395
396 function commentInsert(obj,id) {
397         var tmpStr = $("#comment-edit-text-" + id).val();
398         if (tmpStr == "") {
399                 $("#comment-edit-text-" + id).addClass("comment-edit-text-full");
400                 $("#comment-edit-text-" + id).removeClass("comment-edit-text-empty");
401                 openMenu("comment-edit-submit-wrapper-" + id);
402         }
403         var ins = $(obj).html();
404         ins = ins.replace("&lt;","<");
405         ins = ins.replace("&gt;",">");
406         ins = ins.replace("&amp;","&");
407         ins = ins.replace("&quot;",'"');
408         $("#comment-edit-text-" + id).val(tmpStr + ins);
409 }
410
411 function qCommentInsert(obj,id) {
412         var tmpStr = $("#comment-edit-text-" + id).val();
413         if (tmpStr == "") {
414                 $("#comment-edit-text-" + id).addClass("comment-edit-text-full");
415                 $("#comment-edit-text-" + id).removeClass("comment-edit-text-empty");
416                 openMenu("comment-edit-submit-wrapper-" + id);
417         }
418         var ins = $(obj).val();
419         ins = ins.replace("&lt;","<");
420         ins = ins.replace("&gt;",">");
421         ins = ins.replace("&amp;","&");
422         ins = ins.replace("&quot;",'"');
423         $("#comment-edit-text-" + id).val(tmpStr + ins);
424         $(obj).val("");
425 }
426
427 function insertFormatting(BBcode,id) {
428         var tmpStr = $("#comment-edit-text-" + id).val();
429         if (tmpStr == "") {
430                 $("#comment-edit-text-" + id).addClass("comment-edit-text-full");
431                 $("#comment-edit-text-" + id).removeClass("comment-edit-text-empty");
432                 openMenu("comment-edit-submit-wrapper-" + id);
433         }
434
435         textarea = document.getElementById("comment-edit-text-" +id);
436         if (document.selection) {
437                 textarea.focus();
438                 selected = document.selection.createRange();
439                 if (BBcode == "url") {
440                         selected.text = "["+BBcode+"=http://]" +  selected.text + "[/"+BBcode+"]";
441                 } else {
442                         selected.text = "["+BBcode+"]" + selected.text + "[/"+BBcode+"]";
443                 }
444         } else if (textarea.selectionStart || textarea.selectionStart == "0") {
445                 var start = textarea.selectionStart;
446                 var end = textarea.selectionEnd;
447                 if (BBcode == "url") {
448                         textarea.value = textarea.value.substring(0, start) + "["+BBcode+"=http://]" + textarea.value.substring(start, end) + "[/"+BBcode+"]" + textarea.value.substring(end, textarea.value.length);
449                 } else {
450                         textarea.value = textarea.value.substring(0, start) + "["+BBcode+"]" + textarea.value.substring(start, end) + "[/"+BBcode+"]" + textarea.value.substring(end, textarea.value.length);
451                 }
452         }
453
454         return true;
455 }
456
457 function cmtBbOpen(id) {
458         $(".comment-edit-bb-" + id).show();
459 }
460 function cmtBbClose(id) {
461         $(".comment-edit-bb-" + id).hide();
462 }