keeweb/app/scripts/views/settings/settings-plugins-view.js

250 lines
9.5 KiB
JavaScript
Raw Normal View History

2017-02-18 23:46:59 +01:00
const Backbone = require('backbone');
const Locale = require('../../util/locale');
const PluginManager = require('../../plugins/plugin-manager');
2017-05-13 22:36:07 +02:00
const PluginGallery = require('../../plugins/plugin-gallery');
2017-02-19 12:40:21 +01:00
const AppSettingsModel = require('../../models/app-settings-model');
2017-04-26 22:12:41 +02:00
const Comparators = require('../../util/comparators');
2017-05-13 14:00:03 +02:00
const Format = require('../../util/format');
2017-05-13 23:57:09 +02:00
const SettingsManager = require('../../comp/settings-manager');
2017-05-14 21:23:49 +02:00
const FeatureDetector = require('../../util/feature-detector');
const SemVer = require('../../util/semver');
const RuntimeInfo = require('../../comp/runtime-info');
2017-05-17 19:25:08 +02:00
const Links = require('../../const/links');
2017-02-18 23:46:59 +01:00
const SettingsPluginsView = Backbone.View.extend({
template: require('templates/settings/settings-plugins.hbs'),
events: {
2017-02-19 12:40:21 +01:00
'click .settings_plugins-install-btn': 'installClick',
'click .settings_plugins-uninstall-btn': 'uninstallClick',
2017-04-26 22:06:07 +02:00
'click .settings_plugins-disable-btn': 'disableClick',
'click .settings_plugins-enable-btn': 'enableClick',
2017-04-08 23:35:26 +02:00
'click .settings_plugins-update-btn': 'updateClick',
2017-02-21 22:05:18 +01:00
'click .settings_plugins-use-locale-btn': 'useLocaleClick',
2017-05-13 22:36:07 +02:00
'click .settings_plugins-use-theme-btn': 'useThemeClick',
2017-05-14 13:13:09 +02:00
'click .settings__plugins-gallery-plugin-install-btn': 'galleryInstallClick',
2017-05-19 22:05:35 +02:00
'input .settings__plugins-gallery-search': 'gallerySearchInput',
'change select.settings__plugins-plugin-input': 'pluginSettingChange',
'change input[type=checkbox].settings__plugins-plugin-input': 'pluginSettingChange',
2017-05-20 11:27:28 +02:00
'input input[type=text].settings__plugins-plugin-input': 'pluginSettingChange',
2017-05-28 17:03:10 +02:00
'change .settings__plugins-plugin-updates': 'autoUpdateChange',
'click .settings__plugins-gallery-load-btn': 'loadPluginGalleryClick'
2017-02-19 12:40:21 +01:00
},
2017-05-14 13:13:09 +02:00
searchStr: null,
2017-05-16 22:28:29 +02:00
installFromUrl: null,
installing: {},
installErrors: {},
2017-05-14 13:13:09 +02:00
2017-02-19 12:40:21 +01:00
initialize() {
2017-04-26 22:06:07 +02:00
this.listenTo(PluginManager, 'change', this.render.bind(this));
2018-01-01 21:40:49 +01:00
this.listenTo(Backbone, 'plugin-gallery-load-complete', this.pluginGalleryLoadComplete.bind(this));
2017-02-18 23:46:59 +01:00
},
render() {
this.renderTemplate({
2017-02-19 12:40:21 +01:00
plugins: PluginManager.get('plugins').map(plugin => ({
id: plugin.id,
manifest: plugin.get('manifest'),
2017-02-19 19:30:59 +01:00
status: plugin.get('status'),
2017-04-08 23:35:26 +02:00
installTime: Math.round(plugin.get('installTime')),
2017-04-27 14:50:36 +02:00
updateError: plugin.get('updateError'),
2017-05-13 14:00:03 +02:00
updateCheckDate: Format.dtStr(plugin.get('updateCheckDate')),
2017-05-16 22:28:29 +02:00
installError: plugin.get('installError'),
2017-11-28 18:54:06 +01:00
official: plugin.get('official'),
2017-05-20 11:27:28 +02:00
autoUpdate: plugin.get('autoUpdate'),
2017-05-19 22:05:35 +02:00
settings: plugin.getSettings()
2017-04-26 22:12:41 +02:00
})).sort(Comparators.stringComparator('id', true)),
2017-05-16 22:28:29 +02:00
installingFromUrl: this.installFromUrl && !this.installFromUrl.error,
installUrl: this.installFromUrl ? this.installFromUrl.url : null,
installUrlError: this.installFromUrl ? this.installFromUrl.error : null,
2017-05-13 22:36:07 +02:00
galleryLoading: PluginGallery.loading,
galleryLoadError: PluginGallery.loadError,
2017-05-14 13:13:09 +02:00
galleryPlugins: this.getGalleryPlugins(),
2017-05-14 17:14:21 +02:00
searchStr: this.searchStr,
2017-05-17 19:25:08 +02:00
hasUnicodeFlags: FeatureDetector.hasUnicodeFlags(),
pluginDevLink: Links.PluginDevelopStart,
translateLink: Links.Translation
2017-02-18 23:46:59 +01:00
});
2017-05-14 13:13:09 +02:00
if (this.searchStr) {
this.showFilterResults();
}
2017-02-19 12:40:21 +01:00
return this;
2017-02-18 23:46:59 +01:00
},
2018-01-01 21:40:49 +01:00
pluginGalleryLoadComplete() {
this.render();
Backbone.trigger('page-geometry', { source: 'view' });
},
2017-05-13 22:53:45 +02:00
getGalleryPlugins() {
2017-05-13 22:36:07 +02:00
if (!PluginGallery.gallery) {
return null;
}
2017-05-13 22:53:45 +02:00
const plugins = PluginManager.get('plugins');
return PluginGallery.gallery.plugins
2017-05-16 22:28:29 +02:00
.map(pl => ({
url: pl.url,
manifest: pl.manifest,
installing: this.installing[pl.url],
installError: this.installErrors[pl.url],
2017-11-28 18:54:06 +01:00
official: pl.official
2017-05-16 22:28:29 +02:00
}))
.filter(pl => !plugins.get(pl.manifest.name) && this.canInstallPlugin(pl))
2017-05-13 22:53:45 +02:00
.sort((x, y) => x.manifest.name.localeCompare(y.manifest.name));
2017-05-13 22:36:07 +02:00
},
canInstallPlugin(plugin) {
if (plugin.manifest.locale && SettingsManager.allLocales[plugin.manifest.locale.name]) {
return false;
}
if (plugin.manifest.desktop && !RuntimeInfo.launcher) {
return false;
}
2017-05-25 19:41:06 +02:00
if (plugin.manifest.versionMin && SemVer.compareVersions(plugin.manifest.versionMin, RuntimeInfo.version) > 0) {
return false;
}
2017-05-25 19:41:06 +02:00
if (plugin.manifest.versionMax && SemVer.compareVersions(plugin.manifest.versionMax, RuntimeInfo.version) > 0) {
return false;
}
return true;
},
2017-05-28 17:03:10 +02:00
loadPluginGalleryClick() {
if (PluginGallery.loading) {
return;
}
PluginGallery.loadPlugins();
this.render();
},
2017-02-18 23:46:59 +01:00
installClick() {
const installBtn = this.$el.find('.settings_plugins-install-btn');
const urlTextBox = this.$el.find('#settings__plugins-install-url');
const errorBox = this.$el.find('.settings__plugins-install-error');
errorBox.html('');
const url = urlTextBox.val().trim();
if (!url) {
return;
}
urlTextBox.prop('disabled', true);
installBtn.text(Locale.setPlInstallBtnProgress + '...').prop('disabled', true);
2017-05-16 22:28:29 +02:00
this.installFromUrl = { url };
2017-12-02 21:20:19 +01:00
PluginManager.install(url, undefined, true)
2017-02-18 23:46:59 +01:00
.then(() => {
this.installFinished();
2017-05-16 22:28:29 +02:00
this.installFromUrl = null;
this.render();
this.$el.closest('.scroller').scrollTop(0);
2017-02-18 23:46:59 +01:00
})
.catch(e => {
this.installFinished();
2017-05-16 22:28:29 +02:00
this.installFromUrl.error = e;
this.$el.find('.settings__plugins-install-error').text(e.toString());
this.$el.closest('.scroller').scrollTop(this.$el.height());
2017-02-18 23:46:59 +01:00
});
},
installFinished() {
const installBtn = this.$el.find('.settings_plugins-install-btn');
const urlTextBox = this.$el.find('#settings__plugins-install-url');
urlTextBox.prop('disabled', false);
installBtn.text(Locale.setPlInstallBtn).prop('disabled', false);
2017-02-19 12:40:21 +01:00
},
uninstallClick(e) {
const pluginId = $(e.target).data('plugin');
PluginManager.uninstall(pluginId);
},
2017-04-26 22:06:07 +02:00
disableClick(e) {
const pluginId = $(e.target).data('plugin');
PluginManager.disable(pluginId);
},
enableClick(e) {
const pluginId = $(e.target).data('plugin');
PluginManager.activate(pluginId);
},
2017-04-08 23:35:26 +02:00
updateClick(e) {
const pluginId = $(e.target).data('plugin');
PluginManager.update(pluginId);
},
2017-02-19 12:40:21 +01:00
useLocaleClick(e) {
const locale = $(e.target).data('locale');
AppSettingsModel.instance.set('locale', locale);
2017-02-21 22:05:18 +01:00
},
useThemeClick(e) {
const theme = $(e.target).data('theme');
AppSettingsModel.instance.set('theme', theme);
2017-05-13 22:36:07 +02:00
},
galleryInstallClick(e) {
2017-05-13 22:53:45 +02:00
const installBtn = $(e.target);
const pluginId = installBtn.data('plugin');
2017-05-13 22:36:07 +02:00
const plugin = PluginGallery.gallery.plugins.find(pl => pl.manifest.name === pluginId);
2017-05-13 22:53:45 +02:00
installBtn.text(Locale.setPlInstallBtnProgress + '...').prop('disabled', true);
2017-05-16 22:28:29 +02:00
this.installing[plugin.url] = true;
delete this.installErrors[plugin.url];
2017-05-13 22:53:45 +02:00
PluginManager.install(plugin.url, plugin.manifest)
2017-05-16 22:28:29 +02:00
.catch(e => {
this.installErrors[plugin.url] = e;
delete this.installing[plugin.url];
this.render();
})
2017-05-13 22:53:45 +02:00
.then(() => {
installBtn.prop('disabled', true);
2017-05-16 22:28:29 +02:00
delete this.installing[plugin.url];
2017-05-13 22:53:45 +02:00
});
2017-05-14 13:13:09 +02:00
},
gallerySearchInput(e) {
this.searchStr = e.target.value.toLowerCase();
this.showFilterResults();
},
showFilterResults() {
const pluginsById = {};
for (const plugin of PluginGallery.gallery.plugins) {
pluginsById[plugin.manifest.name] = plugin;
}
for (const pluginEl of $('.settings__plugins-gallery-plugin', this.$el)) {
const pluginId = pluginEl.dataset.plugin;
const visible = this.pluginMatchesFilter(pluginsById[pluginId]);
$(pluginEl).toggle(visible);
}
},
pluginMatchesFilter(plugin) {
const searchStr = this.searchStr;
const manifest = plugin.manifest;
return !searchStr ||
manifest.name.toLowerCase().indexOf(searchStr) >= 0 ||
manifest.description && manifest.description.toLowerCase().indexOf(searchStr) >= 0 ||
manifest.locale &&
(manifest.locale.name.toLowerCase().indexOf(searchStr) >= 0 ||
manifest.locale.title.toLowerCase().indexOf(searchStr) >= 0);
2017-05-19 22:05:35 +02:00
},
pluginSettingChange(e) {
const el = e.target;
const settingEl = $(el).closest('.settings__plugins-plugin-setting');
const setting = settingEl.data('setting');
const pluginId = settingEl.data('plugin');
const val = el.type === 'checkbox' ? el.checked : el.value;
const plugin = PluginManager.getPlugin(pluginId);
plugin.setSettings({ [setting]: val });
2017-05-20 11:27:28 +02:00
},
autoUpdateChange(e) {
const pluginId = $(e.target).data('plugin');
const enabled = e.target.checked;
PluginManager.setAutoUpdate(pluginId, enabled);
2017-02-18 23:46:59 +01:00
}
});
module.exports = SettingsPluginsView;