33var remote = require ( 'remote' )
44var ipc = require ( 'ipc' )
55
6- var SEARCH_INPUT = 1
7- var RESULT_LIST = 2
8- var RESULT_CONTROL = 3
9- var RESULT_CONTENT = 4
10- // var btnClipboard = document.getElementById('btnClipboard')
11- // var btnEdit = document.getElementById('btnEdit')
12- // var btnShare = document.getElementById('btnShare')
13- var aceView = document . getElementById ( 'aceView' )
14-
156angular . module ( 'codexen.popup' , [
167 'ui.ace' ,
178 'satellizer' ,
@@ -27,214 +18,146 @@ angular.module('codexen.popup', [
2718 // Setup Events
2819 remote . getCurrentWindow ( ) . on ( 'focus' , function ( ) {
2920 $scope . $apply ( focusSearchInput )
21+ loadSnippets ( )
3022 } )
3123
3224 hotkeys . bindTo ( $scope )
3325 . add ( 'down' , function ( e ) {
34- switch ( $scope . isFocusing ) {
35- case RESULT_LIST :
36- selectNextItem ( )
37- break
38- case RESULT_CONTROL :
39- focusContent ( )
40- break
41- }
26+ nextSnippet ( )
4227 e . preventDefault ( )
4328 } )
4429 . add ( 'up' , function ( e ) {
45- switch ( $scope . isFocusing ) {
46- case RESULT_LIST :
47- selectPriorItem ( )
48- break
49- case RESULT_CONTENT :
50- focusControl ( )
51- break
52- }
30+ priorSnippet ( )
5331 e . preventDefault ( )
5432 } )
5533 . add ( 'right' , function ( e ) {
56- if ( $scope . isFocusing === RESULT_LIST ) {
57- focusControl ( )
58- return
59- }
60- if ( $scope . isFocusing === RESULT_CONTROL ) {
61- nextControl ( )
62- }
6334 } )
6435 . add ( 'left' , function ( e ) {
65- if ( $scope . isFocusing === RESULT_CONTROL ) {
66- priorControl ( )
67- }
6836 } )
6937 . add ( 'esc' , function ( e ) {
70- switch ( $scope . isFocusing ) {
71- case RESULT_LIST :
72- focusSearchInput ( )
73- break
74- case RESULT_CONTROL :
75- focusList ( )
76- break
77- case RESULT_CONTENT :
78- console . log ( 'esc fr content' )
79- focusControl ( )
80- break
81- case SEARCH_INPUT :
82- hidePopUp ( )
83- }
38+ hidePopUp ( )
8439 } )
8540 . add ( 'shift+tab' , function ( e ) {
8641 e . preventDefault ( )
87- if ( $scope . isFocusing === RESULT_CONTROL ) {
88- priorControl ( )
89- return
90- }
9142 } )
9243 . add ( 'tab' , function ( e ) {
93- e . preventDefault ( )
94- if ( $scope . isFocusing === RESULT_LIST ) {
95- focusControl ( )
96- return
97- }
98- if ( $scope . isFocusing === RESULT_CONTROL ) {
99- nextControl ( )
100- return
101- }
10244 } )
10345 . add ( 'enter' , function ( e ) {
104- switch ( $scope . isFocusing ) {
105- case RESULT_LIST :
106- console . log ( $scope . selectedItem . content )
107- ipc . send ( 'writeCode' , $scope . selectedItem . content )
108- break
109- }
46+ console . log ( $scope . selectedItem . content )
47+ ipc . send ( 'writeCode' , $scope . selectedItem . content )
11048 e . preventDefault ( )
11149 } )
11250
113-
11451 $scope . aceLoaded = function ( editor ) {
11552 editor . commands . addCommand ( {
11653 name : 'escape' ,
11754 bindKey : { win : 'esc' , mac : 'esc' } ,
11855 exec : function ( editor ) {
11956 editor . blur ( )
120- focusControl ( )
12157 $scope . $apply ( )
12258 } ,
12359 readOnly : true
12460 } )
12561 }
12662
63+ $scope . $on ( 'nextSnippetRequested' , function ( e ) {
64+ e . stopPropagation ( )
65+ nextSnippet ( )
66+ } )
67+
68+ $scope . $on ( 'priorSnippetRequested' , function ( e ) {
69+ e . stopPropagation ( )
70+ priorSnippet ( )
71+ } )
72+
73+ $scope . $on ( 'snippetSubmitted' , function ( e ) {
74+ if ( $scope . filteredSnippets . length > 0 ) ipc . send ( 'writeCode' , $scope . selectedItem . content )
75+ else console . log ( '\x07' )
76+ e . stopPropagation ( )
77+ } )
78+
12779 // Init Data
12880 $scope . snippets = [ ]
12981
130- var userId = $auth . getPayload ( ) . sub
131- Snippet . findByUser ( userId )
82+ Snippet . findMine ( )
13283 . success ( function ( data ) {
133- $scope . snippets = data . snippets
84+ $scope . snippets = data
13485 filterList ( )
13586 } )
13687
137- // Functions
138-
139- // Search Filter
140- function filterList ( needle ) {
141- $scope . filteredSnippets = $filter ( 'filter' ) ( $scope . snippets , needle )
142- $scope . selectIndex = 0
143- selectItem ( $scope . selectIndex )
144- }
145- $scope . filterList = filterList
146-
147- function hidePopUp ( ) {
148- ipc . send ( 'hidePopUp' )
149- }
150-
15188 // Result Item control
15289 $scope . selectIndex = 0
15390
154- $scope . selectItem = selectItem
155- function selectItem ( index ) {
156- $scope . selectIndex = index
157- $scope . selectedItem = $scope . filteredSnippets [ index ]
91+ $scope . selectSnippet = selectSnippet
92+ $scope . filterList = filterList
93+ $scope . focusSearchInput = focusSearchInput
15894
159- $scope . controlIndex = 0
95+ // Search Filter
96+ function loadSnippets ( ) {
97+ Snippet . findMine ( )
98+ . success ( function ( data ) {
99+ $scope . snippets = data
100+ filterList ( )
101+ } )
160102 }
161103
162- function selectNextItem ( ) {
163- if ( $scope . selectIndex >= ( $scope . filteredSnippets . length - 1 ) ) {
164- return
165- }
166- selectItem ( ++ $scope . selectIndex )
104+ function filterList ( needle ) {
105+ $scope . filteredSnippets = $filter ( 'filter' ) ( $scope . snippets , needle )
106+ firstSnippet ( )
167107 }
168108
169- function selectPriorItem ( ) {
170- if ( $scope . selectIndex === 0 ) {
171- focusSearchInput ( )
172- return
173- }
174- selectItem ( -- $scope . selectIndex )
109+ function selectSnippet ( index ) {
110+ if ( index !== undefined ) $scope . selectIndex = index
111+ $scope . selectedItem = $scope . filteredSnippets [ $scope . selectIndex ]
175112 }
176113
177- // Focusing control
178- $scope . isFocusing = 0
179-
180- function focusSearchInput ( ) {
181- $scope . isFocusing = SEARCH_INPUT
182- document . getElementById ( 'search-input' ) . focus ( )
183-
184- $scope . controlIndex = 0
114+ function firstSnippet ( ) {
115+ $scope . selectIndex = 0
116+ selectSnippet ( $scope . selectIndex )
185117 }
186- $scope . focusSearchInput = focusSearchInput
187118
188- function focusList ( ) {
189- $scope . isFocusing = RESULT_LIST
190- document . getElementById ( 'search-input' ) . blur ( )
191-
192- $scope . controlIndex = 0
119+ function priorSnippet ( ) {
120+ if ( $scope . selectIndex > 0 ) $scope . selectIndex -= 1
121+ selectSnippet ( )
193122 }
194- $scope . focusList = focusList
195123
196- function focusControl ( ) {
197- if ( $scope . controlIndex === 0 ) {
198- $scope . controlIndex = 1
124+ function nextSnippet ( ) {
125+ if ( $scope . selectIndex < $scope . filteredSnippets . length - 1 ) {
126+ $scope . selectIndex + = 1
199127 }
200- $scope . isFocusing = RESULT_CONTROL
128+ selectSnippet ( )
201129 }
202- $scope . focusControl = focusControl
203130
204- function focusContent ( ) {
205- angular . element ( aceView ) . scope ( ) . focus ( )
206- $scope . isFocusing = RESULT_CONTENT
131+ // Focusing Search Input
132+ function focusSearchInput ( ) {
133+ document . getElementById ( 'search-input' ) . focus ( )
207134 }
208135
209- $scope . controlIndex = 0
210-
211- function nextControl ( ) {
212- if ( $scope . controlIndex === 3 ) {
213- $scope . controlIndex = 0
214- focusContent ( )
215- return
216- }
217- $scope . controlIndex ++
136+ function hidePopUp ( ) {
137+ ipc . send ( 'hidePopUp' )
218138 }
219139
220- function priorControl ( ) {
221- if ( $scope . controlIndex === 1 ) {
222- focusList ( )
223- return
224- }
225- $scope . controlIndex --
226- }
227140} )
228141. directive ( 'searchInput' , function ( ) {
229142 return {
230143 restrict : 'A' ,
231144 link : function ( scope , el , attr ) {
232145 el . on ( 'keydown' , function ( e ) {
233-
234146 // Down key => Focus on Result list
235147 if ( e . keyCode === 40 ) {
236- scope . focusList ( )
237- e . preventDefault ( )
148+ scope . $emit ( 'nextSnippetRequested' )
149+ // e.preventDefault()
150+ }
151+
152+ // Up key => Focus on Result list
153+ if ( e . keyCode === 38 ) {
154+ scope . $emit ( 'priorSnippetRequested' )
155+ // e.preventDefault()
156+ }
157+
158+ // Up key => Focus on Result list
159+ if ( e . keyCode === 13 ) {
160+ scope . $emit ( 'snippetSubmitted' )
238161 }
239162
240163 // Esc key => Dismiss popup
0 commit comments