/* Jappix - An open social platform These are the options JS scripts for Jappix ------------------------------------------------- License: AGPL Author: Valérian Saliou, Maranda */ // Bundle var Options = (function () { /** * Alias of this * @private */ var self = {}; /** * Opens the options popup * @public * @return {boolean} */ self.open = function() { try { // Popup HTML content var html = '
' + Common._e("Edit options") + '
' + '
' + '' + Common._e("General") + '' + '' + Common._e("Channel") + '' + '' + Common._e("Account") + '' + '
' + '
' + '
' + '
' + '' + Common._e("Privacy") + '' + '
' + '' + '' + '
' + '
' + '' + '' + '' + Common._e("Remove all archives") + '' + '
' + '
' + '
' + '' + Common._e("Application") + '' + '
' + '' + '' + '
' + '
' + '' + '' + '
' + '
' + '' + '' + '
' + '
' + '' + '' + '
' + '
' + '' + '' + '
' + '
' + '' + '' + '
' + '' + '
' + '
' + '
' + '
' + Common._e("Remove all archives") + '
' + 'X' + '
' + '
' + '' + '' + '
' + '' + Common._e("Remove") + ' »' + '
' + '
' + '
' + '
' + '' + Common._e("Channel") + '' + '
' + '' + '' + Common._e("Empty channel") + '' + '
' + '
' + '' + '' + '
' + '
' + '' + '' + '
' + '
' + '
' + '
' + '
' + Common._e("Empty channel") + '
' + 'X' + '
' + '
' + '' + '' + '
' + '' + Common._e("Empty") + ' »' + '
' + '
' + '
' + '
' + '' + Common._e("Account") + '' + '' + '' + Common._e("Change password") + '' + '' + '' + '
' + '
' + '
' + '
' + Common._e("Change password") + '
' + 'X' + '
' + '
' + '' + '' + '' + '' + '' + '
' + '' + Common._e("Continue") + ' »' + '
' + '
' + '
' + '
' + Common._e("Delete account") + '
' + 'X' + '
' + '
' + '' + '' + '
' + '' + Common._e("Delete") + ' »' + '
' + '
' + '
' + '
' + '
' + '' + Common._e("Save") + '' + '' + Common._e("Cancel") + '' + '
'; // Create the popup Popup.create('options', html); // Apply the features Features.apply('options'); // Associate the events self.instance(); } catch(e) { Console.error('Options.open', e); } finally { return false; } }; /** * Closes the options popup * @public * @return {boolean} */ self.close = function() { try { // Destroy the popup Popup.destroy('options'); } catch(e) { Console.error('Options.close', e); } finally { return false; } }; /** * Checks whether the options are loaded or not * @public * @return {boolean} */ self.loaded = function() { is_loaded = false; try { if($('.options-hidable').is(':visible')) { is_loaded = true; } } catch(e) { Console.error('Options.loaded', e); } finally { return is_loaded; } }; /** * Switches between the options tabs * @public * @param {string} id * @return {boolean} */ self.switchTab = function(id) { try { var options_sel = $('#options'); options_sel.find('.one-lap').hide(); options_sel.find('#conf' + id).show(); options_sel.find('.tab a').removeClass('tab-active'); options_sel.find('.tab a[data-key="' + id + '"]').addClass('tab-active'); options_sel.find('.sub-ask .sub-ask-close').click(); } catch(e) { Console.error('Options.switchTab', e); } finally { return false; } }; /** * Manages the options wait item * @public * @param {string} id * @return {undefined} */ self.wait = function(id) { try { var options_sel = $('#options'); var content_sel = options_sel.find('.content'); // Remove the current item class content_sel.removeClass(id); // Hide the waiting items if all was received if(!content_sel.hasClass('microblog') && !content_sel.hasClass('mam')) { options_sel.find('.wait').hide(); options_sel.find('.finish:first').removeClass('disabled'); } } catch(e) { Console.error('Options.wait', e); } }; /** * Sends the options to the XMPP server * @public * @return {undefined} */ self.store = function() { try { // Get the values var sounds = DataStore.getDB(Connection.desktop_hash, 'options', 'sounds'); var geolocation = DataStore.getDB(Connection.desktop_hash, 'options', 'geolocation'); var showall = DataStore.getDB(Connection.desktop_hash, 'options', 'roster-showall'); var noxhtmlimg = DataStore.getDB(Connection.desktop_hash, 'options', 'no-xhtml-images'); var groupchatpresence = DataStore.getDB(Connection.desktop_hash, 'options', 'groupchatpresence'); var integratemedias = DataStore.getDB(Connection.desktop_hash, 'options', 'integratemedias'); var localarchives = DataStore.getDB(Connection.desktop_hash, 'options', 'localarchives'); var status = DataStore.getDB(Connection.desktop_hash, 'options', 'presence-status'); // Create an array to be looped var oType = ['sounds', 'geolocation', 'roster-showall', 'no-xhtml-images', 'groupchatpresence', 'integratemedias', 'localarchives', 'presence-status']; var oContent = [sounds, geolocation, showall, noxhtmlimg, groupchatpresence, integratemedias, localarchives, status]; // New IQ var iq = new JSJaCIQ(); iq.setType('set'); var query = iq.setQuery(NS_PRIVATE); var storage = query.appendChild(iq.buildNode('storage', {'xmlns': NS_OPTIONS})); // Loop the array for(var i in oType) { storage.appendChild(iq.buildNode('option', {'type': oType[i], 'xmlns': NS_OPTIONS}, oContent[i])); } con.send(iq, self.handleStore); Console.info('Storing options...'); } catch(e) { Console.error('Options.store', e); } }; /** * Handles the option storing * @public * @param {object} iq * @return {undefined} */ self.handleStore = function(iq) { try { if(!iq || (iq.getType() != 'result')) { Console.warn('Options not stored.'); } else { Console.info('Options stored.'); } } catch(e) { Console.error('Options.handleStore', e); } }; /** * Saves the user options * @public * @return {boolean} */ self.save = function() { try { // We apply the sounds var sounds = '0'; if($('#sounds').filter(':checked').size()) { sounds = '1'; } DataStore.setDB(Connection.desktop_hash, 'options', 'sounds', sounds); // We apply the geolocation if($('#geolocation').filter(':checked').size()) { DataStore.setDB(Connection.desktop_hash, 'options', 'geolocation', '1'); // We geolocate the user on the go PEP.geolocate(); } else { DataStore.setDB(Connection.desktop_hash, 'options', 'geolocation', '0'); // We delete the geolocation informations PEP.sendPosition(); DataStore.removeDB(Connection.desktop_hash, 'geolocation', 'now'); } // We apply the roster show all if($('#showall').filter(':checked').size()) { DataStore.setDB(Connection.desktop_hash, 'options', 'roster-showall', '1'); Interface.showAllBuddies('options'); } else { DataStore.setDB(Connection.desktop_hash, 'options', 'roster-showall', '0'); Interface.showOnlineBuddies('options'); } // We apply the XHTML-IM images filter var noxhtmlimg = '1' ? $('#noxhtmlimg').filter(':checked').size() : '0'; DataStore.setDB(Connection.desktop_hash, 'options', 'no-xhtml-images', noxhtmlimg); // We apply the groupchat presence messages configuration var groupchatpresence = '1' ? $('#groupchatpresence').filter(':checked').size() : '0'; DataStore.setDB(Connection.desktop_hash, 'options', 'groupchatpresence', groupchatpresence); // We apply the media integration var integratemedias = '1' ? $('#integratemedias').filter(':checked').size() : '0'; DataStore.setDB(Connection.desktop_hash, 'options', 'integratemedias', integratemedias); // We apply the local archiving var localarchives = '1' ? $('#localarchives').filter(':checked').size() : '0'; DataStore.setDB(Connection.desktop_hash, 'options', 'localarchives', localarchives); // Flush local archives? if(localarchives === '0') { Message.flushLocalArchive(); } // We apply the message archiving if(Features.enabledMAM()) { MAM.setConfig($('#archiving').val() || 'never'); } // We apply the microblog configuration var persist = '1' ? $('#persist').filter(':checked').size() : '0'; var maximum = $('#maxnotices').val(); if(Features.enabledPEP() && (Features.enabledPubSub() || Features.enabledPubSubCN())) { Pubsub.setup('', NS_URN_MBLOG, persist, maximum, '', '', false); } // We send the options to the database self.store(); // Close the options self.close(); } catch(e) { Console.error('Options.save', e); } finally { return false; } }; /** * Handles the password changing * @public * @param {string} iq * @return {undefined} */ self.handlePwdChange = function(iq) { try { // Remove the general wait item Interface.removeGeneralWait(); // If no errors if(!Errors.handleReply(iq)) { Connection.clearLastSession(); Connection.quit(); Board.openThisInfo(1); Console.info('Password changed.'); } else { Console.warn('Password not changed.'); } } catch(e) { Console.error('Options.handlePwdChange', e); } }; /** * Sends the new account password * @public * @return {boolean} */ self.sendNewPassword = function() { /* REF: http://xmpp.org/extensions/xep-0077.html#usecases-changepw */ try { var password0 = $('#options .old').val(); var password1 = $('#options .new1').val(); var password2 = $('#options .new2').val(); if ((password1 == password2) && (password0 == Utils.getPassword())) { // We show the waiting image Interface.showGeneralWait(); // We send the IQ var iq = new JSJaCIQ(); iq.setTo(Utils.getServer()); iq.setType('set'); var iqQuery = iq.setQuery(NS_REGISTER); iqQuery.appendChild(iq.buildNode('username', {'xmlns': NS_REGISTER}, con.username)); iqQuery.appendChild(iq.buildNode('password', {'xmlns': NS_REGISTER}, password1)); con.send(iq, self.handlePwdChange); Console.info('Password change sent.'); } else { $('.sub-ask-pass input').each(function() { var select = $(this); if(!select.val()) { $(document).oneTime(10, function() { select.addClass('please-complete').focus(); }); } else { select.removeClass('please-complete'); } }); if(password0 != Utils.getPassword()) { $(document).oneTime(10, function() { $('#options .old').addClass('please-complete').focus(); }); } if(password1 != password2) { $(document).oneTime(10, function() { $('#options .new1, #options .new2').addClass('please-complete').focus(); }); } } } catch(e) { Console.error('Options.sendNewPassword', e); } finally { return false; } }; /** * Handles the account deletion request * @public * @param {object} iq * @return {undefined} */ self.handleAccDeletion = function(iq) { try { // Remove the general wait item Interface.removeGeneralWait(); // If no errors if(!Errors.handleReply(iq)) { Connection.clearLastSession(); Talk.destroy(); Board.openThisInfo(2); Connection.logout(); Console.info('Account deleted.'); } else { Console.warn('Account not deleted.'); } } catch(e) { Console.error('Options.handleAccDeletion', e); } }; /** * Purge the user's archives (MAM) * @public * @return {boolean} */ self.purgeMyArchives = function() { try { var pwd_input_sel = $('#options .check-mam'); var password = pwd_input_sel.val(); if(password == Utils.getPassword()) { MAM.purgeArchives(); // Clear archives in UI $('.page-engine-chan[data-type="chat"] .tools-clear').click(); // Hide the tool pwd_input_sel.val(''); $('#options .sub-ask-mam .sub-ask-close').click(); } else { var selector = $('#options .check-mam'); if(password != Utils.getPassword()) { $(document).oneTime(10, function() { selector.addClass('please-complete').focus(); }); } else { selector.removeClass('please-complete'); } } } catch(e) { Console.error('Options.purgeMyArchives', e); } finally { return false; } }; /** * Purge the user's microblog items * @public * @return {boolean} */ self.purgeMyMicroblog = function() { /* REF: http://xmpp.org/extensions/xep-0060.html#owner-purge */ try { var pwd_input_sel = $('#options .check-empty'); var password = pwd_input_sel.val(); if(password == Utils.getPassword()) { // Send the IQ to remove the item (and get eventual error callback) var iq = new JSJaCIQ(); iq.setType('set'); var pubsub = iq.appendNode('pubsub', {'xmlns': NS_PUBSUB_OWNER}); pubsub.appendChild(iq.buildNode('purge', {'node': NS_URN_MBLOG, 'xmlns': NS_PUBSUB_OWNER})); con.send(iq, self.handleMicroblogPurge); // Hide the tool pwd_input_sel.val(''); $('#options .sub-ask-empty .sub-ask-close').click(); Console.info('Microblog purge sent.'); } else { var selector = $('#options .check-empty'); if(password != Utils.getPassword()) { $(document).oneTime(10, function() { selector.addClass('please-complete').focus(); }); } else { selector.removeClass('please-complete'); } } } catch(e) { Console.error('Options.purgeMyMicroblog', e); } finally { return false; } }; /** * Handles the microblog purge * @public * @param {object} iq * @return {undefined} */ self.handleMicroblogPurge = function(iq) { try { // If no errors if(!Errors.handleReply(iq)) { // Remove the microblog items $('.one-update.update_' + hex_md5(Common.getXID())).remove(); Console.info('Microblog purged.'); } else { Console.warn('Microblog not purged.'); } } catch(e) { Console.error('Options.handleMicroblogPurge', e); } }; /** * Deletes the user's account * @public * @return {boolean} */ self.deleteMyAccount = function() { /* REF: http://xmpp.org/extensions/xep-0077.html#usecases-cancel */ try { var password = $('#options .check-password').val(); if(password == Utils.getPassword()) { // We show the waiting image Interface.showGeneralWait(); // We send the IQ var iq = new JSJaCIQ(); iq.setType('set'); var iqQuery = iq.setQuery(NS_REGISTER); iqQuery.appendChild(iq.buildNode('remove', {'xmlns': NS_REGISTER})); con.send(iq, self.handleAccDeletion); Console.info('Delete account sent.'); } else { var selector = $('#options .check-password'); if(password != Utils.getPassword()) { $(document).oneTime(10, function() { selector.addClass('please-complete').focus(); }); } else { selector.removeClass('please-complete'); } } } catch(e) { Console.error('Options.deleteMyAccount', e); } finally { return false; } }; /** * Loads the user options * @public * @return {undefined} */ self.load = function() { try { // Process the good stuffs, depending of the server features var enabled_mam = Features.enabledMAM(); var enabled_pubsub = Features.enabledPubSub(); var enabled_pubsub_cn = Features.enabledPubSubCN(); var enabled_pep = Features.enabledPEP(); var sWait = $('#options .content'); // Show the waiting items if necessary if(enabled_mam || (enabled_pep && (enabled_pubsub || enabled_pubsub_cn))) { $('#options .wait').show(); $('#options .finish:first').addClass('disabled'); } // We get the archiving configuration if(enabled_mam) { sWait.addClass('mam'); MAM.getConfig(); } // We get the microblog configuration if((enabled_pubsub || enabled_pubsub_cn) && enabled_pep) { sWait.addClass('microblog'); Microblog.getConfig(); } // We show the "privacy" form if something is visible into it if(enabled_mam || enabled_pep) { $('#options fieldset.privacy').show(); } // We get the values of the forms for the sounds if(DataStore.getDB(Connection.desktop_hash, 'options', 'sounds') == '0') { $('#sounds').attr('checked', false); } else { $('#sounds').attr('checked', true); } // We get the values of the forms for the geolocation if(DataStore.getDB(Connection.desktop_hash, 'options', 'geolocation') == '1') { $('#geolocation').attr('checked', true); } else { $('#geolocation').attr('checked', false); } // We get the values of the forms for the roster show all if(DataStore.getDB(Connection.desktop_hash, 'options', 'roster-showall') == '1') { $('#showall').attr('checked', true); } else { $('#showall').attr('checked', false); } // We get the values of the forms for the XHTML-IM images filter if(DataStore.getDB(Connection.desktop_hash, 'options', 'no-xhtml-images') == '1') { $('#noxhtmlimg').attr('checked', true); } else { $('#noxhtmlimg').attr('checked', false); } // We get the values of the forms for the integratemedias if(DataStore.getDB(Connection.desktop_hash, 'options', 'integratemedias') == '0') { $('#integratemedias').attr('checked', false); } else { $('#integratemedias').attr('checked', true); } // We get the values of the forms for the groupchatpresence if(DataStore.getDB(Connection.desktop_hash, 'options', 'groupchatpresence') == '0') { $('#groupchatpresence').attr('checked', false); } else { $('#groupchatpresence').attr('checked', true); } // We get the values of the forms for the localarchives if(DataStore.getDB(Connection.desktop_hash, 'options', 'localarchives') == '0') { $('#localarchives').attr('checked', false); } else { $('#localarchives').attr('checked', true); } } catch(e) { Console.error('Options.load', e); } }; /** * Plugin launcher * @public * @return {undefined} */ self.instance = function() { try { // Click events $('#options .tab a').click(function() { // Yet active? if($(this).hasClass('tab-active')) return false; // Switch to the good tab var key = parseInt($(this).attr('data-key')); return self.switchTab(key); }); $('#options .linked').click(function() { $('#options .sub-ask').hide(); $('#options .forms').removeClass('in_background'); }); $('#options .xmpp-links').click(function() { Utils.xmppLinksHandler(); return false; }); $('#options .empty-archives').click(function() { var selector = '#options .sub-ask-mam'; $(selector).show(); $('#options .forms').addClass('in_background'); $(document).oneTime(10, function() { $(selector + ' input').focus(); }); return false; }); $('#options .empty-channel').click(function() { var selector = '#options .sub-ask-empty'; $(selector).show(); $('#options .forms').addClass('in_background'); $(document).oneTime(10, function() { $(selector + ' input').focus(); }); return false; }); $('#options .change-password').click(function() { var selector = '#options .sub-ask-pass'; $(selector).show(); $('#options .forms').addClass('in_background'); $(document).oneTime(10, function() { $(selector + ' input:first').focus(); }); return false; }); $('#options .delete-account').click(function() { var selector = '#options .sub-ask-delete'; $(selector).show(); $('#options .forms').addClass('in_background'); $(document).oneTime(10, function() { $(selector + ' input').focus(); }); return false; }); $('#options .sub-ask-pass .sub-ask-bottom').click(function() { return self.sendNewPassword(); }); $('#options .sub-ask-mam .sub-ask-bottom').click(function() { return self.purgeMyArchives(); }); $('#options .sub-ask-empty .sub-ask-bottom').click(function() { return self.purgeMyMicroblog(); }); $('#options .sub-ask-delete .sub-ask-bottom').click(function() { return self.deleteMyAccount(); }); $('#options .sub-ask-close').click(function() { $('#options .sub-ask').hide(); $('#options .forms').removeClass('in_background'); return false; }); $('#options .bottom .finish').click(function() { if($(this).is('.save') && !$(this).hasClass('disabled')) { return self.save(); } if($(this).is('.cancel')) { return self.close(); } return false; }); // The keyup events $('#options .sub-ask input').keyup(function(e) { if(e.keyCode == 13) { // Archives purge if($(this).is('.purge-archives')) { return self.purgeMyArchives(); } // Microblog purge else if($(this).is('.purge-microblog')) { return self.purgeMyMicroblog(); } // Password change else if($(this).is('.password-change')) { return self.sendNewPassword(); } // Account deletion else if($(this).is('.delete-account')) { return self.deleteMyAccount(); } } }); // Load the options self.load(); } catch(e) { Console.error('Options.instance', e); } }; /** * Return class scope */ return self; })();