snappymail/dev/View/Popup/AdvancedSearch.js

152 lines
3 KiB
JavaScript
Raw Normal View History

import ko from 'ko';
2019-07-05 03:19:24 +08:00
import { i18n, trigger as translatorTrigger } from 'Common/Translator';
import { MessageUserStore } from 'Stores/User/Message';
2014-08-25 15:10:51 +08:00
import { decorateKoCommands } from 'Knoin/Knoin';
import { AbstractViewPopup } from 'Knoin/AbstractViews';
class AdvancedSearchPopupView extends AbstractViewPopup {
constructor() {
super('AdvancedSearch');
this.addObservables({
from: '',
to: '',
subject: '',
text: '',
selectedDateValue: -1,
hasAttachment: false,
starred: false,
unseen: false
});
2021-01-26 18:46:30 +08:00
let prefix = 'SEARCH/LABEL_ADV_DATE_';
this.selectedDates = ko.computed(() => {
translatorTrigger();
return [
2021-01-26 18:46:30 +08:00
{ id: -1, name: i18n(prefix + 'ALL') },
{ id: 3, name: i18n(prefix + '3_DAYS') },
{ id: 7, name: i18n(prefix + '7_DAYS') },
{ id: 30, name: i18n(prefix + 'MONTH') },
{ id: 90, name: i18n(prefix + '3_MONTHS') },
{ id: 180, name: i18n(prefix + '6_MONTHS') },
{ id: 365, name: i18n(prefix + 'YEAR') }
];
});
decorateKoCommands(this, {
searchCommand: 1
});
}
2014-08-21 23:08:34 +08:00
2016-09-10 06:38:16 +08:00
searchCommand() {
const search = this.buildSearchString();
if (search) {
MessageUserStore.mainMessageListSearch(search);
2016-09-10 06:38:16 +08:00
}
this.cancelCommand();
}
parseSearchStringValue(search) {
const parts = (search || '').split(/[\s]+/g);
parts.forEach(part => {
2019-07-05 03:19:24 +08:00
switch (part) {
case 'has:attachment':
this.hasAttachment(true);
break;
case 'is:unseen,flagged':
this.starred(true);
2019-07-05 03:19:24 +08:00
/* falls through */
case 'is:unseen':
this.unseen(true);
break;
// no default
}
});
}
2013-12-17 00:08:05 +08:00
buildSearchStringValue(value) {
2020-07-21 03:39:00 +08:00
if (value.includes(' ')) {
value = '"' + value + '"';
2014-08-21 23:08:34 +08:00
}
return value;
}
2014-08-21 23:08:34 +08:00
buildSearchString() {
2019-07-05 03:19:24 +08:00
const result = [],
from_ = this.from().trim(),
to = this.to().trim(),
subject = this.subject().trim(),
text = this.text().trim(),
isPart = [],
hasPart = [];
if (from_) {
result.push('from:' + this.buildSearchStringValue(from_));
}
2016-06-30 08:02:45 +08:00
if (to) {
result.push('to:' + this.buildSearchStringValue(to));
}
2016-06-30 08:02:45 +08:00
if (subject) {
result.push('subject:' + this.buildSearchStringValue(subject));
}
2014-08-21 23:08:34 +08:00
2019-07-05 03:19:24 +08:00
if (this.hasAttachment()) {
hasPart.push('attachment');
2014-08-21 23:08:34 +08:00
}
2019-07-05 03:19:24 +08:00
if (this.unseen()) {
isPart.push('unseen');
}
2014-08-21 23:08:34 +08:00
2019-07-05 03:19:24 +08:00
if (this.starred()) {
isPart.push('flagged');
}
2014-08-21 23:08:34 +08:00
if (hasPart.length) {
result.push('has:' + hasPart.join(','));
}
2014-08-21 23:08:34 +08:00
if (isPart.length) {
result.push('is:' + isPart.join(','));
}
2014-08-21 23:08:34 +08:00
2019-07-05 03:19:24 +08:00
if (-1 < this.selectedDateValue()) {
let d = new Date();
d.setDate(d.getDate() - this.selectedDateValue());
result.push('date:' + d.format('Y.m.d') + '/');
}
2014-08-21 23:08:34 +08:00
if (text) {
result.push('text:' + this.buildSearchStringValue(text));
}
2014-08-21 23:08:34 +08:00
return result.join(' ').trim();
2016-06-30 08:02:45 +08:00
}
2014-08-21 23:08:34 +08:00
clearPopup() {
this.from('');
this.to('');
this.subject('');
this.text('');
2014-08-21 23:08:34 +08:00
this.selectedDateValue(-1);
this.hasAttachment(false);
this.starred(false);
this.unseen(false);
2016-06-30 08:02:45 +08:00
}
2014-08-21 23:08:34 +08:00
onShow(search) {
this.clearPopup();
this.parseSearchStringValue(search);
2016-06-30 08:02:45 +08:00
}
}
2019-07-05 03:19:24 +08:00
export { AdvancedSearchPopupView, AdvancedSearchPopupView as default };