@@ -3,6 +3,7 @@ var LoadingBar = require('./loading-bar.js');
33$ = require ( "jquery" ) ;
44var debounce = require ( "lodash/debounce" ) ;
55var loader = new LoadingBar ( 'blue' ) ;
6+ var SearchControl = require ( './search' ) ;
67
78// state
89var $articleContainer ,
@@ -12,22 +13,31 @@ var $articleContainer,
1213 $everything ,
1314 $headers ,
1415 $nav ,
16+ $pathPrefix ,
1517 headerHidden ,
1618 animating ,
1719 navigating ,
1820 scrollPositionInterval ,
19- currentHref ;
21+ currentHref ,
22+ searchControl ,
23+ hasShownSearch ;
2024
2125( function ( ) {
26+ //flag that determines whether or not the search has already been shown
27+ //(used for fading in or not)
28+ hasShownSearch = false ;
2229 init ( ) ;
2330
2431 // prevent sidebar from changing width when header hides
2532 $ ( '#left' ) . css ( 'min-width' , $ ( '.top-left' ) . width ( ) ) ;
2633
2734 // Override link behavior
2835 $ ( document . body ) . on ( "click" , "a" , function ( ev ) {
29- var noModifierKeys = ! ev . altKey && ! ev . ctrlKey && ! ev . metaKey && ! ev . shiftKey ;
30- if ( noModifierKeys && this . hostname === window . location . hostname ) {
36+ var noModifierKeys = ! ev . altKey && ! ev . ctrlKey && ! ev . metaKey && ! ev . shiftKey ,
37+ sameHostname = this . hostname === window . location . hostname ,
38+ sameProtocol = this . protocol === window . location . protocol ;
39+
40+ if ( noModifierKeys && sameHostname && sameProtocol ) {
3141 ev . preventDefault ( ) ;
3242 var href = this . href ;
3343 window . history . pushState ( null , null , href ) ;
@@ -69,13 +79,35 @@ function init() {
6979 $everything = $ ( '#everything' ) ;
7080 $headers = getHeaders ( ) ;
7181 $nav = $ ( '.top-left > .brand, .top-right-top' ) ;
82+ $pathPrefix = $ ( "[path-prefix]" ) . first ( ) ;
7283 headerHidden = undefined ;
7384 currentHref = window . location . href ;
7485
86+ setPathPrefix ( ) ;
7587 setOnThisPageContent ( ) ;
7688 buildTOC ( ) ;
7789 setNavToggleListener ( ) ;
7890 setScrollPosition ( ) ;
91+ searchControl = new SearchControl ( ".search-bar" , {
92+ navigate : function ( href ) {
93+ window . history . pushState ( null , null , href ) ;
94+ navigate ( href ) ;
95+ } ,
96+ pathPrefix : window . pathPrefix ,
97+ animateInOnStart : ! hasShownSearch
98+ } ) ;
99+
100+ hasShownSearch = true ;
101+ }
102+
103+ function setPathPrefix ( ) {
104+ var pathPrefix ;
105+ if ( $pathPrefix && $pathPrefix . length ) {
106+ pathPrefix = $pathPrefix . attr ( "path-prefix" ) ;
107+ if ( pathPrefix && pathPrefix . length ) {
108+ window . pathPrefix = pathPrefix ;
109+ }
110+ }
79111}
80112
81113function setDocTitle ( ) {
@@ -163,14 +195,27 @@ function navigate(href) {
163195 if ( ! $content . length ) {
164196 window . location . reload ( ) ;
165197 }
166- var $nav = $content . find ( ".bottom-left > ul" ) ;
167- var $article = $content . find ( "article" ) ;
168- var $breadcrumb = $content . find ( ".breadcrumb" ) ;
169- var homeLink = $content . find ( ".logo > a" ) . attr ( 'href' ) ;
170- $ ( ".bottom-left>ul" ) . replaceWith ( $nav ) ;
198+ var $nav = $content . find ( ".bottom-left > ul" ) ,
199+ $article = $content . find ( "article" ) ,
200+ $breadcrumb = $content . find ( ".breadcrumb" ) ,
201+ homeLink = $content . find ( ".logo > a" ) . attr ( 'href' ) ,
202+ $navReplace = $ ( ".bottom-left>ul" ) ,
203+
204+ //root elements - use .filter; not .find
205+ $pathPrefixDiv = $content . filter ( "[path-prefix]" ) ;
206+
207+
208+ //if any page doesn't have a nav, replacing it won't work,
209+ //and the nav will be gone for any subsequent page visits
210+ if ( $navReplace && $navReplace . length ) {
211+ $navReplace . replaceWith ( $nav ) ;
212+ } else {
213+ $ ( ".bottom-left" ) . append ( $nav ) ;
214+ }
171215 $ ( "article" ) . replaceWith ( $article ) ;
172216 $ ( ".breadcrumb" ) . replaceWith ( $breadcrumb ) ;
173217 $ ( ".logo > a" ) . attr ( 'href' , homeLink ) ;
218+ $ ( "[path-prefix]" ) . replaceWith ( $pathPrefixDiv ) ;
174219
175220 // Initialize jsbin scripts
176221 delete window . jsbinified ;
0 commit comments