2016-08-17 06:01:20 +08:00
|
|
|
import ko from 'ko';
|
2013-12-09 23:16:58 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
import { i18n, trigger as translatorTrigger } from 'Common/Translator';
|
2014-09-02 08:15:31 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
import MessageStore from 'Stores/User/Message';
|
2014-08-25 15:10:51 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
import { popup, command } from 'Knoin/Knoin';
|
|
|
|
import { AbstractViewNext } from 'Knoin/AbstractViewNext';
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2016-09-10 06:38:16 +08:00
|
|
|
@popup({
|
2016-08-17 06:01:20 +08:00
|
|
|
name: 'View/Popup/AdvancedSearch',
|
|
|
|
templateID: 'PopupsAdvancedSearch'
|
|
|
|
})
|
2019-07-05 03:19:24 +08:00
|
|
|
class AdvancedSearchPopupView extends AbstractViewNext {
|
2016-08-17 06:01:20 +08:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
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.selectedDates = ko.computed(() => {
|
|
|
|
translatorTrigger();
|
|
|
|
return [
|
2019-07-05 03:19:24 +08:00
|
|
|
{ id: -1, name: i18n('SEARCH/LABEL_ADV_DATE_ALL') },
|
|
|
|
{ id: 3, name: i18n('SEARCH/LABEL_ADV_DATE_3_DAYS') },
|
|
|
|
{ id: 7, name: i18n('SEARCH/LABEL_ADV_DATE_7_DAYS') },
|
|
|
|
{ id: 30, name: i18n('SEARCH/LABEL_ADV_DATE_MONTH') },
|
|
|
|
{ id: 90, name: i18n('SEARCH/LABEL_ADV_DATE_3_MONTHS') },
|
|
|
|
{ id: 180, name: i18n('SEARCH/LABEL_ADV_DATE_6_MONTHS') },
|
|
|
|
{ id: 365, name: i18n('SEARCH/LABEL_ADV_DATE_YEAR') }
|
2016-08-17 06:01:20 +08:00
|
|
|
];
|
|
|
|
});
|
|
|
|
}
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2016-09-10 06:38:16 +08:00
|
|
|
@command()
|
|
|
|
searchCommand() {
|
|
|
|
const search = this.buildSearchString();
|
2020-07-28 23:20:14 +08:00
|
|
|
if (search) {
|
2016-09-10 06:38:16 +08:00
|
|
|
MessageStore.mainMessageListSearch(search);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.cancelCommand();
|
|
|
|
}
|
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
parseSearchStringValue(search) {
|
|
|
|
const parts = (search || '').split(/[\s]+/g);
|
2020-07-22 20:49:18 +08:00
|
|
|
parts.forEach(part => {
|
2019-07-05 03:19:24 +08:00
|
|
|
switch (part) {
|
2016-08-17 06:01:20 +08:00
|
|
|
case 'has:attachment':
|
|
|
|
this.hasAttachment(true);
|
|
|
|
break;
|
|
|
|
case 'is:unseen,flagged':
|
|
|
|
this.starred(true);
|
2019-07-05 03:19:24 +08:00
|
|
|
/* falls through */
|
2016-08-17 06:01:20 +08:00
|
|
|
case 'is:unseen':
|
|
|
|
this.unseen(true);
|
|
|
|
break;
|
|
|
|
// no default
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-12-17 00:08:05 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
buildSearchStringValue(value) {
|
2020-07-21 03:39:00 +08:00
|
|
|
if (value.includes(' ')) {
|
2016-08-17 06:01:20 +08:00
|
|
|
value = '"' + value + '"';
|
2014-08-21 23:08:34 +08:00
|
|
|
}
|
2016-08-17 06:01:20 +08:00
|
|
|
return value;
|
|
|
|
}
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
buildSearchString() {
|
2019-07-05 03:19:24 +08:00
|
|
|
const result = [],
|
2020-08-07 00:24:46 +08:00
|
|
|
from_ = this.from().trim(),
|
|
|
|
to = this.to().trim(),
|
|
|
|
subject = this.subject().trim(),
|
|
|
|
text = this.text().trim(),
|
2016-08-17 06:01:20 +08:00
|
|
|
isPart = [],
|
|
|
|
hasPart = [];
|
|
|
|
|
2020-07-28 23:20:14 +08:00
|
|
|
if (from_) {
|
2016-08-17 06:01:20 +08:00
|
|
|
result.push('from:' + this.buildSearchStringValue(from_));
|
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2020-07-28 23:20:14 +08:00
|
|
|
if (to) {
|
2016-08-17 06:01:20 +08:00
|
|
|
result.push('to:' + this.buildSearchStringValue(to));
|
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2020-07-28 23:20:14 +08:00
|
|
|
if (subject) {
|
2016-08-17 06:01:20 +08:00
|
|
|
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()) {
|
2016-08-17 06:01:20 +08:00
|
|
|
hasPart.push('attachment');
|
2014-08-21 23:08:34 +08:00
|
|
|
}
|
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (this.unseen()) {
|
2016-08-17 06:01:20 +08:00
|
|
|
isPart.push('unseen');
|
|
|
|
}
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (this.starred()) {
|
2016-08-17 06:01:20 +08:00
|
|
|
isPart.push('flagged');
|
|
|
|
}
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2020-07-28 18:35:41 +08:00
|
|
|
if (hasPart.length) {
|
2016-08-17 06:01:20 +08:00
|
|
|
result.push('has:' + hasPart.join(','));
|
|
|
|
}
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2020-07-28 18:35:41 +08:00
|
|
|
if (isPart.length) {
|
2016-08-17 06:01:20 +08:00
|
|
|
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()) {
|
2020-08-07 20:44:33 +08:00
|
|
|
let d = new Date();
|
2020-07-31 22:45:43 +08:00
|
|
|
d.setDate(d.getDate() - this.selectedDateValue());
|
2020-08-07 20:44:33 +08:00
|
|
|
result.push('date:' + d.format('Y.m.d') + '/');
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2020-07-28 23:20:14 +08:00
|
|
|
if (text) {
|
2016-08-17 06:01:20 +08:00
|
|
|
result.push('text:' + this.buildSearchStringValue(text));
|
|
|
|
}
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2020-08-07 00:24:46 +08:00
|
|
|
return result.join(' ').trim();
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
clearPopup() {
|
|
|
|
this.from('');
|
|
|
|
this.to('');
|
|
|
|
this.subject('');
|
|
|
|
this.text('');
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.selectedDateValue(-1);
|
|
|
|
this.hasAttachment(false);
|
|
|
|
this.starred(false);
|
|
|
|
this.unseen(false);
|
2014-04-26 05:50:17 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.fromFocus(true);
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
onShow(search) {
|
|
|
|
this.clearPopup();
|
|
|
|
this.parseSearchStringValue(search);
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
onShowWithDelay() {
|
|
|
|
this.fromFocus(true);
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
export { AdvancedSearchPopupView, AdvancedSearchPopupView as default };
|