diff options
Diffstat (limited to 'source/js/shikwasa.chapter.min.js')
-rw-r--r-- | source/js/shikwasa.chapter.min.js | 1 |
1 files changed, 1 insertions, 0 deletions
diff --git a/source/js/shikwasa.chapter.min.js b/source/js/shikwasa.chapter.min.js new file mode 100644 index 0000000..070e180 --- /dev/null +++ b/source/js/shikwasa.chapter.min.js @@ -0,0 +1 @@ +(function(a,b){"object"==typeof exports&&"undefined"!=typeof module?module.exports=b():"function"==typeof define&&define.amd?define(b):(a="undefined"==typeof globalThis?a||self:globalThis,a.Chapter=b())})(this,function(){"use strict";var a=Math.round;function b(a,b){var c=Object.keys(a);if(Object.getOwnPropertySymbols){var d=Object.getOwnPropertySymbols(a);b&&(d=d.filter(function(b){return Object.getOwnPropertyDescriptor(a,b).enumerable})),c.push.apply(c,d)}return c}function c(a){for(var c,e=1;e<arguments.length;e++)c=null==arguments[e]?{}:arguments[e],e%2?b(Object(c),!0).forEach(function(b){d(a,b,c[b])}):Object.getOwnPropertyDescriptors?Object.defineProperties(a,Object.getOwnPropertyDescriptors(c)):b(Object(c)).forEach(function(b){Object.defineProperty(a,b,Object.getOwnPropertyDescriptor(c,b))});return a}function d(a,b,c){return b in a?Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}):a[b]=c,a}function e(b){var c=Math.floor;b=a(b);var d=c(b/3600),e=c((b-3600*d)/60),f=c(b-3600*d-60*e);return(e=10>e?"0"+e:e,f=10>f?"0"+f:f,0===d)?"".concat(e,":").concat(f):(d=10>d?"0"+d:d,"".concat(d,":").concat(e,":").concat(f))}function f(a,b){var c=2<arguments.length&&void 0!==arguments[2]?arguments[2]:60,d=b.offsetWidth-a.offsetWidth;if(0<d){a.setAttribute("data-overflow","");var e=b.offsetWidth/c;a.style.animationDuration="".concat(e,"s")}else a.removeAttribute("data-overflow")}function g(a){a.tag=a.tag||"div";var b=document.createElement(a.tag);return a.className&&("string"==typeof a.className?b.classList.add(a.className):a.className.forEach(a=>{b.classList.add(a)})),a.attrs&&Object.keys(a.attrs).forEach(c=>{b.setAttribute(c,a.attrs[c])}),a.innerHTML&&(b.innerHTML=a.innerHTML),b}function h(a,b){return"function"==typeof a.toggleAttribute?void a.toggleAttribute(b):void(a.hasAttribute(b)?a.removeAttribute(b):a.setAttribute(b,""))}function i(a,b,c,d,e,f){var g=(a-b)/1e3/c;f.scrollTop=d+e*g,1>g&&window.requestAnimationFrame(a=>{i(a,b,c,d,e,f)})}var j;class k{constructor(a){this.ctx=a,this.list=[],this.initEvents(),this.current=null,this._currentSrc=null,this._chapterPatched=!1}init(){this.patchPlayer(),this.ui=new l(this.ctx),this.ctx.on("chapterchange",a=>{var b=a&&a.newVal?a.newVal.id:null;this.ui.setChapterActive(b)})}initEvents(){this.ctx.on("audioupdate",a=>{this._chapterPatched||(this.init(),this._chapterPatched=!0),this.updateList(a)}),this.ctx.on("audioparse",a=>{this.updateList(a)}),this.ctx.on("timeupdate",this.onTimeupdate.bind(this))}clearList(){this.ui.chapterList.innerHTML="",this.list=[],this.current=null}updateList(a){this.list.length&&this.clearList(),a.chapters.length&&(this.list=this.handleChapters(a),this.ui.renderChapterList(this.ctx.chapters),this.clickChapterHandler()),this.ui.handleChapterPanel(this.ctx,a)}handleChapters(a){if(a.chapters&&a.chapters.length)return a.chapters.map((a,b)=>(/^ch\d+$/.test(a.id)||(a.id="ch".concat(b)),a))}patchPlayer(){var a=this;Object.defineProperties(this.ctx,{chapters:{get(){return a.list}},currentChapter:{get(){return a.current}}}),this.ctx.events.playerEvents.push("chapterchange"),this.ctx.updateChapter=function(a){this.setCurrent(this.list[a]),this.ctx.seek(this.current.startTime),this.ctx.play()}.bind(a)}setCurrent(a){var b=this.current?c({},this.current):null;this.current=a,this.ctx.events.trigger("chapterchange",{newVal:this.current,oldVal:b})}onTimeupdate(a){if(this._currentSrc!==a.currentTarget.src)return void(this._currentSrc=a.currentTarget.src);var b=this.searchDirection(this.ctx.currentTime,this.current);if(b){var c,d=this.list.indexOf(this.current);c=-1===d?this.list:1===b?this.list.slice(d):this.list.slice(0,d+1);var e=c.find(a=>!this.searchDirection(this.ctx.currentTime,a));this.setCurrent(e)}}searchDirection(b,c){return b=a(b),!c||"object"!=typeof c||c.endTime<=b?1:c.startTime>b?-1:0}clickChapterHandler(){Array.from(this.ui.chapterList.children).forEach(a=>{a.addEventListener("click",()=>{if(this.ctx.seekable){var b=a.getAttribute("data-id").match(/\d+$/);b&&this.ctx.updateChapter(+b[0])}})})}destroy(){this.ui.destroy()}}class l{constructor(a,b){this.initEl(a),this.initEvents(a,b),this.renderChapterList(a.chapters),a.ui.el.append(this.el),this.activeChapterEl=null}initEl(a){this.el=g({className:"shk-chapter",innerHTML:"\n <div class=\"shk-chapter_main\">\n <ol class=\"shk-chapter_list\"></ol>\n </div>\n <button class=\"shk-btn shk-btn_close\" aria-label=\"close chapter panel\" title=\"close chapter panel\">\n <svg class=\"shk-icon_close\" aria-hidden=\"true\">\n <use xlink:href=\"#shk-icon_close\" />\n </svg>\n </button>\n"});var b={title:"view chapters","aria-label":"view chapters"};a.seekable||(b.disabled=""),this.chapterBtn=g({tag:"button",className:["shk-btn","shk-btn_chapter"],attrs:b,innerHTML:"\n <svg aria-hidden=\"true\">\n <use xlink:href=\"#shk-icon_chapter\" />\n </svg>\n "}),a.ui.seekControls.push(this.chapterBtn),a.ui.extraControls.append(this.chapterBtn),this.closeBtn=this.el.querySelector(".shk-btn_close"),this.chapterList=this.el.querySelector(".shk-chapter_list"),this.overflowLayer=this.el.querySelector(".shk-chapter_main")}initEvents(a){this.chapterBtn.addEventListener("click",()=>{h(a.el,"data-show-chapter")}),a.ui.hideExtraControl(this.chapterBtn),this.closeBtn.addEventListener("click",()=>{a.el.removeAttribute("data-show-chapter")}),j=()=>{if(this.activeChapterEl){var a=this.activeChapterEl.querySelector(".shk-chapter_title_wrap"),b=this.activeChapterEl.querySelector(".shk-chapter_title");f.call(this,a,b)}},window.addEventListener("resize",j)}handleChapterPanel(a,b){b.chapters.length?a.el.setAttribute("data-has-chapter",""):a.el.removeAttribute("data-has-chapter"),b.chapters.length&&a.seekable||a.el.removeAttribute("data-show-chapter")}renderChapterList(a){a.length&&a.forEach(a=>{var b=this.renderChapterItem(a);this.chapterList.append(b)})}renderChapterItem(a){var b=e(a.startTime),c="\n <button class=\"shk-btn shk-chapter_btn\" title=\"seek chapter: ".concat(a.title,"\" aria-label=\"seek chapter: ").concat(a.title,"\">\n <div class=\"shk-icon_chapter\" aria-hidden=\"true\">\n <span class=\"shk-icon_playing\"></span>\n <span class=\"shk-icon_triangle\">\n <svg>\n <use xlink:href=\"#shk-icon_triangle\" />\n </svg>\n </span>\n </div>\n <div class=\"shk-chapter_duration\">").concat(b,"</div>\n <div class=\"shk-chapter_title_wrap\">\n <div class=\"shk-chapter_title_inner\" data-chapter=\"").concat(a.title,"\">\n <div class=\"shk-chapter_title\">").concat(a.title,"</div>\n </div>\n </div>\n </button>\n ");return g({tag:"li",className:"shk-chapter_item",innerHTML:c,attrs:{"data-id":a.id}})}setChapterActive(a){this.chapterList.querySelectorAll(".shk-chapter_item").forEach(b=>{if(b.getAttribute("data-id")===a){b.setAttribute("data-active",""),this.scrollIntoView(b),this.activeChapterEl=b;var c=b.querySelector(".shk-chapter_title"),d=b.querySelector(".shk-chapter_title_wrap");f(d,c)}else b.removeAttribute("data-active")})}scrollIntoView(a){if("hidden"!==this.el.style.visibility){var b=window.getComputedStyle(this.overflowLayer).marginTop,c=window.getComputedStyle(this.chapterList).marginTop,d=parseInt(b)+parseInt(c),e=0<this.overflowLayer.scrollTop+d-a.offsetTop||0<a.offsetTop-this.overflowLayer.scrollTop-this.overflowLayer.offsetHeight,f=this.overflowLayer.scrollTop,g=a.offsetTop-f-d,h=performance.now();e&&i(h,h,.2,f,g,this.overflowLayer)}}destroy(){window.removeEventListener("resize",j)}}return k._name="chapter",k});
\ No newline at end of file |