You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

1 lines
5.1 KiB

"use strict";function _defineProperty(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function asyncGeneratorStep(e,t,r,n,a,o,c){try{var i=e[o](c),u=i.value}catch(e){return void r(e)}i.done?t(u):Promise.resolve(u).then(n,a)}function _asyncToGenerator(i){return function(){var e=this,c=arguments;return new Promise(function(t,r){var n=i.apply(e,c);function a(e){asyncGeneratorStep(n,t,r,a,o,"next",e)}function o(e){asyncGeneratorStep(n,t,r,a,o,"throw",e)}a(void 0)})}}function _toConsumableArray(e){return _arrayWithoutHoles(e)||_iterableToArray(e)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance")}function _iterableToArray(e){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e))return Array.from(e)}function _arrayWithoutHoles(e){if(Array.isArray(e)){for(var t=0,r=new Array(e.length);t<e.length;t++)r[t]=e[t];return r}}function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function _createClass(e,t,r){return t&&_defineProperties(e.prototype,t),r&&_defineProperties(e,r),e}!function(){var e=function(){function n(e){_classCallCheck(this,n);var t={target:"a",container:"body"};for(var r in t)t.hasOwnProperty(r)&&!e.hasOwnProperty(r)&&(e[r]=t[r]);this.option=e,this.init(e)}var t;return _createClass(n,[{key:"init",value:function(o){var e=_toConsumableArray(document.querySelectorAll(o.target)),c=this;e.forEach(function(e){var a=e.href,t=document.location.origin,r=a.lastIndexOf("/"),n=a.substring(r+1,a.length);if(!a.match(t)||/#/.test(n))return null;e.onclick=function(){var t=_asyncToGenerator(regeneratorRuntime.mark(function e(t){var r,n;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return(t||window.event).preventDefault(),e.next=4,c.fetchData(a);case 4:return n=e.sent,r=c.parseHtml(n.data,o.body),c.replaceHeader(r.head),c.replaceContainer(o.container,r.container),c.appendScriptTobodyEnd(r.scriptWrap),c.pushState(null,null,a),e.abrupt("return",void 0);case 11:case"end":return e.stop()}},e,this)}));return function(e){return t.apply(this,arguments)}}()}),window.addEventListener("popstate",function(){var t=_asyncToGenerator(regeneratorRuntime.mark(function e(t){var r,n,a;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.target.location.href,e.next=3,c.fetchData(r);case 3:n=e.sent,a=c.parseHtml(n.data,o.body),c.replaceHeader(a.head),c.replaceContainer(o.container,a.container);case 7:case"end":return e.stop()}},e,this)}));return function(e){return t.apply(this,arguments)}}())}},{key:"fetchData",value:(t=_asyncToGenerator(regeneratorRuntime.mark(function e(t){var r;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,fetch(t).then(function(e){return r=e.text()}).then(function(e){r=e}).catch(function(e){e=e});case 2:return e.abrupt("return",{data:r,err:void 0});case 3:case"end":return e.stop()}},e,this)})),function(e){return t.apply(this,arguments)})},{key:"parseHtml",value:function(e,t){var r=new RegExp("(?<=<head>).*|[\\s\\S]*(?=</head>)"),n=document.createElement("div"),a=document.createElement("div");a.innerHTML=e;var o=a.querySelector(t),c=o.querySelectorAll("script");return _toConsumableArray(c).forEach(function(e){o.contains(e)&&e.parentElement.removeChild(e)}),n.innerHTML=_toConsumableArray(e.match(r)).join(),{head:n,container:o,scriptWrap:c}}},{key:"replaceHeader",value:function(e){var t=_toConsumableArray(e.children);document.head.innerHTML=null,t.map(function(e,t){document.head.append(e)})}},{key:"replaceContainer",value:function(e,r){var n=this;if("string"==typeof e){var t=document.querySelector(e),a=this.getDomAttr(t),o=r.querySelector(e);a.map(function(e){var t=Object.keys(e)[0];"class"==e[t]?o.className=e[t]:r[t]=e[t]}),t.parentElement.replaceChild(o,t)}if(Array.isArray(e)){var c=[],i=[],u=[];e.forEach(function(e){var t=document.querySelector(e);c.push(t),i.push(n.getDomAttr(t)),u.push(r.querySelector(e))}),i.forEach(function(e,r){e.forEach(function(e){var t=Object.keys(e)[0];"class"==e[t]?u[r].className=e[t]:u[r][t]=e[t]})}),c.forEach(function(e,t){e.parentElement.replaceChild(u[t],e)})}}},{key:"appendScriptTobodyEnd",value:function(e){document.querySelector(this.option.body).querySelectorAll("script").forEach(function(e){e.parentElement.removeChild(e)}),document.body.querySelectorAll("script").forEach(function(e){e.parentElement.removeChild(e)}),document.body.removeChild,_toConsumableArray(e).forEach(function(e){var t=document.createElement("script");e.getAttribute("src")?t.src=e.src:t.text=e.text,document.body.append(t)})}},{key:"pushState",value:function(e,t,r){history.pushState(e,t,r)}},{key:"getDomAttr",value:function(e){var t,r,n=[],a=e.attributes;for(t=0,r=a.length;t<r;t++)a[t].specified&&n.push(_defineProperty({},a[t].nodeName,a[t].nodeValue));return n}}]),n}();"undefined"!=typeof module&&void 0!==module.exports?module.exports=e:window.miniPjax=e}();