]> BookStack Code Mirror - bookstack/blobdiff - resources/assets/js/vues/search.js
Update maintenance.php
[bookstack] / resources / assets / js / vues / search.js
index 710f4b214730b0732a836955669d640f23faa9e1..c0b828b96a60297fca292f0adcda866c256fbea0 100644 (file)
@@ -1,4 +1,4 @@
-const moment = require('moment');
+import * as Dates from "../services/dates";
 
 let data = {
     terms: '',
@@ -7,12 +7,18 @@ let data = {
         type: {
             page: true,
             chapter: true,
-            book: true
+            book: true,
+            bookshelf: true,
         },
         exactTerms: [],
         tagTerms: [],
         option: {},
-        dates: {}
+        dates: {
+            updated_after: false,
+            updated_before: false,
+            created_after: false,
+            created_before: false,
+        }
     }
 };
 
@@ -41,11 +47,7 @@ let methods = {
     exactChange() {
         let exactFilter = /"(.+?)"/g;
         this.termString = this.termString.replace(exactFilter, '');
-        let matchesTerm = this.search.exactTerms.filter(term => {
-            return term.trim() !== '';
-        }).map(term => {
-            return `"${term}"`
-        }).join(' ');
+        let matchesTerm = this.search.exactTerms.filter(term =>  term.trim() !== '').map(term => `"${term}"`).join(' ');
         this.appendTerm(matchesTerm);
     },
 
@@ -100,23 +102,24 @@ let methods = {
         let match = searchString.match(typeFilter);
         let type = this.search.type;
         if (!match) {
-            type.page = type.book = type.chapter = true;
+            type.page = type.book = type.chapter = type.bookshelf = true;
             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);
+        type.bookshelf = (splitTypes.indexOf('bookshelf') !== -1);
     },
 
     typeChange() {
         let typeFilter = /{\s?type:\s?(.*?)\s?}/;
         let type = this.search.type;
-        if (type.page === type.chapter && type.page === type.book) {
+        if (type.page === type.chapter === type.book === type.bookshelf) {
             this.termString = this.termString.replace(typeFilter, '');
             return;
         }
-        let selectedTypes = Object.keys(type).filter(type => {return this.search.type[type];}).join('|');
+        let selectedTypes = Object.keys(type).filter(type => this.search.type[type]).join('|');
         let typeTerm = '{type:'+selectedTypes+'}';
         if (this.termString.match(typeFilter)) {
             this.termString = this.termString.replace(typeFilter, typeTerm);
@@ -126,7 +129,7 @@ let methods = {
     },
 
     optionParse(searchString) {
-        let optionFilter = /{([a-z_-]+?)}/gi;
+        let optionFilter = /{([a-z_\-:]+?)}/gi;
         let matches;
         while ((matches = optionFilter.exec(searchString)) !== null) {
             this.search.option[matches[1].toLowerCase()] = true;
@@ -144,11 +147,34 @@ let methods = {
 
     updateSearch(e) {
         e.preventDefault();
-        window.location = '/search?term=' + encodeURIComponent(this.termString);
+        window.location = window.baseUrl('/search?term=' + encodeURIComponent(this.termString));
     },
 
     enableDate(optionName) {
-        this.search.dates[optionName] = moment().format('YYYY-MM-DD');
+        this.search.dates[optionName.toLowerCase()] = Dates.getCurrentDay();
+        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);
     }
 
 };
@@ -159,8 +185,9 @@ function created() {
     this.exactParse(this.termString);
     this.tagParse(this.termString);
     this.optionParse(this.termString);
+    this.dateParse(this.termString);
 }
 
-module.exports = {
+export default {
     data, computed, methods, created
-};
\ No newline at end of file
+};