snappymail/dev/View/Popup/AdvancedSearch.js

157 lines
3 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
_ = require('_'),
2014-08-25 23:49:01 +08:00
ko = require('ko'),
2014-08-25 15:10:51 +08:00
2014-09-05 06:49:03 +08:00
Utils = require('Common/Utils'),
2015-02-22 06:00:51 +08:00
MessageStore = require('Stores/User/Message'),
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 AdvancedSearchPopupView()
{
AbstractView.call(this, 'Popups', 'PopupsAdvancedSearch');
2014-08-21 23:08:34 +08:00
this.fromFocus = ko.observable(false);
this.from = ko.observable('');
this.to = ko.observable('');
this.subject = ko.observable('');
this.text = ko.observable('');
this.selectedDateValue = ko.observable(-1);
this.hasAttachment = ko.observable(false);
this.starred = ko.observable(false);
this.unseen = ko.observable(false);
this.searchCommand = Utils.createCommand(this, function () {
var sSearch = this.buildSearchString();
if ('' !== sSearch)
{
2015-02-22 06:00:51 +08:00
MessageStore.mainMessageListSearch(sSearch);
2014-08-21 23:08:34 +08:00
}
this.cancelCommand();
});
kn.constructorEnd(this);
2013-12-17 00:08:05 +08:00
}
kn.extendAsViewModel(['View/Popup/AdvancedSearch', 'PopupsAdvancedSearchViewModel'], AdvancedSearchPopupView);
_.extend(AdvancedSearchPopupView.prototype, AbstractView.prototype);
2014-08-21 23:08:34 +08:00
AdvancedSearchPopupView.prototype.buildSearchStringValue = function (sValue)
2013-12-17 00:08:05 +08:00
{
2014-08-21 23:08:34 +08:00
if (-1 < sValue.indexOf(' '))
{
sValue = '"' + sValue + '"';
}
2013-12-17 00:08:05 +08:00
2014-08-21 23:08:34 +08:00
return sValue;
};
AdvancedSearchPopupView.prototype.buildSearchString = function ()
2013-12-17 00:08:05 +08:00
{
2014-08-21 23:08:34 +08:00
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
if (sTo && '' !== sTo)
{
aResult.push('to:' + this.buildSearchStringValue(sTo));
}
if (sSubject && '' !== sSubject)
{
aResult.push('subject:' + this.buildSearchStringValue(sSubject));
}
if (this.hasAttachment())
{
aHas.push('attachment');
}
if (this.unseen())
{
aIs.push('unseen');
}
if (this.starred())
{
aIs.push('flagged');
}
if (0 < aHas.length)
{
aResult.push('has:' + aHas.join(','));
}
if (0 < aIs.length)
{
aResult.push('is:' + aIs.join(','));
}
if (-1 < this.selectedDateValue())
{
2015-03-07 08:32:06 +08:00
aResult.push('date:' + require('Common/Momentor').searchSubtractFormatDateHelper(this.selectedDateValue()) + '/');
2014-08-21 23:08:34 +08:00
}
if (sText && '' !== sText)
{
aResult.push('text:' + this.buildSearchStringValue(sText));
}
return Utils.trim(aResult.join(' '));
};
AdvancedSearchPopupView.prototype.clearPopup = function ()
{
2014-08-21 23:08:34 +08:00
this.from('');
this.to('');
this.subject('');
this.text('');
this.selectedDateValue(-1);
this.hasAttachment(false);
this.starred(false);
this.unseen(false);
2014-08-21 23:08:34 +08:00
this.fromFocus(true);
};
AdvancedSearchPopupView.prototype.onShow = function ()
{
2014-08-21 23:08:34 +08:00
this.clearPopup();
};
2015-02-16 05:55:59 +08:00
AdvancedSearchPopupView.prototype.onShowWithDelay = function ()
{
2014-08-21 23:08:34 +08:00
this.fromFocus(true);
};
module.exports = AdvancedSearchPopupView;
2014-09-05 06:49:03 +08:00
}());