Skip to content

Commit 0a8c73a

Browse files
author
Tariq Islam
committed
Merge pull request #78 from tripit/dev
Fix scrolling bugs, history bugs, and add search minimum score to display
2 parents 0eb4caa + b363be8 commit 0a8c73a

File tree

5 files changed

+45
-30
lines changed

5 files changed

+45
-30
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Examples of Slate in the Wild
5959
* [ChaiOne Gameplan API docs](http://chaione.github.io/gameplanb2b/#introduction)
6060
* [Drcaban's Build a Quine tutorial](http://drcabana.github.io/build-a-quine/#introduction)
6161
* [PricePlow API docs](https://www.priceplow.com/api/documentation)
62+
* [Emerging Threats API docs](http://apidocs.emergingthreats.net/)
6263

6364
(Feel free to add your site to this list in a pull request!)
6465

source/javascripts/app/lang.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,36 @@ under the License.
2020
global.activateLanguage = activateLanguage;
2121

2222
function activateLanguage(language) {
23+
if (!language) return;
24+
2325
$("#lang-selector a").removeClass('active');
2426
$("#lang-selector a[data-language-name='" + language + "']").addClass('active');
2527
for (var i=0; i < languages.length; i++) {
2628
$(".highlight." + languages[i]).hide();
2729
}
2830
$(".highlight." + language).show();
31+
32+
// scroll to the new location of the position
33+
$(window.location.hash).get(0).scrollIntoView(true);
34+
}
35+
36+
// if a button is clicked, add the state to the history
37+
function pushURL(language) {
38+
if (!history) { return; }
39+
var hash = window.location.hash;
40+
if (hash) {
41+
hash = hash.replace(/^#+/, '');
42+
}
43+
history.pushState({}, '', '?' + language + '#' + hash);
2944
}
3045

3146
function setupLanguages(l) {
3247
var currentLanguage = l[0];
3348
var defaultLanguage = localStorage.getItem("language");
3449

3550
languages = l;
36-
37-
if ((location.search.substr(1) != "") && (jQuery.inArray(location.search.substr(1), languages)) != -1) {
51+
52+
if ((location.search.substr(1) !== "") && (jQuery.inArray(location.search.substr(1), languages)) != -1) {
3853
// the language is in the URL, so use that language!
3954
activateLanguage(location.search.substr(1));
4055

@@ -47,25 +62,14 @@ under the License.
4762
// no language selected, so use the default
4863
activateLanguage(languages[0]);
4964
}
50-
51-
// if we click on a language tab, reload the page with that language in the URL
52-
$("#lang-selector a").bind("click", function() {
53-
window.location.replace("?" + $(this).data("language-name") + window.location.hash);
54-
return false;
55-
});
56-
5765
}
5866

5967
// if we click on a language tab, activate that language
6068
$(function() {
6169
$("#lang-selector a").on("click", function() {
62-
var lang = $(this).data("language-name");
63-
var hash = window.location.hash;
64-
if (hash) hash = hash.replace(/^#+/, '');
65-
// do not reload the page every time the language is changed
66-
if (history) history.pushState({}, '', '?' + lang + '#' + hash);
67-
68-
activateLanguage(lang);
70+
var language = $(this).data("language-name");
71+
pushURL(language);
72+
activateLanguage(language);
6973
return false;
7074
});
7175
window.onpopstate = function(event) {

source/javascripts/app/search.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,13 @@
7171
if (this.value) {
7272
sections.hide();
7373
// results are sorted by score in descending order
74-
var tmpResults = index.search(this.value);
75-
var results = [];
76-
77-
// remove low score matches
78-
$.each(tmpResults, function (index, item) {
79-
if (item.score >= 0.0001) results.push(item);
80-
});
74+
var results = index.search(this.value);
8175

8276
if (results.length) {
83-
lastRef = null;
8477
resetHeaderLocations();
78+
var lastRef;
8579
$.each(results, function (index, item) {
80+
if (item.score <= 0.0001) return; // remove low-score results
8681
var itemRef = item.ref;
8782
$('#section-' + itemRef).show();
8883
// headers must be repositioned in the DOM

source/javascripts/lib/jquery.tocify.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147

148148
var self = this;
149149

150+
self.tocifyWrapper = $('.tocify-wrapper');
150151
self.extendPageScroll = true;
151152

152153
// Internal array that keeps track of all TOC items (Helps to recognize if there are duplicate TOC item strings)
@@ -703,6 +704,20 @@
703704
// Highlights the corresponding list item
704705
elem.addClass(self.focusClass);
705706

707+
// Scroll to highlighted element's header
708+
var tocifyWrapper = self.tocifyWrapper;
709+
var scrollToElem = $(elem).closest('.tocify-header');
710+
711+
var elementOffset = scrollToElem.offset().top,
712+
wrapperOffset = tocifyWrapper.offset().top;
713+
var offset = elementOffset - wrapperOffset;
714+
715+
if (offset >= $(window).height()) {
716+
var scrollPosition = offset + tocifyWrapper.scrollTop();
717+
tocifyWrapper.scrollTop(scrollPosition);
718+
} else if (offset < 0) {
719+
tocifyWrapper.scrollTop(0);
720+
}
706721
}
707722

708723
if(self.options.scrollHistory) {
@@ -1021,4 +1036,4 @@
10211036

10221037
});
10231038

1024-
})); //end of plugin
1039+
})); //end of plugin

source/stylesheets/screen.css.scss

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,15 @@ html, body {
226226
padding: 0 10px;
227227
line-height: 30px;
228228

229-
&.active {
230-
background-color: $lang-select-active-bg;
231-
color: $lang-select-active-text;
232-
}
233-
234229
&:active {
235230
background-color: $lang-select-pressed-bg;
236231
color: $lang-select-pressed-text;
237232
}
233+
234+
&.active {
235+
background-color: $lang-select-active-bg;
236+
color: $lang-select-active-text;
237+
}
238238
}
239239

240240
&:after {

0 commit comments

Comments
 (0)