]> BookStack Code Mirror - bookstack/blob - resources/assets/js/vues/search.js
708418271f3b9cdcceb45fbd8698cc798dfffe1d
[bookstack] / resources / assets / js / vues / search.js
1
2 let termString = document.querySelector('[name=searchTerm]').value;
3 let terms = termString.split(' ');
4
5 let data = {
6     terms: terms,
7         termString : termString,
8         search: {
9         type: {
10             page: true,
11             chapter: true,
12             book: true
13         }
14     }
15 };
16
17 let computed = {
18
19 };
20
21 let methods = {
22
23     appendTerm(term) {
24         if (this.termString.slice(-1) !== " ") this.termString += ' ';
25         this.termString += term;
26     },
27
28     typeParse(searchString) {
29         let typeFilter = /{\s?type:\s?(.*?)\s?}/;
30         let match = searchString.match(typeFilter);
31         let type = this.search.type;
32         if (!match) {
33             type.page = type.book = type.chapter = true;
34             return;
35         }
36         let splitTypes = match[1].replace(/ /g, '').split('|');
37         type.page = (splitTypes.indexOf('page') !== -1);
38         type.chapter = (splitTypes.indexOf('chapter') !== -1);
39         type.book = (splitTypes.indexOf('book') !== -1);
40     },
41
42     typeChange() {
43         let typeFilter = /{\s?type:\s?(.*?)\s?}/;
44         let type = this.search.type;
45         if (type.page === type.chapter && type.page === type.book) {
46             this.termString = this.termString.replace(typeFilter, '');
47             return;
48         }
49         let selectedTypes = Object.keys(type).filter(type => {return this.search.type[type];}).join('|');
50         let typeTerm = '{type:'+selectedTypes+'}';
51         if (this.termString.match(typeFilter)) {
52             this.termString = this.termString.replace(typeFilter, typeTerm);
53             return;
54         }
55         this.appendTerm(typeTerm);
56     },
57
58     updateSearch() {
59         window.location = '/search?term=' + encodeURIComponent(this.termString);
60     }
61
62 };
63
64 function created() {
65     this.typeParse(this.termString);
66 }
67
68 module.exports = {
69     data, computed, methods, created
70 };