snappymail/dev/ViewModels/Popups/PopupsAdvancedSearchViewModel.js

157 lines
3 KiB
JavaScript
Raw Normal View History

/* RainLoop Webmail (c) RainLoop Team | Licensed under CC BY-NC-SA 3.0 */
2014-08-25 23:49:01 +08:00
(function (module, require) {
2014-08-25 23:49:01 +08:00
'use strict';
2014-08-21 23:08:34 +08:00
var
2014-08-25 23:49:01 +08:00
ko = require('ko'),
moment = require('moment'),
2014-08-25 15:10:51 +08:00
2014-08-25 23:49:01 +08:00
Utils = require('Utils'),
2014-08-27 23:59:44 +08:00
Data = require('Storage:RainLoop:Data'),
2014-08-27 23:59:44 +08:00
kn = require('App:Knoin'),
KnoinAbstractViewModel = require('Knoin:AbstractViewModel')
;
2014-08-21 23:08:34 +08:00
/**
* @constructor
* @extends KnoinAbstractViewModel
*/
function PopupsAdvancedSearchViewModel()
{
2014-08-21 23:08:34 +08:00
KnoinAbstractViewModel.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)
{
Data.mainMessageListSearch(sSearch);
}
this.cancelCommand();
});
kn.constructorEnd(this);
2013-12-17 00:08:05 +08:00
}
2014-08-21 23:08:34 +08:00
kn.extendAsViewModel('PopupsAdvancedSearchViewModel', PopupsAdvancedSearchViewModel);
PopupsAdvancedSearchViewModel.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;
};
PopupsAdvancedSearchViewModel.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())
{
aResult.push('date:' + moment().subtract('days', this.selectedDateValue()).format('YYYY.MM.DD') + '/');
}
if (sText && '' !== sText)
{
aResult.push('text:' + this.buildSearchStringValue(sText));
}
return Utils.trim(aResult.join(' '));
};
PopupsAdvancedSearchViewModel.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);
};
PopupsAdvancedSearchViewModel.prototype.onShow = function ()
{
2014-08-21 23:08:34 +08:00
this.clearPopup();
};
2014-08-21 23:08:34 +08:00
PopupsAdvancedSearchViewModel.prototype.onFocus = function ()
{
2014-08-21 23:08:34 +08:00
this.fromFocus(true);
};
2014-08-22 23:08:56 +08:00
module.exports = PopupsAdvancedSearchViewModel;
2014-08-25 23:49:01 +08:00
}(module, require));