8
8
9
9
var patternFinder = {
10
10
11
- data : [ ] ,
11
+ data : [ ] ,
12
+ active : false ,
12
13
13
14
init : function ( ) {
14
15
@@ -43,9 +44,7 @@ var patternFinder = {
43
44
44
45
passPath : function ( item ) {
45
46
// update the iframe via the history api handler
46
- $ ( '#sg-find' ) . removeClass ( 'show-overflow' ) ;
47
- $ ( '#sg-find .typeahead' ) . val ( "" ) ;
48
- $ ( '.sg-acc-handle, .sg-acc-panel' ) . removeClass ( 'active' ) ;
47
+ patternFinder . closeFinder ( ) ;
49
48
var obj = JSON . stringify ( { "path" : urlHandler . getFileName ( item . patternPartial ) } ) ;
50
49
document . getElementById ( "sg-viewport" ) . contentWindow . postMessage ( obj , urlHandler . targetOrigin ) ;
51
50
} ,
@@ -58,22 +57,66 @@ var patternFinder = {
58
57
patternFinder . passPath ( item ) ;
59
58
} ,
60
59
61
- toggleFinder : function ( ) { } ,
60
+ toggleFinder : function ( ) {
61
+ if ( ! patternFinder . active ) {
62
+ patternFinder . openFinder ( ) ;
63
+ } else {
64
+ patternFinder . closeFinder ( ) ;
65
+ }
66
+ } ,
62
67
63
- openFinder : function ( ) { } ,
68
+ openFinder : function ( ) {
69
+ patternFinder . active = true ;
70
+ $ ( '#sg-find .typeahead' ) . val ( "" ) ;
71
+ $ ( "#sg-find" ) . addClass ( 'show-overflow' ) ;
72
+ $ ( '#sg-find .typeahead' ) . focus ( ) ;
73
+ } ,
74
+
75
+ closeFinder : function ( ) {
76
+ patternFinder . active = false ;
77
+ $ ( "#sg-find" ) . removeClass ( 'show-overflow' ) ;
78
+ $ ( '.sg-acc-handle, .sg-acc-panel' ) . removeClass ( 'active' ) ;
79
+ $ ( '#sg-find .typeahead' ) . val ( "" ) ;
80
+ } ,
81
+
82
+ receiveIframeMessage : function ( event ) {
83
+
84
+ var data = ( typeof event . data !== "string" ) ? event . data : JSON . parse ( event . data ) ;
85
+
86
+ // does the origin sending the message match the current host? if not dev/null the request
87
+ if ( ( window . location . protocol !== "file:" ) && ( event . origin !== window . location . protocol + "//" + window . location . host ) ) {
88
+ return ;
89
+ }
90
+
91
+ if ( data . keyPress !== undefined ) {
92
+ if ( data . keyPress == 'ctrl+shift+f' ) {
93
+ patternFinder . toggleFinder ( ) ;
94
+ return false ;
95
+ }
96
+ }
97
+
98
+ }
64
99
65
- closeFinder : function ( ) { }
66
-
67
100
}
68
101
69
102
patternFinder . init ( ) ;
70
103
71
- $ ( '#sg-find' ) . click ( function ( ) { $ ( this ) . toggleClass ( 'show-overflow' ) } ) ;
104
+ window . addEventListener ( "message" , patternFinder . receiveIframeMessage , false ) ;
105
+
106
+ $ ( '#sg-find .typeahead' ) . focus ( function ( ) {
107
+ if ( ! patternFinder . active ) {
108
+ patternFinder . openFinder ( ) ;
109
+ }
110
+ } ) ;
111
+
112
+ $ ( '#sg-find .typeahead' ) . blur ( function ( ) {
113
+ patternFinder . closeFinder ( ) ;
114
+ } ) ;
72
115
73
116
// jwerty stuff
74
117
// toggle the annotations panel
75
- jwerty . key ( 'cmd+shift+f/ ctrl+shif +f' , function ( e ) {
118
+ jwerty . key ( 'ctrl+shift +f' , function ( e ) {
76
119
$ ( '.sg-find .sg-acc-handle, .sg-find .sg-acc-panel' ) . addClass ( 'active' ) ;
77
- $ ( '#sg-find .typeahead' ) . focus ( ) ;
120
+ patternFinder . toggleFinder ( ) ;
78
121
return false ;
79
122
} ) ;
0 commit comments