diff options
author | Raimund Schlüßler <raimund.schluessler@mailbox.org> | 2019-01-24 23:01:02 +0300 |
---|---|---|
committer | Raimund Schlüßler <raimund.schluessler@mailbox.org> | 2019-01-25 22:38:28 +0300 |
commit | 0aed977e3f481e699153954399c334ab206851b2 (patch) | |
tree | 2e25fb941464f1e0e23e9db76d3291c023573478 /src/models | |
parent | 3523269c1b26397ae99bf314066750776899e348 (diff) |
Implement searching in tasks
Diffstat (limited to 'src/models')
-rw-r--r-- | src/models/task.js | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/models/task.js b/src/models/task.js index 2a85b89d..48ace40f 100644 --- a/src/models/task.js +++ b/src/models/task.js @@ -95,6 +95,9 @@ export default class Task { this._categories = categories ? categories.getValues() : [] this._modified = this.vtodo.getFirstPropertyValue('last-modified') this._created = this.vtodo.getFirstPropertyValue('created') + + this._searchQuery = '' + this._matchesSearchQuery = true } /** @@ -442,4 +445,45 @@ export default class Task { this._created = this.vtodo.getFirstPropertyValue('created') } + /** + * Checks if the task matches the search query + * + * @param {String} searchQuery The search string + * @returns {Boolean} If the task matches + */ + matches(searchQuery) { + // If the search query maches the previous search, we don't have to search again. + if (this._searchQuery === searchQuery) { + return this._matchesSearchQuery + } + // We cache the current search query for faster future comparison. + this._searchQuery = searchQuery + // If the search query is empty, the task matches by default. + if (!searchQuery) { + this._matchesSearchQuery = true + return this._matchesSearchQuery + } + // We search in these task properties + var keys = ['summary', 'note', 'categories'] + // Make search case-insensitive. + searchQuery = searchQuery.toLowerCase() + for (const key of keys) { + // For the categories search the array + if (key === 'categories') { + for (const category of this[key]) { + if (category.toLowerCase().indexOf(searchQuery) > -1) { + this._matchesSearchQuery = true + return this._matchesSearchQuery + } + } + } else { + if (this[key].toLowerCase().indexOf(searchQuery) > -1) { + this._matchesSearchQuery = true + return this._matchesSearchQuery + } + } + } + this._matchesSearchQuery = false + return this._matchesSearchQuery + } } |