diff --git a/lib/docsify.js b/lib/docsify.js index 415fb8b..487c5f2 100644 --- a/lib/docsify.js +++ b/lib/docsify.js @@ -4,11 +4,12 @@ var Docsify = (function () { /** * Simple ajax * @param {String} url - * @param {String} [method=get] + * @param {String} [method=GET] + * @param {Function} [loading] handle loading * @return {Promise} */ -function load (url, method) { - if ( method === void 0 ) method = 'get'; +function load (url, method, loading) { + if ( method === void 0 ) method = 'GET'; var xhr = new XMLHttpRequest(); @@ -19,13 +20,18 @@ function load (url, method) { then: function (success, error) { if ( error === void 0 ) error = function () {}; + if (loading) { + xhr.addEventListener('progress', loading); + xhr.addEventListener('loaded', loading); + } xhr.addEventListener('error', error); xhr.addEventListener('load', function (ref) { var target = ref.target; target.status >= 400 ? error(target) : success(target.response); }); - } + }, + abort: function () { return xhr.readyState !== 4 && xhr.abort(); } } } @@ -191,6 +197,12 @@ function bindToggle (dom) { }); } +var cacheContentDOM; +function scroll2Top (dom) { + cacheContentDOM = cacheContentDOM || document.querySelector(dom); + cacheContentDOM.scrollTop = 0; +} + var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; @@ -2399,6 +2411,8 @@ function renderArticle (content) { if (!renderNavbar.rendered) { renderNavbar(null, OPTIONS$1); } renderSidebar.rendered = false; renderNavbar.rendered = false; + + if (OPTIONS$1.auto2top) { scroll2Top('section.content'); } } /** @@ -2436,6 +2450,30 @@ function renderSidebar (content) { toc = []; } +/** + * render loading bar + * @return {[type]} [description] + */ +function renderLoading (ref) { + var loaded = ref.loaded; + var total = ref.total; + + var num = Math.floor(loaded / total * 100); + + if (!CACHE['loading']) { + var div = document.createElement('div'); + + div.classList.add('progress'); + document.body.appendChild(div); + CACHE['loading'] = div; + } + CACHE['loading'].style.width = num >= 95 ? '0%' : num + '%'; +} + +/** + * Load Config + * @param {Object} options + */ function config (options) { OPTIONS$1 = options; } @@ -2448,7 +2486,8 @@ var OPTIONS = { sidebarToggle: false, loadSidebar: null, loadNavbar: null, - router: false + router: false, + auto2top: false }; var script = document.currentScript || [].slice.call(document.getElementsByTagName('script')).pop(); @@ -2467,6 +2506,7 @@ if (script) { config(OPTIONS); var cacheRoute = null; +var cacheXhr = null; var mainRender = function (cb) { var route = getRoute(); @@ -2480,17 +2520,22 @@ var mainRender = function (cb) { basePath = basePath.match(/(\S*\/)[^\/]+$/)[1]; } + cacheXhr && cacheXhr.abort && cacheXhr.abort(); // Render markdown file - load((!route || /\/$/.test(route)) ? (route + "README.md") : (route + ".md")) - .then(function (result) { - renderArticle(result); - if (OPTIONS.loadSidebar) { - if (wait === false) { cb(); } - wait = false; - } else { - cb(); - } - }, function (_) { return renderArticle(null); }); + cacheXhr = load( + (!route || /\/$/.test(route)) ? (route + "README.md") : (route + ".md"), + 'GET', + renderLoading); + + cacheXhr.then(function (result) { + renderArticle(result); + if (OPTIONS.loadSidebar) { + if (wait === false) { cb(); } + wait = false; + } else { + cb(); + } + }, function (_) { return renderArticle(null); }); // Render sidebar if (OPTIONS.loadSidebar) { diff --git a/lib/docsify.min.js b/lib/docsify.min.js index 441baac..dddb056 100644 --- a/lib/docsify.min.js +++ b/lib/docsify.min.js @@ -1,2 +1,2 @@ -var Docsify=function(){"use strict";function e(e,t){void 0===t&&(t="get");var n=new XMLHttpRequest;return n.open(t,e),n.send(),{then:function(e,t){void 0===t&&(t=function(){}),n.addEventListener("error",t),n.addEventListener("load",function(n){var r=n.target;r.status>=400?t(r):e(r.response)})}}}function t(e,t){var n=[],r={};return e.forEach(function(e){var i=e.level||1,a=i-1;i>t||(r[a]?(r[a].children=r[a].children||[],r[a].children.push(e)):n.push(e),r[i]=e)}),n}function n(e){return e.replace(/([A-Z])/g,function(e){return"-"+e.toLowerCase()})}function r(e){return null===e||void 0===e}function i(){var e=window.location;if(v===e.hash&&!r(k))return k;var t=e.hash.match(/^#\/([^#]+)/);return t=t&&2===t.length?t[1]:/^#\//.test(e.hash)?"":e.pathname,k=t,v=e.hash,t}function a(){function e(){for(var e=0,r=t.length;e10){var o=n[a.id];if(!o||o===i)return;return i&&i.setAttribute("class",""),o.setAttribute("class","active"),void(i=o)}}}if(!/mobile/i.test(navigator.userAgent)){for(var t=document.querySelectorAll(".anchor"),n={},r=document.querySelectorAll(".sidebar li"),i=null,a=0,s=r.length;a\n \n '):""}function p(){return'
\n \n
\n
\n
\n
'}function g(e){return e?'':""}function h(e,t){return void 0===t&&(t=""),e&&e.length?(e.forEach(function(e){t+='
  • '+e.title+"
  • ",e.children&&(t+='
    • '+h(e.children)+"
    ")}),t):""}function d(e,t){var n=document.querySelector("nav")||document.createElement("nav");e[t?"outerHTML":"innerHTML"]=g(_.sidebarToggle)+c(_.repo)+p(),document.body.insertBefore(n,document.body.children[0]),l("button.sidebar-toggle")}function f(e){C("article",e?w(e):"not found"),b.rendered||b(null,_),m.rendered||m(null,_),b.rendered=!1,m.rendered=!1}function m(e){L.navbar&&L.navbar===e||(L.navbar=e,m.rendered=!0,e&&C("nav",w(e)),o("nav"))}function b(e){var n=!1;e?e=w(e):_.sidebar?e=h(_.sidebar,"