1
0
mirror of https://github.com/ruanyf/es6tutorial.git synced 2025-05-25 11:12:21 +00:00

Merge pull request #8 from wenzhixin/gh-pages

增加保存阅读进度功能
This commit is contained in:
ruanyf 2014-05-07 22:27:14 +08:00
commit 8c7635e698
3 changed files with 98 additions and 88 deletions

View File

@ -8,6 +8,7 @@
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.min.js"></script>
<script src="js/marked.js"></script>
<script src="js/store.js"></script>
<script src="js/ditto.js"></script>
<script src="js/prism.js"></script>
<link rel="stylesheet" href="app/bower_components/normalize-css/normalize.css">
@ -34,7 +35,7 @@
ditto.sidebar_file = "sidebar.md",
ditto.document_title = "ECMAScript 6入门",
// where the docs are actually stored on github - so you can edit
ditto.base_url = "https://github.com/ruanyf/es6tutorial/edit/gh-pages/"; // <------- EDIT ME!!
ditto.base_url = "https://github.com/ruanyf/es6tutorial/edit/gh-pages"; // <------- EDIT ME!!
// run
ditto.run();

View File

@ -11,13 +11,14 @@ var ditto = {
sidebar: true,
edit_button: true,
back_to_top_button: true,
save_progress: true, // 保存阅读进度
// initialize function
run: initialize
};
var disqusCode = '<h3>留言</h3><div id="disqus_thread"></div>';
var menu=new Array();
var menu = new Array();
function initialize() {
// initialize sidebar and buttons
@ -41,27 +42,27 @@ function initialize() {
function init_sidebar_section() {
$.get(ditto.sidebar_file, function(data) {
$(ditto.sidebar_id).html(marked(data));
// 初始化内容数组
var menuOL = $(ditto.sidebar_id+' ol');
menuOL.attr('start',0);
// 初始化内容数组
var menuOL = $(ditto.sidebar_id + ' ol');
menuOL.attr('start', 0);
menuOL.find('li a').map(function(){
menu.push(this.href.slice(this.href.indexOf('#')));
});
$('#pageup').on('click',function (){
for (var i=0;i<menu.length;i++){
if (location.hash==='') break;
if (menu[i]===location.hash) break;
}
location.hash = menu[i-1]
});
$('#pagedown').on('click',function (){
for (var i=0;i<menu.length;i++){
if (location.hash==='') break;
if (menu[i]===location.hash) break;
}
location.hash = menu[i+1];
});
menuOL.find('li a').map(function() {
menu.push(this.href.slice(this.href.indexOf('#')));
});
$('#pageup').on('click', function() {
for (var i = 0; i < menu.length; i++) {
if (location.hash === '') break;
if (menu[i] === location.hash) break;
}
location.hash = menu[i - 1]
});
$('#pagedown').on('click', function() {
for (var i = 0; i < menu.length; i++) {
if (location.hash === '') break;
if (menu[i] === location.hash) break;
}
location.hash = menu[i + 1];
});
}, "text").fail(function() {
alert("Opps! can't find the sidebar file to display!");
});
@ -139,20 +140,20 @@ function create_page_anchors() {
$('#content h' + i).map(function() {
headers.push($(this).text());
$(this).addClass(replace_symbols($(this).text()));
this.id = 'h'+i+'-'+replace_symbols($(this).text());
this.id = 'h' + i + '-' + replace_symbols($(this).text());
});
if ((i === 2) && headers.length !== 0){
var ul_tag = $('<ol></ol>')
.insertAfter('#content h1')
.addClass("content-toc")
.attr('id','content-toc');
for (var j = 0; j < headers.length; j++) {
if ((i === 2) && headers.length !== 0) {
var ul_tag = $('<ol></ol>')
.insertAfter('#content h1')
.addClass("content-toc")
.attr('id', 'content-toc');
for (var j = 0; j < headers.length; j++) {
var li_tag = $('<li></li>').text(headers[j]);
ul_tag.append(li_tag);
ul_tag.append(li_tag);
li_create_linkage(li_tag, i);
}
}
}
}
}
}
@ -195,6 +196,11 @@ function router() {
var path = location.hash.replace("#", "./");
if (ditto.save_progress && store.get('menu-progress') !== location.hash) {
store.set('menu-progress', location.hash);
store.set('page-progress', 0);
}
// default page if hash is empty
if (location.pathname === "/index.html") {
path = location.pathname.replace("index.html", ditto.index);
@ -208,79 +214,80 @@ function router() {
// otherwise get the markdown and render it
var loading = show_loading();
$.get(path , function(data) {
$.get(path, function(data) {
$(ditto.error_id).hide();
$(ditto.content_id).html(marked(data)+disqusCode);
if ($(ditto.content_id+" h1").text() === ditto.document_title){
document.title = ditto.document_title;
} else {
document.title = $(ditto.content_id+" h1").text() + " - " + ditto.document_title;
}
$(ditto.content_id).html(marked(data) + disqusCode);
if ($(ditto.content_id + " h1").text() === ditto.document_title) {
document.title = ditto.document_title;
} else {
document.title = $(ditto.content_id + " h1").text() + " - " + ditto.document_title;
}
normalize_paths();
create_page_anchors();
// 完成代码高亮
$('#content code').map(function() {
// 完成代码高亮
$('#content code').map(function() {
Prism.highlightElement(this);
});
// 加载disqus
(function () {
// http://docs.disqus.com/help/2/
window.disqus_shortname = 'es6';
window.disqus_identifier = (location.hash?location.hash.replace("#", ""):'READEME');
window.disqus_title =$(ditto.content_id+" h1").text();
window.disqus_url = 'http://es6.ruanyifeng.com/' + (location.hash?location.hash.replace("#", ""):'README');
// 加载disqus
(function() {
// http://docs.disqus.com/help/2/
window.disqus_shortname = 'es6';
window.disqus_identifier = (location.hash ? location.hash.replace("#", "") : 'READEME');
window.disqus_title = $(ditto.content_id + " h1").text();
window.disqus_url = 'http://es6.ruanyifeng.com/' + (location.hash ? location.hash.replace("#", "") : 'README');
// http://docs.disqus.com/developers/universal/
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://'+window.disqus_shortname+'.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
})();
// http://docs.disqus.com/developers/universal/
(function() {
var dsq = document.createElement('script');
dsq.type = 'text/javascript';
dsq.async = true;
dsq.src = 'http://' + window.disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
})();
if(location.hash !== ''){
$('html, body').animate({
scrollTop: $('#content').offset().top
}, 200);
}
if (location.hash === '' || location.hash === menu[0]) {
$('#pageup').css('display', 'none');
} else {
$('#pageup').css('display', 'inline-block');
}
if(location.hash==='' || location.hash===menu[0]){
$('#pageup').css('display','none');
}else{
$('#pageup').css('display','inline-block');
}
if (location.hash === menu[(menu.length - 1)]) {
$('#pagedown').css('display', 'none');
} else {
$('#pagedown').css('display', 'inline-block');
}
if (location.hash===menu[(menu.length-1)]){
$('#pagedown').css('display','none');
}else{
$('#pagedown').css('display','inline-block');
}
(function() {
var $w = $(window);
var $prog2 = $('.progress-indicator-2');
var wh = $w.height();
var h = $('body').height();
var sHeight = h - wh;
var perc = ditto.save_progress ? store.get('page-progress') || 0 : 0;
(function(){
var $w = $(window);
var $prog2 = $('.progress-indicator-2');
var wh = $w.height();
var h = $('body').height();
var sHeight = h - wh;
$w.off('scroll');
$w.on('scroll', function(){
var perc = Math.max(0, Math.min(1, $w.scrollTop()/sHeight));
updateProgress(perc);
});
$w.off('scroll').on('scroll', function() {
var perc = Math.max(0, Math.min(1, $w.scrollTop() / sHeight));
updateProgress(perc);
});
function updateProgress(perc){
$prog2.css({width : perc*100 + '%'});
}
function updateProgress(perc) {
$prog2.css({width: perc * 100 + '%'});
ditto.save_progress && store.set('page-progress', perc);
}
}());
updateProgress(perc);
$('html, body').animate({
scrollTop: sHeight * perc
}, 200);
}());
}).fail(function() {
show_error();
}).always(function() {
clearInterval(loading);
$(ditto.loading_id).hide();
});
});
}

2
js/store.js Normal file

File diff suppressed because one or more lines are too long