mirror of
https://github.com/futurepress/epub.js.git
synced 2025-10-03 14:59:18 +02:00
2 lines
No EOL
28 KiB
JavaScript
2 lines
No EOL
28 KiB
JavaScript
/*! FuturePress - v0.1.0 - 2013-03-25 */
|
|
var FP=FP||{};FP.VERSION="0.1.4",FP.plugins=FP.plugins||{},FP.filePath=FP.filePath||"/fpjs/",FP.Book=function(e,t){typeof e=="string"?this.el=FP.core.getEl(e):this.el=e,this.events=new FP.Events(this,this.el),this.createEvent("book:tocReady"),this.createEvent("book:metadataReady"),this.createEvent("book:spineReady"),this.createEvent("book:bookReady"),this.createEvent("book:chapterReady"),this.createEvent("book:chapterDisplayed"),this.createEvent("book:chapterDestroy"),this.createEvent("book:resized"),this.createEvent("book:stored"),this.createEvent("book:online"),this.createEvent("book:offline"),this.createEvent("book:pageChanged"),this.hooks={beforeChapterDisplay:[]},this.getHooks(),this.useHash=!0,this.initialize(this.el),this.online=navigator.onLine,this.listeners(),this.determineStorageMethod(),t&&this.display(t)},FP.Book.prototype.initialize=function(e){this.iframe=document.createElement("iframe"),this.resizeIframe(!1,this.el.clientWidth,this.el.clientHeight),this.listen("book:resized",this.resizeIframe,this),this.el.appendChild(this.iframe)},FP.Book.prototype.listeners=function(){var e=this;window.addEventListener("resize",e.onResized.bind(this),!1),window.addEventListener("offline",function(t){e.online=!1,e.tell("book:offline")},!1),window.addEventListener("online",function(t){e.online=!0,e.tell("book:online")},!1),window.addEventListener("hashchange",e.route.bind(this),!1)},FP.Book.prototype.start=function(e){var t=window.location,n=t.pathname,r=this.bookUrl.search("://")!=-1,i=n[0]=="/",s=[],o="/",u,a;if(this.isContained(e)){this.bookPath=e,this.bookUrl="",this.contained=!0,this.tell("book:offline"),this.online&&this.unarchive(e);return}this.bookPath=e,u=t.origin||t.protocol+"//"+t.host,r&&(this.bookUrl=e),!r&&i&&(this.bookUrl=u+e),!r&&!i&&(n.split("/").forEach(function(e){e.indexOf(".")==-1&&s.push(e)}),o=s.join("/")+"/",this.bookUrl=u+o+e);if(!this.isSaved()){if(!this.online){console.error("Not Online");return}this.parseContainer()}else this.tell("book:tocReady"),this.tell("book:metadataReady"),this.tell("book:spineReady"),this.startDisplay()},FP.Book.prototype.unarchive=function(e){var t;this.isSaved()?(this.tell("book:tocReady"),this.tell("book:metadataReady"),this.tell("book:spineReady"),this.startDisplay()):t=new FP.Unarchiver(e,function(){FP.storage.get("META-INF/container.xml",function(e){this.parseContainer(e)}.bind(this))}.bind(this))},FP.Book.prototype.isSaved=function(e){return localStorage.getItem("bookPath")!=this.bookPath||localStorage.getItem("fpjs-version")!=FP.VERSION||e==1?(localStorage.setItem("fpjs-version",FP.VERSION),localStorage.setItem("bookPath",this.bookPath),localStorage.setItem("stored",0),localStorage.setItem("spinePos",0),localStorage.setItem("chapterPos",0),localStorage.setItem("displayedPages",0),this.spinePos=0,this.stored=0,!1):(this.spinePos=parseInt(localStorage.getItem("spinePos"))||0,this.stored=parseInt(localStorage.getItem("stored"))||0,this.basePath=localStorage.getItem("basePath"),this.contentsPath=localStorage.getItem("contentsPath"),this.metadata=JSON.parse(localStorage.getItem("metadata")),this.assets=JSON.parse(localStorage.getItem("assets")),this.spine=JSON.parse(localStorage.getItem("spine")),this.spineIndexByURL=JSON.parse(localStorage.getItem("spineIndexByURL")),this.toc=JSON.parse(localStorage.getItem("toc")),this.prevChapterPos=parseInt(localStorage.getItem("chapterPos")),this.prevDisplayedPages=parseInt(localStorage.getItem("displayedPages")),!this.assets||!this.spine||!this.spineIndexByURL||!this.toc?(this.stored=0,!1):!0)},FP.Book.prototype.isContained=function(e){var t=/\.[0-9a-z]+$/i,n=t.exec(e);return!n||n[0]!=".epub"&&n[0]!=".zip"?!1:!0},FP.Book.prototype.onResized=function(){this.tell("book:resized",{width:this.el.clientWidth,height:this.el.clientHeight})},FP.Book.prototype.resizeIframe=function(e,t,n){var r,i;e?(r=e.msg.width,i=e.msg.height):(r=t,i=n),this.iframe.height=i,r%2!=0&&(r+=1),this.iframe.width=r},FP.Book.prototype.parseContainer=function(e){var t=this,n=e||this.bookUrl+"META-INF/container.xml";FP.core.loadXML(n,function(e){var n;rootfile=e.querySelector("rootfile"),n=rootfile.getAttribute("full-path").split("/"),n[1]?(t.basePath=t.bookUrl+n[0]+"/",t.contentsPath=t.basePath+n[1]):(t.basePath=t.bookUrl,t.contentsPath=t.bookUrl+n),t.contained&&(t.basePath=n[0]+"/"),localStorage.setItem("basePath",t.basePath),localStorage.setItem("contentsPath",t.contentsPath),t.contained?FP.storage.get(t.contentsPath,function(e){t.parseContents(e)}):t.parseContents()})},FP.Book.prototype.parseContents=function(e){var t=this,n=e||this.contentsPath;FP.core.loadXML(n,function(e){var n=e.querySelector("metadata"),r=e.querySelector("manifest"),i=e.querySelector("spine");t.parseMetadata(n),t.parseManifest(r),t.parseSpine(i),t.startDisplay()})},FP.Book.prototype.parseMetadata=function(e){var t=this,n=e.getElementsByTagNameNS("http://purl.org/dc/elements/1.1/","title")[0];creator=e.getElementsByTagNameNS("http://purl.org/dc/elements/1.1/","creator")[0],this.metadata={},this.metadata.bookTitle=n?n.childNodes[0].nodeValue:"",this.metadata.creator=creator?creator.childNodes[0].nodeValue:"",localStorage.setItem("metadata",JSON.stringify(this.metadata)),this.tell("book:metadataReady")},FP.Book.prototype.parseManifest=function(e){var t=this;this.assets={},items=Array.prototype.slice.call(e.querySelectorAll("item")),items.forEach(function(e){var n=e.getAttribute("id"),r=e.getAttribute("href");t.assets[n]=t.basePath+r,e.getAttribute("media-type")=="application/x-dtbncx+xml"&&(t.contained?FP.storage.get(t.basePath+r,function(e){t.parseTOC(e)}):t.parseTOC(t.basePath+r))}),localStorage.setItem("assets",JSON.stringify(this.assets))},FP.Book.prototype.parseSpine=function(e){var t=this;this.spine=[],this.spineIndexByID={},this.spineIndexByURL={},items=Array.prototype.slice.call(e.getElementsByTagName("itemref")),items.forEach(function(e,n){var r=e.getAttribute("idref"),i=t.assets[r];t.spine.push({id:r,href:i}),t.spineIndexByID[r]=n,t.spineIndexByURL[i]=n}),localStorage.setItem("spine",JSON.stringify(this.spine)),localStorage.setItem("spineIndexByURL",JSON.stringify(this.spineIndexByURL)),this.tell("book:spineReady")},FP.Book.prototype.parseTOC=function(e){var t=this,n=e;this.toc=[],FP.core.loadXML(n,function(e){function s(e,t){var n=[];return items=Array.prototype.slice.call(e),items.forEach(function(e){var r=e.getAttribute("id"),i=e.querySelector("content"),o=i.getAttribute("src"),u=o.split("#"),a=e.querySelector("navLabel"),f=a.textContent?a.textContent:"",l=e.querySelectorAll("navPoint")||!1,c=!1,h=e.parentNode==t;if(!h)return;l&&(c=s(l,e)),n.push({id:r,href:o,label:f,subitems:c||!1})}),n}var n=e.querySelector("navMap"),r=e.querySelector("meta[name='cover']"),i;r&&(i=r.getAttribute("content"),t.toc.push({id:i,href:t.assets[i],label:i})),t.toc=t.toc.concat(s(n.querySelectorAll("navPoint"),n)),localStorage.setItem("toc",JSON.stringify(t.toc)),t.tell("book:tocReady")})},FP.Book.prototype.destroy=function(){window.removeEventListener("resize",this.onResized,!1)},FP.Book.prototype.getTitle=function(){return this.metadata.bookTitle},FP.Book.prototype.getCreator=function(){return this.metadata.creator},FP.Book.prototype.chapterTitle=function(){return this.spine[this.spinePos].id},FP.Book.prototype.startDisplay=function(e){var t,n=this.spinePos,r=function(e){this.prevChapterPos&&n==e.pos&&this.prevDisplayedPages==e.displayedPages&&e.page(this.prevChapterPos),this.online&&!this.contained&&this.storeOffline()}.bind(this);this.tell("book:bookReady"),this.useHash&&(t=this.route(!1,r)),(!this.useHash||!t)&&this.displayChapter(this.spinePos,r)},FP.Book.prototype.show=function(e,t){var n=e.split("#"),r=n[0],i=n[1]||!1,s=r.search("://")==-1?this.basePath+r:r,o=this.spineIndexByURL[s];r||(o=this.spinePos);if(typeof o!="number")return!1;o!=this.spinePos||!this.currentChapter?this.displayChapter(o,function(e){i&&e.section(i),t&&t(e)}):(i&&this.currentChapter.section(i),t&&t(this.currentChapter))},FP.Book.prototype.displayChapter=function(e,t){var n=this;if(e>=this.spine.length)return console.log("Reached End of Book"),!1;if(e<0)return console.log("Reached Start of Book"),!1;localStorage.setItem("spinePos",e),this.spinePos=e,this.currentChapter&&this.tell("book:chapterDestroy",this.currentChapter.getID()),this.currentChapter=new FP.Chapter(this),this.currentChapter.afterLoaded=function(e){n.tell("book:chapterReady",e.getID()),t&&t(e)}},FP.Book.prototype.nextPage=function(){var e=this.currentChapter.nextPage();e||this.nextChapter()},FP.Book.prototype.prevPage=function(){var e=this.currentChapter.prevPage();e||this.prevChapter()},FP.Book.prototype.nextChapter=function(){this.spinePos++,this.displayChapter(this.spinePos)},FP.Book.prototype.prevChapter=function(){this.spinePos--,this.displayChapter(this.spinePos,function(e){e.goToChapterEnd()})},FP.Book.prototype.getTOC=function(){return this.toc},FP.Book.prototype.storeOffline=function(e){var t=FP.core.toArray(this.assets);FP.storage.batch(t,function(){this.stored=1,localStorage.setItem("stored",1),this.tell("book:stored"),e&&e()}.bind(this))},FP.Book.prototype.availableOffline=function(){return this.stored>0?!0:!1},FP.Book.prototype.fromStorage=function(e){if(this.contained)return;e?this.availableOffline?(this.online=!1,this.tell("book:offline")):this.storeOffline(function(){this.online=!1,this.tell("book:offline")}.bind(this)):(this.online=!0,this.tell("book:online"))},FP.Book.prototype.determineStorageMethod=function(e){var t="ram";e?t=e:(Modernizr.websqldatabase&&(t="websql"),Modernizr.indexeddb&&(t="indexedDB"),Modernizr.filesystem&&(t="filesystem")),FP.storage.storageMethod(t)},FP.Book.prototype.route=function(e,t){var n=window.location.hash.replace("#/","");return this.useHash&&n.length&&n!=this.prevLocation?(this.show(n,t),this.prevLocation=n,!0):!1},FP.Book.prototype.hideHashChanges=function(){this.useHash=!1},FP.Book.prototype.getHooks=function(){var e=this;plugTypes=FP.core.toArray(this.hooks),plugTypes.forEach(function(t){var n=t.ident;plugs=FP.core.toArray(FP.Hooks[n]),plugs.forEach(function(t){e.registerHook(n,t)})})},FP.Book.prototype.registerHook=function(e,t){var n=this;typeof this.hooks[e]!="undefined"?typeof t=="function"?this.hooks[e].push(t):Array.isArray(t)&&t.forEach(function(t){n.hooks[e].push(t)}):this.hooks[e]=[func]},FP.Book.prototype.triggerHooks=function(e,t,n){function s(){i--,i<=0&&t&&t()}var r,i;if(typeof this.hooks[e]=="undefined")return!1;r=this.hooks[e],i=r.length,r.forEach(function(e){e(s,n)})},FP.Chapter=function(e,t){return this.book=e,this.iframe=this.book.iframe,this.pos=t||this.book.spinePos,this.chapInfo=this.book.spine[this.pos],this.path=this.chapInfo.href,this.ID=this.chapInfo.id,this.chapterPos=1,this.leftPos=0,localStorage.setItem("chapterPos",this.chapterPos),this.book.registerHook("beforeChapterDisplay",[this.replaceLinks.bind(this),this.replaceResources.bind(this)]),this.load(),this},FP.Chapter.prototype.load=function(){var e=this.path;this.book.online&&!this.book.contained?this.setIframeSrc(e):this.loadFromStorage(e)},FP.Chapter.prototype.loadFromStorage=function(e){var t=FP.storage.get(e,this.setIframeSrc.bind(this))},FP.Chapter.prototype.setIframeSrc=function(e){var t=this;this.visible(!1),this.iframe.src=e,this.iframe.onload=function(){t.doc=t.iframe.contentDocument,t.bodyEl=t.doc.body,t.formatSpread(),t.beforeDisplay(function(){t.book.tell("book:chapterDisplayed"),t.visible(!0)}),t.afterLoaded(t),t.book.listen("book:resized",t.formatSpread,t)}},FP.Chapter.prototype.afterLoaded=function(e){},FP.Chapter.prototype.error=function(e){console.log("error",error)},FP.Chapter.prototype.formatSpread=function(){var e=2,t=800;this.colWidth&&(this.OldcolWidth=this.colWidth,this.OldspreadWidth=this.spreadWidth),this.elWidth=this.iframe.width,this.gap=this.gap||Math.ceil(this.elWidth/8),this.elWidth<t||this.book.single?(this.spread=!1,e=1,this.colWidth=Math.floor(this.elWidth/e)):(this.spread=!0,this.colWidth=Math.floor((this.elWidth-this.gap)/e),this.colWidth%2!=0&&(this.colWidth-=1)),this.spreadWidth=(this.colWidth+this.gap)*e,this.bodyEl.style.fontSize=localStorage.getItem("fontSize")||"medium",this.bodyEl.style.visibility="hidden",this.bodyEl.style.margin="0",this.bodyEl.style.overflow="hidden",this.bodyEl.style.width=this.elWidth,this.bodyEl.style.height=this.book.el.clientHeight+"px",this.bodyEl.style[FP.core.columnAxis]="horizontal",this.bodyEl.style[FP.core.columnGap]=this.gap+"px",this.bodyEl.style[FP.core.columnWidth]=this.colWidth+"px",this.calcPages(),this.OldcolWidth&&(this.leftPos=(this.chapterPos-1)*this.spreadWidth,this.bodyEl.scrollLeft=this.leftPos,this.visible(!0))},FP.Chapter.prototype.goToChapterEnd=function(){this.chapterEnd()},FP.Chapter.prototype.visible=function(e){if(!this.bodyEl)return!1;e?this.bodyEl.style.visibility="visible":this.bodyEl.style.visibility="hidden"},FP.Chapter.prototype.calcPages=function(){this.totalWidth=this.iframe.contentDocument.documentElement.scrollWidth,this.displayedPages=Math.ceil(this.totalWidth/this.spreadWidth),localStorage.setItem("displayedPages",this.displayedPages)},FP.Chapter.prototype.nextPage=function(){return this.chapterPos<this.displayedPages?(this.chapterPos++,this.leftPos+=this.spreadWidth,this.setLeft(this.leftPos),localStorage.setItem("chapterPos",this.chapterPos),this.book.tell("book:pageChanged",this.chapterPos),this.chapterPos):!1},FP.Chapter.prototype.prevPage=function(){return this.chapterPos>1?(this.chapterPos--,this.leftPos-=this.spreadWidth,this.setLeft(this.leftPos),localStorage.setItem("chapterPos",this.chapterPos),this.book.tell("book:pageChanged",this.chapterPos),this.chapterPos):!1},FP.Chapter.prototype.chapterEnd=function(){this.page(this.displayedPages)},FP.Chapter.prototype.setLeft=function(e){this.bodyEl.style.marginLeft=-e+"px"},FP.Chapter.prototype.replaceLinks=function(e){var t=this.doc.querySelectorAll("[href]"),n=Array.prototype.slice.call(t),r=this;n.forEach(function(e){var t,n=e.getAttribute("href"),i=n.search("://"),s=n[0]=="#";i!=-1?e.setAttribute("target","_blank"):e.onclick=function(){r.book.useHash?window.location.hash="#/"+n:r.book.show(n)}}),e&&e()},FP.Chapter.prototype.replaceResources=function(e){var t,n,r;if(this.book.online&&!this.book.contained||FP.storage.getStorageType()=="filesystem")return e&&e(),!1;t=this.doc.querySelectorAll("[src]"),n=Array.prototype.slice.call(t),r=n.length,n.forEach(function(t){var n=t.getAttribute("src"),i=this.book.basePath+n;FP.storage.get(i,function(n){t.setAttribute("src",n),r--,r<=0&&e&&e()})}.bind(this))},FP.Chapter.prototype.getID=function(){return this.ID},FP.Chapter.prototype.page=function(e){return e>=1&&e<=this.displayedPages?(this.chapterPos=e,this.leftPos=this.spreadWidth*(e-1),this.setLeft(this.leftPos),localStorage.setItem("chapterPos",e),!0):!1},FP.Chapter.prototype.section=function(e){var t=this.doc.getElementById(e),n,r;t&&(n=this.leftPos+t.offsetLeft,r=Math.floor(n/this.spreadWidth)+1,this.page(r))},FP.Chapter.prototype.beforeDisplay=function(e){this.book.triggerHooks("beforeChapterDisplay",e.bind(this),this)};var FP=FP||{};FP.core={},FP.core.getEl=function(e){return document.getElementById(e)},FP.core.getEls=function(e){return document.getElementsByClassName(e)},FP.core.loadXML=function(e,t){var n=new XMLHttpRequest;n.open("GET",e,!0),n.overrideMimeType("text/xml"),n.onload=function(e){this.status==200&&t(this.responseXML)},n.send()},FP.core.loadFile=function(e,t){var n=new XMLHttpRequest;return this.succeeded=function(e){t&&t(e)},this.failed=function(e){console.log("Error:",e)},this.start=function(){var t=this;n.open("GET",e,!0),n.responseType="blob",n.onload=function(e){this.status==200&&t.succeeded(this.response)},n.onerror=function(e){t.failed(this.status)},n.send()},{start:this.start,succeeded:this.succeeded,failed:this.failed}},FP.core.crossBrowserColumnCss=function(){var e=function(e){return e.replace(/([A-Z])/g,function(e,t){return"-"+t.toLowerCase()}).replace(/^ms-/,"-ms-")};FP.core.columnAxis=Modernizr.prefixed("columnAxis")||"columnAxis",FP.core.columnGap=Modernizr.prefixed("columnGap")||"columnGap",FP.core.columnWidth=Modernizr.prefixed("columnWidth")||"columnWidth"},FP.core.toArray=function(e){var t=[];for(member in e){var n;e.hasOwnProperty(member)&&(n=e[member],n.ident=member,t.push(n))}return t},FP.core.dataURLToBlob=function(e){var t=";base64,";if(e.indexOf(t)==-1){var n=e.split(","),r=n[0].split(":")[1],i=n[1];return new Blob([i],{type:r})}var n=e.split(t),r=n[0].split(":")[1],i=window.atob(n[1]),s=i.length,o=new Uint8Array(s);for(var u=0;u<s;++u)o[u]=i.charCodeAt(u);return new Blob([o],{type:r})},FP.core.loadScript=function(e,t,n){var r,i;i=!1,r=document.createElement("script"),r.type="text/javascript",r.async=!1,r.src=e,r.onload=r.onreadystatechange=function(){!i&&(!this.readyState||this.readyState=="complete")&&(i=!0,t&&t())},n=n||document.body,n.appendChild(r)},FP.core.loadScripts=function(e,t,n){var r=e.length,i=0,s=function(){i++,r==i?t&&t():FP.core.loadScript(e[i],s,n)};FP.core.loadScript(e[i],s,n)},FP.core.addCss=function(e,t,n){var r,i;i=!1,r=document.createElement("link"),r.type="text/css",r.rel="stylesheet",r.href=e,r.onload=r.onreadystatechange=function(){!i&&(!this.readyState||this.readyState=="complete")&&(i=!0,t&&t())},n=n||document.body,n.appendChild(r)},FP.Events=function(e,t){return this.events={},t?this.el=t:this.el=document.createElement("div"),e.createEvent=this.createEvent,e.tell=this.tell,e.listen=this.listen,e.deafen=this.deafen,e.listenUntil=this.listenUntil,this},FP.Events.prototype.createEvent=function(e){var t=new CustomEvent(e);return this.events[e]=t,t},FP.Events.prototype.tell=function(e,t){var n;this.events[e]?n=this.events[e]:(console.warn("No event:",e,"defined yet, creating."),n=this.createEvent(e)),t&&(n.msg=t),this.el.dispatchEvent(n)},FP.Events.prototype.listen=function(e,t,n){if(!this.events[e]){console.warn("No event:",e,"defined yet, creating."),this.createEvent(e);return}n?this.el.addEventListener(e,t.bind(n),!1):this.el.addEventListener(e,t,!1)},FP.Events.prototype.deafen=function(e,t){this.el.removeEventListener(e,t,!1)},FP.Events.prototype.listenUntil=function(e,t,n,r){function i(){this.deafen(e,n),this.deafen(t,i)}this.listen(e,n,r),this.listen(t,i,this)},FP.Hooks=function(){"use strict";return{register:function(e){this[e]===undefined&&(this[e]={});if(typeof this[e]!="object")throw"Already registered: "+e;return this[e]}}}(),FP.Queue=function(e,t){this._q=[],this._tasks={},this.idCount=0,this.concurrency=0,this.workers=[],this.available=[],typeof e=="string"&&(this.workerStr=e,this.addWorkers(t||1)),typeof e=="function"&&(this.workerFunction=e,this.addFakeWorkers(t||1))},FP.Queue.prototype.addWorkers=function(e){var t=this.concurrency,n=t+e;for(var r=t;r<e;r++){var i=new Worker(this.workerStr);this.workers.push(i),this.available.push(r)}this.concurrency=e},FP.Queue.prototype.addFakeWorkers=function(e){var t=this.concurrency,n=t+e;for(var r=t;r<e;r++){var i=new FP.FakeWorker(this.workerFunction);this.workers.push(i),this.available.push(r)}this.concurrency=e},FP.Queue.prototype.add=function(e,t,n){var r=this.idCount;return this._tasks[r]={msg:e,callback:t||function(){}},n?(this._q.unshift(r),this.running||this.run()):this._q.push(r),this.idCount++,r},FP.Queue.prototype.addGroup=function(e,t){var n=this,r=e.length,i=function(){r--,r<=0&&t()};return e.forEach(function(e){n.add(e,i)}),this.running||this.run(),i},FP.Queue.prototype.run=function(e){if(this.running)return;this.running=!0;while(this.available.length){var t=this.next();if(!t)break}},FP.Queue.prototype.find=function(e){},FP.Queue.prototype.next=function(){var e=this,t=this._q.shift(),n,r,i;return typeof t=="undefined"?(this.running=!1,!1):(n=this._tasks[t],r=this.available.pop(),i=this.workers[r],i.postMessage(n.msg),i.onmessage=function(i){var s=i.data;n.callback(s),delete e._tasks[t],e.available.push(r),e.next()},i)},FP.Queue.prototype.empty=function(){this._q=[],this._tasks={}},FP.FakeWorker=function(e){this.func=e},FP.FakeWorker.prototype.postMessage=function(e){setTimeout(function(){this.func(e,this.onmessage)}.bind(this),1)},FP.FakeWorker.prototype.onmessage=function(e){},FP.FakeWorker.prototype.close=function(e){},FP.storage=function(){function e(e){console.log("storageMethod",e),!e||typeof FP.store[e]=="undefined"?this.storageType="none":this.storageType=e,this._store=new FP.store[this.storageType],this._store.failed=o}function t(e,t){return this._store.get(e,t)}function n(e,t){return this._store.preload(e,t)}function r(e,t){return this._store.batch(e,t)}function i(e){return this._store.getURL(e)}function s(e,t,n){return this._store.save(e,t,n)}function o(e){console.log("error",e)}function u(){return this.storageType}return this._URL=window.URL,{get:t,preload:n,batch:r,storageMethod:e,getURL:i,save:s,getStorageType:u}}(),FP.store=FP.store||{},FP.store.filesystem=function(){function o(e){if(r){e(r);return}n(s,i,function(t){r=t,e(t)},v)}function u(e){}function a(e,n){t.addGroup(e,n)}function f(n,r){if(typeof e[n]!="undefined")return e[n];l(n,function(e){var i;e?(i=d(n,e),typeof r!="undefined"&&r(i)):t.add(n,function(e){l(e,function(t){e=d(n,t),typeof r!="undefined"&&r(e)})},!0)})}function l(e,t){var n,r;o(function(n){n.root.getFile(e,{},function(e){t(e)},function(){t(!1)})})}function c(e,t){var n=new FP.core.loadFile(e);n.succeeded=function(e){typeof t!="undefined"&&t(e)},n.failed=v,n.start()}function h(e,t,n){o(function(r){var i=e.split("/").slice(0,-1);p(r.root,i),r.root.getFile(e,{create:!0},function(e){e.createWriter(function(e){e.onwriteend=function(e){n&&n(e)},e.onerror=function(e){v(err)},e.write(t)})},v)})}function p(e,t){if(t[0]=="."||t[0]=="")t=t.slice(1);e.getDirectory(t[0],{create:!0},function(e){t.length&&p(e,t.slice(1))},v)}function d(t,n){var r;return typeof e[t]!="undefined"?e[t]:(r=n.toURL(),e[t]=r,r)}function v(e){typeof this.failed=="undefined"?console.log("Error: ",m(e)):this.failed(e)}function m(e){switch(e.code){case FileError.QUOTA_EXCEEDED_ERR:return"QUOTA_EXCEEDED_ERR";case FileError.NOT_FOUND_ERR:return"NOT_FOUND_ERR";case FileError.SECURITY_ERR:return"SECURITY_ERR";case FileError.INVALID_MODIFICATION_ERR:return"INVALID_MODIFICATION_ERR";case FileError.INVALID_STATE_ERR:return"INVALID_STATE_ERR";case FileError.TYPE_MISMATCH_ERR:return"TYPE_MISMATCH_ERR";default:return"Unknown Error:"+e.code}}var e={},t=new FP.Queue(FP.filePath+"workers/loader_filesystem.js",6),n=window.requestFileSystem||window.webkitRequestFileSystem,r;const i=5242880,s=TEMPORARY;return{get:f,preload:u,batch:a,getURL:d,save:h}},FP.store=FP.store||{},FP.store.indexedDB=function(){function o(e,t){var n={data:null},r=e;h(e,function(e){p(r,e),t(n)})}function u(e){var t;if(i){e(i);return}t=indexedDB.open(s),t.onsuccess=function(n){i=t.result,i.onerror=function(e){console.log("Database error: "+e.target.errorCode)},e&&e(i)},t.onerror=function(e){},t.onupgradeneeded=function(e){var t=e.target.result,n=t.createObjectStore("files",{keyPath:"path"})}}function a(e){}function f(e,t){n.addGroup(e,t)}function l(e,r){if(typeof t[e]!="undefined"){r(t[e]);return}c(e,function(t){var i;t?(i=d(e,t),typeof r!="undefined"&&r(i)):n.add(e,function(t){i=d(e,t),typeof r!="undefined"&&r(i)},!0)})}function c(e,t){var n,r;u(function(i){r=i.transaction(["files"]).objectStore("files"),n=r.get(e),n.onerror=function(e){t(!1)},n.onsuccess=function(e){t(n.result.file)}})}function h(e,t){var n=new FP.core.loadFile(e);n.succeeded=function(e){typeof t!="undefined"&&t(e)},n.failed=v,n.start()}function p(e,t){var n={path:e,file:t},r;u(function(t){var i=t.transaction(["files"],"readwrite"),s=i.objectStore("files");r=s.put(n),r.onerror=function(e){console.log("failed: "+e.target.errorCode)},r.onsuccess=function(t){console.log("saved",e)}})}function d(e,n){var r;if(typeof t[e]!="undefined"){callback(t[e]);return}return r=this._URL.createObjectURL(n),t[e]=r,r}function v(e){typeof this.failed=="undefined"?console.log("Error: ",e):this.failed(e)}var e={},t={},n=new FP.Queue(o,6),r=window.indexedDB||window.mozIndexedDB||window.webkitIndexedDB||window.msIndexedDB,i;const s="fpjs_db";return{get:l,preload:a,batch:f,getURL:d,save:p}},FP.store=FP.store||{},FP.store.none=function(){function r(e,t){var n={data:null},r=u(e);r?(n.data=r,t(n)):a(e,function(e){n.data=e,t(n)})}function i(e){var t=u(e);t||n.add(e)}function s(e,t){n.addGroup(e,t)}function o(e,t){var r=u(e),i;r?typeof t!="undefined"&&t(r):n.add(e,function(n){i=getURL(e,n),typeof t!="undefined"&&t(i)},!0)}function u(t){var n=e[t];return typeof n!="undefined"?n:!1}function a(e,t){var n=new FP.core.loadFile(e);n.succeeded=function(n){f(e,n),typeof t!="undefined"&&t(n)},n.failed=l,n.start()}function f(t,n){if(e[t])return;e[t]=t}function l(e){typeof this.failed=="undefined"?console.log("Error: ",e):this.failed(e)}var e={},t={},n=new FP.Queue(r,6);return{get:o,preload:i,batch:s}},FP.store=FP.store||{},FP.store.ram=function(){function r(e,t){var n={data:null},r=u(e);r?(n.data=r,t(n)):a(e,function(e){n.data=e,t(n)})}function i(e){var t=u(e);t||n.add(e)}function s(e,t){n.addGroup(e,t)}function o(e,t){var r=u(e),i;r?(i=l(e,r),typeof t!="undefined"&&t(i)):n.add(e,function(n){i=l(e,n),typeof t!="undefined"&&t(i)},!0)}function u(t){var n=e[t];return typeof n!="undefined"?n:!1}function a(e,t){var n=new FP.core.loadFile(e);n.succeeded=function(n){f(e,n),typeof t!="undefined"&&t(n)},n.failed=c,n.start()}function f(t,n){if(e[t])return;e[t]=n}function l(e,n){var r;return typeof t[e]!="undefined"?t[e]:(r=this._URL.createObjectURL(n),t[e]=r,r)}function c(e){typeof this.failed=="undefined"?console.log("Error: ",e):this.failed(e)}var e={},t={},n=new FP.Queue(r,6);return{get:o,preload:i,batch:s}},FP.store=FP.store||{},FP.store.websql=function(){function a(e,t){var n={data:null},r=e;d(e,function(e){v(r,e),t(n)})}function f(e){if(n){e(n);return}n=openDatabase(r,i,s,o);if(!n){console.error("Database error");return}n.transaction(function(t){t.executeSql("CREATE TABLE IF NOT EXISTS "+u+" (path TEXT PRIMARY KEY ASC UNIQUE, file BLOB, type TEXT)"),e&&e(n)})}function l(e){}function c(e,n){t.addGroup(e,n)}function h(n,r){if(typeof e[n]!="undefined"){r(e[n]);return}p(n,function(e){var i;e?(i=m(n,e),typeof r!="undefined"&&r(i)):t.add(n,function(e){i=m(n,e),typeof r!="undefined"&&r(i)},!0)})}function p(e,t){var n={};n.onError=function(e,n){console.log("get Error",n),t(!1)},n.onSuccess=function(e,n){var r;n.rows.length&&(r=n.rows.item(0),t(r.file))},f(function(t){t.transaction(function(t){t.executeSql("SELECT * FROM "+u+" WHERE path='"+e+"' LIMIT 1",[],n.onSuccess,n.onError)})})}function d(e,t){var n=new FP.core.loadFile(e);n.succeeded=function(e){typeof t!="undefined"&&t(e)},n.failed=g,n.start()}function v(e,t){var n={},r=new FileReader,i;r.readAsDataURL(t),r.onload=function(r){i=r.target.result,f(function(r){r.transaction(function(r){r.executeSql("REPLACE INTO "+u+" (path, file, type) VALUES (?,?,?)",[e,i,t.type],n.onSuccess,n.onError)})})},n.onError=function(e,t){console.log("failed: ",t)},n.onSuccess=function(e){}}function m(t,n){var r,i;if(typeof e[t]!="undefined"){callback(e[t]);return}return i=FP.core.dataURLToBlob(n),r=this._URL.createObjectURL(i),e[t]=r,r}function g(e){typeof this.failed=="undefined"?console.log("Error: ",e):this.failed(e)}var e={},t=new FP.Queue(a,6),n;const r="fpjs_db",i="1",s="cache for files",o=5242880,u="files";return{get:h,preload:l,batch:c,getURL:m,save:v}},FP.Unarchiver=function(e,t){this.libPath=FP.filePath+"libs/",this.zipUrl=e,this.callback=t,this.loadLib(function(){this.getZip(this.zipUrl)}.bind(this))},FP.Unarchiver.prototype.loadLib=function(e){typeof zip!="undefined"&&e(),FP.core.loadScript(this.libPath+"zip.js",function(){zip.workerScriptsPath=this.libPath,e()}.bind(this))},FP.Unarchiver.prototype.getZip=function(e){var t=new FP.core.loadFile(e);t.succeeded=function(e){this.getEntries(e,this.toStorage.bind(this))}.bind(this),t.failed=this.failed,t.start()},FP.Unarchiver.prototype.getEntries=function(e,t){zip.createReader(new zip.BlobReader(e),function(e){e.getEntries(t)},this.failed)},FP.Unarchiver.prototype.failed=function(e){console.log("Error:",e)},FP.Unarchiver.prototype.afterSaved=function(e){this.callback()},FP.Unarchiver.prototype.toStorage=function(e){function s(){i--,i==0&&r.afterSaved()}var t=0,n=20,r=this,i=e.length;e.forEach(function(e){setTimeout(function(e){r.saveEntryFileToStorage(e,s)},t,e),t+=n}),console.log("time",t)},FP.Unarchiver.prototype.saveEntryFileToStorage=function(e,t){var n=this;e.getData(new zip.BlobWriter,function(n){FP.storage.save(e.filename,n,t)})}; |