@@ -62,29 +62,31 @@ docsApp.controller.DocsVersionsCtrl = ['$scope', '$window', 'NG_VERSIONS', funct
62
62
} ;
63
63
} ] ;
64
64
65
- docsApp . controller . DocsNavigationCtrl = [ '$scope' , 'fullTextSearch' , '$location' , function ( $scope , fullTextSearch , $location ) {
66
- fullTextSearch . init ( ) ;
65
+ docsApp . controller . DocsNavigationCtrl = [ '$scope' , '$location' , 'docsSearch' , function ( $scope , $location , docsSearch ) {
66
+ function clearResults ( ) {
67
+ $scope . results = [ ] ;
68
+ $scope . colClassName = null ;
69
+ $scope . hasResults = false ;
70
+ }
71
+
67
72
$scope . search = function ( q ) {
68
- fullTextSearch . search ( q , function ( results ) {
69
- if ( q && q . length >= 4 ) {
70
- $scope . results = results ;
71
- var totalSections = 0 ;
72
- for ( var i in results ) {
73
- ++ totalSections ;
74
- }
75
- if ( totalSections > 0 ) {
76
- $scope . colClassName = 'cols-' + totalSections ;
77
- $scope . hasResults = true ;
78
- }
79
- else {
80
- $scope . hasResults = false ;
81
- }
73
+ var MIN_SEARCH_LENGTH = 4 ;
74
+ if ( q . length >= MIN_SEARCH_LENGTH ) {
75
+ var results = docsSearch ( q ) ;
76
+ var totalSections = 0 ;
77
+ for ( var i in results ) {
78
+ ++ totalSections ;
82
79
}
83
- else {
84
- $scope . hasResults = false ;
80
+ if ( totalSections > 0 ) {
81
+ $scope . colClassName = 'cols-' + totalSections ;
82
+ $scope . hasResults = true ;
85
83
}
86
- if ( ! $scope . $$phase ) $scope . $apply ( ) ;
87
- } ) ;
84
+ $scope . results = results ;
85
+ }
86
+ else {
87
+ clearResults ( ) ;
88
+ }
89
+ if ( ! $scope . $$phase ) $scope . $apply ( ) ;
88
90
} ;
89
91
$scope . submit = function ( ) {
90
92
var result ;
@@ -100,82 +102,60 @@ docsApp.controller.DocsNavigationCtrl = ['$scope', 'fullTextSearch', '$location'
100
102
}
101
103
} ;
102
104
$scope . hideResults = function ( ) {
103
- $scope . hasResults = false ;
105
+ clearResults ( ) ;
104
106
$scope . q = '' ;
105
107
} ;
106
108
} ] ;
107
109
108
- docsApp . serviceFactory . fullTextSearch = [ '$q' , '$rootScope' , 'NG_PAGES' , function ( $q , $rootScope , NG_PAGES ) {
109
- return {
110
- dbName : 'docs' ,
111
- indexName : 'docsindex' ,
110
+ docsApp . serviceFactory . lunrSearch = function ( ) {
111
+ return function ( properties ) {
112
+ var engine = lunr ( properties ) ;
113
+ return {
114
+ store : function ( values ) {
115
+ engine . add ( values ) ;
116
+ } ,
117
+ search : function ( q ) {
118
+ return engine . search ( q ) ;
119
+ }
120
+ } ;
121
+ } ;
122
+ } ;
112
123
113
- init : function ( onReady ) {
114
- this . init = function ( ) { } ;
124
+ docsApp . serviceFactory . docsSearch = [ '$rootScope' , 'lunrSearch' , 'NG_PAGES' ,
125
+ function ( $rootScope , lunrSearch , NG_PAGES ) {
115
126
116
- var self = this ;
117
- this . deferReady = $q . defer ( ) ;
118
- this . readyPromise = this . deferReady . promise ;
127
+ var index = lunrSearch ( function ( ) {
128
+ this . ref ( 'id' ) ;
129
+ this . field ( 'title' , { boost : 50 } ) ;
130
+ this . field ( 'description' , { boost : 20 } ) ;
131
+ } ) ;
119
132
120
- this . engine = lunr ( function ( ) {
121
- this . ref ( 'id' ) ;
122
- this . field ( 'title' , { boost : 50 } ) ;
123
- this . field ( 'description' , { boost : 20 } ) ;
124
- } ) ;
125
- this . prepare ( ) ;
126
- this . onReady ( ) ;
127
- } ,
128
- onReady : function ( ) {
129
- this . ready = true ;
130
- var self = this ;
131
- self . deferReady . resolve ( ) ;
132
- if ( ! $rootScope . $$phase ) {
133
- $rootScope . $apply ( ) ;
134
- }
135
- } ,
136
- whenReady : function ( fn ) {
137
- if ( this . ready ) {
138
- fn ( ) ;
139
- }
140
- else {
141
- this . init ( ) ;
142
- this . readyPromise . then ( fn ) ;
133
+ angular . forEach ( NG_PAGES , function ( page , i ) {
134
+ var title = page . shortName ;
135
+ if ( title . charAt ( 0 ) == 'n' && title . charAt ( 1 ) == 'g' ) {
136
+ title = title + ' ' + title . charAt ( 2 ) . toLowerCase ( ) + title . substr ( 3 ) ;
137
+ }
138
+ index . store ( {
139
+ id : i ,
140
+ title : title ,
141
+ description : page . keywords
142
+ } ) ;
143
+ } ) ;
144
+
145
+ return function ( q ) {
146
+ var results = { } ;
147
+ angular . forEach ( index . search ( q ) , function ( result ) {
148
+ var item = NG_PAGES [ result . ref ] ;
149
+ var section = item . section ;
150
+ if ( section == 'cookbook' ) {
151
+ section = 'tutorial' ;
143
152
}
144
- } ,
145
- prepare : function ( injector , callback ) {
146
- for ( var i = 0 ; i < NG_PAGES . length ; i ++ ) {
147
- var page = NG_PAGES [ i ] ;
148
- var title = page . shortName ;
149
- if ( title . charAt ( 0 ) == 'n' && title . charAt ( 1 ) == 'g' ) {
150
- title = title + ' ' + title . substr ( 2 ) ;
151
- }
152
- this . engine . add ( {
153
- id : i ,
154
- title : title ,
155
- description : page . keywords
156
- } ) ;
153
+ results [ section ] = results [ section ] || [ ] ;
154
+ if ( results [ section ] . length < 15 ) {
155
+ results [ section ] . push ( item ) ;
157
156
}
158
- } ,
159
- search : function ( q , onReady ) {
160
- var self = this ;
161
- this . whenReady ( function ( ) {
162
- var data = [ ] ;
163
- var results = self . engine . search ( q ) ;
164
- var groups = { } ;
165
- angular . forEach ( results , function ( result ) {
166
- var item = NG_PAGES [ result . ref ] ;
167
- var section = item . section ;
168
- if ( section == 'cookbook' ) {
169
- section = 'tutorial' ;
170
- }
171
- groups [ section ] = groups [ section ] || [ ] ;
172
- if ( groups [ section ] . length < 15 ) {
173
- groups [ section ] . push ( item ) ;
174
- }
175
- } ) ;
176
- onReady ( groups ) ;
177
- } ) ;
178
- }
157
+ } ) ;
158
+ return results ;
179
159
} ;
180
160
} ] ;
181
161
0 commit comments