snappymail/dev/Model/Filter.js

319 lines
7.6 KiB
JavaScript
Raw Normal View History

2016-06-30 08:02:45 +08:00
var
_ = require('_'),
ko = require('ko'),
2014-08-25 23:49:01 +08:00
2016-06-30 08:02:45 +08:00
Enums = require('Common/Enums'),
Utils = require('Common/Utils'),
Translator = require('Common/Translator'),
2016-06-30 08:02:45 +08:00
Cache = require('Common/Cache'),
2016-06-30 08:02:45 +08:00
FilterConditionModel = require('Model/FilterCondition'),
2016-06-30 08:02:45 +08:00
AbstractModel = require('Knoin/AbstractModel');
2016-06-30 08:02:45 +08:00
/**
* @constructor
*/
function FilterModel()
{
AbstractModel.call(this, 'FilterModel');
2014-10-04 19:58:01 +08:00
2016-06-30 08:02:45 +08:00
this.enabled = ko.observable(true);
2016-06-30 08:02:45 +08:00
this.id = '';
2016-06-30 08:02:45 +08:00
this.name = ko.observable('');
this.name.error = ko.observable(false);
this.name.focused = ko.observable(false);
2014-10-04 19:58:01 +08:00
2016-06-30 08:02:45 +08:00
this.conditions = ko.observableArray([]);
this.conditionsType = ko.observable(Enums.FilterRulesType.Any);
// Actions
this.actionValue = ko.observable('');
this.actionValue.error = ko.observable(false);
this.actionValueSecond = ko.observable('');
this.actionValueThird = ko.observable('');
this.actionValueFourth = ko.observable('');
this.actionValueFourth.error = ko.observable(false);
this.actionMarkAsRead = ko.observable(false);
this.actionKeep = ko.observable(true);
this.actionNoStop = ko.observable(false);
this.actionType = ko.observable(Enums.FiltersAction.MoveTo);
this.actionType.subscribe(function() {
this.actionValue('');
this.actionValue.error(false);
this.actionValueSecond('');
this.actionValueThird('');
this.actionValueFourth('');
this.actionValueFourth.error(false);
}, this);
var fGetRealFolderName = function(sFolderFullNameRaw) {
var oFolder = Cache.getFolderFromCacheList(sFolderFullNameRaw);
return oFolder ? oFolder.fullName.replace(
'.' === oFolder.delimiter ? /\./ : /[\\\/]+/, ' / ') : sFolderFullNameRaw;
};
2016-06-30 08:02:45 +08:00
this.nameSub = ko.computed(function() {
2016-06-30 08:02:45 +08:00
var
sResult = '',
sActionValue = this.actionValue();
2015-01-22 04:15:40 +08:00
2016-06-30 08:02:45 +08:00
switch (this.actionType())
{
2016-06-30 08:02:45 +08:00
case Enums.FiltersAction.MoveTo:
sResult = Translator.i18n('SETTINGS_FILTERS/SUBNAME_MOVE_TO', {
FOLDER: fGetRealFolderName(sActionValue)
});
break;
case Enums.FiltersAction.Forward:
sResult = Translator.i18n('SETTINGS_FILTERS/SUBNAME_FORWARD_TO', {
EMAIL: sActionValue
});
break;
case Enums.FiltersAction.Vacation:
sResult = Translator.i18n('SETTINGS_FILTERS/SUBNAME_VACATION_MESSAGE');
break;
case Enums.FiltersAction.Reject:
sResult = Translator.i18n('SETTINGS_FILTERS/SUBNAME_REJECT');
break;
case Enums.FiltersAction.Discard:
sResult = Translator.i18n('SETTINGS_FILTERS/SUBNAME_DISCARD');
break;
// no default
}
2016-06-30 08:02:45 +08:00
return sResult ? '(' + sResult + ')' : '';
}, this);
this.actionTemplate = ko.computed(function() {
2016-06-30 08:02:45 +08:00
var sTemplate = '';
switch (this.actionType())
2015-07-30 02:13:49 +08:00
{
2016-06-30 08:02:45 +08:00
case Enums.FiltersAction.Forward:
sTemplate = 'SettingsFiltersActionForward';
break;
case Enums.FiltersAction.Vacation:
sTemplate = 'SettingsFiltersActionVacation';
break;
case Enums.FiltersAction.Reject:
sTemplate = 'SettingsFiltersActionReject';
break;
case Enums.FiltersAction.None:
sTemplate = 'SettingsFiltersActionNone';
break;
case Enums.FiltersAction.Discard:
sTemplate = 'SettingsFiltersActionDiscard';
break;
case Enums.FiltersAction.MoveTo:
default:
sTemplate = 'SettingsFiltersActionMoveToFolder';
break;
2015-07-30 02:13:49 +08:00
}
2016-06-30 08:02:45 +08:00
return sTemplate;
2016-06-30 08:02:45 +08:00
}, this);
this.regDisposables(this.conditions.subscribe(Utils.windowResizeCallback));
this.regDisposables(this.name.subscribe(function(sValue) {
this.name.error('' === sValue);
}, this));
this.regDisposables(this.actionValue.subscribe(function(sValue) {
this.actionValue.error('' === sValue);
}, this));
this.regDisposables([this.actionNoStop, this.actionTemplate]);
2016-06-30 08:02:45 +08:00
this.deleteAccess = ko.observable(false);
this.canBeDeleted = ko.observable(true);
}
_.extend(FilterModel.prototype, AbstractModel.prototype);
FilterModel.prototype.generateID = function()
{
this.id = Utils.fakeMd5();
};
FilterModel.prototype.verify = function()
{
if ('' === this.name())
{
2016-06-30 08:02:45 +08:00
this.name.error(true);
return false;
}
2016-06-30 08:02:45 +08:00
if (0 < this.conditions().length)
{
2016-06-30 08:02:45 +08:00
if (_.find(this.conditions(), function(oCond) {
return oCond && !oCond.verify();
}))
{
return false;
}
}
2016-06-30 08:02:45 +08:00
if ('' === this.actionValue())
{
2016-06-30 08:02:45 +08:00
if (-1 < Utils.inArray(this.actionType(), [
Enums.FiltersAction.MoveTo,
Enums.FiltersAction.Forward,
Enums.FiltersAction.Reject,
Enums.FiltersAction.Vacation
]))
{
this.actionValue.error(true);
return false;
}
}
2016-06-30 08:02:45 +08:00
if (Enums.FiltersAction.Forward === this.actionType() &&
-1 === this.actionValue().indexOf('@'))
2015-07-30 02:13:49 +08:00
{
2016-06-30 08:02:45 +08:00
this.actionValue.error(true);
return false;
}
2015-07-30 02:13:49 +08:00
2016-06-30 08:02:45 +08:00
if (Enums.FiltersAction.Vacation === this.actionType() &&
'' !== this.actionValueFourth() && -1 === this.actionValueFourth().indexOf('@')
)
2014-08-20 23:03:12 +08:00
{
2016-06-30 08:02:45 +08:00
this.actionValueFourth.error(true);
return false;
}
2015-01-22 04:15:40 +08:00
2016-06-30 08:02:45 +08:00
this.name.error(false);
this.actionValue.error(false);
return true;
};
FilterModel.prototype.toJson = function()
{
return {
ID: this.id,
Enabled: this.enabled() ? '1' : '0',
Name: this.name(),
ConditionsType: this.conditionsType(),
Conditions: _.map(this.conditions(), function(oItem) {
return oItem.toJson();
}),
ActionValue: this.actionValue(),
ActionValueSecond: this.actionValueSecond(),
ActionValueThird: this.actionValueThird(),
ActionValueFourth: this.actionValueFourth(),
ActionType: this.actionType(),
Stop: this.actionNoStop() ? '0' : '1',
Keep: this.actionKeep() ? '1' : '0',
MarkAsRead: this.actionMarkAsRead() ? '1' : '0'
};
};
FilterModel.prototype.addCondition = function()
{
this.conditions.push(new FilterConditionModel());
};
FilterModel.prototype.removeCondition = function(oConditionToDelete)
{
this.conditions.remove(oConditionToDelete);
Utils.delegateRunOnDestroy(oConditionToDelete);
};
FilterModel.prototype.setRecipients = function()
{
this.actionValueFourth(require('Stores/User/Account').accountsEmails().join(', '));
};
FilterModel.prototype.parse = function(oItem)
{
var bResult = false;
if (oItem && 'Object/Filter' === oItem['@Object'])
{
this.id = Utils.pString(oItem.ID);
this.name(Utils.pString(oItem.Name));
this.enabled(!!oItem.Enabled);
2015-01-22 04:15:40 +08:00
2016-06-30 08:02:45 +08:00
this.conditionsType(Utils.pString(oItem.ConditionsType));
2015-01-22 04:15:40 +08:00
2016-06-30 08:02:45 +08:00
this.conditions([]);
2015-01-22 04:15:40 +08:00
2016-06-30 08:02:45 +08:00
if (Utils.isNonEmptyArray(oItem.Conditions))
{
this.conditions(_.compact(_.map(oItem.Conditions, function(aData) {
var oFilterCondition = new FilterConditionModel();
return oFilterCondition && oFilterCondition.parse(aData) ?
oFilterCondition : null;
})));
}
2015-01-22 04:15:40 +08:00
2016-06-30 08:02:45 +08:00
this.actionType(Utils.pString(oItem.ActionType));
2015-01-22 04:15:40 +08:00
2016-06-30 08:02:45 +08:00
this.actionValue(Utils.pString(oItem.ActionValue));
this.actionValueSecond(Utils.pString(oItem.ActionValueSecond));
this.actionValueThird(Utils.pString(oItem.ActionValueThird));
this.actionValueFourth(Utils.pString(oItem.ActionValueFourth));
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
this.actionNoStop(!oItem.Stop);
this.actionKeep(!!oItem.Keep);
this.actionMarkAsRead(!!oItem.MarkAsRead);
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
bResult = true;
}
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
return bResult;
};
2016-06-30 08:02:45 +08:00
FilterModel.prototype.cloneSelf = function()
{
var oClone = new FilterModel();
2016-06-30 08:02:45 +08:00
oClone.id = this.id;
2016-06-30 08:02:45 +08:00
oClone.enabled(this.enabled());
2016-06-30 08:02:45 +08:00
oClone.name(this.name());
oClone.name.error(this.name.error());
2016-06-30 08:02:45 +08:00
oClone.conditionsType(this.conditionsType());
2016-06-30 08:02:45 +08:00
oClone.actionMarkAsRead(this.actionMarkAsRead());
2016-06-30 08:02:45 +08:00
oClone.actionType(this.actionType());
2016-06-30 08:02:45 +08:00
oClone.actionValue(this.actionValue());
oClone.actionValue.error(this.actionValue.error());
2016-06-30 08:02:45 +08:00
oClone.actionValueSecond(this.actionValueSecond());
oClone.actionValueThird(this.actionValueThird());
oClone.actionValueFourth(this.actionValueFourth());
2015-01-18 22:01:07 +08:00
2016-06-30 08:02:45 +08:00
oClone.actionKeep(this.actionKeep());
oClone.actionNoStop(this.actionNoStop());
2016-06-30 08:02:45 +08:00
oClone.conditions(_.map(this.conditions(), function(oCondition) {
return oCondition.cloneSelf();
}));
2016-06-30 08:02:45 +08:00
return oClone;
};
2016-06-30 08:02:45 +08:00
module.exports = FilterModel;