Skip to content

Commit 4610356

Browse files
Merge pull request #251 from canjs/249-search-links-and-search-disappearance
search links and search disappearance
2 parents 20dfbe5 + c78e867 commit 4610356

File tree

3 files changed

+78
-24
lines changed

3 files changed

+78
-24
lines changed

static/canjs.js

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,19 @@ var $articleContainer,
1111
$everything,
1212
$headers,
1313
$nav,
14+
$pathPrefix,
1415
headerHidden,
1516
animating,
1617
navigating,
1718
scrollPositionInterval,
1819
currentHref,
19-
searchControl;
20+
searchControl,
21+
hasShownSearch;
2022

2123
(function() {
24+
//flag that determines whether or not the search has already been shown
25+
//(used for fading in or not)
26+
hasShownSearch = false;
2227
init();
2328

2429
// prevent sidebar from changing width when header hides
@@ -72,9 +77,11 @@ function init() {
7277
$everything = $('#everything');
7378
$headers = getHeaders();
7479
$nav = $('.top-left > .brand, .top-right-top');
80+
$pathPrefix = $("[path-prefix]").first();
7581
headerHidden = undefined;
7682
currentHref = window.location.href;
7783

84+
setPathPrefix();
7885
setOnThisPageContent();
7986
buildTOC();
8087
setNavToggleListener();
@@ -83,8 +90,22 @@ function init() {
8390
navigate: function(href){
8491
window.history.pushState(null, null, href);
8592
navigate(href);
86-
}
93+
},
94+
pathPrefix: window.pathPrefix,
95+
animateInOnStart: !hasShownSearch
8796
});
97+
98+
hasShownSearch = true;
99+
}
100+
101+
function setPathPrefix(){
102+
var pathPrefix;
103+
if($pathPrefix && $pathPrefix.length){
104+
pathPrefix = $pathPrefix.attr("path-prefix");
105+
if(pathPrefix && pathPrefix.length){
106+
window.pathPrefix = pathPrefix;
107+
}
108+
}
88109
}
89110

90111
function setDocTitle() {
@@ -160,14 +181,27 @@ function navigate(href) {
160181
if (!$content.length) {
161182
window.location.reload();
162183
}
163-
var $nav = $content.find(".bottom-left > ul");
164-
var $article = $content.find("article");
165-
var $breadcrumb = $content.find(".breadcrumb");
166-
var homeLink = $content.find(".logo > a").attr('href');
167-
$(".bottom-left>ul").replaceWith($nav);
184+
var $nav = $content.find(".bottom-left > ul"),
185+
$article = $content.find("article"),
186+
$breadcrumb = $content.find(".breadcrumb"),
187+
homeLink = $content.find(".logo > a").attr('href'),
188+
$navReplace = $(".bottom-left>ul"),
189+
190+
//root elements - use .filter; not .find
191+
$pathPrefixDiv = $content.filter("[path-prefix]");
192+
193+
194+
//if any page doesn't have a nav, replacing it won't work,
195+
//and the nav will be gone for any subsequent page visits
196+
if($navReplace && $navReplace.length){
197+
$navReplace.replaceWith($nav);
198+
}else{
199+
$(".bottom-left").append($nav);
200+
}
168201
$("article").replaceWith($article);
169202
$(".breadcrumb").replaceWith($breadcrumb);
170203
$(".logo > a").attr('href', homeLink);
204+
$("[path-prefix]").replaceWith($pathPrefixDiv);
171205

172206
// Initialize jsbin scripts
173207
delete window.jsbinified;

static/search.js

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ var Search = Control.extend({
1616
//renderer stuff
1717
resultsRenderer: searchResultsRenderer,
1818
pathPrefix: window.pathPrefix,
19-
searchMapHashUrl: window.pathPrefix + '/searchMapHash.json',
20-
searchMapUrl: window.pathPrefix + '/searchMap.json',
19+
searchMapHashUrl: '/searchMapHash.json',
20+
searchMapUrl: '/searchMap.json',
2121

2222
//callbacks
2323
onResultsHide: null,
@@ -33,7 +33,10 @@ var Search = Control.extend({
3333
minSearchLength: 3,
3434
searchTimeout: 400,
3535

36-
localStorageKeyPrefix: "bit-docs-search"
36+
localStorageKeyPrefix: "bit-docs-search",
37+
38+
//whether or not to animate in upon initialization
39+
animateInOnStart: true
3740
}
3841
}, {
3942

@@ -69,12 +72,16 @@ var Search = Control.extend({
6972
//hide the input until the search engine is ready
7073
this.$inputWrap.hide();
7174

72-
this.checkSearchMapHash(this.options.searchMapHashUrl).then(function(searchMapHashChangedObject){
73-
self.getSearchMap(self.options.searchMapUrl, searchMapHashChangedObject).then(function(searchMap){
75+
this.checkSearchMapHash(this.options.pathPrefix + this.options.searchMapHashUrl).then(function(searchMapHashChangedObject){
76+
self.getSearchMap(self.options.pathPrefix + self.options.searchMapUrl, searchMapHashChangedObject).then(function(searchMap){
7477
self.initSearchEngine(searchMap);
7578

7679
//show the search input when the search engine is ready
77-
self.$inputWrap.fadeIn(400);
80+
if(self.options.animateInOnStart){
81+
self.$inputWrap.fadeIn(400);
82+
}else{
83+
self.$inputWrap.show();
84+
}
7885

7986
//focus the search on init
8087
//only do stuff if we have an input to work with
@@ -222,23 +229,28 @@ var Search = Control.extend({
222229
// @returns thenable that resolves to true if localStorage was cleared and false otherwise
223230
checkSearchMapHash: function(dataUrl) {
224231
var self = this,
225-
returnDeferred = $.Deferred();
232+
returnDeferred = $.Deferred(),
233+
localStorageKey = self.formatLocalStorageKey(self.searchMapHashLocalStorageKey),
234+
searchMapHashLocalStorage = self.getLocalStorageItem(localStorageKey),
235+
lsHash = searchMapHashLocalStorage && searchMapHashLocalStorage.hash;
226236

227237
//no need to do anything if localStorage isn't present
228238
if(!window.localStorage){
229239
returnDeferred.resolve(false);
230240
return;
231241
}
232242

243+
244+
localStorageKey = self.formatLocalStorageKey(self.searchMapHashLocalStorageKey);
245+
searchMapHashLocalStorage = self.getLocalStorageItem(localStorageKey);
246+
lsHash = searchMapHashLocalStorage && searchMapHashLocalStorage.hash;
247+
233248
$.ajax({
234249
url: dataUrl,
235250
dataType: "json",
236251
cache: false
237252
}).then(function(data){
238-
var localStorageKey = self.formatLocalStorageKey(self.searchMapHashLocalStorageKey),
239-
searchMapHashLocalStorage = self.getLocalStorageItem(localStorageKey),
240-
lsHash = searchMapHashLocalStorage && searchMapHashLocalStorage.hash,
241-
dataHash = data && data.hash;
253+
var dataHash = data && data.hash;
242254

243255
//no lsHash && no dataHash => resolve
244256
//lsHash && no dataHash => resolve
@@ -258,7 +270,14 @@ var Search = Control.extend({
258270
}
259271

260272
returnDeferred.resolve(false);
261-
}, returnDeferred.reject);
273+
}, function(error){
274+
//if we have a localStorage item, use it
275+
if(searchMapHashLocalStorage){
276+
returnDeferred.resolve(false);
277+
}else{
278+
returnDeferred.reject(error);
279+
}
280+
});
262281

263282
return returnDeferred;
264283
},
@@ -490,15 +509,15 @@ var Search = Control.extend({
490509
},{
491510
docUrl: function(){
492511
if(!docObject.pathToRoot){
493-
return self.url;
512+
return this.url;
494513
}
495514

496515
var root = joinURIs(window.location.href, docObject.pathToRoot);
497516
if(root.substr(-1) === "/"){
498517
root = root.substr(0, root.length-1);
499518
}
500519

501-
return root + "/" + self.url;
520+
return root + "/" + this.url;
502521
}
503522
});
504523

templates/layout.mustache

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@
4949
<script type='text/javascript' data-main="bit-docs-site/static" src="{{pathToDest}}/static/node_modules/steal/steal.production.js"></script>
5050
{{/if}}
5151
<script async defer src="https://buttons.github.io/buttons.js"></script>
52-
<script type="text/javascript">
53-
window.pathPrefix = '{{pathToDest}}';
54-
</script>
52+
53+
<!-- root-level elements with attributes necessary for the app -->
54+
<div path-prefix="{{pathToDest}}" />
55+
5556
</body>
5657
</html>

0 commit comments

Comments
 (0)