snappymail/dev/View/Popup/AdvancedSearch.js

126 lines
2.9 KiB
JavaScript
Raw Normal View History

import { koComputable } from 'External/ko';
2019-07-05 03:19:24 +08:00
import { i18n, trigger as translatorTrigger } from 'Common/Translator';
import { MessagelistUserStore } from 'Stores/User/Messagelist';
2014-08-25 15:10:51 +08:00
import { AbstractViewPopup } from 'Knoin/AbstractViews';
import { FolderUserStore } from 'Stores/User/Folder';
2022-02-24 21:01:41 +08:00
export class AdvancedSearchPopupView extends AbstractViewPopup {
constructor() {
super('AdvancedSearch');
this.addObservables({
from: '',
to: '',
subject: '',
text: '',
selectedDateValue: -1,
selectedTreeValue: '',
hasAttachment: false,
starred: false,
unseen: false
});
this.showMultisearch = koComputable(() => FolderUserStore.hasCapability('MULTISEARCH'));
this.selectedDates = koComputable(() => {
translatorTrigger();
let prefix = 'SEARCH/LABEL_ADV_DATE_';
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') }
];
});
this.selectedTree = koComputable(() => {
translatorTrigger();
let prefix = 'SEARCH/LABEL_ADV_SUBFOLDERS_';
return [
{ id: '', name: i18n(prefix + 'NONE') },
{ id: 'subtree-one', name: i18n(prefix + 'SUBTREE_ONE') },
{ id: 'subtree', name: i18n(prefix + 'SUBTREE') }
];
});
}
2014-08-21 23:08:34 +08:00
2022-02-21 22:36:34 +08:00
submitForm() {
2016-09-10 06:38:16 +08:00
const search = this.buildSearchString();
if (search) {
MessagelistUserStore.mainSearch(search);
2016-09-10 06:38:16 +08:00
}
this.close();
2016-09-10 06:38:16 +08:00
}
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
buildSearchString() {
const
2021-11-03 21:47:03 +08:00
data = new FormData(),
append = (key, value) => value.length && data.append(key, value);
append('from', this.from().trim());
append('to', this.to().trim());
append('subject', this.subject().trim());
append('text', this.text().trim());
append('in', this.selectedTreeValue());
if (-1 < this.selectedDateValue()) {
let d = new Date();
d.setDate(d.getDate() - this.selectedDateValue());
append('date', d.format('Y.m.d') + '/');
}
2016-06-30 08:02:45 +08:00
2021-11-03 21:47:03 +08:00
let result = new URLSearchParams(data).toString();
2014-08-21 23:08:34 +08:00
2019-07-05 03:19:24 +08:00
if (this.hasAttachment()) {
2021-11-03 21:47:03 +08:00
result += '&attachment';
2014-08-21 23:08:34 +08:00
}
2019-07-05 03:19:24 +08:00
if (this.unseen()) {
2021-11-03 21:47:03 +08:00
result += '&unseen';
}
2019-07-05 03:19:24 +08:00
if (this.starred()) {
2021-11-03 21:47:03 +08:00
result += '&flagged';
}
2014-08-21 23:08:34 +08:00
2021-11-03 21:47:03 +08:00
return result.replace(/^&+/, '');
2016-06-30 08:02:45 +08:00
}
2014-08-21 23:08:34 +08:00
2021-11-03 21:58:34 +08:00
onShow(search) {
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);
2014-08-21 23:08:34 +08:00
this.parseSearchStringValue(search);
2016-06-30 08:02:45 +08:00
}
}