2016-08-17 06:01:20 +08:00
|
|
|
import ko from 'ko';
|
2014-08-25 23:49:01 +08:00
|
|
|
|
2020-08-14 04:58:41 +08:00
|
|
|
import { KeyState, StorageResultType, Notification } from 'Common/Enums';
|
2019-07-05 03:19:24 +08:00
|
|
|
import { getNotification, i18n } from 'Common/Translator';
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2020-09-15 01:40:56 +08:00
|
|
|
import Remote from 'Remote/Admin/Fetch';
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
import { popup, command, isPopupVisible, showScreenPopup } from 'Knoin/Knoin';
|
|
|
|
import { AbstractViewNext } from 'Knoin/AbstractViewNext';
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2016-09-10 06:38:16 +08:00
|
|
|
@popup({
|
2016-08-17 06:01:20 +08:00
|
|
|
name: 'View/Popup/Plugin',
|
|
|
|
templateID: 'PopupsPlugin'
|
|
|
|
})
|
2019-07-05 03:19:24 +08:00
|
|
|
class PluginPopupView extends AbstractViewNext {
|
2016-08-17 06:01:20 +08:00
|
|
|
constructor() {
|
|
|
|
super();
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2020-07-20 21:47:33 +08:00
|
|
|
this.onPluginSettingsUpdateResponse = this.onPluginSettingsUpdateResponse.bind(this);
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.saveError = ko.observable('');
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.name = ko.observable('');
|
|
|
|
this.readme = ko.observable('');
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.configures = ko.observableArray([]);
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2020-07-28 23:20:14 +08:00
|
|
|
this.hasReadme = ko.computed(() => !!this.readme());
|
2016-08-17 06:01:20 +08:00
|
|
|
this.hasConfiguration = ko.computed(() => 0 < this.configures().length);
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2016-09-10 06:38:16 +08:00
|
|
|
this.bDisabeCloseOnEsc = true;
|
|
|
|
this.sDefaultKeyScope = KeyState.All;
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2020-08-19 02:24:17 +08:00
|
|
|
this.tryToClosePopup = this.tryToClosePopup.debounce(200);
|
2016-09-10 06:38:16 +08:00
|
|
|
}
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2016-09-10 06:38:16 +08:00
|
|
|
@command((self) => self.hasConfiguration())
|
|
|
|
saveCommand() {
|
|
|
|
const list = {};
|
|
|
|
list.Name = this.name();
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2020-07-22 20:49:18 +08:00
|
|
|
this.configures().forEach(oItem => {
|
2016-09-10 06:38:16 +08:00
|
|
|
let value = oItem.value();
|
2019-07-05 03:19:24 +08:00
|
|
|
if (false === value || true === value) {
|
2016-09-10 06:38:16 +08:00
|
|
|
value = value ? '1' : '0';
|
|
|
|
}
|
|
|
|
list['_' + oItem.Name] = value;
|
|
|
|
});
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2016-09-10 06:38:16 +08:00
|
|
|
this.saveError('');
|
|
|
|
Remote.pluginSettingsUpdate(this.onPluginSettingsUpdateResponse, list);
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2016-08-17 06:01:20 +08:00
|
|
|
|
|
|
|
onPluginSettingsUpdateResponse(result, data) {
|
2019-07-05 03:19:24 +08:00
|
|
|
if (StorageResultType.Success === result && data && data.Result) {
|
2016-08-17 06:01:20 +08:00
|
|
|
this.cancelCommand();
|
2019-07-05 03:19:24 +08:00
|
|
|
} else {
|
2016-08-17 06:01:20 +08:00
|
|
|
this.saveError('');
|
2019-07-05 03:19:24 +08:00
|
|
|
if (data && data.ErrorCode) {
|
2016-08-17 06:01:20 +08:00
|
|
|
this.saveError(getNotification(data.ErrorCode));
|
2019-07-05 03:19:24 +08:00
|
|
|
} else {
|
2016-08-17 06:01:20 +08:00
|
|
|
this.saveError(getNotification(Notification.CantSavePluginSettings));
|
|
|
|
}
|
2013-11-16 06:21:12 +08:00
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2013-12-14 07:27:12 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
onShow(oPlugin) {
|
2020-09-28 16:29:58 +08:00
|
|
|
this.name('');
|
|
|
|
this.readme('');
|
2016-08-17 06:01:20 +08:00
|
|
|
this.configures([]);
|
2014-08-27 23:59:44 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (oPlugin) {
|
2016-08-17 06:01:20 +08:00
|
|
|
this.name(oPlugin.Name);
|
|
|
|
this.readme(oPlugin.Readme);
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
const config = oPlugin.Config;
|
2020-09-04 23:07:35 +08:00
|
|
|
if (Array.isNotEmpty(config)) {
|
2019-07-05 03:19:24 +08:00
|
|
|
this.configures(
|
2020-07-23 02:09:31 +08:00
|
|
|
config.map(item => ({
|
2019-07-05 03:19:24 +08:00
|
|
|
'value': ko.observable(item[0]),
|
|
|
|
'placeholder': ko.observable(item[6]),
|
|
|
|
'Name': item[1],
|
|
|
|
'Type': item[2],
|
|
|
|
'Label': item[3],
|
|
|
|
'Default': item[4],
|
|
|
|
'Desc': item[5]
|
|
|
|
}))
|
|
|
|
);
|
2014-08-21 23:08:34 +08:00
|
|
|
}
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
tryToClosePopup() {
|
|
|
|
const PopupsAskViewModel = require('View/Popup/Ask');
|
2019-07-05 03:19:24 +08:00
|
|
|
if (!isPopupVisible(PopupsAskViewModel)) {
|
|
|
|
showScreenPopup(PopupsAskViewModel, [
|
|
|
|
i18n('POPUPS_ASK/DESC_WANT_CLOSE_THIS_WINDOW'),
|
2020-08-19 02:24:17 +08:00
|
|
|
() => this.modalVisibility() && this.cancelCommand && this.cancelCommand()
|
2019-07-05 03:19:24 +08:00
|
|
|
]);
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
onBuild() {
|
2020-09-26 06:02:29 +08:00
|
|
|
shortcuts.add('escape', '', KeyState.All, () => {
|
2019-07-05 03:19:24 +08:00
|
|
|
if (this.modalVisibility()) {
|
2016-08-17 06:01:20 +08:00
|
|
|
this.tryToClosePopup();
|
|
|
|
}
|
2016-07-16 05:29:42 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
export { PluginPopupView, PluginPopupView as default };
|