2018-04-01 09:10:12 -04:00
|
|
|
import * as Dates from "../services/dates";
|
2017-04-09 15:59:57 -04:00
|
|
|
|
|
|
|
let data = {
|
2017-04-14 13:47:33 -04:00
|
|
|
terms: '',
|
|
|
|
termString : '',
|
|
|
|
search: {
|
2017-04-09 15:59:57 -04:00
|
|
|
type: {
|
|
|
|
page: true,
|
|
|
|
chapter: true,
|
2018-09-23 07:34:30 -04:00
|
|
|
book: true,
|
|
|
|
bookshelf: true,
|
2017-04-14 13:47:33 -04:00
|
|
|
},
|
|
|
|
exactTerms: [],
|
|
|
|
tagTerms: [],
|
|
|
|
option: {},
|
2017-04-15 10:04:30 -04:00
|
|
|
dates: {
|
|
|
|
updated_after: false,
|
|
|
|
updated_before: false,
|
|
|
|
created_after: false,
|
|
|
|
created_before: false,
|
|
|
|
}
|
2017-04-09 15:59:57 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let computed = {
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
let methods = {
|
|
|
|
|
|
|
|
appendTerm(term) {
|
2017-04-14 13:47:33 -04:00
|
|
|
this.termString += ' ' + term;
|
|
|
|
this.termString = this.termString.replace(/\s{2,}/g, ' ');
|
|
|
|
this.termString = this.termString.replace(/^\s+/, '');
|
|
|
|
this.termString = this.termString.replace(/\s+$/, '');
|
|
|
|
},
|
|
|
|
|
|
|
|
exactParse(searchString) {
|
|
|
|
this.search.exactTerms = [];
|
|
|
|
let exactFilter = /"(.+?)"/g;
|
|
|
|
let matches;
|
|
|
|
while ((matches = exactFilter.exec(searchString)) !== null) {
|
|
|
|
this.search.exactTerms.push(matches[1]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
exactChange() {
|
|
|
|
let exactFilter = /"(.+?)"/g;
|
|
|
|
this.termString = this.termString.replace(exactFilter, '');
|
2018-09-23 07:34:30 -04:00
|
|
|
let matchesTerm = this.search.exactTerms.filter(term => term.trim() !== '').map(term => `"${term}"`).join(' ');
|
2017-04-14 13:47:33 -04:00
|
|
|
this.appendTerm(matchesTerm);
|
|
|
|
},
|
|
|
|
|
|
|
|
addExact() {
|
|
|
|
this.search.exactTerms.push('');
|
|
|
|
setTimeout(() => {
|
|
|
|
let exactInputs = document.querySelectorAll('.exact-input');
|
|
|
|
exactInputs[exactInputs.length - 1].focus();
|
|
|
|
}, 100);
|
|
|
|
},
|
|
|
|
|
|
|
|
removeExact(index) {
|
|
|
|
this.search.exactTerms.splice(index, 1);
|
|
|
|
this.exactChange();
|
|
|
|
},
|
|
|
|
|
|
|
|
tagParse(searchString) {
|
|
|
|
this.search.tagTerms = [];
|
|
|
|
let tagFilter = /\[(.+?)\]/g;
|
|
|
|
let matches;
|
|
|
|
while ((matches = tagFilter.exec(searchString)) !== null) {
|
|
|
|
this.search.tagTerms.push(matches[1]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
tagChange() {
|
|
|
|
let tagFilter = /\[(.+?)\]/g;
|
|
|
|
this.termString = this.termString.replace(tagFilter, '');
|
|
|
|
let matchesTerm = this.search.tagTerms.filter(term => {
|
|
|
|
return term.trim() !== '';
|
|
|
|
}).map(term => {
|
|
|
|
return `[${term}]`
|
|
|
|
}).join(' ');
|
|
|
|
this.appendTerm(matchesTerm);
|
|
|
|
},
|
|
|
|
|
|
|
|
addTag() {
|
|
|
|
this.search.tagTerms.push('');
|
|
|
|
setTimeout(() => {
|
|
|
|
let tagInputs = document.querySelectorAll('.tag-input');
|
|
|
|
tagInputs[tagInputs.length - 1].focus();
|
|
|
|
}, 100);
|
|
|
|
},
|
|
|
|
|
|
|
|
removeTag(index) {
|
|
|
|
this.search.tagTerms.splice(index, 1);
|
|
|
|
this.tagChange();
|
2017-04-09 15:59:57 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
typeParse(searchString) {
|
|
|
|
let typeFilter = /{\s?type:\s?(.*?)\s?}/;
|
|
|
|
let match = searchString.match(typeFilter);
|
|
|
|
let type = this.search.type;
|
|
|
|
if (!match) {
|
2018-09-23 07:34:30 -04:00
|
|
|
type.page = type.book = type.chapter = type.bookshelf = true;
|
2017-04-09 15:59:57 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
let splitTypes = match[1].replace(/ /g, '').split('|');
|
|
|
|
type.page = (splitTypes.indexOf('page') !== -1);
|
|
|
|
type.chapter = (splitTypes.indexOf('chapter') !== -1);
|
|
|
|
type.book = (splitTypes.indexOf('book') !== -1);
|
2018-09-23 07:34:30 -04:00
|
|
|
type.bookshelf = (splitTypes.indexOf('bookshelf') !== -1);
|
2017-04-09 15:59:57 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
typeChange() {
|
|
|
|
let typeFilter = /{\s?type:\s?(.*?)\s?}/;
|
|
|
|
let type = this.search.type;
|
2018-09-23 07:34:30 -04:00
|
|
|
if (type.page === type.chapter === type.book === type.bookshelf) {
|
2017-04-09 15:59:57 -04:00
|
|
|
this.termString = this.termString.replace(typeFilter, '');
|
|
|
|
return;
|
|
|
|
}
|
2018-09-23 07:34:30 -04:00
|
|
|
let selectedTypes = Object.keys(type).filter(type => this.search.type[type]).join('|');
|
2017-04-09 15:59:57 -04:00
|
|
|
let typeTerm = '{type:'+selectedTypes+'}';
|
|
|
|
if (this.termString.match(typeFilter)) {
|
|
|
|
this.termString = this.termString.replace(typeFilter, typeTerm);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.appendTerm(typeTerm);
|
2017-04-09 16:12:13 -04:00
|
|
|
},
|
|
|
|
|
2017-04-14 13:47:33 -04:00
|
|
|
optionParse(searchString) {
|
2017-04-15 10:04:30 -04:00
|
|
|
let optionFilter = /{([a-z_\-:]+?)}/gi;
|
2017-04-14 13:47:33 -04:00
|
|
|
let matches;
|
|
|
|
while ((matches = optionFilter.exec(searchString)) !== null) {
|
|
|
|
this.search.option[matches[1].toLowerCase()] = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
optionChange(optionName) {
|
|
|
|
let isChecked = this.search.option[optionName];
|
|
|
|
if (isChecked) {
|
|
|
|
this.appendTerm(`{${optionName}}`);
|
|
|
|
} else {
|
|
|
|
this.termString = this.termString.replace(`{${optionName}}`, '');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
updateSearch(e) {
|
|
|
|
e.preventDefault();
|
2017-07-24 20:06:15 -04:00
|
|
|
window.location = window.baseUrl('/search?term=' + encodeURIComponent(this.termString));
|
2017-04-14 13:47:33 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
enableDate(optionName) {
|
2018-04-01 09:10:12 -04:00
|
|
|
this.search.dates[optionName.toLowerCase()] = Dates.getCurrentDay();
|
2017-04-15 10:04:30 -04:00
|
|
|
this.dateChange(optionName);
|
|
|
|
},
|
|
|
|
|
|
|
|
dateParse(searchString) {
|
|
|
|
let dateFilter = /{([a-z_\-]+?):([a-z_\-0-9]+?)}/gi;
|
|
|
|
let dateTags = Object.keys(this.search.dates);
|
|
|
|
let matches;
|
|
|
|
while ((matches = dateFilter.exec(searchString)) !== null) {
|
|
|
|
if (dateTags.indexOf(matches[1]) === -1) continue;
|
|
|
|
this.search.dates[matches[1].toLowerCase()] = matches[2];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
dateChange(optionName) {
|
|
|
|
let dateFilter = new RegExp('{\\s?'+optionName+'\\s?:([a-z_\\-0-9]+?)}', 'gi');
|
|
|
|
this.termString = this.termString.replace(dateFilter, '');
|
|
|
|
if (!this.search.dates[optionName]) return;
|
|
|
|
this.appendTerm(`{${optionName}:${this.search.dates[optionName]}}`);
|
|
|
|
},
|
|
|
|
|
|
|
|
dateRemove(optionName) {
|
|
|
|
this.search.dates[optionName] = false;
|
|
|
|
this.dateChange(optionName);
|
2017-04-09 15:59:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
function created() {
|
2017-04-14 13:47:33 -04:00
|
|
|
this.termString = document.querySelector('[name=searchTerm]').value;
|
2017-04-09 15:59:57 -04:00
|
|
|
this.typeParse(this.termString);
|
2017-04-14 13:47:33 -04:00
|
|
|
this.exactParse(this.termString);
|
|
|
|
this.tagParse(this.termString);
|
|
|
|
this.optionParse(this.termString);
|
2017-04-15 10:04:30 -04:00
|
|
|
this.dateParse(this.termString);
|
2017-04-09 15:59:57 -04:00
|
|
|
}
|
|
|
|
|
2018-11-09 16:17:35 -05:00
|
|
|
export default {
|
2017-04-09 15:59:57 -04:00
|
|
|
data, computed, methods, created
|
2017-07-24 20:06:15 -04:00
|
|
|
};
|