@@ -5,13 +5,18 @@ function Search(menu) {
5
5
this . $search = document . getElementById ( 'menu-search' ) ;
6
6
this . $searchBox = document . getElementById ( 'menu-search-box' ) ;
7
7
this . $searchResults = document . getElementById ( 'menu-search-results' ) ;
8
-
8
+
9
9
this . loadBiblio ( ) ;
10
-
10
+
11
11
document . addEventListener ( 'keydown' , this . documentKeydown . bind ( this ) ) ;
12
-
12
+
13
13
this . $searchBox . addEventListener ( 'keydown' , debounce ( this . searchBoxKeydown . bind ( this ) , { stopPropagation : true } ) ) ;
14
14
this . $searchBox . addEventListener ( 'keyup' , debounce ( this . searchBoxKeyup . bind ( this ) , { stopPropagation : true } ) ) ;
15
+
16
+ // Perform an initial search if the box is not empty.
17
+ if ( this . $searchBox . value ) {
18
+ this . search ( this . $searchBox . value ) ;
19
+ }
15
20
}
16
21
17
22
Search . prototype . loadBiblio = function ( ) {
@@ -51,7 +56,7 @@ Search.prototype.searchBoxKeyup = function (e) {
51
56
if ( e . keyCode === 13 || e . keyCode === 9 ) {
52
57
return ;
53
58
}
54
-
59
+
55
60
this . search ( e . target . value ) ;
56
61
}
57
62
@@ -74,38 +79,36 @@ Search.prototype.triggerSearch = function (e) {
74
79
// prefer shorter matches.
75
80
function relevance ( result , searchString ) {
76
81
var relevance = 0 ;
77
-
82
+
78
83
relevance = Math . max ( 0 , 8 - result . match . chunks ) << 7 ;
79
-
84
+
80
85
if ( result . match . caseMatch ) {
81
86
relevance *= 2 ;
82
87
}
83
-
88
+
84
89
if ( result . match . prefix ) {
85
90
relevance += 2048
86
91
}
87
-
92
+
88
93
relevance += Math . max ( 0 , 255 - result . entry . key . length ) ;
89
-
94
+
90
95
return relevance ;
91
96
}
92
97
93
98
Search . prototype . search = function ( searchString ) {
94
- var s = Date . now ( ) ;
95
-
96
99
if ( searchString === '' ) {
97
100
this . displayResults ( [ ] ) ;
98
101
this . hideSearch ( ) ;
99
102
return ;
100
103
} else {
101
104
this . showSearch ( ) ;
102
105
}
103
-
106
+
104
107
if ( searchString . length === 1 ) {
105
108
this . displayResults ( [ ] ) ;
106
109
return ;
107
110
}
108
-
111
+
109
112
var results ;
110
113
111
114
if ( / ^ [ \d \. ] * $ / . test ( searchString ) ) {
@@ -116,7 +119,7 @@ Search.prototype.search = function (searchString) {
116
119
} ) ;
117
120
} else {
118
121
results = [ ] ;
119
-
122
+
120
123
for ( var i = 0 ; i < this . biblio . length ; i ++ ) {
121
124
var entry = this . biblio [ i ] ;
122
125
if ( ! entry . key ) {
@@ -129,19 +132,19 @@ Search.prototype.search = function (searchString) {
129
132
results . push ( { entry : entry , match : match } ) ;
130
133
}
131
134
}
132
-
135
+
133
136
results . forEach ( function ( result ) {
134
137
result . relevance = relevance ( result , searchString ) ;
135
138
} ) ;
136
-
139
+
137
140
results = results . sort ( function ( a , b ) { return b . relevance - a . relevance } ) ;
138
141
139
142
}
140
143
141
144
if ( results . length > 50 ) {
142
145
results = results . slice ( 0 , 50 ) ;
143
146
}
144
-
147
+
145
148
this . displayResults ( results ) ;
146
149
}
147
150
Search . prototype . hideSearch = function ( ) {
@@ -158,7 +161,7 @@ Search.prototype.selectResult = function () {
158
161
if ( $first ) {
159
162
document . location = $first . getAttribute ( 'href' ) ;
160
163
}
161
-
164
+
162
165
this . $searchBox . value = '' ;
163
166
this . $searchBox . blur ( ) ;
164
167
this . displayResults ( [ ] ) ;
@@ -172,7 +175,7 @@ Search.prototype.selectResult = function () {
172
175
Search . prototype . displayResults = function ( results ) {
173
176
if ( results . length > 0 ) {
174
177
this . $searchResults . classList . remove ( 'no-results' ) ;
175
-
178
+
176
179
var html = '<ul>' ;
177
180
178
181
results . forEach ( function ( result ) {
@@ -189,7 +192,7 @@ Search.prototype.displayResults = function (results) {
189
192
} else if ( entry . type === 'production' ) {
190
193
text = entry . key ;
191
194
cssClass = 'prod' ;
192
- id = entry . id ;
195
+ id = entry . id ;
193
196
} else if ( entry . type === 'op' ) {
194
197
text = entry . key ;
195
198
cssClass = 'op' ;
@@ -201,7 +204,7 @@ Search.prototype.displayResults = function (results) {
201
204
}
202
205
203
206
if ( text ) {
204
- html += '<li class=menu-search-result-' + cssClass + '><a href="#' + id + '">' + text + '</a></li>'
207
+ html += '<li class=menu-search-result-' + cssClass + '><a href="#' + id + '">' + text + '</a></li>' ;
205
208
}
206
209
} ) ;
207
210
@@ -224,7 +227,7 @@ function Menu() {
224
227
this . $toc = document . querySelector ( '#menu-toc > ol' ) ;
225
228
this . $specContainer = document . getElementById ( 'spec-container' ) ;
226
229
this . search = new Search ( this ) ;
227
-
230
+
228
231
this . _pinnedIds = { } ;
229
232
this . loadPinEntries ( ) ;
230
233
@@ -269,9 +272,9 @@ function Menu() {
269
272
&& target . offsetHeight + target . scrollTop >= target . scrollHeight ;
270
273
271
274
if ( offBottom ) {
272
- e . preventDefault ( ) ;
273
- }
274
- } )
275
+ e . preventDefault ( ) ;
276
+ }
277
+ } ) ;
275
278
}
276
279
277
280
Menu . prototype . documentKeydown = function ( e ) {
@@ -298,7 +301,7 @@ Menu.prototype.revealInToc = function (path) {
298
301
current [ i ] . classList . remove ( 'revealed' ) ;
299
302
current [ i ] . classList . remove ( 'revealed-leaf' ) ;
300
303
}
301
-
304
+
302
305
var current = this . $toc ;
303
306
var index = 0 ;
304
307
while ( index < path . length ) {
@@ -309,7 +312,7 @@ Menu.prototype.revealInToc = function (path) {
309
312
if ( index === path . length - 1 ) {
310
313
children [ i ] . classList . add ( 'revealed-leaf' ) ;
311
314
var rect = children [ i ] . getBoundingClientRect ( ) ;
312
- this . $toc . getBoundingClientRect ( ) . top
315
+ // this.$toc.getBoundingClientRect().top;
313
316
var tocRect = this . $toc . getBoundingClientRect ( ) ;
314
317
if ( rect . top + 10 > tocRect . bottom ) {
315
318
this . $toc . scrollTop = this . $toc . scrollTop + ( rect . top - tocRect . bottom ) + ( rect . bottom - rect . top ) ;
@@ -320,29 +323,27 @@ Menu.prototype.revealInToc = function (path) {
320
323
current = children [ i ] . querySelector ( 'ol' ) ;
321
324
index ++ ;
322
325
break ;
323
- }
326
+ }
324
327
}
325
-
328
+
326
329
}
327
330
}
328
331
329
332
function findActiveClause ( root , path ) {
330
333
var clauses = new ClauseWalker ( root ) ;
331
334
var $clause ;
332
- var found = false ;
333
335
var path = path || [ ] ;
334
-
336
+
335
337
while ( $clause = clauses . nextNode ( ) ) {
336
338
var rect = $clause . getBoundingClientRect ( ) ;
337
- var $header = $clause . children [ 0 ] ;
339
+ var $header = $clause . querySelector ( "h1" ) ;
338
340
var marginTop = parseInt ( getComputedStyle ( $header ) [ "margin-top" ] ) ;
339
-
341
+
340
342
if ( ( rect . top - marginTop ) <= 0 && rect . bottom > 0 ) {
341
- found = true ;
342
343
return findActiveClause ( $clause , path . concat ( $clause ) ) || path ;
343
344
}
344
345
}
345
-
346
+
346
347
return path ;
347
348
}
348
349
@@ -367,7 +368,7 @@ function ClauseWalker(root) {
367
368
} ,
368
369
false
369
370
) ;
370
-
371
+
371
372
return treeWalker ;
372
373
}
373
374
@@ -450,7 +451,7 @@ Menu.prototype.loadPinEntries = function () {
450
451
} catch ( e ) {
451
452
return ;
452
453
}
453
-
454
+
454
455
var pinsString = window . localStorage . pinEntries ;
455
456
if ( ! pinsString ) return ;
456
457
var pins = JSON . parse ( pinsString ) ;
@@ -482,6 +483,11 @@ function init() {
482
483
$container . addEventListener ( 'mouseover' , debounce ( function ( e ) {
483
484
Toolbox . activateIfMouseOver ( e ) ;
484
485
} ) ) ;
486
+ document . addEventListener ( 'keydown' , debounce ( function ( e ) {
487
+ if ( e . code === "Escape" && Toolbox . active ) {
488
+ Toolbox . deactivate ( ) ;
489
+ }
490
+ } ) ) ;
485
491
}
486
492
487
493
document . addEventListener ( 'DOMContentLoaded' , init ) ;
@@ -580,12 +586,11 @@ function fuzzysearch (searchString, haystack, caseInsensitive) {
580
586
var qlen = searchString . length ;
581
587
var chunks = 1 ;
582
588
var finding = false ;
583
- var prefix = true ;
584
-
589
+
585
590
if ( qlen > tlen ) {
586
591
return false ;
587
592
}
588
-
593
+
589
594
if ( qlen === tlen ) {
590
595
if ( searchString === haystack ) {
591
596
return { caseMatch : true , chunks : 1 , prefix : true } ;
@@ -595,7 +600,7 @@ function fuzzysearch (searchString, haystack, caseInsensitive) {
595
600
return false ;
596
601
}
597
602
}
598
-
603
+
599
604
outer: for ( var i = 0 , j = 0 ; i < qlen ; i ++ ) {
600
605
var nch = searchString [ i ] ;
601
606
while ( j < tlen ) {
@@ -609,12 +614,12 @@ function fuzzysearch (searchString, haystack, caseInsensitive) {
609
614
finding = false ;
610
615
}
611
616
}
612
-
613
- if ( caseInsensitive ) { return false }
614
-
617
+
618
+ if ( caseInsensitive ) { return false ; }
619
+
615
620
return fuzzysearch ( searchString . toLowerCase ( ) , haystack . toLowerCase ( ) , true ) ;
616
621
}
617
-
622
+
618
623
return { caseMatch : ! caseInsensitive , chunks : chunks , prefix : j <= qlen } ;
619
624
}
620
625
@@ -771,7 +776,6 @@ var referencePane = {
771
776
var target = document . getElementById ( id ) ;
772
777
var cid = findParentClauseId ( target ) ;
773
778
var clause = menu . search . biblio . byId [ cid ] ;
774
- var dupCount = 0 ;
775
779
return { id : id , clause : clause }
776
780
} ) . sort ( function ( a , b ) {
777
781
return sortByClauseNumber ( a . clause , b . clause ) ;
@@ -811,7 +815,7 @@ function sortByClauseNumber(c1, c2) {
811
815
if ( i >= c2c . length ) {
812
816
return 1 ;
813
817
}
814
-
818
+
815
819
var c1 = c1c [ i ] ;
816
820
var c2 = c2c [ i ] ;
817
821
var c1cn = Number ( c1 ) ;
0 commit comments