Skip to content

Commit 9334f79

Browse files
authored
Merge pull request #1574 from ivan-nginx/master
Fixes and enhancements. [10]
2 parents f8a3ae2 + 0070675 commit 9334f79

File tree

7 files changed

+206
-8
lines changed

7 files changed

+206
-8
lines changed

_config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ sidebar:
213213
# Automatically scroll page to section which is under <!-- more --> mark.
214214
scroll_to_more: true
215215

216+
# Automatically saving scroll position on each post/page in cookies.
217+
save_scroll: false
218+
216219
# Automatically excerpt description in homepage as preamble text.
217220
excerpt_description: true
218221

languages/ru.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title:
44
tag: Тэг
55
schedule: Календарь
66

7-
author: Author
7+
author: Автор
88

99
menu:
1010
home: Главная
@@ -34,11 +34,11 @@ post:
3434
wordcount: Кол-во слов в статье
3535
min2read: Время чтения в минутах
3636
copyright:
37-
author: Post author
38-
link: Post link
39-
license_title: Copyright Notice
40-
license_content: 'All articles in this blog are licensed under
41-
<a href="%s" rel="external nofollow" target="_blank">%s</a> unless stating additionally.'
37+
author: Автор записи
38+
link: Ссылка на запись
39+
license_title: Информация об авторских правах
40+
license_content: 'Все записи на этом сайте защищены лицензией
41+
<a href="%s" rel="external nofollow" target="_blank">%s</a> если не указано дополнительно.'
4242

4343
page:
4444
totally: Всего

layout/_layout.swig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
{% include '_third-party/analytics/lean-analytics.swig' %}
7777
{% include '_third-party/seo/baidu-push.swig' %}
7878
{% include '_third-party/mathjax.swig' %}
79+
{% include '_third-party/scroll-cookie.swig' %}
7980
{% include '_third-party/exturl.swig' %}
8081
</body>
8182
</html>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{% if theme.save_scroll %}
2+
<script type="text/javascript" src="{{ url_for(theme.js) }}/src/js.cookie.js?v={{ theme.version }}"></script>
3+
<script type="text/javascript" src="{{ url_for(theme.js) }}/src/scroll-cookie.js?v={{ theme.version }}"></script>
4+
{% endif %}

source/js/src/js.cookie.js

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*!
2+
* JavaScript Cookie v2.1.4
3+
* https://github.com/js-cookie/js-cookie
4+
*
5+
* Copyright 2006, 2015 Klaus Hartl & Fagner Brack
6+
* Released under the MIT license
7+
*/
8+
;(function (factory) {
9+
var registeredInModuleLoader = false;
10+
if (typeof define === 'function' && define.amd) {
11+
define(factory);
12+
registeredInModuleLoader = true;
13+
}
14+
if (typeof exports === 'object') {
15+
module.exports = factory();
16+
registeredInModuleLoader = true;
17+
}
18+
if (!registeredInModuleLoader) {
19+
var OldCookies = window.Cookies;
20+
var api = window.Cookies = factory();
21+
api.noConflict = function () {
22+
window.Cookies = OldCookies;
23+
return api;
24+
};
25+
}
26+
}(function () {
27+
function extend () {
28+
var i = 0;
29+
var result = {};
30+
for (; i < arguments.length; i++) {
31+
var attributes = arguments[ i ];
32+
for (var key in attributes) {
33+
result[key] = attributes[key];
34+
}
35+
}
36+
return result;
37+
}
38+
39+
function init (converter) {
40+
function api (key, value, attributes) {
41+
var result;
42+
if (typeof document === 'undefined') {
43+
return;
44+
}
45+
46+
// Write
47+
48+
if (arguments.length > 1) {
49+
attributes = extend({
50+
path: '/'
51+
}, api.defaults, attributes);
52+
53+
if (typeof attributes.expires === 'number') {
54+
var expires = new Date();
55+
expires.setMilliseconds(expires.getMilliseconds() + attributes.expires * 864e+5);
56+
attributes.expires = expires;
57+
}
58+
59+
// We're using "expires" because "max-age" is not supported by IE
60+
attributes.expires = attributes.expires ? attributes.expires.toUTCString() : '';
61+
62+
try {
63+
result = JSON.stringify(value);
64+
if (/^[\{\[]/.test(result)) {
65+
value = result;
66+
}
67+
} catch (e) {}
68+
69+
if (!converter.write) {
70+
value = encodeURIComponent(String(value))
71+
.replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);
72+
} else {
73+
value = converter.write(value, key);
74+
}
75+
76+
key = encodeURIComponent(String(key));
77+
key = key.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent);
78+
key = key.replace(/[\(\)]/g, escape);
79+
80+
var stringifiedAttributes = '';
81+
82+
for (var attributeName in attributes) {
83+
if (!attributes[attributeName]) {
84+
continue;
85+
}
86+
stringifiedAttributes += '; ' + attributeName;
87+
if (attributes[attributeName] === true) {
88+
continue;
89+
}
90+
stringifiedAttributes += '=' + attributes[attributeName];
91+
}
92+
return (document.cookie = key + '=' + value + stringifiedAttributes);
93+
}
94+
95+
// Read
96+
97+
if (!key) {
98+
result = {};
99+
}
100+
101+
// To prevent the for loop in the first place assign an empty array
102+
// in case there are no cookies at all. Also prevents odd result when
103+
// calling "get()"
104+
var cookies = document.cookie ? document.cookie.split('; ') : [];
105+
var rdecode = /(%[0-9A-Z]{2})+/g;
106+
var i = 0;
107+
108+
for (; i < cookies.length; i++) {
109+
var parts = cookies[i].split('=');
110+
var cookie = parts.slice(1).join('=');
111+
112+
if (cookie.charAt(0) === '"') {
113+
cookie = cookie.slice(1, -1);
114+
}
115+
116+
try {
117+
var name = parts[0].replace(rdecode, decodeURIComponent);
118+
cookie = converter.read ?
119+
converter.read(cookie, name) : converter(cookie, name) ||
120+
cookie.replace(rdecode, decodeURIComponent);
121+
122+
if (this.json) {
123+
try {
124+
cookie = JSON.parse(cookie);
125+
} catch (e) {}
126+
}
127+
128+
if (key === name) {
129+
result = cookie;
130+
break;
131+
}
132+
133+
if (!key) {
134+
result[name] = cookie;
135+
}
136+
} catch (e) {}
137+
}
138+
139+
return result;
140+
}
141+
142+
api.set = api;
143+
api.get = function (key) {
144+
return api.call(api, key);
145+
};
146+
api.getJSON = function () {
147+
return api.apply({
148+
json: true
149+
}, [].slice.call(arguments));
150+
};
151+
api.defaults = {};
152+
153+
api.remove = function (key, attributes) {
154+
api(key, '', extend(attributes, {
155+
expires: -1
156+
}));
157+
};
158+
159+
api.withConverter = init;
160+
161+
return api;
162+
}
163+
164+
return init(function () {});
165+
}));

source/js/src/scroll-cookie.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
$(document).ready(function() {
2+
3+
// Set relative link path (without domain)
4+
var rpath = window.location.href.replace(window.location.origin, "");
5+
6+
// Write position in cookie
7+
var timeout;
8+
$(window).on("scroll", function() {
9+
clearTimeout(timeout);
10+
timeout = setTimeout(function () {
11+
Cookies.set("scroll-cookie", ($(window).scrollTop() + "|" + rpath), { expires: 365, path: '' });
12+
}, 250);
13+
});
14+
15+
// Read position from cookie
16+
if (Cookies.get("scroll-cookie") !== undefined) {
17+
var cvalues = Cookies.get("scroll-cookie").split('|');
18+
if (cvalues[1] == rpath) {
19+
$(window).scrollTop(cvalues[0]);
20+
}
21+
}
22+
23+
});

source/js/src/utils.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,13 @@ NexT.utils = NexT.$u = {
6464
$top.toggleClass('back-to-top-on', window.pageYOffset > THRESHOLD);
6565

6666
var scrollTop = $(window).scrollTop();
67-
var docHeight = $(document).height();
67+
var docHeight = $('#content').height();
6868
var winHeight = $(window).height();
6969
var scrollPercent = (scrollTop) / (docHeight - winHeight);
7070
var scrollPercentRounded = Math.round(scrollPercent*100);
71-
$('#scrollpercent>span').html(scrollPercentRounded);
71+
var scrollPercentMaxed = (scrollPercentRounded > 100) ? 100 : scrollPercentRounded;
72+
$('#scrollpercent>span').html(scrollPercentMaxed);
73+
});
7274
});
7375

7476
$top.on('click', function () {

0 commit comments

Comments
 (0)