@@ -7,99 +7,46 @@ openNote.controller("searchController", ["$scope",
77 "config" ,
88 "storageService" ,
99 "$location" ,
10+ "$routeParams" ,
11+ "$timeout" ,
1012 function ( $scope ,
1113 $rootScope ,
1214 config ,
1315 storageService ,
14- $location ) {
15- /**
16- * Default valie
17- */
18- $scope . searchRequest = {
19- type : "Both" ,
20- field : "Both" ,
21- search : ""
22- } ;
16+ $location ,
17+ $routeParams ,
18+ $timeout ) {
2319
24- $scope . notes = null ;
25- $scope . folders = null ;
20+ $scope . searchString = $routeParams . id ; //Default
2621
27- /**
28- * Search the database
29- */
3022 $scope . search = function ( ) {
23+ $location . url ( "/search/" + $scope . searchString ) ;
24+ } ;
25+
26+ $scope . loadResults = function ( ) {
27+ if ( ! $routeParams . id )
28+ return ;
29+
3130 alertify . log ( "Search started" ) ;
32- $scope . notes = [ ] ;
33- $scope . folders = [ ] ;
31+ $scope . results = [ ] ;
3432
35- var removeDuplicates = function ( array ) {
36- var listOfIDs = [ ] ;
37- array . forEach ( function ( element ) { //for each is synchronous
38- var index = listOfIDs . indexOf ( element . id ) ;
39- if ( index == - 1 ) {
40- listOfIDs . push ( element . id ) ;
41- } else
42- array . splice ( index , 1 ) ;
33+ storageService . allDocs ( ) . then ( function ( result ) {
34+ result . rows . filter ( storageService . folderFilter ) . forEach ( function ( folder ) { // search folders
35+ if ( folder . doc . name . match ( $routeParams . id ) ) //search folder name
36+ return $scope . results . push ( folder ) ;
4337 } ) ;
44- return array ;
45- } ;
4638
47- var appendNotes = function ( notes ) {
48- $scope . notes = removeDuplicates ( $scope . notes . concat ( notes ) ) ;
49- $scope . $apply ( ) ;
50- } ;
51-
52- var appendFolders = function ( folders ) {
53- $scope . folders = $scope . folders . concat ( folders ) ;
54- alertify . success ( folders . length + " objects found" ) ;
39+ result . rows . filter ( storageService . noteFilter ) . forEach ( function ( note ) { //Search notes
40+ if ( note . doc . title . match ( $routeParams . id ) || note . doc . note . match ( $routeParams . id ) ) //search note name and title
41+ return $scope . results . push ( note ) ;
42+ } ) ;
5543 $scope . $apply ( ) ;
56- } ;
57-
58- var type = $scope . searchRequest . type ;
59- var search = $scope . searchRequest . search ;
60- var field = $scope . searchRequest . field ;
61-
62- if ( type == "Both" || type == "Folders" )
63- storageService . searchFolderNames ( search , appendFolders ) ;
64-
65- if ( type == "Both" || type == "Notes" ) {
66- if ( field == "Both" || type == "Title" )
67- storageService . searchNoteTitles ( search , appendNotes ) ;
68-
69- if ( field == "Both" || type == "Body" )
70- storageService . searchNoteBody ( search , appendNotes ) ;
71- }
72- } ;
73-
74- /**
75- * Load a folder
76- * @param folder- the folder to load
77- */
78- $scope . loadFolder = function ( folder ) {
79- $scope . fadeOutBoxes ( function ( ) {
80- $location . url ( "/folder/" + folder . doc . _id ) ;
44+ alertify . success ( $scope . results . length + " objects found" ) ;
8145 } ) ;
82- } ;
8346
84- /**
85- * Load a note
86- * @param note - load a note
87- */
88- $scope . loadNote = function ( note ) {
89- $scope . fadeOutBoxes ( function ( ) {
90- $location . url ( "/note/" + note . doc . _id ) ;
91- } ) ;
9247 } ;
9348
94- /**
95- * fade out all boxes
96- */
97- $scope . fadeOutBoxes = function ( callback ) {
98- $ ( ".box" ) . fadeOut ( config . fadeSpeedShort ( ) , function ( ) {
99- $scope . $apply ( function ( ) {
100- callback ( ) ;
101- } ) ;
102- } ) ;
103- } ;
49+ //Load results
50+ $timeout ( $scope . loadResults ) ;
10451 }
10552] ) ;
0 commit comments