1+ /*!
2+ * Pattern Finder
3+ *
4+ * Copyright (c) 2014 Dave Olsen, http://dmolsen.com
5+ * Licensed under the MIT license
6+ *
7+ */
8+
9+ var patternFinder = {
10+
11+ data : [ ] ,
12+ active : false ,
13+
14+ init : function ( ) {
15+
16+ for ( var patternType in patternPaths ) {
17+ if ( patternPaths . hasOwnProperty ( patternType ) ) {
18+ for ( var pattern in patternPaths [ patternType ] ) {
19+ var obj = { } ;
20+ obj . patternPartial = patternType + "-" + pattern ;
21+ obj . patternPath = patternPaths [ patternType ] [ pattern ] ;
22+ this . data . push ( obj ) ;
23+ }
24+ }
25+ }
26+
27+ // instantiate the bloodhound suggestion engine
28+ var patterns = new Bloodhound ( {
29+ datumTokenizer : function ( d ) { return Bloodhound . tokenizers . nonword ( d . patternPartial ) ; } ,
30+ queryTokenizer : Bloodhound . tokenizers . nonword ,
31+ limit : 10 ,
32+ local : this . data
33+ } ) ;
34+
35+ // initialize the bloodhound suggestion engine
36+ patterns . initialize ( ) ;
37+
38+ $ ( '#sg-find .typeahead' ) . typeahead ( { highlight : true } , {
39+ displayKey : 'patternPartial' ,
40+ source : patterns . ttAdapter ( )
41+ } ) . on ( 'typeahead:selected' , patternFinder . onAutocompleted ) . on ( 'typeahead:autocompleted' , patternFinder . onSelected ) ;
42+
43+ } ,
44+
45+ passPath : function ( item ) {
46+ // update the iframe via the history api handler
47+ patternFinder . closeFinder ( ) ;
48+ var obj = JSON . stringify ( { "path" : urlHandler . getFileName ( item . patternPartial ) } ) ;
49+ document . getElementById ( "sg-viewport" ) . contentWindow . postMessage ( JSON . parse ( obj ) , urlHandler . targetOrigin ) ;
50+ } ,
51+
52+ onSelected : function ( e , item ) {
53+ patternFinder . passPath ( item ) ;
54+ } ,
55+
56+ onAutocompleted : function ( e , item ) {
57+ patternFinder . passPath ( item ) ;
58+ } ,
59+
60+ toggleFinder : function ( ) {
61+ if ( ! patternFinder . active ) {
62+ patternFinder . openFinder ( ) ;
63+ } else {
64+ patternFinder . closeFinder ( ) ;
65+ }
66+ } ,
67+
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+ }
99+
100+ }
101+
102+ patternFinder . init ( ) ;
103+
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+ } ) ;
115+
116+ // jwerty stuff
117+ // toggle the annotations panel
118+ jwerty . key ( 'ctrl+shift+f' , function ( e ) {
119+ $ ( '.sg-find .sg-acc-handle, .sg-find .sg-acc-panel' ) . addClass ( 'active' ) ;
120+ patternFinder . toggleFinder ( ) ;
121+ return false ;
122+ } ) ;
0 commit comments