notes-filter.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536
  1. // This is a custom filter which will filter by the filter value and search within the result
  2. // of the category filter using the value from the search. If there is no category value, then
  3. // the search is applied to the entire pool of notes.
  4. // Usage:
  5. // ng-repeat='note in notes | notesFilter:searchTerm:categoryFilterTerm'
  6. //
  7. // The search term value should come first, followed by the category filter term
  8. //
  9. angular.module('NoteWrangler')
  10. .filter('notesFilter', function(){
  11. return function(notesInput, titleSearch, category) {
  12. var note, categoryMatches, titleMatches;
  13. var result = [];
  14. for(var i=0, l = notesInput.length; i < l; i++) {
  15. note = notesInput[i];
  16. // If the category doesn't exist we'll assume there is no category selected, and not filter by a category
  17. // if the category does exist, then check to see if the note has a category that matches the category given
  18. categoryMatches = !category || note.category.name === category;
  19. // If the titleSearch doesn't exist, we'll assume the search box is empty and not filter by title
  20. // If the titleSearch does exist, then we'll use a case insensitive(i) regular expression
  21. // to match the search value to the title.
  22. titleMatches = !titleSearch || note.header.match(new RegExp(titleSearch, 'i'));
  23. // If the category matches and title matches then save the note as a result
  24. if(categoryMatches && titleMatches) {
  25. result.push(note);
  26. }
  27. }
  28. return result;
  29. };
  30. });