]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/jappix/js/options.js
Merge branch '3.6-release'
[friendica-addons.git] / jappixmini / jappix / js / options.js
1 /*
2
3 Jappix - An open social platform
4 These are the options JS scripts for Jappix
5
6 -------------------------------------------------
7
8 License: AGPL
9 Author: Vanaryon
10 Last revision: 17/07/11
11
12 */
13
14 // Opens the options popup
15 function optionsOpen() {
16         // Popup HTML content
17         var html = 
18         '<div class="top">' + _e("Edit options") + '</div>' + 
19         
20         '<div class="tab">' + 
21                 '<a href="#" class="tab-active" data-key="1">' + _e("General") + '</a>' + 
22                 '<a href="#" class="pubsub-hidable" data-key="2">' + _e("Channel") + '</a>' + 
23                 '<a href="#" data-key="3">' + _e("Account") + '</a>' + 
24         '</div>' + 
25         
26         '<div class="content">' + 
27                 '<div id="conf1" class="lap-active one-lap forms">' + 
28                         '<fieldset class="privacy">' + 
29                                 '<legend>' + _e("Privacy") + '</legend>' + 
30                                 
31                                 '<label for="geolocation" class="pep-hidable">' + _e("Geolocation") + '</label>' + 
32                                 '<input id="geolocation" type="checkbox" class="pep-hidable" />' + 
33                                 
34                                 '<label for="archiving" class="archives-hidable pref">' + _e("Message archiving") + '</label>' + 
35                                 '<input id="archiving" type="checkbox" class="archives-hidable pref" />' + 
36                         '</fieldset>' + 
37                         
38                         '<fieldset>' + 
39                                 '<legend>' + _e("Application") + '</legend>' + 
40                                 
41                                 '<label for="sounds">' + _e("Sounds") + '</label>' + 
42                                 '<input id="sounds" type="checkbox" />' + 
43                                 
44                                 '<label for="showall">' + _e("Show all friends") + '</label>' + 
45                                 '<input id="showall" type="checkbox" />' + 
46                                 
47                                 '<label for="integratemedias">' + _e("Media integration") + '</label>' + 
48                                 '<input id="integratemedias" type="checkbox" />' + 
49                                 
50                                 '<label class="xmpplinks-hidable">' + _e("XMPP links") + '</label>' + 
51                                 '<a href="#" class="linked xmpp-links xmpplinks-hidable">' + _e("Open XMPP links with Jappix") + '</a>' + 
52                         '</fieldset>' + 
53                 '</div>' + 
54                 
55                 '<div id="conf2" class="one-lap forms">' + 
56                         '<fieldset>' + 
57                                 '<legend>' + _e("Channel") + '</legend>' + 
58                                 
59                                 '<label>' + _e("Empty") + '</label>' + 
60                                 '<a href="#" class="linked empty-channel">' + _e("Empty channel") + '</a>' + 
61                                 
62                                 '<label>' + _e("Persistent") + '</label>' + 
63                                 '<input id="persistent" type="checkbox" />' + 
64                                 
65                                 '<label>' + _e("Maximum notices") + '</label>' + 
66                                 '<select id="maxnotices">' + 
67                                         '<option value="1">1</option>' + 
68                                         '<option value="100">100</option>' + 
69                                         '<option value="1000">1000</option>' + 
70                                         '<option value="10000">10000</option>' + 
71                                         '<option value="100000">100000</option>' + 
72                                         '<option value="1000000">1000000</option>' + 
73                                 '</select>' + 
74                         '</fieldset>' + 
75                         
76                         '<div class="sub-ask sub-ask-empty sub-ask-element">' + 
77                                 '<div class="sub-ask-top">' + 
78                                         '<div class="sub-ask-title">' + _e("Empty channel") + '</div>' + 
79                                         '<a href="#" class="sub-ask-close">X</a>' + 
80                                 '</div>' + 
81                                 
82                                 '<div class="sub-ask-content">' + 
83                                         '<label>' + _e("Password") + '</label>' + 
84                                         '<input type="password" class="purge-microblog check-empty" required="" />' + 
85                                 '</div>' + 
86                                 
87                                 '<a href="#" class="sub-ask-bottom">' + _e("Empty") + ' &raquo;</a>' + 
88                         '</div>' + 
89                 '</div>' + 
90                 
91                 '<div id="conf3" class="one-lap forms">' + 
92                         '<fieldset>' + 
93                                 '<legend>' + _e("Account") + '</legend>' + 
94                                 
95                                 '<label>' + _e("Password") + '</label>' + 
96                                 '<a href="#" class="linked change-password">' + _e("Change password") + '</a>' + 
97                                 
98                                 '<label>' + _e("Delete") + '</label>' + 
99                                 '<a href="#" class="linked delete-account">' + _e("Delete account") + '</a>' + 
100                         '</fieldset>' + 
101                                 
102                         '<div class="sub-ask sub-ask-pass sub-ask-element">' + 
103                                 '<div class="sub-ask-top">' + 
104                                         '<div class="sub-ask-title">' + _e("Change password") + '</div>' + 
105                                         '<a href="#" class="sub-ask-close">X</a>' + 
106                                 '</div>' + 
107                                 
108                                 '<div class="sub-ask-content">' + 
109                                         '<label>' + _e("Old") + '</label>' + 
110                                         '<input type="password" class="password-change old" required="" />' + 
111                                         
112                                         '<label>' + _e("New (2 times)") + '</label>' + 
113                                         '<input type="password" class="password-change new1" required="" />' + 
114                                         '<input type="password" class="password-change new2" required="" />' + 
115                                 '</div>' + 
116                                 
117                                 '<a href="#" class="sub-ask-bottom">' + _e("Continue") + ' &raquo;</a>' + 
118                         '</div>' + 
119                         
120                         '<div class="sub-ask sub-ask-delete sub-ask-element">' + 
121                                 '<div class="sub-ask-top">' + 
122                                         '<div class="sub-ask-title">' + _e("Delete account") + '</div>' + 
123                                         '<a href="#" class="sub-ask-close">X</a>' + 
124                                 '</div>' + 
125                                 
126                                 '<div class="sub-ask-content">' + 
127                                         '<label>' + _e("Password") + '</label>' + 
128                                         '<input type="password" class="delete-account check-password" required="" />' + 
129                                 '</div>' + 
130                                 
131                                 '<a href="#" class="sub-ask-bottom">' + _e("Delete") + ' &raquo;</a>' + 
132                         '</div>' + 
133                 '</div>' + 
134         '</div>' + 
135         
136         '<div class="bottom">' + 
137                 '<div class="wait wait-medium"></div>' + 
138                 
139                 '<a href="#" class="finish save">' + _e("Save") + '</a>' + 
140                 '<a href="#" class="finish cancel">' + _e("Cancel") + '</a>' + 
141         '</div>';
142         
143         // Create the popup
144         createPopup('options', html);
145         
146         // Apply the features
147         applyFeatures('options');
148         
149         // Associate the events
150         launchOptions();
151         
152         return false;
153 }
154
155 // Closes the options popup
156 function closeOptions() {
157         // Destroy the popup
158         destroyPopup('options');
159         
160         return false;
161 }
162
163 // Checks whether the options are loaded or not
164 function loadedOptions() {
165         if($('.options-hidable').is(':visible'))
166                 return true;
167         
168         return false;
169 }
170
171 // Switches between the options tabs
172 function switchOptions(id) {
173         $('#options .one-lap').hide();
174         $('#options #conf' + id).show();
175         $('#options .tab a').removeClass('tab-active');
176         $('#options .tab a[data-key=' + id + ']').addClass('tab-active');
177         
178         return false;
179 }
180
181 // Manages the options wait item
182 function waitOptions(id) {
183         var sOptions = $('#options .content');
184         
185         // Remove the current item class
186         sOptions.removeClass(id);
187         
188         // Hide the waiting items if all was received
189         if(!sOptions.hasClass('microblog') && !sOptions.hasClass('archives')) {
190                 $('#options .wait').hide();
191                 $('#options .finish:first').removeClass('disabled');
192         }
193 }
194
195 // Sends the options to the XMPP server
196 function storeOptions() {
197         // Get the values
198         var sounds = getDB('options', 'sounds');
199         var geolocation = getDB('options', 'geolocation');
200         var showall = getDB('options', 'roster-showall');
201         var integratemedias = getDB('options', 'integratemedias');
202         var status = getDB('options', 'presence-status');
203         
204         // Create an array to be looped
205         var oType = new Array('sounds', 'geolocation', 'roster-showall', 'integratemedias', 'presence-status');
206         var oContent = new Array(sounds, geolocation, showall, integratemedias, status);
207         
208         // New IQ
209         var iq = new JSJaCIQ();
210         iq.setType('set');
211         
212         var query = iq.setQuery(NS_PRIVATE);
213         var storage = query.appendChild(iq.buildNode('storage', {'xmlns': NS_OPTIONS}));
214         
215         // Loop the array
216         for(i in oType)
217                 storage.appendChild(iq.buildNode('option', {'type': oType[i], 'xmlns': NS_OPTIONS}, oContent[i]));
218         
219         con.send(iq, handleStoreOptions);
220         
221         logThis('Storing options...', 3);
222 }
223
224 // Handles the option storing
225 function handleStoreOptions(iq) {
226         if(!iq || (iq.getType() != 'result'))
227                 logThis('Options not stored.', 2);
228         else
229                 logThis('Options stored.', 3);
230 }
231
232 // Saves the user options
233 function saveOptions() {
234         // We apply the sounds
235         var sounds = '0';
236         
237         if($('#sounds').filter(':checked').size())
238                 sounds = '1';
239         
240         setDB('options', 'sounds', sounds);
241         
242         // We apply the geolocation
243         if($('#geolocation').filter(':checked').size()) {
244                 setDB('options', 'geolocation', '1');
245                 
246                 // We geolocate the user on the go
247                 geolocate();
248         }
249         
250         else {
251                 setDB('options', 'geolocation', '0');
252                 
253                 // We delete the geolocation informations
254                 sendPosition();
255                 removeDB('geolocation', 'now');
256         }
257         
258         // We apply the roster show all
259         if($('#showall').filter(':checked').size()) {
260                 setDB('options', 'roster-showall', '1');
261                 showAllBuddies('options');
262         }
263         
264         else {
265                 setDB('options', 'roster-showall', '0');
266                 showOnlineBuddies('options');
267         }
268         
269         // We apply the media integration
270         var integratemedias = '0';
271         
272         if($('#integratemedias').filter(':checked').size())
273                 integratemedias = '1';
274         
275         setDB('options', 'integratemedias', integratemedias);
276         
277         // We apply the message archiving
278         if(enabledArchives('pref')) {
279                 var aEnabled = false;
280                 
281                 if($('#archiving').filter(':checked').size())
282                         aEnabled = true;
283                 
284                 configArchives(aEnabled);
285         }
286         
287         // We apply the microblog configuration
288         var persist = '0';
289         var maximum = $('#maxnotices').val();
290         
291         if($('#persistent').filter(':checked').size())
292                 persist = '1';
293         
294         if(enabledPEP() && enabledPubSub())
295                 setupMicroblog('', NS_URN_MBLOG, persist, maximum, '', '', false);
296         
297         // We send the options to the database
298         storeOptions();
299         
300         // Close the options
301         closeOptions();
302         
303         return false;
304 }
305
306 // Handles the password changing
307 function handlePwdChange(iq) {
308         // Remove the general wait item
309         removeGeneralWait();
310         
311         // If no errors
312         if(!handleErrorReply(iq)) {
313                 clearLastSession();
314                 quit();
315                 openThisInfo(1);
316                 
317                 logThis('Password changed.', 3);
318         }
319         
320         else
321                 logThis('Password not changed.', 2);
322 }
323
324 // Sends the new account password
325 function sendNewPassword() {
326         /* REF: http://xmpp.org/extensions/xep-0077.html#usecases-changepw */
327         
328         var password0 = $('#options .old').val();
329         var password1 = $('#options .new1').val();
330         var password2 = $('#options .new2').val();
331         
332         if ((password1 == password2) && (password0 == getPassword())) {
333                 // We show the waiting image
334                 showGeneralWait();
335                 
336                 // We send the IQ
337                 var iq = new JSJaCIQ();
338                 
339                 iq.setTo(getServer());
340                 iq.setType('set');
341                 
342                 var iqQuery = iq.setQuery(NS_REGISTER);
343                 
344                 iqQuery.appendChild(iq.buildNode('username', {'xmlns': NS_REGISTER}, con.username));
345                 iqQuery.appendChild(iq.buildNode('password', {'xmlns': NS_REGISTER}, password1));
346                 
347                 con.send(iq, handlePwdChange);
348                 
349                 logThis('Password change sent.', 3);
350         }
351         
352         else {
353                 $('.sub-ask-pass input').each(function() {
354                         var select = $(this);
355                         
356                         if(!select.val())
357                                 $(document).oneTime(10, function() {
358                                         select.addClass('please-complete').focus();
359                                 });
360                         else
361                                 select.removeClass('please-complete');  
362                 });
363                 
364                 if(password0 != getPassword())
365                         $(document).oneTime(10, function() {
366                                 $('#options .old').addClass('please-complete').focus();
367                         });
368                 if(password1 != password2)
369                         $(document).oneTime(10, function() {
370                                 $('#options .new1, #options .new2').addClass('please-complete').focus();
371                         });
372         }
373         
374         return false;
375 }
376
377 // Handles the account deletion request
378 function handleAccDeletion(iq) {
379         // Remove the general wait item
380         removeGeneralWait();
381         
382         // If no errors
383         if(!handleErrorReply(iq)) {
384                 clearLastSession();
385                 destroyTalkPage();
386                 openThisInfo(2);
387                 logout();
388                 
389                 logThis('Account deleted.', 3);
390         }
391         
392         else
393                 logThis('Account not deleted.', 2);
394 }
395
396 // Purge the user's microblog items
397 function purgeMyMicroblog() {
398         /* REF: http://xmpp.org/extensions/xep-0060.html#owner-purge */
399         
400         var password = $('#options .check-empty').val();
401         
402         if(password == getPassword()) {
403                 // Send the IQ to remove the item (and get eventual error callback)
404                 var iq = new JSJaCIQ();
405                 iq.setType('set');
406                 
407                 var pubsub = iq.appendNode('pubsub', {'xmlns': NS_PUBSUB_OWNER});
408                 pubsub.appendChild(iq.buildNode('purge', {'node': NS_URN_MBLOG, 'xmlns': NS_PUBSUB_OWNER}));
409                 
410                 con.send(iq, handleMicroblogPurge);
411                 
412                 // Hide the tool
413                 $('#options .sub-ask').hide();
414                 
415                 logThis('Microblog purge sent.', 3);
416         }
417         
418         else {
419                 var selector = $('#options .check-empty');
420                 
421                 if(password != getPassword())
422                         $(document).oneTime(10, function() {
423                                 selector.addClass('please-complete').focus();
424                         });
425                 else
426                         selector.removeClass('please-complete');
427         }
428         
429         return false;
430 }
431
432 // Handles the microblog purge
433 function handleMicroblogPurge(iq) {
434         // If no errors
435         if(!handleErrorReply(iq)) {
436                 // Remove the microblog items
437                 $('.one-update.update_' + hex_md5(getXID())).remove();
438                 
439                 logThis('Microblog purged.', 3);
440         }
441         
442         else
443                 logThis('Microblog not purged.', 2);
444 }
445
446 // Deletes the user's account
447 function deleteMyAccount() {
448         /* REF: http://xmpp.org/extensions/xep-0077.html#usecases-cancel */
449         
450         var password = $('#options .check-password').val();
451         
452         if(password == getPassword()) {
453                 // We show the waiting image
454                 showGeneralWait();
455                 
456                 // We send the IQ
457                 var iq = new JSJaCIQ();
458                 iq.setType('set');
459                 
460                 var iqQuery = iq.setQuery(NS_REGISTER);
461                 iqQuery.appendChild(iq.buildNode('remove', {'xmlns': NS_REGISTER}));
462                 
463                 con.send(iq, handleAccDeletion);
464                 
465                 logThis('Delete account sent.', 3);
466         }
467         
468         else {
469                 var selector = $('#options .check-password');
470                 
471                 if(password != getPassword())
472                         $(document).oneTime(10, function() {
473                                 selector.addClass('please-complete').focus();
474                         });
475                 else
476                         selector.removeClass('please-complete');
477         }
478         
479         return false;
480 }
481
482 // Loads the user options
483 function loadOptions() {
484         // Process the good stuffs, depending of the server features
485         var enabled_archives_pref = enabledArchives('pref');
486         var enabled_pubsub = enabledPubSub();
487         var enabled_pep = enabledPEP();
488         var sWait = $('#options .content');
489         
490         // Show the waiting items if necessary
491         if(enabled_archives_pref || (enabled_pep && enabled_pubsub)) {
492                 $('#options .wait').show();
493                 $('#options .finish:first').addClass('disabled');
494         }
495         
496         // We get the archiving configuration
497         if(enabled_archives_pref) {
498                 sWait.addClass('archives');
499                 getConfigArchives();
500         }
501         
502         // We get the microblog configuration
503         if(enabled_pubsub && enabled_pep) {
504                 sWait.addClass('microblog');
505                 getConfigMicroblog();
506         }
507         
508         // We show the "privacy" form if something is visible into it
509         if(enabled_archives_pref || enabled_pep)
510                 $('#options fieldset.privacy').show();
511         
512         // We get the values of the forms for the sounds
513         if(getDB('options', 'sounds') == '0')
514                 $('#sounds').attr('checked', false);
515         else
516                 $('#sounds').attr('checked', true);
517         
518         // We get the values of the forms for the geolocation
519         if(getDB('options', 'geolocation') == '1')
520                 $('#geolocation').attr('checked', true);
521         else
522                 $('#geolocation').attr('checked', false);
523         
524         // We get the values of the forms for the roster show all
525         if(getDB('options', 'roster-showall') == '1')
526                 $('#showall').attr('checked', true);
527         else
528                 $('#showall').attr('checked', false);
529         
530         // We get the values of the forms for the integratemedias
531         if(getDB('options', 'integratemedias') == '0')
532                 $('#integratemedias').attr('checked', false);
533         else
534                 $('#integratemedias').attr('checked', true);
535 }
536
537 // Addon launcher
538 function launchOptions() {
539         // Click events
540         $('#options .tab a').click(function() {
541                 // Yet active?
542                 if($(this).hasClass('tab-active'))
543                         return false;
544                 
545                 // Switch to the good tab
546                 var key = parseInt($(this).attr('data-key'));
547                 
548                 return switchOptions(key);
549         });
550         
551         $('#options .linked').click(function() {
552                 $('#options .sub-ask').hide();
553         });
554         
555         $('#options .xmpp-links').click(function() {
556                 xmppLinksHandler();
557                 
558                 return false;
559         });
560         
561         $('#options .empty-channel').click(function() {
562                 var selector = '#options .sub-ask-empty';
563                 
564                 $(selector).show();
565                 
566                 $(document).oneTime(10, function() {
567                         $(selector + ' input').focus();
568                 });
569                 
570                 return false;
571         });
572         
573         $('#options .change-password').click(function() {
574                 var selector = '#options .sub-ask-pass';
575                 
576                 $(selector).show();
577                 
578                 $(document).oneTime(10, function() {
579                         $(selector + ' input:first').focus();
580                 });
581                 
582                 return false;
583         });
584         
585         $('#options .delete-account').click(function() {
586                 var selector = '#options .sub-ask-delete';
587                 
588                 $(selector).show();
589                 
590                 $(document).oneTime(10, function() {
591                         $(selector + ' input').focus();
592                 });
593                 
594                 return false;
595         });
596         
597         $('#options .sub-ask-pass .sub-ask-bottom').click(function() {
598                 return sendNewPassword();
599         });
600         
601         $('#options .sub-ask-empty .sub-ask-bottom').click(function() {
602                 return purgeMyMicroblog();
603         });
604         
605         $('#options .sub-ask-delete .sub-ask-bottom').click(function() {
606                 return deleteMyAccount();
607         });
608         
609         $('#options .sub-ask-close').click(function() {
610                 $('#options .sub-ask').hide();
611                 
612                 return false;
613         });
614         
615         $('#options .bottom .finish').click(function() {
616                 if($(this).is('.save') && !$(this).hasClass('disabled'))
617                         return saveOptions();
618                 if($(this).is('.cancel'))
619                         return closeOptions();
620                 
621                 return false;
622         });
623         
624         // The keyup events
625         $('#options .sub-ask input').keyup(function(e) {
626                 if(e.keyCode == 13) {
627                         // Microblog purge
628                         if($(this).is('.purge-microblog'))
629                                 return purgeMyMicroblog();
630                         
631                         // Password change
632                         else if($(this).is('.password-change'))
633                                 return sendNewPassword();
634                         
635                         // Account deletion
636                         else if($(this).is('.delete-account'))
637                                 return deleteMyAccount();
638                 }
639         });
640         
641         // Load the options
642         loadOptions();
643 }