3
3
/// <reference path="../Utils.ts"/>
4
4
5
5
module Popup {
6
+ var prevButton = document . getElementById ( "prev" ) ;
7
+ var nextButton = document . getElementById ( "next" ) ;
8
+ var queryInput = < HTMLInputElement > document . getElementById ( "query" ) ;
9
+ var caseInsensitiveCheckbox = < HTMLInputElement > document . getElementById ( "case-insensitive" ) ;
10
+
6
11
Utils . withActiveTab ( function ( tab : chrome . tabs . Tab ) {
7
12
var id = tab . id ;
8
13
var tabStates = BackgroundInterface . getTabStateManager ( ) ;
@@ -15,29 +20,31 @@ module Popup {
15
20
tabStates . resetState ( id ) ;
16
21
}
17
22
18
- var prevButton = document . getElementById ( "prev" ) ;
19
- var nextButton = document . getElementById ( "next" ) ;
20
- var queryInput = < HTMLInputElement > document . getElementById ( "query" ) ;
21
- var caseInsensitiveCheckbox = < HTMLInputElement > document . getElementById ( "case-insensitive" ) ;
23
+ addListeners ( id , tabStates ) ;
24
+ restoreState ( id , tabStates ) ;
25
+ } ) ;
22
26
23
- prevButton . addEventListener ( "click" , function ( event ) {
27
+ function addListeners ( id : number , tabStates : TabStateManager ) {
28
+ var prevButtonClick = function ( ) {
24
29
Utils . sendCommand ( "prev" ) ;
25
- } ) ;
26
- nextButton . addEventListener ( "click" , function ( event ) {
30
+ } ;
31
+
32
+ var nextButtonClick = function ( ) {
27
33
if ( tabStates . isSearching ( id ) ) {
28
34
Utils . sendCommand ( "next" ) ;
29
35
} else {
30
36
search ( id , tabStates ) ;
31
37
}
32
- } ) ;
38
+ } ;
33
39
34
- queryInput . addEventListener ( "keydown" , function ( event ) {
40
+ var queryInputKeyDown = function ( event ) {
35
41
if ( event . keyCode == 13 ) {
36
42
Utils . log ( "Enter pressed" ) ;
37
43
search ( id , tabStates ) ;
38
44
}
39
- } ) ;
40
- queryInput . addEventListener ( "input" , ( ) => {
45
+ }
46
+
47
+ var queryInputInput = ( ) => {
41
48
tabStates . set ( id , "query" , this . value ) ;
42
49
43
50
if ( tabStates . isSearching ( id ) ) {
@@ -53,43 +60,46 @@ module Popup {
53
60
} else {
54
61
setEnabled ( "next" , true ) ;
55
62
}
56
- } ) ;
57
-
58
- this . value = tabStates . get ( id , "query" ) ;
59
- if ( this . value == "" ) {
60
- setEnabled ( "next" , false ) ;
61
- } else {
62
- setEnabled ( "next" , true ) ;
63
63
}
64
64
65
- caseInsensitiveCheckbox . onclick = function ( ) {
65
+ var checkboxClick = function ( ) {
66
66
Utils . log ( "Set checkbox state to " + this . checked ) ;
67
67
tabStates . set ( id , "caseInsensitive" , this . checked ) ;
68
68
}
69
69
70
- caseInsensitiveCheckbox . checked = tabStates . get ( id , "caseInsensitive" ) ;
71
- } ) ;
70
+ prevButton . addEventListener ( "click" , prevButtonClick ) ;
71
+ nextButton . addEventListener ( "click" , nextButtonClick ) ;
72
+ queryInput . addEventListener ( "keydown" , queryInputKeyDown ) ;
73
+ queryInput . addEventListener ( "input" , queryInputInput ) ;
74
+ caseInsensitiveCheckbox . onclick = checkboxClick ;
75
+ }
76
+
77
+ function restoreState ( tabId : number , tabStates : TabStateManager ) {
78
+ queryInput . value = tabStates . get ( tabId , "query" ) ;
79
+ if ( queryInput . value == "" ) {
80
+ setEnabled ( "next" , false ) ;
81
+ } else {
82
+ setEnabled ( "next" , true ) ;
83
+ }
84
+
85
+ caseInsensitiveCheckbox . checked = tabStates . get ( tabId , "caseInsensitive" ) ;
86
+ }
72
87
73
88
function search ( tabId : number , tabStates : TabStateManager ) {
74
- var el = < HTMLInputElement > document . getElementById ( "query" ) ;
75
- if ( validate ( el . value ) ) {
76
- el . className = '' ;
77
- var checkbox = < HTMLInputElement > document . getElementById ( "case-insensitive" ) ;
78
- if ( checkbox . checked ) {
79
- var insensitive = true ;
80
- } else {
81
- var insensitive = false ;
82
- }
89
+ if ( validate ( queryInput . value ) ) {
90
+ queryInput . className = '' ;
91
+ var insensitive = caseInsensitiveCheckbox . checked ;
92
+
83
93
chrome . tabs . sendMessage ( tabId ,
84
94
{
85
95
command : "search" ,
86
96
caseInsensitive : insensitive ,
87
- regexp : el . value
97
+ regexp : queryInput . value
88
98
} ) ;
89
99
tabStates . set ( tabId , "searching" , true ) ;
90
100
} else {
91
101
Utils . log ( "Invalid regex" ) ;
92
- el . className = 'invalid' ;
102
+ queryInput . className = 'invalid' ;
93
103
}
94
104
}
95
105
0 commit comments