Migrate settings to use a sandboxed iframe

This commit is contained in:
yflory
2017-11-09 14:23:40 +01:00
parent c1ba08cfc6
commit 101482b8cc
24 changed files with 2066 additions and 672 deletions

View File

@@ -0,0 +1,81 @@
define([
'jquery',
'/customize/messages.js'
], function($, Messages) {
var LS_LANG = "CRYPTPAD_LANG";
var Msg = {};
Msg.getLanguage = Messages._getLanguage;
// Add handler to the language selector
Msg.setLanguage = function (l, sframeChan, cb) {
console.log(sframeChan);
if (sframeChan) {
// We're in the sandbox
sframeChan.query("Q_LANGUAGE_SET", l, cb);
return;
}
localStorage.setItem(LS_LANG, l);
cb();
};
Msg.initSelector = function ($select, sfcommon) {
var selector = $select || $('#cp-language-selector');
if (!selector.length) { return; }
var language = Messages._getLanguage();
// Select the current language in the list
selector.setValue(language || 'en');
// Listen for language change
$(selector).find('a.cp-language-value').on('click', function () {
var newLanguage = $(this).attr('data-value');
Msg.setLanguage(newLanguage, sfcommon && sfcommon.getSframeChannel(), function () {
if (newLanguage !== language) {
if (sfcommon) {
sfcommon.gotoURL();
return;
}
window.location.reload();
}
});
});
};
Msg.applyTranslation = function () {
var translateText = function (i, e) {
var $el = $(e);
var key = $el.data('localization');
$el.html(Messages[key]);
};
var translateAppend = function (i, e) {
var $el = $(e);
var key = $el.data('localization-append');
$el.append(Messages[key]);
};
var translateTitle = function () {
var $el = $(this);
var key = $el.data('localization-title');
$el.attr('title', Messages[key]);
};
var translatePlaceholder = function () {
var $el = $(this);
var key = $el.data('localization-placeholder');
$el.attr('placeholder', Messages[key]);
};
$('[data-localization]').each(translateText);
$('[data-localization-append]').each(translateAppend);
$('[data-localization-title]').each(translateTitle);
$('[data-localization-placeholder]').each(translatePlaceholder);
$('#pad-iframe').contents().find('[data-localization]').each(translateText);
$('#pad-iframe').contents().find('[data-localization-append]').each(translateAppend);
$('#pad-iframe').contents().find('[data-localization-title]').each(translateTitle);
$('#pad-iframe').contents().find('[data-localization-placeholder]').each(translatePlaceholder);
};
return Msg;
});

View File

@@ -15,6 +15,7 @@ define([
'/common/common-file.js',
'/file/file-crypto.js',
'/common/common-realtime.js',
'/common/common-language.js',
'/common/clipboard.js',
'/common/pinpad.js',
@@ -23,7 +24,7 @@ define([
'/bower_components/nthen/index.js',
'/bower_components/localforage/dist/localforage.min.js',
], function ($, Config, Messages, Store, Util, Hash, UI, History, UserList, Title, Metadata,
Messaging, CodeMirror, Files, FileCrypto, Realtime, Clipboard,
Messaging, CodeMirror, Files, FileCrypto, Realtime, Language, Clipboard,
Pinpad, AppConfig, MediaTag, Nthen, localForage) {
// Configure MediaTags to use our local viewer
@@ -188,6 +189,9 @@ define([
common.getLanguage = function () {
return Messages._languageUsed;
};
common.setLanguage = function (l, cb) {
Language.setLanguage(l, null, cb);
};
common.getUserlist = function () {
if (store) {
if (store.getProxy() && store.getProxy().info) {
@@ -225,7 +229,12 @@ define([
common.isFeedbackAllowed = function () {
try {
if (!getStore().getProxy().proxy.allowUserFeedback) { return false; }
var entry = common.find(getProxy(), [
'settings',
'general',
'allowUserFeedback'
]);
if (!entry) { return false; }
return true;
} catch (e) {
console.error(e);
@@ -499,7 +508,7 @@ define([
if (getProxy()) {
getProxy()[common.displayNameKey] = value;
}
if (typeof cb === "function") { cb(); }
if (typeof cb === "function") { whenRealtimeSyncs(getRealtime(), cb); }
};
common.setAttribute = function (attr, value, cb) {
getStore().setAttribute(attr, value, function (err, data) {
@@ -527,6 +536,9 @@ define([
common.getThumbnail = function (key, cb) {
localForage.getItem(key, cb);
};
common.clearThumbnail = function (cb) {
localForage.clear(cb);
};
/* this returns a reference to your proxy. changing it will change your drive.
*/
@@ -1901,7 +1913,7 @@ define([
$block.appendTo($container);
}
Messages._initSelector($block);
Language.initSelector($block);
return $block;
};
@@ -2315,7 +2327,7 @@ define([
// MAGIC that happens implicitly
$(function () {
Messages._applyTranslation();
Language.applyTranslation();
});
return common;

View File

@@ -1,5 +1,5 @@
define([
'/customize/messages.js',
], function (Messages) {
Messages._applyTranslation();
'/common/common-language.js',
], function (Language) {
Language.applyTranslation();
});

View File

@@ -237,8 +237,11 @@ define([
}
}
if (typeof(proxy.allowUserFeedback) !== 'boolean') {
proxy.allowUserFeedback = true;
if (!proxy.settings || !proxy.settings.general ||
typeof(proxy.settings.general.allowUserFeedback) !== 'boolean') {
proxy.settings = proxy.settings || {};
proxy.settings.general = proxy.settings.general || {};
proxy.settings.general.allowUserFeedback = true;
}
if (typeof(proxy.uid) !== 'string' || proxy.uid.length !== 32) {

View File

@@ -63,5 +63,22 @@ define([], function () {
Cryptpad.feedback('Migrate-3', true);
userObject.version = version = 3;
}
// Migration 4: allowUserFeedback to settings
var migrateLanguage = function () {
var settings = userObject.settings = userObject.settings || {};
if (typeof(userObject['allowUserFeedback']) !== "undefined") {
settings.general = settings.general || {};
settings.general.allowUserFeedback = userObject['allowUserFeedback'];
delete userObject['allowUserFeedback'];
}
};
if (version < 4) {
migrateLanguage();
Cryptpad.feedback('Migrate-4', true);
userObject.version = version = 4;
}
};
});

View File

@@ -3,12 +3,13 @@ define([
'/api/config',
'/common/cryptpad-common.js',
'/common/common-util.js',
'/common/common-language.js',
'/common/media-tag.js',
'/common/tippy.min.js',
'/customize/application_config.js',
'css!/common/tippy.css',
], function ($, Config, Cryptpad, Util, MediaTag, Tippy, AppConfig) {
], function ($, Config, Cryptpad, Util, Language, MediaTag, Tippy, AppConfig) {
var UI = {};
var Messages = Cryptpad.Messages;
@@ -574,6 +575,43 @@ define([
return $userAdmin;
};
// Provide $container if you want to put the generated block in another element
// Provide $initBlock if you already have the menu block and you want the content inserted in it
UI.createLanguageSelector = function (common, $container, $initBlock) {
var options = [];
var languages = Messages._languages;
var keys = Object.keys(languages).sort();
keys.forEach(function (l) {
options.push({
tag: 'a',
attributes: {
'class': 'cp-language-value',
'data-value': l,
'href': '#',
},
content: languages[l] // Pretty name of the language value
});
});
var dropdownConfig = {
text: Messages.language, // Button initial text
options: options, // Entries displayed in the menu
//left: true, // Open to the left of the button
container: $initBlock, // optional
isSelect: true
};
var $block = Cryptpad.createDropdown(dropdownConfig);
$block.attr('id', 'cp-language-selector');
if ($container) {
$block.appendTo($container);
}
Language.initSelector($block, common);
return $block;
};
UI.initFilePicker = function (common, cfg) {
var onSelect = cfg.onSelect || $.noop;
var sframeChan = common.getSframeChannel();

View File

@@ -118,6 +118,7 @@ define([
netfluxId: Cryptpad.getNetwork().webChannels[0].myID,
},
priv: {
edPublic: proxy.edPublic,
accountName: Cryptpad.getAccountName(),
origin: window.location.origin,
pathname: window.location.pathname,
@@ -452,6 +453,10 @@ define([
});
});
sframeChan.on('Q_LANGUAGE_SET', function (data, cb) {
Cryptpad.setLanguage(data, cb);
});
if (cfg.addRpc) {
cfg.addRpc(sframeChan, Cryptpad);
}

View File

@@ -83,6 +83,7 @@ define([
funcs.createButton = callWithCommon(UI.createButton);
funcs.createUsageBar = callWithCommon(UI.createUsageBar);
funcs.updateTags = callWithCommon(UI.updateTags);
funcs.createLanguageSelector = callWithCommon(UI.createLanguageSelector);
// Thumb
funcs.displayThumbnail = callWithCommon(Thumb.displayThumbnail);

View File

@@ -176,4 +176,19 @@ define({
// Store outside and get thumbnails inside (stored with localForage (indexedDB) outside)
'Q_THUMBNAIL_SET': true,
'Q_THUMBNAIL_GET': true,
// Settings app only
// Clear all thumbnails
'Q_THUMBNAIL_CLEAR': true,
// Backup and restore a drive
'Q_SETTINGS_DRIVE_GET': true,
'Q_SETTINGS_DRIVE_SET': true,
'Q_SETTINGS_DRIVE_RESET': true,
// Logout from all the devices where the account is logged in
'Q_SETTINGS_LOGOUT': true,
// Import pads from this computer's anon session into the current user account
'Q_SETTINGS_IMPORT_LOCAL': true,
// Store the language selected in the iframe into localStorage outside
'Q_LANGUAGE_SET': true,
});