2013-11-16 06:21:12 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
var
|
|
|
|
_ = require('_'),
|
|
|
|
ko = require('ko'),
|
2013-12-09 23:16:58 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
Utils = require('Common/Utils'),
|
|
|
|
Translator = require('Common/Translator'),
|
2014-09-02 08:15:31 +08:00
|
|
|
|
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');
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @extends AbstractView
|
|
|
|
*/
|
|
|
|
function AdvancedSearchPopupView()
|
|
|
|
{
|
|
|
|
AbstractView.call(this, 'Popups', 'PopupsAdvancedSearch');
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
this.fromFocus = ko.observable(false);
|
2013-11-16 06:21:12 +08:00
|
|
|
|
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())
|
2014-04-26 05:50:17 +08:00
|
|
|
{
|
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(','));
|
|
|
|
}
|
2014-04-26 05:50:17 +08:00
|
|
|
|
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())
|
2013-11-16 06:21:12 +08:00
|
|
|
{
|
2016-06-30 08:02:45 +08:00
|
|
|
aResult.push('date:' + require('Common/Momentor').searchSubtractFormatDateHelper(this.selectedDateValue()) + '/');
|
|
|
|
}
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
if (sText && '' !== sText)
|
2013-11-16 06:21:12 +08:00
|
|
|
{
|
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);
|
|
|
|
};
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
module.exports = AdvancedSearchPopupView;
|