1
1
/// <reference path="../d.ts/DefinitelyTyped/chrome/chrome.d.ts"/>
2
2
/// <reference path="../bg-interface.ts"/>
3
3
/// <reference path="../Utils.ts"/>
4
- var logging = false ;
5
4
6
- function log ( message : string ) {
7
- if ( logging ) {
8
- console . log ( message ) ;
9
- }
10
- }
5
+ module Popup {
6
+ Utils . withActiveTab ( function ( tab : chrome . tabs . Tab ) {
7
+ var id = tab . id ;
8
+ var tabStates = BackgroundInterface . getTabStateManager ( ) ;
11
9
12
- Utils . withActiveTab ( function ( tab : chrome . tabs . Tab ) {
13
- var id = tab . id ;
14
- var tabStates = BackgroundInterface . getTabStateManager ( ) ;
15
-
16
- // In most cases the map entry will already be initialized. However, there
17
- // may be a few edge cases where we need to initialize it ourselves.
18
- if ( ! tabStates . exists ( id ) ) {
19
- log ( "ID doesn't exist. Initializing entry." )
20
- tabStates . set ( id , { query : "" , searching : false , caseInsensitive : false } ) ;
21
- }
22
- var tabState = tabStates . get ( id ) ;
10
+ // In most cases the map entry will already be initialized. However, there
11
+ // may be a few edge cases where we need to initialize it ourselves.
12
+ if ( ! tabStates . exists ( id ) ) {
13
+ Utils . log ( "ID doesn't exist. Initializing entry." )
14
+ tabStates . resetState ( id ) ;
15
+ }
23
16
24
- var prevButton = document . getElementById ( "prev" ) ;
25
- var nextButton = document . getElementById ( "next" ) ;
26
- var queryInput = < HTMLInputElement > document . getElementById ( "query" ) ;
27
- var caseInsensitiveCheckbox = < HTMLInputElement > document . getElementById ( "case-insensitive" ) ;
17
+ var prevButton = document . getElementById ( "prev" ) ;
18
+ var nextButton = document . getElementById ( "next" ) ;
19
+ var queryInput = < HTMLInputElement > document . getElementById ( "query" ) ;
20
+ var caseInsensitiveCheckbox = < HTMLInputElement > document . getElementById ( "case-insensitive" ) ;
28
21
29
- function setEnabled ( id , val ) {
30
- document . getElementById ( id ) . disabled = ! val ;
31
- }
22
+ prevButton . addEventListener ( "click" , function ( event ) {
23
+ Utils . sendCommand ( "prev" ) ;
24
+ } ) ;
25
+ nextButton . addEventListener ( "click" , function ( event ) {
26
+ if ( tabStates . isSearching ( id ) ) {
27
+ Utils . sendCommand ( "next" ) ;
28
+ } else {
29
+ search ( id , tabStates ) ;
30
+ }
31
+ } ) ;
32
32
33
- prevButton . addEventListener ( "click" , function ( event ) {
34
- Utils . sendCommand ( "prev" ) ;
35
- } ) ;
36
- nextButton . addEventListener ( "click" , function ( event ) {
37
- if ( tabState . searching ) {
38
- Utils . sendCommand ( "next" ) ;
39
- } else {
40
- search ( ) ;
41
- }
42
- } ) ;
33
+ queryInput . addEventListener ( "keydown" , function ( event ) {
34
+ if ( event . keyCode == 13 ) {
35
+ Utils . log ( "Enter pressed" ) ;
36
+ search ( id , tabStates ) ;
37
+ }
38
+ } ) ;
39
+ queryInput . addEventListener ( "input" , ( ) => {
40
+ tabStates . set ( id , "query" , this . value ) ;
43
41
44
- queryInput . addEventListener ( "keydown" , function ( event ) {
45
- if ( event . keyCode == 13 ) {
46
- log ( "Enter pressed" ) ;
47
- search ( ) ;
48
- }
49
- } ) ;
50
- queryInput . addEventListener ( "input" , function ( event ) {
51
- tabState . query = queryInput . value ;
42
+ if ( tabStates . isSearching ( id ) ) {
43
+ tabStates . set ( id , "searching" , false ) ;
44
+ Utils . sendCommand ( "clear" ) ;
45
+ }
52
46
53
- if ( tabState . searching ) {
54
- tabState . searching = false ;
55
- Utils . sendCommand ( "clear" ) ;
56
- }
47
+ // Remove the invalid class if it's there
48
+ this . className = '' ;
57
49
58
- // Remove the invalid class if it's there
59
- queryInput . className = '' ;
50
+ if ( this . value == "" ) {
51
+ setEnabled ( "next" , false ) ;
52
+ } else {
53
+ setEnabled ( "next" , true ) ;
54
+ }
55
+ } ) ;
60
56
61
- if ( queryInput . value == "" ) {
57
+ this . value = tabStates . get ( id , "query" ) ;
58
+ if ( this . value == "" ) {
62
59
setEnabled ( "next" , false ) ;
63
60
} else {
64
61
setEnabled ( "next" , true ) ;
65
62
}
66
- } ) ;
67
-
68
- queryInput . value = tabState . query ;
69
- if ( queryInput . value == "" ) {
70
- setEnabled ( "next" , false ) ;
71
- } else {
72
- setEnabled ( "next" , true ) ;
73
- }
74
63
75
- caseInsensitiveCheckbox . onclick = function ( ) {
76
- log ( "Set checkbox state to " + this . checked ) ;
77
- tabState . caseInsensitive = this . checked ;
78
- }
64
+ caseInsensitiveCheckbox . onclick = function ( ) {
65
+ Utils . log ( "Set checkbox state to " + this . checked ) ;
66
+ tabStates . set ( id , " caseInsensitive" , this . checked ) ;
67
+ }
79
68
80
- caseInsensitiveCheckbox . checked = tabState . caseInsensitive ;
69
+ caseInsensitiveCheckbox . checked = tabStates . get ( id , "caseInsensitive" ) ;
70
+ } ) ;
81
71
82
- function search ( ) {
72
+ function search ( tabId : number , tabStates : TabStateManager ) {
83
73
var el = < HTMLInputElement > document . getElementById ( "query" ) ;
84
74
if ( validate ( el . value ) ) {
85
75
el . className = '' ;
@@ -89,20 +79,20 @@ Utils.withActiveTab(function (tab: chrome.tabs.Tab) {
89
79
} else {
90
80
var insensitive = false ;
91
81
}
92
- chrome . tabs . sendMessage ( id ,
82
+ chrome . tabs . sendMessage ( tabId ,
93
83
{
94
84
command : "search" ,
95
85
caseInsensitive : insensitive ,
96
86
regexp : el . value
97
87
} ) ;
98
- tabState . searching = true ;
88
+ tabStates . set ( tabId , " searching" , true ) ;
99
89
} else {
100
- log ( "Invalid regex" ) ;
90
+ Utils . log ( "Invalid regex" ) ;
101
91
el . className = 'invalid' ;
102
92
}
103
93
}
104
94
105
- function validate ( regexp ) {
95
+ function validate ( regexp : string ) : boolean {
106
96
if ( regexp != "" ) {
107
97
try {
108
98
"" . match ( regexp ) ;
@@ -112,4 +102,8 @@ Utils.withActiveTab(function (tab: chrome.tabs.Tab) {
112
102
}
113
103
return false ;
114
104
}
115
- } ) ;
105
+
106
+ function setEnabled ( id : string , val : boolean ) {
107
+ document . getElementById ( id ) . disabled = ! val ;
108
+ }
109
+ }
0 commit comments