snappymail/dev/View/Popup/AdvancedSearch.js

188 lines
3.9 KiB
JavaScript
Raw Normal View History

2016-06-30 08:02:45 +08:00
var
_ = require('_'),
ko = require('ko'),
2016-06-30 08:02:45 +08:00
Utils = require('Common/Utils'),
Translator = require('Common/Translator'),
2016-06-30 08:02:45 +08:00
MessageStore = require('Stores/User/Message'),
2014-08-25 15:10:51 +08:00
2016-06-30 08:02:45 +08:00
kn = require('Knoin/Knoin'),
AbstractView = require('Knoin/AbstractView');
2016-06-30 08:02:45 +08:00
/**
* @constructor
* @extends AbstractView
*/
function AdvancedSearchPopupView()
{
AbstractView.call(this, 'Popups', 'PopupsAdvancedSearch');
2016-06-30 08:02:45 +08:00
this.fromFocus = ko.observable(false);
2016-06-30 08:02:45 +08:00
this.from = ko.observable('');
this.to = ko.observable('');
this.subject = ko.observable('');
this.text = ko.observable('');
this.selectedDateValue = ko.observable(-1);
2013-12-17 00:08:05 +08:00
2016-06-30 08:02:45 +08:00
this.hasAttachment = ko.observable(false);
this.starred = ko.observable(false);
this.unseen = ko.observable(false);
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
this.searchCommand = Utils.createCommand(this, function() {
2013-12-17 00:08:05 +08:00
2016-06-30 08:02:45 +08:00
var sSearch = this.buildSearchString();
if ('' !== sSearch)
2014-08-21 23:08:34 +08:00
{
2016-06-30 08:02:45 +08:00
MessageStore.mainMessageListSearch(sSearch);
2014-08-21 23:08:34 +08:00
}
2016-06-30 08:02:45 +08:00
this.cancelCommand();
});
this.selectedDates = ko.computed(function() {
Translator.trigger();
return [
{'id': -1, 'name': Translator.i18n('SEARCH/LABEL_ADV_DATE_ALL')},
{'id': 3, 'name': Translator.i18n('SEARCH/LABEL_ADV_DATE_3_DAYS')},
{'id': 7, 'name': Translator.i18n('SEARCH/LABEL_ADV_DATE_7_DAYS')},
{'id': 30, 'name': Translator.i18n('SEARCH/LABEL_ADV_DATE_MONTH')},
{'id': 90, 'name': Translator.i18n('SEARCH/LABEL_ADV_DATE_3_MONTHS')},
{'id': 180, 'name': Translator.i18n('SEARCH/LABEL_ADV_DATE_6_MONTHS')},
{'id': 365, 'name': Translator.i18n('SEARCH/LABEL_ADV_DATE_YEAR')}
];
}, this);
kn.constructorEnd(this);
}
kn.extendAsViewModel(['View/Popup/AdvancedSearch', 'PopupsAdvancedSearchViewModel'], AdvancedSearchPopupView);
_.extend(AdvancedSearchPopupView.prototype, AbstractView.prototype);
AdvancedSearchPopupView.prototype.parseSearchStringValue = function(search)
{
var
self = this,
parts = (search || '').split(/[\s]+/g);
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
_.each(parts, function(part) {
switch (part)
2014-08-21 23:08:34 +08:00
{
2016-06-30 08:02:45 +08:00
case 'has:attachment':
self.hasAttachment(true);
break;
case 'is:unseen,flagged':
self.starred(true);
/* falls through */
case 'is:unseen':
self.unseen(true);
break;
// no default
2014-08-21 23:08:34 +08:00
}
2016-06-30 08:02:45 +08:00
});
};
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
AdvancedSearchPopupView.prototype.buildSearchStringValue = function(sValue)
{
if (-1 < sValue.indexOf(' '))
{
sValue = '"' + sValue + '"';
}
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
return sValue;
};
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
AdvancedSearchPopupView.prototype.buildSearchString = function()
{
var
aResult = [],
sFrom = Utils.trim(this.from()),
sTo = Utils.trim(this.to()),
sSubject = Utils.trim(this.subject()),
sText = Utils.trim(this.text()),
aIs = [],
aHas = [];
if (sFrom && '' !== sFrom)
{
aResult.push('from:' + this.buildSearchStringValue(sFrom));
}
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
if (sTo && '' !== sTo)
{
aResult.push('to:' + this.buildSearchStringValue(sTo));
}
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
if (sSubject && '' !== sSubject)
{
aResult.push('subject:' + this.buildSearchStringValue(sSubject));
}
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
if (this.hasAttachment())
{
aHas.push('attachment');
}
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
if (this.unseen())
{
aIs.push('unseen');
}
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
if (this.starred())
{
2016-06-30 08:02:45 +08:00
aIs.push('flagged');
}
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
if (0 < aHas.length)
{
aResult.push('has:' + aHas.join(','));
}
2016-06-30 08:02:45 +08:00
if (0 < aIs.length)
{
aResult.push('is:' + aIs.join(','));
}
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
if (-1 < this.selectedDateValue())
{
2016-06-30 08:02:45 +08:00
aResult.push('date:' + require('Common/Momentor').searchSubtractFormatDateHelper(this.selectedDateValue()) + '/');
}
2016-06-30 08:02:45 +08:00
if (sText && '' !== sText)
{
2016-06-30 08:02:45 +08:00
aResult.push('text:' + this.buildSearchStringValue(sText));
}
return Utils.trim(aResult.join(' '));
};
AdvancedSearchPopupView.prototype.clearPopup = function()
{
this.from('');
this.to('');
this.subject('');
this.text('');
this.selectedDateValue(-1);
this.hasAttachment(false);
this.starred(false);
this.unseen(false);
this.fromFocus(true);
};
AdvancedSearchPopupView.prototype.onShow = function(search)
{
this.clearPopup();
this.parseSearchStringValue(search);
};
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
AdvancedSearchPopupView.prototype.onShowWithDelay = function()
{
this.fromFocus(true);
};
2016-06-30 08:02:45 +08:00
module.exports = AdvancedSearchPopupView;