snappymail/dev/View/Popup/Plugin.js

172 lines
3.6 KiB
JavaScript
Raw Normal View History

2014-09-05 06:49:03 +08:00
(function () {
2014-08-25 23:49:01 +08:00
'use strict';
2014-08-21 23:08:34 +08:00
var
2014-08-25 23:49:01 +08:00
_ = require('_'),
ko = require('ko'),
key = require('key'),
2014-08-25 15:10:51 +08:00
2014-09-05 06:49:03 +08:00
Enums = require('Common/Enums'),
Utils = require('Common/Utils'),
Translator = require('Common/Translator'),
2014-08-21 23:08:34 +08:00
Remote = require('Storage/Admin/Remote'),
2014-08-21 23:08:34 +08:00
kn = require('Knoin/Knoin'),
AbstractView = require('Knoin/AbstractView')
2014-08-21 23:08:34 +08:00
;
/**
* @constructor
* @extends AbstractView
2014-08-21 23:08:34 +08:00
*/
function PluginPopupView()
2014-08-21 23:08:34 +08:00
{
AbstractView.call(this, 'Popups', 'PopupsPlugin');
2014-08-21 23:08:34 +08:00
var self = this;
this.onPluginSettingsUpdateResponse = _.bind(this.onPluginSettingsUpdateResponse, this);
this.saveError = ko.observable('');
this.name = ko.observable('');
this.readme = ko.observable('');
this.configures = ko.observableArray([]);
this.hasReadme = ko.computed(function () {
return '' !== this.readme();
}, this);
this.hasConfiguration = ko.computed(function () {
return 0 < this.configures().length;
}, this);
2014-08-21 23:08:34 +08:00
this.readmePopoverConf = {
'placement': 'top',
'trigger': 'hover',
'title': 'About',
'container': 'body',
2014-08-21 23:08:34 +08:00
'content': function () {
return self.readme();
}
};
2014-08-21 23:08:34 +08:00
this.saveCommand = Utils.createCommand(this, function () {
2014-07-10 22:44:45 +08:00
2014-08-21 23:08:34 +08:00
var oList = {};
2014-08-21 23:08:34 +08:00
oList['Name'] = this.name();
2014-08-21 23:08:34 +08:00
_.each(this.configures(), function (oItem) {
var mValue = oItem.value();
if (false === mValue || true === mValue)
{
mValue = mValue ? '1' : '0';
}
oList['_' + oItem['Name']] = mValue;
}, this);
this.saveError('');
Remote.pluginSettingsUpdate(this.onPluginSettingsUpdateResponse, oList);
}, this.hasConfiguration);
this.bDisabeCloseOnEsc = true;
this.sDefaultKeyScope = Enums.KeyState.All;
this.tryToClosePopup = _.debounce(_.bind(this.tryToClosePopup, this), 200);
kn.constructorEnd(this);
}
2014-08-21 23:08:34 +08:00
kn.extendAsViewModel(['View/Popup/Plugin', 'PopupsPluginViewModel'], PluginPopupView);
_.extend(PluginPopupView.prototype, AbstractView.prototype);
2014-08-21 23:08:34 +08:00
PluginPopupView.prototype.onPluginSettingsUpdateResponse = function (sResult, oData)
{
2014-08-21 23:08:34 +08:00
if (Enums.StorageResultType.Success === sResult && oData && oData.Result)
{
2014-08-21 23:08:34 +08:00
this.cancelCommand();
}
else
{
2014-08-21 23:08:34 +08:00
this.saveError('');
if (oData && oData.ErrorCode)
{
this.saveError(Translator.getNotification(oData.ErrorCode));
2014-08-21 23:08:34 +08:00
}
else
{
this.saveError(Translator.getNotification(Enums.Notification.CantSavePluginSettings));
2014-08-21 23:08:34 +08:00
}
}
2014-08-21 23:08:34 +08:00
};
PluginPopupView.prototype.onShow = function (oPlugin)
{
2014-08-21 23:08:34 +08:00
this.name();
this.readme();
this.configures([]);
if (oPlugin)
{
2014-08-21 23:08:34 +08:00
this.name(oPlugin['Name']);
this.readme(oPlugin['Readme']);
2014-08-21 23:08:34 +08:00
var aConfig = oPlugin['Config'];
if (Utils.isNonEmptyArray(aConfig))
2014-07-10 22:44:45 +08:00
{
2014-08-21 23:08:34 +08:00
this.configures(_.map(aConfig, function (aItem) {
return {
'value': ko.observable(aItem[0]),
'placeholder': ko.observable(aItem[6]),
2014-08-21 23:08:34 +08:00
'Name': aItem[1],
'Type': aItem[2],
'Label': aItem[3],
'Default': aItem[4],
'Desc': aItem[5]
};
}));
2014-07-10 22:44:45 +08:00
}
2014-08-21 23:08:34 +08:00
}
};
PluginPopupView.prototype.tryToClosePopup = function ()
2014-08-21 23:08:34 +08:00
{
2014-08-27 23:59:44 +08:00
var
self = this,
PopupsAskViewModel = require('View/Popup/Ask')
2014-08-27 23:59:44 +08:00
;
2014-08-21 23:08:34 +08:00
if (!kn.isPopupVisible(PopupsAskViewModel))
{
kn.showScreenPopup(PopupsAskViewModel, [Translator.i18n('POPUPS_ASK/DESC_WANT_CLOSE_THIS_WINDOW'), function () {
2014-08-21 23:08:34 +08:00
if (self.modalVisibility())
{
Utils.delegateRun(self, 'cancelCommand');
}
}]);
}
2014-08-21 23:08:34 +08:00
};
PluginPopupView.prototype.onBuild = function ()
2014-08-21 23:08:34 +08:00
{
key('esc', Enums.KeyState.All, _.bind(function () {
if (this.modalVisibility())
{
this.tryToClosePopup();
}
return false;
}, this));
};
module.exports = PluginPopupView;
2014-08-21 23:08:34 +08:00
2014-09-05 06:49:03 +08:00
}());