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