@@ -21,7 +21,7 @@ templateFile, commandTemplateFile) {
2121 }
2222 } ) ;
2323
24- // View for one command result
24+ // View for a command in teh command palette
2525 var CommandItem = hr . List . Item . extend ( {
2626 className : "command" ,
2727 template : commandTemplateFile ,
@@ -46,36 +46,44 @@ templateFile, commandTemplateFile) {
4646 }
4747 } ) ;
4848
49- // Commands list
49+ // View for the list of comamdns in the command palette
5050 var CommandsView = hr . List . extend ( {
5151 Item : CommandItem ,
5252 Collection : CommandsWithQuery ,
5353 className : "palette-commands"
5454 } ) ;
5555
56+ /**
57+ * Command palette view
58+ */
5659 var PaletteView = hr . View . extend ( {
5760 className : "cb-commands-palette close" ,
5861 template : templateFile ,
5962 defaults : { } ,
6063 events : {
61- "keydown input" : "keydown " ,
62- "keyup input" : "keyup " ,
63- "mousedown" : "mousedown "
64+ "keydown input" : "onKeydown " ,
65+ "keyup input" : "onKeyup " ,
66+ "mousedown" : "onMousedown "
6467 } ,
6568
6669 initialize : function ( options ) {
6770 PaletteView . __super__ . initialize . apply ( this , arguments ) ;
6871
72+ /* Collection of commands currently in the command palette */
6973 this . commands = new CommandsView ( { } , this ) ;
70- this . selected = 0 ;
7174
75+ /* Key navigation interval */
76+ this . keydownInterval = null ;
77+
78+ /* Document keydown binding for ESC */
7279 this . _keydown = _ . bind ( function ( e ) {
7380 var key = e . which || e . keyCode ;
7481 if ( key == 27 ) {
7582 this . close ( ) ;
7683 }
7784 } , this ) ;
7885
86+ /* Document mousedown binding */
7987 this . _mousedown = _ . bind ( function ( e ) {
8088 this . close ( ) ;
8189 } , this ) ;
@@ -93,7 +101,9 @@ templateFile, commandTemplateFile) {
93101 return ! this . $el . hasClass ( "close" ) ;
94102 } ,
95103
96- // Open the command palette
104+ /**
105+ * Open the command palette
106+ */
97107 open : function ( ) {
98108 if ( this . isOpen ( ) ) return ;
99109
@@ -106,7 +116,9 @@ templateFile, commandTemplateFile) {
106116 $ ( document ) . bind ( "mousedown" , this . _mousedown ) ;
107117 } ,
108118
109- // Close the command palette
119+ /**
120+ * Close the command palette
121+ */
110122 close : function ( ) {
111123 if ( ! this . isOpen ( ) ) return ;
112124
@@ -117,7 +129,9 @@ templateFile, commandTemplateFile) {
117129 $ ( document ) . unbind ( "mousedown" , this . _mousedown ) ;
118130 } ,
119131
120- // Toggle the command palette
132+ /**
133+ * Toggle the command palette
134+ */
121135 toggle : function ( ) {
122136 if ( this . isOpen ( ) ) {
123137 this . close ( ) ;
@@ -126,13 +140,19 @@ templateFile, commandTemplateFile) {
126140 }
127141 } ,
128142
129- // Clear results
143+ /**
144+ * Clear results
145+ */
130146 clearResults : function ( ) {
131147 this . commands . collection . clear ( ) ;
132148 return this ;
133149 } ,
134150
135- // Do search
151+ /**
152+ * Do search
153+ *
154+ * @param {string } query
155+ */
136156 doSearch : function ( query ) {
137157 var that = this , toRemove = [ ] ;
138158 if ( this . commands . collection . query == query ) return ;
@@ -165,38 +185,11 @@ templateFile, commandTemplateFile) {
165185 }
166186 } ,
167187
168- // (event) Key input in search
169- keydown : function ( e ) {
170- var key = e . which || e . keyCode ;
171- if ( key == 38 || key == 40 || key == 13 ) {
172- e . preventDefault ( ) ;
173- }
174- } ,
175- keyup : function ( e ) {
176- var key = e . which || e . keyCode ;
177- var q = $ ( e . currentTarget ) . val ( ) ;
178- var selected = this . getSelectedItem ( ) ;
179- var pSelected = selected ;
180-
181- if ( key == 27 ) {
182- /* ESC */
183- e . preventDefault ( ) ;
184- return ;
185- } else if ( key == 38 ) {
186- /* UP */
187- selected = selected - 1 ;
188- } else if ( key == 40 ) {
189- /* DOWN */
190- selected = selected + 1 ;
191- } else if ( key == 13 ) {
192- /* ENTER */
193- e . preventDefault ( ) ;
194- this . openItem ( selected ) ;
195- }
196- this . doSearch ( q ) ;
197- if ( selected != pSelected ) this . selectItem ( selected ) ;
198- } ,
199-
188+ /**
189+ * Select a specific item
190+ *
191+ * @param {number } i
192+ */
200193 selectItem : function ( i ) {
201194 var i , boxH = this . $ ( ".results" ) . height ( ) ;
202195
@@ -225,6 +218,11 @@ templateFile, commandTemplateFile) {
225218 } , this ) ;
226219 } ,
227220
221+ /**
222+ * Return index of the current selected item
223+ *
224+ * @return {number }
225+ */
228226 getSelectedItem : function ( ) {
229227 var _ret = 0 ;
230228 this . commands . collection . each ( function ( model , i ) {
@@ -237,15 +235,71 @@ templateFile, commandTemplateFile) {
237235 return _ret ;
238236 } ,
239237
238+ /**
239+ * Open current selected item or a specific one
240+ *
241+ * @param {number } i
242+ */
240243 openItem : function ( i ) {
244+ if ( i == null ) i = this . getSelectedItem ( ) ;
245+
241246 var item , model = this . commands . collection . at ( i ) ;
242247 if ( ! model ) return false ;
248+
243249 item = this . commands . items [ model . id ] ;
244250 return item . run ( ) ;
245251 } ,
246252
247- // (event) Mouse down
248- mousedown : function ( e ) {
253+ onKeydown : function ( e ) {
254+ var key = e . which || e . keyCode ;
255+
256+ if ( key == 38 || key == 40 || key == 13 ) {
257+ e . preventDefault ( ) ;
258+ }
259+
260+ var interval = function ( ) {
261+ var selected = this . getSelectedItem ( ) ;
262+ var pSelected = selected ;
263+
264+ if ( key == 38 ) {
265+ /* UP */
266+ selected = selected - 1 ;
267+ } else if ( key == 40 ) {
268+ /* DOWN */
269+ selected = selected + 1 ;
270+ }
271+ if ( selected != pSelected ) this . selectItem ( selected ) ;
272+ } . bind ( this ) ;
273+
274+ if ( this . keydownInterval ) {
275+ clearInterval ( this . keydownInterval ) ;
276+ this . keydownInterval = null ;
277+ }
278+ interval ( ) ;
279+ this . keydownInterval = setInterval ( interval , 600 ) ;
280+ } ,
281+ onKeyup : function ( e ) {
282+ var key = e . which || e . keyCode ;
283+ var q = $ ( e . currentTarget ) . val ( ) ;
284+
285+ if ( key == 27 ) {
286+ /* ESC */
287+ e . preventDefault ( ) ;
288+ return ;
289+ } else if ( key == 13 ) {
290+ /* ENTER */
291+ e . preventDefault ( ) ;
292+ this . openItem ( ) ;
293+ }
294+
295+ if ( this . keydownInterval ) {
296+ clearInterval ( this . keydownInterval ) ;
297+ this . keydownInterval = null ;
298+ }
299+
300+ this . doSearch ( q ) ;
301+ } ,
302+ onMousedown : function ( e ) {
249303 e . stopPropagation ( ) ;
250304 }
251305 } ) ;
0 commit comments