diff --git a/.gitignore b/.gitignore index c5a0fb1d..c1a2a1fd 100644 --- a/.gitignore +++ b/.gitignore @@ -67,3 +67,6 @@ nbproject # Generated from .drone.star .drone.yml +js/package-lock.json +js/public/app.min.js +js/public/app.min.js.map diff --git a/js/app/controllers/notescontroller.js b/js/app/controllers/notescontroller.js index d180e04b..25817d4d 100644 --- a/js/app/controllers/notescontroller.js +++ b/js/app/controllers/notescontroller.js @@ -43,4 +43,45 @@ app.controller('NotesController', function($routeParams, $scope, $location, }); }; -}); + var searchform = $('.searchbox'); + var searchbox = $('#searchbox'); + + initSearch(); + + function initSearch() { + $scope.queryString = searchbox.val().trim(); + + /** Conduct the search when there is a pause in typing in text */ + var checkQueryChange = _.debounce(function() { + if ($scope.queryString != searchbox.val().trim()) { + onEnterSearchString(); + } + }, 250); + searchbox.bind('propertychange change keyup input paste', checkQueryChange); + + /** Handle clearing the searchbox. This has to be registered to the parent form + * of the #searchbox element. + */ + searchform.on('reset', function() { + setQueryString(''); + }); + + /** Run search when enter pressed within the searchbox */ + searchbox.bind('keydown', function (event) { + if (event.which === 13) { + onEnterSearchString(); + } + }); + } + + function onEnterSearchString() { + setQueryString(searchbox.val().trim()); + } + + function setQueryString(query) { + $scope.$apply(() => { + $scope.queryString = query; + }); + } + +}); \ No newline at end of file diff --git a/js/app/filters/noteFilter.js b/js/app/filters/noteFilter.js new file mode 100644 index 00000000..54e5cd9f --- /dev/null +++ b/js/app/filters/noteFilter.js @@ -0,0 +1,10 @@ +app.filter('noteFilter', function() { + 'use strict'; + return function (items, searchString) { + if (!searchString || searchString.length == 0) + return items; + + var regex = new RegExp(searchString, 'i'); + return items.filter(x => x.title.match(regex) || x.content.match(regex)); + }; +}); \ No newline at end of file diff --git a/templates/main.php b/templates/main.php index 2baf6daf..d81f2e6c 100644 --- a/templates/main.php +++ b/templates/main.php @@ -41,7 +41,7 @@ + t('New note')); ?> -
  • {{ note.title | noteTitle }} @@ -67,4 +67,7 @@
    + + +