mirror of
https://github.com/futurepress/epub.js.git
synced 2025-10-02 14:49:16 +02:00
Fix eslint errors
This commit is contained in:
parent
c99bfd07b7
commit
a435650c3b
31 changed files with 1077 additions and 1102 deletions
42
.eslintrc.js
Normal file
42
.eslintrc.js
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
module.exports = {
|
||||||
|
"env": {
|
||||||
|
"browser": true,
|
||||||
|
"commonjs": true,
|
||||||
|
"es6": true,
|
||||||
|
"node": true
|
||||||
|
},
|
||||||
|
"globals": {
|
||||||
|
"ePub": true,
|
||||||
|
"JSZip": true
|
||||||
|
},
|
||||||
|
"extends": "eslint:recommended",
|
||||||
|
"parserOptions": {
|
||||||
|
"sourceType": "module"
|
||||||
|
},
|
||||||
|
"rules": {
|
||||||
|
"indent": [
|
||||||
|
"error",
|
||||||
|
"tab",
|
||||||
|
{ "VariableDeclarator": { "var": 2, "let": 2, "const": 3 } }
|
||||||
|
],
|
||||||
|
"linebreak-style": [
|
||||||
|
"error",
|
||||||
|
"unix"
|
||||||
|
],
|
||||||
|
"quotes": [
|
||||||
|
"warn",
|
||||||
|
"double"
|
||||||
|
],
|
||||||
|
"semi": [
|
||||||
|
"error",
|
||||||
|
"always"
|
||||||
|
],
|
||||||
|
"no-unused-vars" : ["warn"],
|
||||||
|
"no-console" : ["warn"],
|
||||||
|
"no-unused-vars": [
|
||||||
|
"error",
|
||||||
|
{ "vars": "all", "args": "none" }
|
||||||
|
],
|
||||||
|
"no-mixed-spaces-and-tabs": ["error", "smart-tabs"]
|
||||||
|
}
|
||||||
|
};
|
|
@ -1,7 +1,7 @@
|
||||||
import {defer, isXml, parse} from './utils/core';
|
import {defer, isXml, parse} from "./utils/core";
|
||||||
import request from './request';
|
import request from "./utils/request";
|
||||||
import mime from '../libs/mime/mime';
|
import mime from "../libs/mime/mime";
|
||||||
import Path from './utils/path';
|
import Path from "./utils/path";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles Unzipping a requesting files from an Epub Archive
|
* Handles Unzipping a requesting files from an Epub Archive
|
||||||
|
@ -22,14 +22,16 @@ class Archive {
|
||||||
*/
|
*/
|
||||||
checkRequirements(){
|
checkRequirements(){
|
||||||
try {
|
try {
|
||||||
if (typeof JSZip === 'undefined') {
|
if (typeof JSZip === "undefined") {
|
||||||
let JSZip = require('jszip');
|
let JSZip = require("jszip");
|
||||||
}
|
this.zip = new JSZip();
|
||||||
|
} else {
|
||||||
this.zip = new JSZip();
|
this.zip = new JSZip();
|
||||||
} catch (e) {
|
|
||||||
console.error("JSZip lib not loaded");
|
|
||||||
}
|
}
|
||||||
};
|
} catch (e) {
|
||||||
|
throw new Error("JSZip lib not loaded");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Open an archive
|
* Open an archive
|
||||||
|
@ -39,7 +41,7 @@ class Archive {
|
||||||
*/
|
*/
|
||||||
open(input, isBase64){
|
open(input, isBase64){
|
||||||
return this.zip.loadAsync(input, {"base64": isBase64});
|
return this.zip.loadAsync(input, {"base64": isBase64});
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load and Open an archive
|
* Load and Open an archive
|
||||||
|
@ -52,7 +54,7 @@ class Archive {
|
||||||
.then(function(data){
|
.then(function(data){
|
||||||
return this.zip.loadAsync(data, {"base64": isBase64});
|
return this.zip.loadAsync(data, {"base64": isBase64});
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Request
|
* Request
|
||||||
|
@ -63,7 +65,6 @@ class Archive {
|
||||||
request(url, type){
|
request(url, type){
|
||||||
var deferred = new defer();
|
var deferred = new defer();
|
||||||
var response;
|
var response;
|
||||||
var r;
|
|
||||||
var path = new Path(url);
|
var path = new Path(url);
|
||||||
|
|
||||||
// If type isn't set, determine it from the file extension
|
// If type isn't set, determine it from the file extension
|
||||||
|
@ -71,7 +72,7 @@ class Archive {
|
||||||
type = path.extension;
|
type = path.extension;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(type == 'blob'){
|
if(type == "blob"){
|
||||||
response = this.getBlob(url);
|
response = this.getBlob(url);
|
||||||
} else {
|
} else {
|
||||||
response = this.getText(url);
|
response = this.getText(url);
|
||||||
|
@ -89,7 +90,7 @@ class Archive {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return deferred.promise;
|
return deferred.promise;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle the response from request
|
* Handle the response from request
|
||||||
|
@ -109,18 +110,18 @@ class Archive {
|
||||||
r = parse(response, "text/xml");
|
r = parse(response, "text/xml");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
if(type == 'xhtml') {
|
if(type == "xhtml") {
|
||||||
r = parse(response, "application/xhtml+xml");
|
r = parse(response, "application/xhtml+xml");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
if(type == 'html' || type == 'htm') {
|
if(type == "html" || type == "htm") {
|
||||||
r = parse(response, "text/html");
|
r = parse(response, "text/html");
|
||||||
} else {
|
} else {
|
||||||
r = response;
|
r = response;
|
||||||
}
|
}
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a Blob from Archive by Url
|
* Get a Blob from Archive by Url
|
||||||
|
@ -138,7 +139,7 @@ class Archive {
|
||||||
return new Blob([uint8array], {type : mimeType});
|
return new Blob([uint8array], {type : mimeType});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get Text from Archive by Url
|
* Get Text from Archive by Url
|
||||||
|
@ -155,7 +156,7 @@ class Archive {
|
||||||
return text;
|
return text;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a base64 encoded result from Archive by Url
|
* Get a base64 encoded result from Archive by Url
|
||||||
|
@ -173,7 +174,7 @@ class Archive {
|
||||||
return "data:" + mimeType + ";base64," + data;
|
return "data:" + mimeType + ";base64," + data;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a Url from an unarchived item
|
* Create a Url from an unarchived item
|
||||||
|
@ -185,7 +186,6 @@ class Archive {
|
||||||
var deferred = new defer();
|
var deferred = new defer();
|
||||||
var _URL = window.URL || window.webkitURL || window.mozURL;
|
var _URL = window.URL || window.webkitURL || window.mozURL;
|
||||||
var tempUrl;
|
var tempUrl;
|
||||||
var blob;
|
|
||||||
var response;
|
var response;
|
||||||
var useBase64 = options && options.base64;
|
var useBase64 = options && options.base64;
|
||||||
|
|
||||||
|
@ -232,7 +232,7 @@ class Archive {
|
||||||
}
|
}
|
||||||
|
|
||||||
return deferred.promise;
|
return deferred.promise;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Revoke Temp Url for a achive item
|
* Revoke Temp Url for a achive item
|
||||||
|
@ -242,7 +242,7 @@ class Archive {
|
||||||
var _URL = window.URL || window.webkitURL || window.mozURL;
|
var _URL = window.URL || window.webkitURL || window.mozURL;
|
||||||
var fromCache = this.urlCache[url];
|
var fromCache = this.urlCache[url];
|
||||||
if(fromCache) _URL.revokeObjectURL(fromCache);
|
if(fromCache) _URL.revokeObjectURL(fromCache);
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Archive;
|
export default Archive;
|
||||||
|
|
65
src/book.js
65
src/book.js
|
@ -1,21 +1,22 @@
|
||||||
import EventEmitter from 'event-emitter';
|
import EventEmitter from "event-emitter";
|
||||||
// import path from 'path';
|
// import path from "path";
|
||||||
import {extend, defer} from './utils/core';
|
import {extend, defer} from "./utils/core";
|
||||||
import Url from './utils/url';
|
import Url from "./utils/url";
|
||||||
import Path from './utils/path';
|
import Path from "./utils/path";
|
||||||
import Spine from './spine';
|
import Spine from "./spine";
|
||||||
import Locations from './locations';
|
import Locations from "./locations";
|
||||||
import Container from './container';
|
import Container from "./container";
|
||||||
import Packaging from './packaging';
|
import Packaging from "./packaging";
|
||||||
import Navigation from './navigation';
|
import Navigation from "./navigation";
|
||||||
import Resources from './resources';
|
import Resources from "./resources";
|
||||||
import PageList from './pagelist';
|
import PageList from "./pagelist";
|
||||||
import Rendition from './rendition';
|
import Rendition from "./rendition";
|
||||||
import Archive from './archive';
|
import Archive from "./archive";
|
||||||
import request from './request';
|
import request from "./utils/request";
|
||||||
import EpubCFI from './epubcfi';
|
import EpubCFI from "./epubcfi";
|
||||||
|
|
||||||
const CONTAINER_PATH = "META-INF/container.xml";
|
const CONTAINER_PATH = "META-INF/container.xml";
|
||||||
|
const EPUBJS_VERSION = "0.3.1";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new Book
|
* Creates a new Book
|
||||||
|
@ -45,7 +46,7 @@ class Book {
|
||||||
requestCredentials: undefined,
|
requestCredentials: undefined,
|
||||||
requestHeaders: undefined,
|
requestHeaders: undefined,
|
||||||
encoding: undefined,
|
encoding: undefined,
|
||||||
replacements: 'base64'
|
replacements: "base64"
|
||||||
});
|
});
|
||||||
|
|
||||||
extend(this.settings, options);
|
extend(this.settings, options);
|
||||||
|
@ -84,12 +85,14 @@ class Book {
|
||||||
* @property {promise} ready returns after the book is loaded and parsed
|
* @property {promise} ready returns after the book is loaded and parsed
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
this.ready = Promise.all([this.loaded.manifest,
|
this.ready = Promise.all([
|
||||||
|
this.loaded.manifest,
|
||||||
this.loaded.spine,
|
this.loaded.spine,
|
||||||
this.loaded.metadata,
|
this.loaded.metadata,
|
||||||
this.loaded.cover,
|
this.loaded.cover,
|
||||||
this.loaded.navigation,
|
this.loaded.navigation,
|
||||||
this.loaded.resources ]);
|
this.loaded.resources
|
||||||
|
]);
|
||||||
|
|
||||||
|
|
||||||
// Queue for methods used before opening
|
// Queue for methods used before opening
|
||||||
|
@ -143,12 +146,11 @@ class Book {
|
||||||
if(url) {
|
if(url) {
|
||||||
this.open(url).catch((error) => {
|
this.open(url).catch((error) => {
|
||||||
var err = new Error("Cannot load book at "+ url );
|
var err = new Error("Cannot load book at "+ url );
|
||||||
console.error(err);
|
// console.error(err);
|
||||||
this.emit("openFailed", err);
|
this.emit("openFailed", err);
|
||||||
console.log(error);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Open a epub or url
|
* Open a epub or url
|
||||||
|
@ -172,7 +174,7 @@ class Book {
|
||||||
} else if (type === "epub") {
|
} else if (type === "epub") {
|
||||||
this.archived = true;
|
this.archived = true;
|
||||||
this.url = new Url("/", "");
|
this.url = new Url("/", "");
|
||||||
opening = this.request(input, 'binary')
|
opening = this.request(input, "binary")
|
||||||
.then(this.openEpub.bind(this));
|
.then(this.openEpub.bind(this));
|
||||||
} else if(type == "opf") {
|
} else if(type == "opf") {
|
||||||
this.url = new Url(input);
|
this.url = new Url(input);
|
||||||
|
@ -224,7 +226,6 @@ class Book {
|
||||||
* @return {Promise}
|
* @return {Promise}
|
||||||
*/
|
*/
|
||||||
openPackaging(url) {
|
openPackaging(url) {
|
||||||
var packageUrl;
|
|
||||||
this.path = new Path(url);
|
this.path = new Path(url);
|
||||||
return this.load(url)
|
return this.load(url)
|
||||||
.then((xml) => {
|
.then((xml) => {
|
||||||
|
@ -258,7 +259,7 @@ class Book {
|
||||||
*/
|
*/
|
||||||
resolve(path, absolute) {
|
resolve(path, absolute) {
|
||||||
var resolved = path;
|
var resolved = path;
|
||||||
var isAbsolute = (path.indexOf('://') > -1);
|
var isAbsolute = (path.indexOf("://") > -1);
|
||||||
|
|
||||||
if (isAbsolute) {
|
if (isAbsolute) {
|
||||||
return path;
|
return path;
|
||||||
|
@ -309,7 +310,7 @@ class Book {
|
||||||
if(extension === "opf"){
|
if(extension === "opf"){
|
||||||
return "opf";
|
return "opf";
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -369,7 +370,7 @@ class Book {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.load(navPath, 'xml')
|
return this.load(navPath, "xml")
|
||||||
.then((xml) => {
|
.then((xml) => {
|
||||||
this.navigation = new Navigation(xml);
|
this.navigation = new Navigation(xml);
|
||||||
this.pageList = new PageList(xml);
|
this.pageList = new PageList(xml);
|
||||||
|
@ -400,7 +401,7 @@ class Book {
|
||||||
this.rendition.attachTo(element);
|
this.rendition.attachTo(element);
|
||||||
|
|
||||||
return this.rendition;
|
return this.rendition;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set if request should use withCredentials
|
* Set if request should use withCredentials
|
||||||
|
@ -408,7 +409,7 @@ class Book {
|
||||||
*/
|
*/
|
||||||
setRequestCredentials(credentials) {
|
setRequestCredentials(credentials) {
|
||||||
this.settings.requestCredentials = credentials;
|
this.settings.requestCredentials = credentials;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set headers request should use
|
* Set headers request should use
|
||||||
|
@ -416,7 +417,7 @@ class Book {
|
||||||
*/
|
*/
|
||||||
setRequestHeaders(headers) {
|
setRequestHeaders(headers) {
|
||||||
this.settings.requestHeaders = headers;
|
this.settings.requestHeaders = headers;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unarchive a zipped epub
|
* Unarchive a zipped epub
|
||||||
|
@ -478,7 +479,7 @@ class Book {
|
||||||
return item.load().then(function (contents) {
|
return item.load().then(function (contents) {
|
||||||
var range = cfi.toRange(item.document);
|
var range = cfi.toRange(item.document);
|
||||||
return range;
|
return range;
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -488,7 +489,7 @@ class Book {
|
||||||
*/
|
*/
|
||||||
key(identifier) {
|
key(identifier) {
|
||||||
var ident = identifier || this.package.metadata.identifier || this.url.filename;
|
var ident = identifier || this.package.metadata.identifier || this.url.filename;
|
||||||
return "epubjs:" + (EPUBJS_VERSION || ePub.VERSION) + ":" + ident;
|
return `epubjs:${EPUBJS_VERSION}:${ident}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import path from 'path-webpack';
|
import path from "path-webpack";
|
||||||
import {qs} from './utils/core';
|
import {qs} from "./utils/core";
|
||||||
import EpubCFI from './epubcfi';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles Parsing and Accessing an Epub Container
|
* Handles Parsing and Accessing an Epub Container
|
||||||
|
@ -12,7 +11,7 @@ class Container {
|
||||||
if (containerDocument) {
|
if (containerDocument) {
|
||||||
this.parse(containerDocument);
|
this.parse(containerDocument);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse the Container XML
|
* Parse the Container XML
|
||||||
|
@ -20,24 +19,22 @@ class Container {
|
||||||
*/
|
*/
|
||||||
parse(containerDocument){
|
parse(containerDocument){
|
||||||
//-- <rootfile full-path="OPS/package.opf" media-type="application/oebps-package+xml"/>
|
//-- <rootfile full-path="OPS/package.opf" media-type="application/oebps-package+xml"/>
|
||||||
var rootfile, fullpath, folder, encoding;
|
var rootfile;
|
||||||
|
|
||||||
if(!containerDocument) {
|
if(!containerDocument) {
|
||||||
console.error("Container File Not Found");
|
throw new Error("Container File Not Found");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rootfile = qs(containerDocument, "rootfile");
|
rootfile = qs(containerDocument, "rootfile");
|
||||||
|
|
||||||
if(!rootfile) {
|
if(!rootfile) {
|
||||||
console.error("No RootFile Found");
|
throw new Error("No RootFile Found");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.packagePath = rootfile.getAttribute('full-path');
|
this.packagePath = rootfile.getAttribute("full-path");
|
||||||
this.directory = path.dirname(this.packagePath);
|
this.directory = path.dirname(this.packagePath);
|
||||||
this.encoding = containerDocument.xmlEncoding;
|
this.encoding = containerDocument.xmlEncoding;
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Container;
|
export default Container;
|
||||||
|
|
175
src/contents.js
175
src/contents.js
|
@ -1,7 +1,7 @@
|
||||||
import EventEmitter from 'event-emitter';
|
import EventEmitter from "event-emitter";
|
||||||
import {isNumber, prefixed} from './utils/core';
|
import {isNumber, prefixed} from "./utils/core";
|
||||||
import EpubCFI from './epubcfi';
|
import EpubCFI from "./epubcfi";
|
||||||
import Mapping from './mapping';
|
import Mapping from "./mapping";
|
||||||
|
|
||||||
|
|
||||||
class Contents {
|
class Contents {
|
||||||
|
@ -19,12 +19,12 @@ class Contents {
|
||||||
this._size = {
|
this._size = {
|
||||||
width: 0,
|
width: 0,
|
||||||
height: 0
|
height: 0
|
||||||
}
|
};
|
||||||
|
|
||||||
this.cfiBase = cfiBase || "";
|
this.cfiBase = cfiBase || "";
|
||||||
|
|
||||||
this.listeners();
|
this.listeners();
|
||||||
};
|
}
|
||||||
|
|
||||||
width(w) {
|
width(w) {
|
||||||
// var frame = this.documentElement;
|
// var frame = this.documentElement;
|
||||||
|
@ -39,10 +39,10 @@ class Contents {
|
||||||
// this.content.style.width = w;
|
// this.content.style.width = w;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.window.getComputedStyle(frame)['width'];
|
return this.window.getComputedStyle(frame)["width"];
|
||||||
|
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
height(h) {
|
height(h) {
|
||||||
// var frame = this.documentElement;
|
// var frame = this.documentElement;
|
||||||
|
@ -57,9 +57,9 @@ class Contents {
|
||||||
// this.content.style.height = h;
|
// this.content.style.height = h;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.window.getComputedStyle(frame)['height'];
|
return this.window.getComputedStyle(frame)["height"];
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
contentWidth(w) {
|
contentWidth(w) {
|
||||||
|
|
||||||
|
@ -73,10 +73,10 @@ class Contents {
|
||||||
content.style.width = w;
|
content.style.width = w;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.window.getComputedStyle(content)['width'];
|
return this.window.getComputedStyle(content)["width"];
|
||||||
|
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
contentHeight(h) {
|
contentHeight(h) {
|
||||||
|
|
||||||
|
@ -90,9 +90,9 @@ class Contents {
|
||||||
content.style.height = h;
|
content.style.height = h;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.window.getComputedStyle(content)['height'];
|
return this.window.getComputedStyle(content)["height"];
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
textWidth() {
|
textWidth() {
|
||||||
var width;
|
var width;
|
||||||
|
@ -107,7 +107,7 @@ class Contents {
|
||||||
|
|
||||||
return width;
|
return width;
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
textHeight() {
|
textHeight() {
|
||||||
var height;
|
var height;
|
||||||
|
@ -119,19 +119,19 @@ class Contents {
|
||||||
height = range.getBoundingClientRect().height;
|
height = range.getBoundingClientRect().height;
|
||||||
|
|
||||||
return height;
|
return height;
|
||||||
};
|
}
|
||||||
|
|
||||||
scrollWidth() {
|
scrollWidth() {
|
||||||
var width = this.documentElement.scrollWidth;
|
var width = this.documentElement.scrollWidth;
|
||||||
|
|
||||||
return width;
|
return width;
|
||||||
};
|
}
|
||||||
|
|
||||||
scrollHeight() {
|
scrollHeight() {
|
||||||
var height = this.documentElement.scrollHeight;
|
var height = this.documentElement.scrollHeight;
|
||||||
|
|
||||||
return height;
|
return height;
|
||||||
};
|
}
|
||||||
|
|
||||||
overflow(overflow) {
|
overflow(overflow) {
|
||||||
|
|
||||||
|
@ -139,8 +139,8 @@ class Contents {
|
||||||
this.documentElement.style.overflow = overflow;
|
this.documentElement.style.overflow = overflow;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.window.getComputedStyle(this.documentElement)['overflow'];
|
return this.window.getComputedStyle(this.documentElement)["overflow"];
|
||||||
};
|
}
|
||||||
|
|
||||||
overflowX(overflow) {
|
overflowX(overflow) {
|
||||||
|
|
||||||
|
@ -148,8 +148,8 @@ class Contents {
|
||||||
this.documentElement.style.overflowX = overflow;
|
this.documentElement.style.overflowX = overflow;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.window.getComputedStyle(this.documentElement)['overflowX'];
|
return this.window.getComputedStyle(this.documentElement)["overflowX"];
|
||||||
};
|
}
|
||||||
|
|
||||||
overflowY(overflow) {
|
overflowY(overflow) {
|
||||||
|
|
||||||
|
@ -157,8 +157,8 @@ class Contents {
|
||||||
this.documentElement.style.overflowY = overflow;
|
this.documentElement.style.overflowY = overflow;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.window.getComputedStyle(this.documentElement)['overflowY'];
|
return this.window.getComputedStyle(this.documentElement)["overflowY"];
|
||||||
};
|
}
|
||||||
|
|
||||||
css(property, value) {
|
css(property, value) {
|
||||||
var content = this.content || this.document.body;
|
var content = this.content || this.document.body;
|
||||||
|
@ -168,31 +168,31 @@ class Contents {
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.window.getComputedStyle(content)[property];
|
return this.window.getComputedStyle(content)[property];
|
||||||
};
|
}
|
||||||
|
|
||||||
viewport(options) {
|
viewport(options) {
|
||||||
var width, height, scale, scalable;
|
var width, height, scale, scalable;
|
||||||
var $viewport = this.document.querySelector("meta[name='viewport']");
|
var $viewport = this.document.querySelector("meta[name='viewport']");
|
||||||
var newContent = '';
|
var newContent = "";
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* check for the viewport size
|
* check for the viewport size
|
||||||
* <meta name="viewport" content="width=1024,height=697" />
|
* <meta name="viewport" content="width=1024,height=697" />
|
||||||
*/
|
*/
|
||||||
if($viewport && $viewport.hasAttribute("content")) {
|
if($viewport && $viewport.hasAttribute("content")) {
|
||||||
content = $viewport.getAttribute("content");
|
let content = $viewport.getAttribute("content");
|
||||||
contents = content.split(/\s*,\s*/);
|
let contents = content.split(/\s*,\s*/);
|
||||||
if(contents[0]){
|
if(contents[0]){
|
||||||
width = contents[0].replace("width=", '').trim();
|
width = contents[0].replace("width=", "").trim();
|
||||||
}
|
}
|
||||||
if(contents[1]){
|
if(contents[1]){
|
||||||
height = contents[1].replace("height=", '').trim();
|
height = contents[1].replace("height=", "").trim();
|
||||||
}
|
}
|
||||||
if(contents[2]){
|
if(contents[2]){
|
||||||
scale = contents[2].replace("initial-scale=", '').trim();
|
scale = contents[2].replace("initial-scale=", "").trim();
|
||||||
}
|
}
|
||||||
if(contents[3]){
|
if(contents[3]){
|
||||||
scalable = contents[3].replace("user-scalable=", '').trim();
|
scalable = contents[3].replace("user-scalable=", "").trim();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -210,7 +210,7 @@ class Contents {
|
||||||
if (!$viewport) {
|
if (!$viewport) {
|
||||||
$viewport = this.document.createElement("meta");
|
$viewport = this.document.createElement("meta");
|
||||||
$viewport.setAttribute("name", "viewport");
|
$viewport.setAttribute("name", "viewport");
|
||||||
this.document.querySelector('head').appendChild($viewport);
|
this.document.querySelector("head").appendChild($viewport);
|
||||||
}
|
}
|
||||||
|
|
||||||
$viewport.setAttribute("content", newContent);
|
$viewport.setAttribute("content", newContent);
|
||||||
|
@ -221,7 +221,7 @@ class Contents {
|
||||||
width: parseInt(width),
|
width: parseInt(width),
|
||||||
height: parseInt(height)
|
height: parseInt(height)
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
|
|
||||||
|
|
||||||
// layout(layoutFunc) {
|
// layout(layoutFunc) {
|
||||||
|
@ -247,7 +247,7 @@ class Contents {
|
||||||
|
|
||||||
expand() {
|
expand() {
|
||||||
this.emit("expand");
|
this.emit("expand");
|
||||||
};
|
}
|
||||||
|
|
||||||
listeners() {
|
listeners() {
|
||||||
|
|
||||||
|
@ -263,14 +263,14 @@ class Contents {
|
||||||
|
|
||||||
this.resizeListeners();
|
this.resizeListeners();
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
removeListeners() {
|
removeListeners() {
|
||||||
|
|
||||||
this.removeEventListeners();
|
this.removeEventListeners();
|
||||||
|
|
||||||
this.removeSelectionListeners();
|
this.removeSelectionListeners();
|
||||||
};
|
}
|
||||||
|
|
||||||
resizeListeners() {
|
resizeListeners() {
|
||||||
var width, height;
|
var width, height;
|
||||||
|
@ -285,13 +285,13 @@ class Contents {
|
||||||
this._size = {
|
this._size = {
|
||||||
width: width,
|
width: width,
|
||||||
height: height
|
height: height
|
||||||
}
|
};
|
||||||
|
|
||||||
this.emit("resize", this._size);
|
this.emit("resize", this._size);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.expanding = setTimeout(this.resizeListeners.bind(this), 350);
|
this.expanding = setTimeout(this.resizeListeners.bind(this), 350);
|
||||||
};
|
}
|
||||||
|
|
||||||
//https://github.com/tylergaw/media-query-events/blob/master/js/mq-events.js
|
//https://github.com/tylergaw/media-query-events/blob/master/js/mq-events.js
|
||||||
mediaQueryListeners() {
|
mediaQueryListeners() {
|
||||||
|
@ -321,7 +321,7 @@ class Contents {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
observe(target) {
|
observe(target) {
|
||||||
var renderer = this;
|
var renderer = this;
|
||||||
|
@ -343,7 +343,7 @@ class Contents {
|
||||||
observer.observe(target, config);
|
observer.observe(target, config);
|
||||||
|
|
||||||
return observer;
|
return observer;
|
||||||
};
|
}
|
||||||
|
|
||||||
imageLoadListeners(target) {
|
imageLoadListeners(target) {
|
||||||
var images = this.document.querySelectorAll("img");
|
var images = this.document.querySelectorAll("img");
|
||||||
|
@ -356,7 +356,7 @@ class Contents {
|
||||||
img.onload = this.expand.bind(this);
|
img.onload = this.expand.bind(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
fontLoadListeners(target) {
|
fontLoadListeners(target) {
|
||||||
if (!this.document || !this.document.fonts) {
|
if (!this.document || !this.document.fonts) {
|
||||||
|
@ -367,12 +367,12 @@ class Contents {
|
||||||
this.expand();
|
this.expand();
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
root() {
|
root() {
|
||||||
if(!this.document) return null;
|
if(!this.document) return null;
|
||||||
return this.document.documentElement;
|
return this.document.documentElement;
|
||||||
};
|
}
|
||||||
|
|
||||||
locationOf(target, ignoreClass) {
|
locationOf(target, ignoreClass) {
|
||||||
var position;
|
var position;
|
||||||
|
@ -404,8 +404,8 @@ class Contents {
|
||||||
} else if(typeof target === "string" &&
|
} else if(typeof target === "string" &&
|
||||||
target.indexOf("#") > -1) {
|
target.indexOf("#") > -1) {
|
||||||
|
|
||||||
id = target.substring(target.indexOf("#")+1);
|
let id = target.substring(target.indexOf("#")+1);
|
||||||
el = this.document.getElementById(id);
|
let el = this.document.getElementById(id);
|
||||||
|
|
||||||
if(el) {
|
if(el) {
|
||||||
position = el.getBoundingClientRect();
|
position = el.getBoundingClientRect();
|
||||||
|
@ -415,7 +415,7 @@ class Contents {
|
||||||
}
|
}
|
||||||
|
|
||||||
return targetPos;
|
return targetPos;
|
||||||
};
|
}
|
||||||
|
|
||||||
addStylesheet(src) {
|
addStylesheet(src) {
|
||||||
return new Promise(function(resolve, reject){
|
return new Promise(function(resolve, reject){
|
||||||
|
@ -428,18 +428,18 @@ class Contents {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if link already exists
|
// Check if link already exists
|
||||||
$stylesheet = this.document.querySelector('link[href="'+src+'"]');
|
$stylesheet = this.document.querySelector("link[href='"+src+"']");
|
||||||
if ($stylesheet) {
|
if ($stylesheet) {
|
||||||
resolve(true);
|
resolve(true);
|
||||||
return; // already present
|
return; // already present
|
||||||
}
|
}
|
||||||
|
|
||||||
$stylesheet = this.document.createElement('link');
|
$stylesheet = this.document.createElement("link");
|
||||||
$stylesheet.type = 'text/css';
|
$stylesheet.type = "text/css";
|
||||||
$stylesheet.rel = "stylesheet";
|
$stylesheet.rel = "stylesheet";
|
||||||
$stylesheet.href = src;
|
$stylesheet.href = src;
|
||||||
$stylesheet.onload = $stylesheet.onreadystatechange = function() {
|
$stylesheet.onload = $stylesheet.onreadystatechange = function() {
|
||||||
if ( !ready && (!this.readyState || this.readyState == 'complete') ) {
|
if ( !ready && (!this.readyState || this.readyState == "complete") ) {
|
||||||
ready = true;
|
ready = true;
|
||||||
// Let apply
|
// Let apply
|
||||||
setTimeout(function(){
|
setTimeout(function(){
|
||||||
|
@ -451,7 +451,7 @@ class Contents {
|
||||||
this.document.head.appendChild($stylesheet);
|
this.document.head.appendChild($stylesheet);
|
||||||
|
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
};
|
}
|
||||||
|
|
||||||
// https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet/insertRule
|
// https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet/insertRule
|
||||||
addStylesheetRules(rules) {
|
addStylesheetRules(rules) {
|
||||||
|
@ -464,7 +464,7 @@ class Contents {
|
||||||
// Check if link already exists
|
// Check if link already exists
|
||||||
styleEl = this.document.getElementById("#"+key);
|
styleEl = this.document.getElementById("#"+key);
|
||||||
if (!styleEl) {
|
if (!styleEl) {
|
||||||
styleEl = this.document.createElement('style');
|
styleEl = this.document.createElement("style");
|
||||||
styleEl.id = key;
|
styleEl.id = key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -475,22 +475,22 @@ class Contents {
|
||||||
styleSheet = styleEl.sheet;
|
styleSheet = styleEl.sheet;
|
||||||
|
|
||||||
for (var i = 0, rl = rules.length; i < rl; i++) {
|
for (var i = 0, rl = rules.length; i < rl; i++) {
|
||||||
var j = 1, rule = rules[i], selector = rules[i][0], propStr = '';
|
var j = 1, rule = rules[i], selector = rules[i][0], propStr = "";
|
||||||
// If the second argument of a rule is an array of arrays, correct our variables.
|
// If the second argument of a rule is an array of arrays, correct our variables.
|
||||||
if (Object.prototype.toString.call(rule[1][0]) === '[object Array]') {
|
if (Object.prototype.toString.call(rule[1][0]) === "[object Array]") {
|
||||||
rule = rule[1];
|
rule = rule[1];
|
||||||
j = 0;
|
j = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var pl = rule.length; j < pl; j++) {
|
for (var pl = rule.length; j < pl; j++) {
|
||||||
var prop = rule[j];
|
var prop = rule[j];
|
||||||
propStr += prop[0] + ':' + prop[1] + (prop[2] ? ' !important' : '') + ';\n';
|
propStr += prop[0] + ":" + prop[1] + (prop[2] ? " !important" : "") + ";\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert CSS Rule
|
// Insert CSS Rule
|
||||||
styleSheet.insertRule(selector + '{' + propStr + '}', styleSheet.cssRules.length);
|
styleSheet.insertRule(selector + "{" + propStr + "}", styleSheet.cssRules.length);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
addScript(src) {
|
addScript(src) {
|
||||||
|
|
||||||
|
@ -503,12 +503,12 @@ class Contents {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$script = this.document.createElement('script');
|
$script = this.document.createElement("script");
|
||||||
$script.type = 'text/javascript';
|
$script.type = "text/javascript";
|
||||||
$script.async = true;
|
$script.async = true;
|
||||||
$script.src = src;
|
$script.src = src;
|
||||||
$script.onload = $script.onreadystatechange = function() {
|
$script.onload = $script.onreadystatechange = function() {
|
||||||
if ( !ready && (!this.readyState || this.readyState == 'complete') ) {
|
if ( !ready && (!this.readyState || this.readyState == "complete") ) {
|
||||||
ready = true;
|
ready = true;
|
||||||
setTimeout(function(){
|
setTimeout(function(){
|
||||||
resolve(true);
|
resolve(true);
|
||||||
|
@ -519,7 +519,7 @@ class Contents {
|
||||||
this.document.head.appendChild($script);
|
this.document.head.appendChild($script);
|
||||||
|
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
};
|
}
|
||||||
|
|
||||||
addClass(className) {
|
addClass(className) {
|
||||||
var content;
|
var content;
|
||||||
|
@ -530,7 +530,7 @@ class Contents {
|
||||||
|
|
||||||
content.classList.add(className);
|
content.classList.add(className);
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
removeClass(className) {
|
removeClass(className) {
|
||||||
var content;
|
var content;
|
||||||
|
@ -541,7 +541,7 @@ class Contents {
|
||||||
|
|
||||||
content.classList.remove(className);
|
content.classList.remove(className);
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
addEventListeners(){
|
addEventListeners(){
|
||||||
if(!this.document) {
|
if(!this.document) {
|
||||||
|
@ -551,7 +551,7 @@ class Contents {
|
||||||
this.document.addEventListener(eventName, this.triggerEvent.bind(this), false);
|
this.document.addEventListener(eventName, this.triggerEvent.bind(this), false);
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
removeEventListeners(){
|
removeEventListeners(){
|
||||||
if(!this.document) {
|
if(!this.document) {
|
||||||
|
@ -561,26 +561,26 @@ class Contents {
|
||||||
this.document.removeEventListener(eventName, this.triggerEvent, false);
|
this.document.removeEventListener(eventName, this.triggerEvent, false);
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
// Pass browser events
|
// Pass browser events
|
||||||
triggerEvent(e){
|
triggerEvent(e){
|
||||||
this.emit(e.type, e);
|
this.emit(e.type, e);
|
||||||
};
|
}
|
||||||
|
|
||||||
addSelectionListeners(){
|
addSelectionListeners(){
|
||||||
if(!this.document) {
|
if(!this.document) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.document.addEventListener("selectionchange", this.onSelectionChange.bind(this), false);
|
this.document.addEventListener("selectionchange", this.onSelectionChange.bind(this), false);
|
||||||
};
|
}
|
||||||
|
|
||||||
removeSelectionListeners(){
|
removeSelectionListeners(){
|
||||||
if(!this.document) {
|
if(!this.document) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.document.removeEventListener("selectionchange", this.onSelectionChange, false);
|
this.document.removeEventListener("selectionchange", this.onSelectionChange, false);
|
||||||
};
|
}
|
||||||
|
|
||||||
onSelectionChange(e){
|
onSelectionChange(e){
|
||||||
if (this.selectionEndTimeout) {
|
if (this.selectionEndTimeout) {
|
||||||
|
@ -590,7 +590,7 @@ class Contents {
|
||||||
var selection = this.window.getSelection();
|
var selection = this.window.getSelection();
|
||||||
this.triggerSelectedEvent(selection);
|
this.triggerSelectedEvent(selection);
|
||||||
}.bind(this), 500);
|
}.bind(this), 500);
|
||||||
};
|
}
|
||||||
|
|
||||||
triggerSelectedEvent(selection){
|
triggerSelectedEvent(selection){
|
||||||
var range, cfirange;
|
var range, cfirange;
|
||||||
|
@ -604,17 +604,17 @@ class Contents {
|
||||||
this.emit("selectedRange", range);
|
this.emit("selectedRange", range);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
range(_cfi, ignoreClass){
|
range(_cfi, ignoreClass){
|
||||||
var cfi = new EpubCFI(_cfi);
|
var cfi = new EpubCFI(_cfi);
|
||||||
return cfi.toRange(this.document, ignoreClass);
|
return cfi.toRange(this.document, ignoreClass);
|
||||||
};
|
}
|
||||||
|
|
||||||
map(layout){
|
map(layout){
|
||||||
var map = new Mapping(layout);
|
var map = new Mapping(layout);
|
||||||
return map.section();
|
return map.section();
|
||||||
};
|
}
|
||||||
|
|
||||||
size(width, height){
|
size(width, height){
|
||||||
|
|
||||||
|
@ -629,14 +629,13 @@ class Contents {
|
||||||
this.css("margin", "0");
|
this.css("margin", "0");
|
||||||
this.css("boxSizing", "border-box");
|
this.css("boxSizing", "border-box");
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
columns(width, height, columnWidth, gap){
|
columns(width, height, columnWidth, gap){
|
||||||
var COLUMN_AXIS = prefixed('columnAxis');
|
var COLUMN_AXIS = prefixed("columnAxis");
|
||||||
var COLUMN_GAP = prefixed('columnGap');
|
var COLUMN_GAP = prefixed("columnGap");
|
||||||
var COLUMN_WIDTH = prefixed('columnWidth');
|
var COLUMN_WIDTH = prefixed("columnWidth");
|
||||||
var COLUMN_FILL = prefixed('columnFill');
|
var COLUMN_FILL = prefixed("columnFill");
|
||||||
var textWidth;
|
|
||||||
|
|
||||||
this.width(width);
|
this.width(width);
|
||||||
this.height(height);
|
this.height(height);
|
||||||
|
@ -655,20 +654,20 @@ class Contents {
|
||||||
|
|
||||||
this.css(COLUMN_GAP, gap+"px");
|
this.css(COLUMN_GAP, gap+"px");
|
||||||
this.css(COLUMN_WIDTH, columnWidth+"px");
|
this.css(COLUMN_WIDTH, columnWidth+"px");
|
||||||
};
|
}
|
||||||
|
|
||||||
scaler(scale, offsetX, offsetY){
|
scaler(scale, offsetX, offsetY){
|
||||||
var scale = "scale(" + scale + ")";
|
var scaleStr = "scale(" + scale + ")";
|
||||||
var translate = '';
|
var translateStr = "";
|
||||||
// this.css("position", "absolute"));
|
// this.css("position", "absolute"));
|
||||||
this.css("transformOrigin", "top left");
|
this.css("transformOrigin", "top left");
|
||||||
|
|
||||||
if (offsetX >= 0 || offsetY >= 0) {
|
if (offsetX >= 0 || offsetY >= 0) {
|
||||||
translate = " translate(" + (offsetX || 0 )+ "px, " + (offsetY || 0 )+ "px )";
|
translateStr = " translate(" + (offsetX || 0 )+ "px, " + (offsetY || 0 )+ "px )";
|
||||||
}
|
}
|
||||||
|
|
||||||
this.css("transform", scale + translate);
|
this.css("transform", scaleStr + translateStr);
|
||||||
};
|
}
|
||||||
|
|
||||||
fit(width, height){
|
fit(width, height){
|
||||||
var viewport = this.viewport();
|
var viewport = this.viewport();
|
||||||
|
@ -689,13 +688,13 @@ class Contents {
|
||||||
this.scaler(scale, 0, offsetY);
|
this.scaler(scale, 0, offsetY);
|
||||||
|
|
||||||
this.css("backgroundColor", "transparent");
|
this.css("backgroundColor", "transparent");
|
||||||
};
|
}
|
||||||
|
|
||||||
mapPage(cfiBase, start, end) {
|
mapPage(cfiBase, start, end) {
|
||||||
var mapping = new Mapping();
|
var mapping = new Mapping();
|
||||||
|
|
||||||
return mapping.page(this, cfiBase, start, end);
|
return mapping.page(this, cfiBase, start, end);
|
||||||
};
|
}
|
||||||
|
|
||||||
destroy() {
|
destroy() {
|
||||||
// Stop observing
|
// Stop observing
|
||||||
|
@ -705,7 +704,7 @@ class Contents {
|
||||||
|
|
||||||
this.removeListeners();
|
this.removeListeners();
|
||||||
|
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
EventEmitter(Contents.prototype);
|
EventEmitter(Contents.prototype);
|
||||||
|
|
18
src/epub.js
18
src/epub.js
|
@ -1,7 +1,7 @@
|
||||||
import Book from './book';
|
import Book from "./book";
|
||||||
import EpubCFI from './epubcfi';
|
import EpubCFI from "./epubcfi";
|
||||||
import Rendition from './rendition';
|
import Rendition from "./rendition";
|
||||||
import Contents from './contents';
|
import Contents from "./contents";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new Book
|
* Creates a new Book
|
||||||
|
@ -12,9 +12,9 @@ import Contents from './contents';
|
||||||
*/
|
*/
|
||||||
function ePub(url, options) {
|
function ePub(url, options) {
|
||||||
return new Book(url, options);
|
return new Book(url, options);
|
||||||
};
|
}
|
||||||
|
|
||||||
ePub.VERSION = "0.3.0";
|
ePub.VERSION = "0.3.1";
|
||||||
|
|
||||||
if (typeof(global) !== "undefined") {
|
if (typeof(global) !== "undefined") {
|
||||||
global.EPUBJS_VERSION = ePub.VERSION;
|
global.EPUBJS_VERSION = ePub.VERSION;
|
||||||
|
@ -45,10 +45,10 @@ ePub.register = {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Default Views
|
// Default Views
|
||||||
ePub.register.view("iframe", require('./managers/views/iframe'));
|
ePub.register.view("iframe", require("./managers/views/iframe"));
|
||||||
|
|
||||||
// Default View Managers
|
// Default View Managers
|
||||||
ePub.register.manager("default", require('./managers/default'));
|
ePub.register.manager("default", require("./managers/default"));
|
||||||
ePub.register.manager("continuous", require('./managers/continuous'));
|
ePub.register.manager("continuous", require("./managers/continuous"));
|
||||||
|
|
||||||
export default ePub;
|
export default ePub;
|
||||||
|
|
218
src/epubcfi.js
218
src/epubcfi.js
|
@ -1,4 +1,4 @@
|
||||||
import {extend, type, findChildren} from './utils/core';
|
import {extend, type, findChildren} from "./utils/core";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
EPUB CFI spec: http://www.idpf.org/epub/linking/cfi/epub-cfi.html
|
EPUB CFI spec: http://www.idpf.org/epub/linking/cfi/epub-cfi.html
|
||||||
|
@ -14,16 +14,16 @@ import {extend, type, findChildren} from './utils/core';
|
||||||
- Text Location Assertion ([)
|
- Text Location Assertion ([)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var ELEMENT_NODE = 1;
|
const ELEMENT_NODE = 1;
|
||||||
var TEXT_NODE = 3;
|
const TEXT_NODE = 3;
|
||||||
var COMMENT_NODE = 8;
|
// const COMMENT_NODE = 8;
|
||||||
var DOCUMENT_NODE = 9;
|
const DOCUMENT_NODE = 9;
|
||||||
|
|
||||||
class EpubCFI {
|
class EpubCFI {
|
||||||
constructor(cfiFrom, base, ignoreClass){
|
constructor(cfiFrom, base, ignoreClass){
|
||||||
var type;
|
var type;
|
||||||
|
|
||||||
this.str = '';
|
this.str = "";
|
||||||
|
|
||||||
this.base = {};
|
this.base = {};
|
||||||
this.spinePos = 0; // For compatibility
|
this.spinePos = 0; // For compatibility
|
||||||
|
@ -34,52 +34,52 @@ class EpubCFI {
|
||||||
this.start = null;
|
this.start = null;
|
||||||
this.end = null;
|
this.end = null;
|
||||||
|
|
||||||
// Allow instantiation without the 'new' keyword
|
// Allow instantiation without the "new" keyword
|
||||||
if (!(this instanceof EpubCFI)) {
|
if (!(this instanceof EpubCFI)) {
|
||||||
return new EpubCFI(cfiFrom, base, ignoreClass);
|
return new EpubCFI(cfiFrom, base, ignoreClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(typeof base === 'string') {
|
if(typeof base === "string") {
|
||||||
this.base = this.parseComponent(base);
|
this.base = this.parseComponent(base);
|
||||||
} else if(typeof base === 'object' && base.steps) {
|
} else if(typeof base === "object" && base.steps) {
|
||||||
this.base = base;
|
this.base = base;
|
||||||
}
|
}
|
||||||
|
|
||||||
type = this.checkType(cfiFrom);
|
type = this.checkType(cfiFrom);
|
||||||
|
|
||||||
|
|
||||||
if(type === 'string') {
|
if(type === "string") {
|
||||||
this.str = cfiFrom;
|
this.str = cfiFrom;
|
||||||
return extend(this, this.parse(cfiFrom));
|
return extend(this, this.parse(cfiFrom));
|
||||||
} else if (type === 'range') {
|
} else if (type === "range") {
|
||||||
return extend(this, this.fromRange(cfiFrom, this.base, ignoreClass));
|
return extend(this, this.fromRange(cfiFrom, this.base, ignoreClass));
|
||||||
} else if (type === 'node') {
|
} else if (type === "node") {
|
||||||
return extend(this, this.fromNode(cfiFrom, this.base, ignoreClass));
|
return extend(this, this.fromNode(cfiFrom, this.base, ignoreClass));
|
||||||
} else if (type === 'EpubCFI' && cfiFrom.path) {
|
} else if (type === "EpubCFI" && cfiFrom.path) {
|
||||||
return cfiFrom;
|
return cfiFrom;
|
||||||
} else if (!cfiFrom) {
|
} else if (!cfiFrom) {
|
||||||
return this;
|
return this;
|
||||||
} else {
|
} else {
|
||||||
throw new TypeError('not a valid argument for EpubCFI');
|
throw new TypeError("not a valid argument for EpubCFI");
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
checkType(cfi) {
|
checkType(cfi) {
|
||||||
|
|
||||||
if (this.isCfiString(cfi)) {
|
if (this.isCfiString(cfi)) {
|
||||||
return 'string';
|
return "string";
|
||||||
// Is a range object
|
// Is a range object
|
||||||
} else if (typeof cfi === 'object' && (type(cfi) === "Range" || typeof(cfi.startContainer) != "undefined")){
|
} else if (typeof cfi === "object" && (type(cfi) === "Range" || typeof(cfi.startContainer) != "undefined")){
|
||||||
return 'range';
|
return "range";
|
||||||
} else if (typeof cfi === 'object' && typeof(cfi.nodeType) != "undefined" ){ // || typeof cfi === 'function'
|
} else if (typeof cfi === "object" && typeof(cfi.nodeType) != "undefined" ){ // || typeof cfi === "function"
|
||||||
return 'node';
|
return "node";
|
||||||
} else if (typeof cfi === 'object' && cfi instanceof EpubCFI){
|
} else if (typeof cfi === "object" && cfi instanceof EpubCFI){
|
||||||
return 'EpubCFI';
|
return "EpubCFI";
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
parse(cfiStr) {
|
parse(cfiStr) {
|
||||||
var cfi = {
|
var cfi = {
|
||||||
|
@ -128,7 +128,7 @@ class EpubCFI {
|
||||||
cfi.spinePos = cfi.base.steps[1].index;
|
cfi.spinePos = cfi.base.steps[1].index;
|
||||||
|
|
||||||
return cfi;
|
return cfi;
|
||||||
};
|
}
|
||||||
|
|
||||||
parseComponent(componentStr){
|
parseComponent(componentStr){
|
||||||
var component = {
|
var component = {
|
||||||
|
@ -138,8 +138,8 @@ class EpubCFI {
|
||||||
assertion: null
|
assertion: null
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
var parts = componentStr.split(':');
|
var parts = componentStr.split(":");
|
||||||
var steps = parts[0].split('/');
|
var steps = parts[0].split("/");
|
||||||
var terminal;
|
var terminal;
|
||||||
|
|
||||||
if(parts.length > 1) {
|
if(parts.length > 1) {
|
||||||
|
@ -147,7 +147,7 @@ class EpubCFI {
|
||||||
component.terminal = this.parseTerminal(terminal);
|
component.terminal = this.parseTerminal(terminal);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (steps[0] === '') {
|
if (steps[0] === "") {
|
||||||
steps.shift(); // Ignore the first slash
|
steps.shift(); // Ignore the first slash
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,7 +156,7 @@ class EpubCFI {
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
return component;
|
return component;
|
||||||
};
|
}
|
||||||
|
|
||||||
parseStep(stepStr){
|
parseStep(stepStr){
|
||||||
var type, num, index, has_brackets, id;
|
var type, num, index, has_brackets, id;
|
||||||
|
@ -183,46 +183,46 @@ class EpubCFI {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"type" : type,
|
"type" : type,
|
||||||
'index' : index,
|
"index" : index,
|
||||||
'id' : id || null
|
"id" : id || null
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
parseTerminal(termialStr){
|
parseTerminal(termialStr){
|
||||||
var characterOffset, textLocationAssertion;
|
var characterOffset, textLocationAssertion;
|
||||||
var assertion = termialStr.match(/\[(.*)\]/);
|
var assertion = termialStr.match(/\[(.*)\]/);
|
||||||
|
|
||||||
if(assertion && assertion[1]){
|
if(assertion && assertion[1]){
|
||||||
characterOffset = parseInt(termialStr.split('[')[0]) || null;
|
characterOffset = parseInt(termialStr.split("[")[0]) || null;
|
||||||
textLocationAssertion = assertion[1];
|
textLocationAssertion = assertion[1];
|
||||||
} else {
|
} else {
|
||||||
characterOffset = parseInt(termialStr) || null;
|
characterOffset = parseInt(termialStr) || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'offset': characterOffset,
|
"offset": characterOffset,
|
||||||
'assertion': textLocationAssertion
|
"assertion": textLocationAssertion
|
||||||
};
|
};
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
getChapterComponent(cfiStr) {
|
getChapterComponent(cfiStr) {
|
||||||
|
|
||||||
var indirection = cfiStr.split("!");
|
var indirection = cfiStr.split("!");
|
||||||
|
|
||||||
return indirection[0];
|
return indirection[0];
|
||||||
};
|
}
|
||||||
|
|
||||||
getPathComponent(cfiStr) {
|
getPathComponent(cfiStr) {
|
||||||
|
|
||||||
var indirection = cfiStr.split("!");
|
var indirection = cfiStr.split("!");
|
||||||
|
|
||||||
if(indirection[1]) {
|
if(indirection[1]) {
|
||||||
let ranges = indirection[1].split(',');
|
let ranges = indirection[1].split(",");
|
||||||
return ranges[0];
|
return ranges[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
getRange(cfiStr) {
|
getRange(cfiStr) {
|
||||||
|
|
||||||
|
@ -236,12 +236,12 @@ class EpubCFI {
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
};
|
}
|
||||||
|
|
||||||
getCharecterOffsetComponent(cfiStr) {
|
getCharecterOffsetComponent(cfiStr) {
|
||||||
var splitStr = cfiStr.split(":");
|
var splitStr = cfiStr.split(":");
|
||||||
return splitStr[1] || '';
|
return splitStr[1] || "";
|
||||||
};
|
}
|
||||||
|
|
||||||
joinSteps(steps) {
|
joinSteps(steps) {
|
||||||
if(!steps) {
|
if(!steps) {
|
||||||
|
@ -249,13 +249,13 @@ class EpubCFI {
|
||||||
}
|
}
|
||||||
|
|
||||||
return steps.map(function(part){
|
return steps.map(function(part){
|
||||||
var segment = '';
|
var segment = "";
|
||||||
|
|
||||||
if(part.type === 'element') {
|
if(part.type === "element") {
|
||||||
segment += (part.index + 1) * 2;
|
segment += (part.index + 1) * 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(part.type === 'text') {
|
if(part.type === "text") {
|
||||||
segment += 1 + (2 * part.index); // TODO: double check that this is odd
|
segment += 1 + (2 * part.index); // TODO: double check that this is odd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -265,58 +265,58 @@ class EpubCFI {
|
||||||
|
|
||||||
return segment;
|
return segment;
|
||||||
|
|
||||||
}).join('/');
|
}).join("/");
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
segmentString(segment) {
|
segmentString(segment) {
|
||||||
var segmentString = '/';
|
var segmentString = "/";
|
||||||
|
|
||||||
segmentString += this.joinSteps(segment.steps);
|
segmentString += this.joinSteps(segment.steps);
|
||||||
|
|
||||||
if(segment.terminal && segment.terminal.offset != null){
|
if(segment.terminal && segment.terminal.offset != null){
|
||||||
segmentString += ':' + segment.terminal.offset;
|
segmentString += ":" + segment.terminal.offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(segment.terminal && segment.terminal.assertion != null){
|
if(segment.terminal && segment.terminal.assertion != null){
|
||||||
segmentString += '[' + segment.terminal.assertion + ']';
|
segmentString += "[" + segment.terminal.assertion + "]";
|
||||||
}
|
}
|
||||||
|
|
||||||
return segmentString;
|
return segmentString;
|
||||||
};
|
}
|
||||||
|
|
||||||
toString() {
|
toString() {
|
||||||
var cfiString = 'epubcfi(';
|
var cfiString = "epubcfi(";
|
||||||
|
|
||||||
cfiString += this.segmentString(this.base);
|
cfiString += this.segmentString(this.base);
|
||||||
|
|
||||||
cfiString += '!';
|
cfiString += "!";
|
||||||
cfiString += this.segmentString(this.path);
|
cfiString += this.segmentString(this.path);
|
||||||
|
|
||||||
// Add Range, if present
|
// Add Range, if present
|
||||||
if(this.start) {
|
if(this.start) {
|
||||||
cfiString += ',';
|
cfiString += ",";
|
||||||
cfiString += this.segmentString(this.start);
|
cfiString += this.segmentString(this.start);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(this.end) {
|
if(this.end) {
|
||||||
cfiString += ',';
|
cfiString += ",";
|
||||||
cfiString += this.segmentString(this.end);
|
cfiString += this.segmentString(this.end);
|
||||||
}
|
}
|
||||||
|
|
||||||
cfiString += ")";
|
cfiString += ")";
|
||||||
|
|
||||||
return cfiString;
|
return cfiString;
|
||||||
};
|
}
|
||||||
|
|
||||||
compare(cfiOne, cfiTwo) {
|
compare(cfiOne, cfiTwo) {
|
||||||
var stepsA, stepsB;
|
var stepsA, stepsB;
|
||||||
var terminalA, terminalB;
|
var terminalA, terminalB;
|
||||||
|
|
||||||
if(typeof cfiOne === 'string') {
|
if(typeof cfiOne === "string") {
|
||||||
cfiOne = new EpubCFI(cfiOne);
|
cfiOne = new EpubCFI(cfiOne);
|
||||||
}
|
}
|
||||||
if(typeof cfiTwo === 'string') {
|
if(typeof cfiTwo === "string") {
|
||||||
cfiTwo = new EpubCFI(cfiTwo);
|
cfiTwo = new EpubCFI(cfiTwo);
|
||||||
}
|
}
|
||||||
// Compare Spine Positions
|
// Compare Spine Positions
|
||||||
|
@ -375,18 +375,18 @@ class EpubCFI {
|
||||||
|
|
||||||
// CFI's are equal
|
// CFI's are equal
|
||||||
return 0;
|
return 0;
|
||||||
};
|
}
|
||||||
|
|
||||||
step(node) {
|
step(node) {
|
||||||
var nodeType = (node.nodeType === TEXT_NODE) ? 'text' : 'element';
|
var nodeType = (node.nodeType === TEXT_NODE) ? "text" : "element";
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'id' : node.id,
|
"id" : node.id,
|
||||||
'tagName' : node.tagName,
|
"tagName" : node.tagName,
|
||||||
'type' : nodeType,
|
"type" : nodeType,
|
||||||
'index' : this.position(node)
|
"index" : this.position(node)
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
filteredStep(node, ignoreClass) {
|
filteredStep(node, ignoreClass) {
|
||||||
var filteredNode = this.filter(node, ignoreClass);
|
var filteredNode = this.filter(node, ignoreClass);
|
||||||
|
@ -398,15 +398,15 @@ class EpubCFI {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise add the filter node in
|
// Otherwise add the filter node in
|
||||||
nodeType = (filteredNode.nodeType === TEXT_NODE) ? 'text' : 'element';
|
nodeType = (filteredNode.nodeType === TEXT_NODE) ? "text" : "element";
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'id' : filteredNode.id,
|
"id" : filteredNode.id,
|
||||||
'tagName' : filteredNode.tagName,
|
"tagName" : filteredNode.tagName,
|
||||||
'type' : nodeType,
|
"type" : nodeType,
|
||||||
'index' : this.filteredPosition(filteredNode, ignoreClass)
|
"index" : this.filteredPosition(filteredNode, ignoreClass)
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
pathTo(node, offset, ignoreClass) {
|
pathTo(node, offset, ignoreClass) {
|
||||||
var segment = {
|
var segment = {
|
||||||
|
@ -443,8 +443,8 @@ class EpubCFI {
|
||||||
// Make sure we are getting to a textNode if there is an offset
|
// Make sure we are getting to a textNode if there is an offset
|
||||||
if(segment.steps[segment.steps.length-1].type != "text") {
|
if(segment.steps[segment.steps.length-1].type != "text") {
|
||||||
segment.steps.push({
|
segment.steps.push({
|
||||||
'type' : "text",
|
"type" : "text",
|
||||||
'index' : 0
|
"index" : 0
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -466,7 +466,7 @@ class EpubCFI {
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
};
|
}
|
||||||
|
|
||||||
fromRange(range, base, ignoreClass) {
|
fromRange(range, base, ignoreClass) {
|
||||||
var cfi = {
|
var cfi = {
|
||||||
|
@ -487,14 +487,14 @@ class EpubCFI {
|
||||||
|
|
||||||
if (ignoreClass) {
|
if (ignoreClass) {
|
||||||
// Tell pathTo if / what to ignore
|
// Tell pathTo if / what to ignore
|
||||||
needsIgnoring = (start.ownerDocument.querySelector('.' + ignoreClass) != null);
|
needsIgnoring = (start.ownerDocument.querySelector("." + ignoreClass) != null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (typeof base === 'string') {
|
if (typeof base === "string") {
|
||||||
cfi.base = this.parseComponent(base);
|
cfi.base = this.parseComponent(base);
|
||||||
cfi.spinePos = cfi.base.steps[1].index;
|
cfi.spinePos = cfi.base.steps[1].index;
|
||||||
} else if (typeof base === 'object') {
|
} else if (typeof base === "object") {
|
||||||
cfi.base = base;
|
cfi.base = base;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -544,7 +544,7 @@ class EpubCFI {
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
cfi.start.steps = cfi.start.steps.slice(cfi.path.steps.length);
|
cfi.start.steps = cfi.start.steps.slice(cfi.path.steps.length);
|
||||||
cfi.end.steps = cfi.end.steps.slice(cfi.path.steps.length);
|
cfi.end.steps = cfi.end.steps.slice(cfi.path.steps.length);
|
||||||
|
@ -564,24 +564,17 @@ class EpubCFI {
|
||||||
end: null
|
end: null
|
||||||
};
|
};
|
||||||
|
|
||||||
var needsIgnoring = false;
|
if (typeof base === "string") {
|
||||||
|
|
||||||
if (ignoreClass) {
|
|
||||||
// Tell pathTo if / what to ignore
|
|
||||||
needsIgnoring = (anchor.ownerDocument.querySelector('.' + ignoreClass) != null);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof base === 'string') {
|
|
||||||
cfi.base = this.parseComponent(base);
|
cfi.base = this.parseComponent(base);
|
||||||
cfi.spinePos = cfi.base.steps[1].index;
|
cfi.spinePos = cfi.base.steps[1].index;
|
||||||
} else if (typeof base === 'object') {
|
} else if (typeof base === "object") {
|
||||||
cfi.base = base;
|
cfi.base = base;
|
||||||
}
|
}
|
||||||
|
|
||||||
cfi.path = this.pathTo(anchor, null, ignoreClass);
|
cfi.path = this.pathTo(anchor, null, ignoreClass);
|
||||||
|
|
||||||
return cfi;
|
return cfi;
|
||||||
};
|
}
|
||||||
|
|
||||||
filter(anchor, ignoreClass) {
|
filter(anchor, ignoreClass) {
|
||||||
var needsIgnoring;
|
var needsIgnoring;
|
||||||
|
@ -624,15 +617,11 @@ class EpubCFI {
|
||||||
return anchor;
|
return anchor;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
patchOffset(anchor, offset, ignoreClass) {
|
patchOffset(anchor, offset, ignoreClass) {
|
||||||
var needsIgnoring;
|
|
||||||
var sibling;
|
|
||||||
|
|
||||||
if (anchor.nodeType != TEXT_NODE) {
|
if (anchor.nodeType != TEXT_NODE) {
|
||||||
console.error("Anchor must be a text node");
|
throw new Error("Anchor must be a text node");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var curr = anchor;
|
var curr = anchor;
|
||||||
|
@ -661,7 +650,7 @@ class EpubCFI {
|
||||||
|
|
||||||
return totalOffset;
|
return totalOffset;
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
normalizedMap(children, nodeType, ignoreClass) {
|
normalizedMap(children, nodeType, ignoreClass) {
|
||||||
var output = {};
|
var output = {};
|
||||||
|
@ -695,11 +684,10 @@ class EpubCFI {
|
||||||
}
|
}
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
};
|
}
|
||||||
|
|
||||||
position(anchor) {
|
position(anchor) {
|
||||||
var children, index, map;
|
var children, index;
|
||||||
var childNodes, node;
|
|
||||||
if (anchor.nodeType === ELEMENT_NODE) {
|
if (anchor.nodeType === ELEMENT_NODE) {
|
||||||
children = anchor.parentNode.children;
|
children = anchor.parentNode.children;
|
||||||
if (!children) {
|
if (!children) {
|
||||||
|
@ -712,7 +700,7 @@ class EpubCFI {
|
||||||
}
|
}
|
||||||
|
|
||||||
return index;
|
return index;
|
||||||
};
|
}
|
||||||
|
|
||||||
filteredPosition(anchor, ignoreClass) {
|
filteredPosition(anchor, ignoreClass) {
|
||||||
var children, index, map;
|
var children, index, map;
|
||||||
|
@ -734,7 +722,7 @@ class EpubCFI {
|
||||||
index = Array.prototype.indexOf.call(children, anchor);
|
index = Array.prototype.indexOf.call(children, anchor);
|
||||||
|
|
||||||
return map[index];
|
return map[index];
|
||||||
};
|
}
|
||||||
|
|
||||||
stepsToXpath(steps) {
|
stepsToXpath(steps) {
|
||||||
var xpath = [".", "*"];
|
var xpath = [".", "*"];
|
||||||
|
@ -752,7 +740,7 @@ class EpubCFI {
|
||||||
});
|
});
|
||||||
|
|
||||||
return xpath.join("/");
|
return xpath.join("/");
|
||||||
};
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -788,7 +776,7 @@ class EpubCFI {
|
||||||
|
|
||||||
return query.join(">");
|
return query.join(">");
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
textNodes(container, ignoreClass) {
|
textNodes(container, ignoreClass) {
|
||||||
return Array.prototype.slice.call(container.childNodes).
|
return Array.prototype.slice.call(container.childNodes).
|
||||||
|
@ -800,7 +788,7 @@ class EpubCFI {
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
walkToNode(steps, _doc, ignoreClass) {
|
walkToNode(steps, _doc, ignoreClass) {
|
||||||
var doc = _doc || document;
|
var doc = _doc || document;
|
||||||
|
@ -818,17 +806,17 @@ class EpubCFI {
|
||||||
container = this.textNodes(container, ignoreClass)[step.index];
|
container = this.textNodes(container, ignoreClass)[step.index];
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
return container;
|
return container;
|
||||||
};
|
}
|
||||||
|
|
||||||
findNode(steps, _doc, ignoreClass) {
|
findNode(steps, _doc, ignoreClass) {
|
||||||
var doc = _doc || document;
|
var doc = _doc || document;
|
||||||
var container;
|
var container;
|
||||||
var xpath;
|
var xpath;
|
||||||
|
|
||||||
if(!ignoreClass && typeof doc.evaluate != 'undefined') {
|
if(!ignoreClass && typeof doc.evaluate != "undefined") {
|
||||||
xpath = this.stepsToXpath(steps);
|
xpath = this.stepsToXpath(steps);
|
||||||
container = doc.evaluate(xpath, doc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
|
container = doc.evaluate(xpath, doc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
|
||||||
} else if(ignoreClass) {
|
} else if(ignoreClass) {
|
||||||
|
@ -838,19 +826,17 @@ class EpubCFI {
|
||||||
}
|
}
|
||||||
|
|
||||||
return container;
|
return container;
|
||||||
};
|
}
|
||||||
|
|
||||||
fixMiss(steps, offset, _doc, ignoreClass) {
|
fixMiss(steps, offset, _doc, ignoreClass) {
|
||||||
var container = this.findNode(steps.slice(0,-1), _doc, ignoreClass);
|
var container = this.findNode(steps.slice(0,-1), _doc, ignoreClass);
|
||||||
var children = container.childNodes;
|
var children = container.childNodes;
|
||||||
var map = this.normalizedMap(children, TEXT_NODE, ignoreClass);
|
var map = this.normalizedMap(children, TEXT_NODE, ignoreClass);
|
||||||
var i;
|
|
||||||
var child;
|
var child;
|
||||||
var len;
|
var len;
|
||||||
var childIndex;
|
|
||||||
var lastStepIndex = steps[steps.length-1].index;
|
var lastStepIndex = steps[steps.length-1].index;
|
||||||
|
|
||||||
for (var childIndex in map) {
|
for (let childIndex in map) {
|
||||||
if (!map.hasOwnProperty(childIndex)) return;
|
if (!map.hasOwnProperty(childIndex)) return;
|
||||||
|
|
||||||
if(map[childIndex] === lastStepIndex) {
|
if(map[childIndex] === lastStepIndex) {
|
||||||
|
@ -874,7 +860,7 @@ class EpubCFI {
|
||||||
offset: offset
|
offset: offset
|
||||||
};
|
};
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
toRange(_doc, ignoreClass) {
|
toRange(_doc, ignoreClass) {
|
||||||
var doc = _doc || document;
|
var doc = _doc || document;
|
||||||
|
@ -882,7 +868,7 @@ class EpubCFI {
|
||||||
var start, end, startContainer, endContainer;
|
var start, end, startContainer, endContainer;
|
||||||
var cfi = this;
|
var cfi = this;
|
||||||
var startSteps, endSteps;
|
var startSteps, endSteps;
|
||||||
var needsIgnoring = ignoreClass ? (doc.querySelector('.' + ignoreClass) != null) : false;
|
var needsIgnoring = ignoreClass ? (doc.querySelector("." + ignoreClass) != null) : false;
|
||||||
var missed;
|
var missed;
|
||||||
|
|
||||||
if (cfi.range) {
|
if (cfi.range) {
|
||||||
|
@ -934,23 +920,23 @@ class EpubCFI {
|
||||||
|
|
||||||
// doc.defaultView.getSelection().addRange(range);
|
// doc.defaultView.getSelection().addRange(range);
|
||||||
return range;
|
return range;
|
||||||
};
|
}
|
||||||
|
|
||||||
// is a cfi string, should be wrapped with "epubcfi()"
|
// is a cfi string, should be wrapped with "epubcfi()"
|
||||||
isCfiString(str) {
|
isCfiString(str) {
|
||||||
if(typeof str === 'string' &&
|
if(typeof str === "string" &&
|
||||||
str.indexOf("epubcfi(") === 0 &&
|
str.indexOf("epubcfi(") === 0 &&
|
||||||
str[str.length-1] === ")") {
|
str[str.length-1] === ")") {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
};
|
}
|
||||||
|
|
||||||
generateChapterComponent(_spineNodeIndex, _pos, id) {
|
generateChapterComponent(_spineNodeIndex, _pos, id) {
|
||||||
var pos = parseInt(_pos),
|
var pos = parseInt(_pos),
|
||||||
spineNodeIndex = _spineNodeIndex + 1,
|
spineNodeIndex = _spineNodeIndex + 1,
|
||||||
cfi = '/'+spineNodeIndex+'/';
|
cfi = "/"+spineNodeIndex+"/";
|
||||||
|
|
||||||
cfi += (pos + 1) * 2;
|
cfi += (pos + 1) * 2;
|
||||||
|
|
||||||
|
@ -959,7 +945,7 @@ class EpubCFI {
|
||||||
}
|
}
|
||||||
|
|
||||||
return cfi;
|
return cfi;
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default EpubCFI;
|
export default EpubCFI;
|
||||||
|
|
12
src/index.js
12
src/index.js
|
@ -1,9 +1,9 @@
|
||||||
import Book from './book';
|
import Book from "./book";
|
||||||
import EpubCFI from './epubcfi';
|
import EpubCFI from "./epubcfi";
|
||||||
import Rendition from './rendition';
|
import Rendition from "./rendition";
|
||||||
import Contents from './contents';
|
import Contents from "./contents";
|
||||||
import Layout from './layout';
|
import Layout from "./layout";
|
||||||
import ePub from './epub';
|
import ePub from "./epub";
|
||||||
|
|
||||||
export default ePub;
|
export default ePub;
|
||||||
export {
|
export {
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
import core from './utils/core';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Figures out the CSS to apply for a layout
|
* Figures out the CSS to apply for a layout
|
||||||
* @class
|
* @class
|
||||||
|
@ -32,7 +30,7 @@ class Layout {
|
||||||
this.columnWidth = 0;
|
this.columnWidth = 0;
|
||||||
this.gap = 0;
|
this.gap = 0;
|
||||||
this.divisor = 1;
|
this.divisor = 1;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Switch the flow between paginated and scrolled
|
* Switch the flow between paginated and scrolled
|
||||||
|
@ -69,7 +67,7 @@ class Layout {
|
||||||
var gap = _gap || 0;
|
var gap = _gap || 0;
|
||||||
|
|
||||||
//-- Check the width and create even width columns
|
//-- Check the width and create even width columns
|
||||||
var fullWidth = Math.floor(_width);
|
// var fullWidth = Math.floor(_width);
|
||||||
var width = _width;
|
var width = _width;
|
||||||
|
|
||||||
var section = Math.floor(width / 8);
|
var section = Math.floor(width / 8);
|
||||||
|
@ -115,7 +113,7 @@ class Layout {
|
||||||
this.columnWidth = colWidth;
|
this.columnWidth = colWidth;
|
||||||
this.gap = gap;
|
this.gap = gap;
|
||||||
this.divisor = divisor;
|
this.divisor = divisor;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Apply Css to a Document
|
* Apply Css to a Document
|
||||||
|
@ -134,7 +132,7 @@ class Layout {
|
||||||
}
|
}
|
||||||
|
|
||||||
return formating; // might be a promise in some View Managers
|
return formating; // might be a promise in some View Managers
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Count number of pages
|
* Count number of pages
|
||||||
|
@ -150,7 +148,7 @@ class Layout {
|
||||||
spreads : spreads,
|
spreads : spreads,
|
||||||
pages : spreads * this.divisor
|
pages : spreads * this.divisor
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Layout;
|
export default Layout;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import {qs, sprint, locationOf} from './utils/core';
|
import {qs, sprint, locationOf} from "./utils/core";
|
||||||
import Queue from './queue';
|
import Queue from "./utils/queue";
|
||||||
import EpubCFI from './epubcfi';
|
import EpubCFI from "./epubcfi";
|
||||||
import EventEmitter from 'event-emitter';
|
import EventEmitter from "event-emitter";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find Locations for a Book
|
* Find Locations for a Book
|
||||||
|
@ -23,7 +23,7 @@ class Locations {
|
||||||
|
|
||||||
this._current = 0;
|
this._current = 0;
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load all of sections in the book to generate locations
|
* Load all of sections in the book to generate locations
|
||||||
|
@ -55,7 +55,7 @@ class Locations {
|
||||||
// console.log(this.percentage(this.book.rendition.location.start), this.percentage(this.book.rendition.location.end));
|
// console.log(this.percentage(this.book.rendition.location.start), this.percentage(this.book.rendition.location.end));
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
createRange () {
|
createRange () {
|
||||||
return {
|
return {
|
||||||
|
@ -63,8 +63,8 @@ class Locations {
|
||||||
startOffset: undefined,
|
startOffset: undefined,
|
||||||
endContainer: undefined,
|
endContainer: undefined,
|
||||||
endOffset: undefined
|
endOffset: undefined
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
process(section) {
|
process(section) {
|
||||||
|
|
||||||
|
@ -74,13 +74,13 @@ class Locations {
|
||||||
this._locations = this._locations.concat(locations);
|
this._locations = this._locations.concat(locations);
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
parse(contents, cfiBase, chars) {
|
parse(contents, cfiBase, chars) {
|
||||||
var locations = [];
|
var locations = [];
|
||||||
var range;
|
var range;
|
||||||
var doc = contents.ownerDocument;
|
var doc = contents.ownerDocument;
|
||||||
var body = qs(doc, 'body');
|
var body = qs(doc, "body");
|
||||||
var counter = 0;
|
var counter = 0;
|
||||||
var prev;
|
var prev;
|
||||||
var _break = chars || this.break;
|
var _break = chars || this.break;
|
||||||
|
@ -151,7 +151,7 @@ class Locations {
|
||||||
}
|
}
|
||||||
|
|
||||||
return locations;
|
return locations;
|
||||||
};
|
}
|
||||||
|
|
||||||
locationFromCfi(cfi){
|
locationFromCfi(cfi){
|
||||||
// Check if the location has not been set yet
|
// Check if the location has not been set yet
|
||||||
|
@ -159,7 +159,7 @@ class Locations {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return locationOf(cfi.start, this._locations, this.epubcfi.compare);
|
return locationOf(cfi.start, this._locations, this.epubcfi.compare);
|
||||||
};
|
}
|
||||||
|
|
||||||
percentageFromCfi(cfi) {
|
percentageFromCfi(cfi) {
|
||||||
if(this._locations.length === 0) {
|
if(this._locations.length === 0) {
|
||||||
|
@ -169,20 +169,20 @@ class Locations {
|
||||||
var loc = this.locationFromCfi(cfi);
|
var loc = this.locationFromCfi(cfi);
|
||||||
// Get percentage in total
|
// Get percentage in total
|
||||||
return this.percentageFromLocation(loc);
|
return this.percentageFromLocation(loc);
|
||||||
};
|
}
|
||||||
|
|
||||||
percentageFromLocation(loc) {
|
percentageFromLocation(loc) {
|
||||||
if (!loc || !this.total) {
|
if (!loc || !this.total) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return (loc / this.total);
|
return (loc / this.total);
|
||||||
};
|
}
|
||||||
|
|
||||||
cfiFromLocation(loc){
|
cfiFromLocation(loc){
|
||||||
var cfi = -1;
|
var cfi = -1;
|
||||||
// check that pg is an int
|
// check that pg is an int
|
||||||
if(typeof loc != "number"){
|
if(typeof loc != "number"){
|
||||||
loc = parseInt(pg);
|
loc = parseInt(loc);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(loc >= 0 && loc < this._locations.length) {
|
if(loc >= 0 && loc < this._locations.length) {
|
||||||
|
@ -190,28 +190,28 @@ class Locations {
|
||||||
}
|
}
|
||||||
|
|
||||||
return cfi;
|
return cfi;
|
||||||
};
|
}
|
||||||
|
|
||||||
cfiFromPercentage(value){
|
cfiFromPercentage(value){
|
||||||
var percentage = (value > 1) ? value / 100 : value; // Normalize value to 0-1
|
var percentage = (value > 1) ? value / 100 : value; // Normalize value to 0-1
|
||||||
var loc = Math.ceil(this.total * percentage);
|
var loc = Math.ceil(this.total * percentage);
|
||||||
|
|
||||||
return this.cfiFromLocation(loc);
|
return this.cfiFromLocation(loc);
|
||||||
};
|
}
|
||||||
|
|
||||||
load(locations){
|
load(locations){
|
||||||
this._locations = JSON.parse(locations);
|
this._locations = JSON.parse(locations);
|
||||||
this.total = this._locations.length-1;
|
this.total = this._locations.length-1;
|
||||||
return this._locations;
|
return this._locations;
|
||||||
};
|
}
|
||||||
|
|
||||||
save(json){
|
save(json){
|
||||||
return JSON.stringify(this._locations);
|
return JSON.stringify(this._locations);
|
||||||
};
|
}
|
||||||
|
|
||||||
getCurrent(json){
|
getCurrent(json){
|
||||||
return this._current;
|
return this._current;
|
||||||
};
|
}
|
||||||
|
|
||||||
setCurrent(curr){
|
setCurrent(curr){
|
||||||
var loc;
|
var loc;
|
||||||
|
@ -238,7 +238,7 @@ class Locations {
|
||||||
this.emit("changed", {
|
this.emit("changed", {
|
||||||
percentage: this.percentageFromLocation(loc)
|
percentage: this.percentageFromLocation(loc)
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
get currentLocation() {
|
get currentLocation() {
|
||||||
return this._current;
|
return this._current;
|
||||||
|
@ -250,7 +250,7 @@ class Locations {
|
||||||
|
|
||||||
length () {
|
length () {
|
||||||
return this._locations.length;
|
return this._locations.length;
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
EventEmitter(Locations.prototype);
|
EventEmitter(Locations.prototype);
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
import {extend, defer, requestAnimationFrame} from '../../utils/core';
|
import {extend, defer, requestAnimationFrame} from "../../utils/core";
|
||||||
import DefaultViewManager from '../default';
|
import DefaultViewManager from "../default";
|
||||||
|
|
||||||
class ContinuousViewManager extends DefaultViewManager {
|
class ContinuousViewManager extends DefaultViewManager {
|
||||||
constructor(options) {
|
constructor(options) {
|
||||||
super(options)
|
super(options);
|
||||||
// DefaultViewManager.apply(this, arguments); // call super constructor.
|
// DefaultViewManager.apply(this, arguments); // call super constructor.
|
||||||
|
|
||||||
this.name = "continuous";
|
this.name = "continuous";
|
||||||
|
@ -36,14 +36,14 @@ class ContinuousViewManager extends DefaultViewManager {
|
||||||
|
|
||||||
this.scrollTop = 0;
|
this.scrollTop = 0;
|
||||||
this.scrollLeft = 0;
|
this.scrollLeft = 0;
|
||||||
};
|
}
|
||||||
|
|
||||||
display(section, target){
|
display(section, target){
|
||||||
return DefaultViewManager.prototype.display.call(this, section, target)
|
return DefaultViewManager.prototype.display.call(this, section, target)
|
||||||
.then(function () {
|
.then(function () {
|
||||||
return this.fill();
|
return this.fill();
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
};
|
}
|
||||||
|
|
||||||
fill(_full){
|
fill(_full){
|
||||||
var full = _full || new defer();
|
var full = _full || new defer();
|
||||||
|
@ -80,7 +80,7 @@ class ContinuousViewManager extends DefaultViewManager {
|
||||||
.then(function(){
|
.then(function(){
|
||||||
this.scrollBy(distX, distY);
|
this.scrollBy(distX, distY);
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
};
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
afterDisplayed(currView){
|
afterDisplayed(currView){
|
||||||
|
@ -103,7 +103,7 @@ class ContinuousViewManager extends DefaultViewManager {
|
||||||
// currView.onShown = this.afterDisplayed.bind(this);
|
// currView.onShown = this.afterDisplayed.bind(this);
|
||||||
this.emit("added", currView.section);
|
this.emit("added", currView.section);
|
||||||
|
|
||||||
};
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
resize(width, height){
|
resize(width, height){
|
||||||
|
@ -134,7 +134,7 @@ class ContinuousViewManager extends DefaultViewManager {
|
||||||
height: this.stage.height
|
height: this.stage.height
|
||||||
});
|
});
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
onResized(e) {
|
onResized(e) {
|
||||||
|
|
||||||
|
@ -144,11 +144,11 @@ class ContinuousViewManager extends DefaultViewManager {
|
||||||
this.resizeTimeout = setTimeout(function(){
|
this.resizeTimeout = setTimeout(function(){
|
||||||
this.resize();
|
this.resize();
|
||||||
}.bind(this), 150);
|
}.bind(this), 150);
|
||||||
};
|
}
|
||||||
|
|
||||||
afterResized(view){
|
afterResized(view){
|
||||||
this.emit("resize", view.section);
|
this.emit("resize", view.section);
|
||||||
};
|
}
|
||||||
|
|
||||||
// Remove Previous Listeners if present
|
// Remove Previous Listeners if present
|
||||||
removeShownListeners(view){
|
removeShownListeners(view){
|
||||||
|
@ -157,7 +157,7 @@ class ContinuousViewManager extends DefaultViewManager {
|
||||||
// view.off("shown", this.afterDisplayedAbove);
|
// view.off("shown", this.afterDisplayedAbove);
|
||||||
view.onDisplayed = function(){};
|
view.onDisplayed = function(){};
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
|
|
||||||
// append(section){
|
// append(section){
|
||||||
|
@ -182,7 +182,7 @@ class ContinuousViewManager extends DefaultViewManager {
|
||||||
var view = this.createView(section);
|
var view = this.createView(section);
|
||||||
this.views.append(view);
|
this.views.append(view);
|
||||||
return view;
|
return view;
|
||||||
};
|
}
|
||||||
|
|
||||||
prepend(section){
|
prepend(section){
|
||||||
var view = this.createView(section);
|
var view = this.createView(section);
|
||||||
|
@ -191,7 +191,7 @@ class ContinuousViewManager extends DefaultViewManager {
|
||||||
|
|
||||||
this.views.prepend(view);
|
this.views.prepend(view);
|
||||||
return view;
|
return view;
|
||||||
};
|
}
|
||||||
|
|
||||||
counter(bounds){
|
counter(bounds){
|
||||||
|
|
||||||
|
@ -201,7 +201,7 @@ class ContinuousViewManager extends DefaultViewManager {
|
||||||
this.scrollBy(bounds.widthDelta, 0, true);
|
this.scrollBy(bounds.widthDelta, 0, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
update(_offset){
|
update(_offset){
|
||||||
var container = this.bounds();
|
var container = this.bounds();
|
||||||
|
@ -245,7 +245,7 @@ class ContinuousViewManager extends DefaultViewManager {
|
||||||
return updating.promise;
|
return updating.promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
check(_offsetLeft, _offsetTop){
|
check(_offsetLeft, _offsetTop){
|
||||||
var last, first, next, prev;
|
var last, first, next, prev;
|
||||||
|
@ -303,7 +303,7 @@ class ContinuousViewManager extends DefaultViewManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
trim(){
|
trim(){
|
||||||
var task = new defer();
|
var task = new defer();
|
||||||
|
@ -327,7 +327,7 @@ class ContinuousViewManager extends DefaultViewManager {
|
||||||
|
|
||||||
task.resolve();
|
task.resolve();
|
||||||
return task.promise;
|
return task.promise;
|
||||||
};
|
}
|
||||||
|
|
||||||
erase(view, above){ //Trim
|
erase(view, above){ //Trim
|
||||||
|
|
||||||
|
@ -355,18 +355,18 @@ class ContinuousViewManager extends DefaultViewManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
addEventListeners(stage){
|
addEventListeners(stage){
|
||||||
|
|
||||||
window.addEventListener('unload', function(e){
|
window.addEventListener("unload", function(e){
|
||||||
this.ignore = true;
|
this.ignore = true;
|
||||||
// this.scrollTo(0,0);
|
// this.scrollTo(0,0);
|
||||||
this.destroy();
|
this.destroy();
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
this.addScrollListeners();
|
this.addScrollListeners();
|
||||||
};
|
}
|
||||||
|
|
||||||
addScrollListeners() {
|
addScrollListeners() {
|
||||||
var scroller;
|
var scroller;
|
||||||
|
@ -400,7 +400,7 @@ class ContinuousViewManager extends DefaultViewManager {
|
||||||
|
|
||||||
this.scrolled = false;
|
this.scrolled = false;
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
onScroll(){
|
onScroll(){
|
||||||
let scrollTop;
|
let scrollTop;
|
||||||
|
@ -471,7 +471,7 @@ class ContinuousViewManager extends DefaultViewManager {
|
||||||
|
|
||||||
// this.tick.call(window, this.onScroll.bind(this));
|
// this.tick.call(window, this.onScroll.bind(this));
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
|
|
||||||
// resizeView(view) {
|
// resizeView(view) {
|
||||||
|
@ -493,14 +493,14 @@ class ContinuousViewManager extends DefaultViewManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.location;
|
return this.location;
|
||||||
};
|
}
|
||||||
|
|
||||||
scrolledLocation(){
|
scrolledLocation(){
|
||||||
|
|
||||||
var visible = this.visible();
|
var visible = this.visible();
|
||||||
var startPage, endPage;
|
var startPage, endPage;
|
||||||
|
|
||||||
var container = this.container.getBoundingClientRect();
|
// var container = this.container.getBoundingClientRect();
|
||||||
|
|
||||||
if(visible.length === 1) {
|
if(visible.length === 1) {
|
||||||
return this.mapping.page(visible[0].contents, visible[0].section.cfiBase);
|
return this.mapping.page(visible[0].contents, visible[0].section.cfiBase);
|
||||||
|
@ -517,7 +517,7 @@ class ContinuousViewManager extends DefaultViewManager {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
paginatedLocation(){
|
paginatedLocation(){
|
||||||
var visible = this.visible();
|
var visible = this.visible();
|
||||||
|
@ -550,7 +550,7 @@ class ContinuousViewManager extends DefaultViewManager {
|
||||||
end: pageRight.end
|
end: pageRight.end
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
current(what){
|
current(what){
|
||||||
|
@ -595,7 +595,7 @@ class ContinuousViewManager extends DefaultViewManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
return this._current;
|
return this._current;
|
||||||
};
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
updateLayout() {
|
updateLayout() {
|
||||||
|
@ -626,7 +626,7 @@ class ContinuousViewManager extends DefaultViewManager {
|
||||||
|
|
||||||
this.setLayout(this.layout);
|
this.setLayout(this.layout);
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
next(){
|
next(){
|
||||||
|
|
||||||
|
@ -645,7 +645,7 @@ class ContinuousViewManager extends DefaultViewManager {
|
||||||
} else {
|
} else {
|
||||||
this.scrollBy(0, this.layout.height);
|
this.scrollBy(0, this.layout.height);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
prev(){
|
prev(){
|
||||||
if(this.settings.axis === "horizontal") {
|
if(this.settings.axis === "horizontal") {
|
||||||
|
@ -653,7 +653,7 @@ class ContinuousViewManager extends DefaultViewManager {
|
||||||
} else {
|
} else {
|
||||||
this.scrollBy(0, -this.layout.height);
|
this.scrollBy(0, -this.layout.height);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
updateFlow(flow){
|
updateFlow(flow){
|
||||||
var axis = (flow === "paginated") ? "horizontal" : "vertical";
|
var axis = (flow === "paginated") ? "horizontal" : "vertical";
|
||||||
|
@ -674,7 +674,7 @@ class ContinuousViewManager extends DefaultViewManager {
|
||||||
this.settings.infinite = false;
|
this.settings.infinite = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default ContinuousViewManager;
|
export default ContinuousViewManager;
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
import EventEmitter from 'event-emitter';
|
import EventEmitter from "event-emitter";
|
||||||
import {extend, defer} from '../../utils/core';
|
import {extend, defer} from "../../utils/core";
|
||||||
import EpubCFI from '../../epubcfi';
|
import Mapping from "../../mapping";
|
||||||
import Mapping from '../../mapping';
|
import Queue from "../../utils/queue";
|
||||||
import Queue from '../../queue';
|
import Stage from "../helpers/stage";
|
||||||
import Stage from '../helpers/stage';
|
import Views from "../helpers/views";
|
||||||
import Views from '../helpers/views';
|
|
||||||
|
|
||||||
class DefaultViewManager {
|
class DefaultViewManager {
|
||||||
constructor(options) {
|
constructor(options) {
|
||||||
|
@ -20,10 +19,10 @@ class DefaultViewManager {
|
||||||
hidden: false,
|
hidden: false,
|
||||||
width: undefined,
|
width: undefined,
|
||||||
height: undefined,
|
height: undefined,
|
||||||
// globalLayoutProperties : { layout: 'reflowable', spread: 'auto', orientation: 'auto'},
|
// globalLayoutProperties : { layout: "reflowable", spread: "auto", orientation: "auto"},
|
||||||
// layout: null,
|
// layout: null,
|
||||||
axis: "vertical",
|
axis: "vertical",
|
||||||
ignoreClass: ''
|
ignoreClass: ""
|
||||||
});
|
});
|
||||||
|
|
||||||
extend(this.settings, options.settings || {});
|
extend(this.settings, options.settings || {});
|
||||||
|
@ -77,13 +76,13 @@ class DefaultViewManager {
|
||||||
if (this.layout) {
|
if (this.layout) {
|
||||||
this.updateLayout();
|
this.updateLayout();
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
addEventListeners(){
|
addEventListeners(){
|
||||||
window.addEventListener('unload', function(e){
|
window.addEventListener("unload", function(e){
|
||||||
this.destroy();
|
this.destroy();
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
};
|
}
|
||||||
|
|
||||||
destroy(){
|
destroy(){
|
||||||
// this.views.each(function(view){
|
// this.views.each(function(view){
|
||||||
|
@ -99,14 +98,14 @@ class DefaultViewManager {
|
||||||
this.element.removeChild(this.container);
|
this.element.removeChild(this.container);
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
};
|
}
|
||||||
|
|
||||||
onResized(e) {
|
onResized(e) {
|
||||||
clearTimeout(this.resizeTimeout);
|
clearTimeout(this.resizeTimeout);
|
||||||
this.resizeTimeout = setTimeout(function(){
|
this.resizeTimeout = setTimeout(function(){
|
||||||
this.resize();
|
this.resize();
|
||||||
}.bind(this), 150);
|
}.bind(this), 150);
|
||||||
};
|
}
|
||||||
|
|
||||||
resize(width, height){
|
resize(width, height){
|
||||||
|
|
||||||
|
@ -132,11 +131,11 @@ class DefaultViewManager {
|
||||||
height: this.stage.height
|
height: this.stage.height
|
||||||
});
|
});
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
createView(section) {
|
createView(section) {
|
||||||
return new this.View(section, this.viewSettings);
|
return new this.View(section, this.viewSettings);
|
||||||
};
|
}
|
||||||
|
|
||||||
display(section, target){
|
display(section, target){
|
||||||
|
|
||||||
|
@ -185,7 +184,7 @@ class DefaultViewManager {
|
||||||
|
|
||||||
displaying.resolve();
|
displaying.resolve();
|
||||||
|
|
||||||
}.bind(this))
|
}.bind(this));
|
||||||
// .then(function(){
|
// .then(function(){
|
||||||
// return this.hooks.display.trigger(view);
|
// return this.hooks.display.trigger(view);
|
||||||
// }.bind(this))
|
// }.bind(this))
|
||||||
|
@ -193,15 +192,15 @@ class DefaultViewManager {
|
||||||
// this.views.show();
|
// this.views.show();
|
||||||
// }.bind(this));
|
// }.bind(this));
|
||||||
return displayed;
|
return displayed;
|
||||||
};
|
}
|
||||||
|
|
||||||
afterDisplayed(view){
|
afterDisplayed(view){
|
||||||
this.emit("added", view);
|
this.emit("added", view);
|
||||||
};
|
}
|
||||||
|
|
||||||
afterResized(view){
|
afterResized(view){
|
||||||
this.emit("resize", view.section);
|
this.emit("resize", view.section);
|
||||||
};
|
}
|
||||||
|
|
||||||
// moveTo(offset){
|
// moveTo(offset){
|
||||||
// this.scrollTo(offset.left, offset.top);
|
// this.scrollTo(offset.left, offset.top);
|
||||||
|
@ -222,7 +221,7 @@ class DefaultViewManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.scrollTo(distX, distY);
|
this.scrollTo(distX, distY);
|
||||||
};
|
}
|
||||||
|
|
||||||
add(section){
|
add(section){
|
||||||
var view = this.createView(section);
|
var view = this.createView(section);
|
||||||
|
@ -235,20 +234,20 @@ class DefaultViewManager {
|
||||||
|
|
||||||
return view.display(this.request);
|
return view.display(this.request);
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
append(section){
|
append(section){
|
||||||
var view = this.createView(section);
|
var view = this.createView(section);
|
||||||
this.views.append(view);
|
this.views.append(view);
|
||||||
return view.display(this.request);
|
return view.display(this.request);
|
||||||
};
|
}
|
||||||
|
|
||||||
prepend(section){
|
prepend(section){
|
||||||
var view = this.createView(section);
|
var view = this.createView(section);
|
||||||
|
|
||||||
this.views.prepend(view);
|
this.views.prepend(view);
|
||||||
return view.display(this.request);
|
return view.display(this.request);
|
||||||
};
|
}
|
||||||
// resizeView(view) {
|
// resizeView(view) {
|
||||||
//
|
//
|
||||||
// if(this.settings.globalLayoutProperties.layout === "pre-paginated") {
|
// if(this.settings.globalLayoutProperties.layout === "pre-paginated") {
|
||||||
|
@ -261,7 +260,6 @@ class DefaultViewManager {
|
||||||
|
|
||||||
next(){
|
next(){
|
||||||
var next;
|
var next;
|
||||||
var view;
|
|
||||||
var left;
|
var left;
|
||||||
|
|
||||||
if(!this.views.length) return;
|
if(!this.views.length) return;
|
||||||
|
@ -306,11 +304,10 @@ class DefaultViewManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
prev(){
|
prev(){
|
||||||
var prev;
|
var prev;
|
||||||
var view;
|
|
||||||
var left;
|
var left;
|
||||||
|
|
||||||
if(!this.views.length) return;
|
if(!this.views.length) return;
|
||||||
|
@ -354,7 +351,7 @@ class DefaultViewManager {
|
||||||
this.views.show();
|
this.views.show();
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
current(){
|
current(){
|
||||||
var visible = this.visible();
|
var visible = this.visible();
|
||||||
|
@ -363,7 +360,7 @@ class DefaultViewManager {
|
||||||
return visible[visible.length-1];
|
return visible[visible.length-1];
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
};
|
}
|
||||||
|
|
||||||
currentLocation(){
|
currentLocation(){
|
||||||
|
|
||||||
|
@ -373,7 +370,7 @@ class DefaultViewManager {
|
||||||
this.location = this.paginatedLocation();
|
this.location = this.paginatedLocation();
|
||||||
}
|
}
|
||||||
return this.location;
|
return this.location;
|
||||||
};
|
}
|
||||||
|
|
||||||
scrolledLocation(){
|
scrolledLocation(){
|
||||||
var view;
|
var view;
|
||||||
|
@ -383,7 +380,7 @@ class DefaultViewManager {
|
||||||
return this.mapping.page(view, view.section.cfiBase);
|
return this.mapping.page(view, view.section.cfiBase);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
paginatedLocation(){
|
paginatedLocation(){
|
||||||
var view;
|
var view;
|
||||||
|
@ -396,7 +393,7 @@ class DefaultViewManager {
|
||||||
return this.mapping.page(view, view.section.cfiBase, start, end);
|
return this.mapping.page(view, view.section.cfiBase, start, end);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
isVisible(view, offsetPrev, offsetNext, _container){
|
isVisible(view, offsetPrev, offsetNext, _container){
|
||||||
var position = view.position();
|
var position = view.position();
|
||||||
|
@ -417,7 +414,7 @@ class DefaultViewManager {
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
visible(){
|
visible(){
|
||||||
// return this.views.displayed();
|
// return this.views.displayed();
|
||||||
|
@ -438,7 +435,7 @@ class DefaultViewManager {
|
||||||
|
|
||||||
}
|
}
|
||||||
return visible;
|
return visible;
|
||||||
};
|
}
|
||||||
|
|
||||||
scrollBy(x, y, silent){
|
scrollBy(x, y, silent){
|
||||||
if(silent) {
|
if(silent) {
|
||||||
|
@ -456,7 +453,7 @@ class DefaultViewManager {
|
||||||
// console.log("scrollBy", x, y);
|
// console.log("scrollBy", x, y);
|
||||||
this.scrolled = true;
|
this.scrolled = true;
|
||||||
this.onScroll();
|
this.onScroll();
|
||||||
};
|
}
|
||||||
|
|
||||||
scrollTo(x, y, silent){
|
scrollTo(x, y, silent){
|
||||||
if(silent) {
|
if(silent) {
|
||||||
|
@ -478,11 +475,11 @@ class DefaultViewManager {
|
||||||
// }.bind(this), 10);
|
// }.bind(this), 10);
|
||||||
// return;
|
// return;
|
||||||
// };
|
// };
|
||||||
};
|
}
|
||||||
|
|
||||||
onScroll(){
|
onScroll(){
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
bounds() {
|
bounds() {
|
||||||
var bounds;
|
var bounds;
|
||||||
|
@ -490,7 +487,7 @@ class DefaultViewManager {
|
||||||
bounds = this.stage.bounds();
|
bounds = this.stage.bounds();
|
||||||
|
|
||||||
return bounds;
|
return bounds;
|
||||||
};
|
}
|
||||||
|
|
||||||
applyLayout(layout) {
|
applyLayout(layout) {
|
||||||
|
|
||||||
|
@ -499,7 +496,7 @@ class DefaultViewManager {
|
||||||
|
|
||||||
this.mapping = new Mapping(this.layout);
|
this.mapping = new Mapping(this.layout);
|
||||||
// this.manager.layout(this.layout.format);
|
// this.manager.layout(this.layout.format);
|
||||||
};
|
}
|
||||||
|
|
||||||
updateLayout() {
|
updateLayout() {
|
||||||
if (!this.stage) {
|
if (!this.stage) {
|
||||||
|
@ -530,7 +527,7 @@ class DefaultViewManager {
|
||||||
|
|
||||||
this.setLayout(this.layout);
|
this.setLayout(this.layout);
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
setLayout(layout){
|
setLayout(layout){
|
||||||
|
|
||||||
|
@ -544,7 +541,7 @@ class DefaultViewManager {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
updateFlow(flow){
|
updateFlow(flow){
|
||||||
var axis = (flow === "paginated") ? "horizontal" : "vertical";
|
var axis = (flow === "paginated") ? "horizontal" : "vertical";
|
||||||
|
@ -558,7 +555,7 @@ class DefaultViewManager {
|
||||||
// view.setAxis(axis);
|
// view.setAxis(axis);
|
||||||
// });
|
// });
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
getContents(){
|
getContents(){
|
||||||
var contents = [];
|
var contents = [];
|
||||||
|
@ -566,7 +563,7 @@ class DefaultViewManager {
|
||||||
contents.push(view.contents);
|
contents.push(view.contents);
|
||||||
});
|
});
|
||||||
return contents;
|
return contents;
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//-- Enable binding events to Manager
|
//-- Enable binding events to Manager
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import {uuid, isNumber, isElement, windowBounds} from '../../utils/core';
|
import {uuid, isNumber, isElement, windowBounds} from "../../utils/core";
|
||||||
|
|
||||||
class Stage {
|
class Stage {
|
||||||
constructor(_options) {
|
constructor(_options) {
|
||||||
|
@ -60,7 +60,7 @@ class Stage {
|
||||||
}
|
}
|
||||||
|
|
||||||
return container;
|
return container;
|
||||||
};
|
}
|
||||||
|
|
||||||
wrap(container) {
|
wrap(container) {
|
||||||
var wrapper = document.createElement("div");
|
var wrapper = document.createElement("div");
|
||||||
|
@ -72,7 +72,7 @@ class Stage {
|
||||||
|
|
||||||
wrapper.appendChild(container);
|
wrapper.appendChild(container);
|
||||||
return wrapper;
|
return wrapper;
|
||||||
};
|
}
|
||||||
|
|
||||||
|
|
||||||
getElement(_element){
|
getElement(_element){
|
||||||
|
@ -85,12 +85,11 @@ class Stage {
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!element){
|
if(!element){
|
||||||
console.error("Not an Element");
|
throw new Error("Not an Element");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return element;
|
return element;
|
||||||
};
|
}
|
||||||
|
|
||||||
attachTo(what){
|
attachTo(what){
|
||||||
|
|
||||||
|
@ -113,11 +112,11 @@ class Stage {
|
||||||
|
|
||||||
return element;
|
return element;
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
getContainer() {
|
getContainer() {
|
||||||
return this.container;
|
return this.container;
|
||||||
};
|
}
|
||||||
|
|
||||||
onResize(func){
|
onResize(func){
|
||||||
// Only listen to window for resize event if width and height are not fixed.
|
// Only listen to window for resize event if width and height are not fixed.
|
||||||
|
@ -127,7 +126,7 @@ class Stage {
|
||||||
window.addEventListener("resize", func, false);
|
window.addEventListener("resize", func, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
size(width, height){
|
size(width, height){
|
||||||
var bounds;
|
var bounds;
|
||||||
|
@ -185,7 +184,7 @@ class Stage {
|
||||||
this.containerPadding.bottom
|
this.containerPadding.bottom
|
||||||
};
|
};
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
bounds(){
|
bounds(){
|
||||||
|
|
||||||
|
@ -222,7 +221,7 @@ class Stage {
|
||||||
rules += prop + ":" + set[prop] + ";";
|
rules += prop + ":" + set[prop] + ";";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
this.sheet.insertRule(scope + selector + " {" + rules + "}", 0);
|
this.sheet.insertRule(scope + selector + " {" + rules + "}", 0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,31 +4,31 @@ class Views {
|
||||||
this._views = [];
|
this._views = [];
|
||||||
this.length = 0;
|
this.length = 0;
|
||||||
this.hidden = false;
|
this.hidden = false;
|
||||||
};
|
}
|
||||||
|
|
||||||
all() {
|
all() {
|
||||||
return this._views;
|
return this._views;
|
||||||
};
|
}
|
||||||
|
|
||||||
first() {
|
first() {
|
||||||
return this._views[0];
|
return this._views[0];
|
||||||
};
|
}
|
||||||
|
|
||||||
last() {
|
last() {
|
||||||
return this._views[this._views.length-1];
|
return this._views[this._views.length-1];
|
||||||
};
|
}
|
||||||
|
|
||||||
indexOf(view) {
|
indexOf(view) {
|
||||||
return this._views.indexOf(view);
|
return this._views.indexOf(view);
|
||||||
};
|
}
|
||||||
|
|
||||||
slice() {
|
slice() {
|
||||||
return this._views.slice.apply(this._views, arguments);
|
return this._views.slice.apply(this._views, arguments);
|
||||||
};
|
}
|
||||||
|
|
||||||
get(i) {
|
get(i) {
|
||||||
return this._views[i];
|
return this._views[i];
|
||||||
};
|
}
|
||||||
|
|
||||||
append(view){
|
append(view){
|
||||||
this._views.push(view);
|
this._views.push(view);
|
||||||
|
@ -37,7 +37,7 @@ class Views {
|
||||||
}
|
}
|
||||||
this.length++;
|
this.length++;
|
||||||
return view;
|
return view;
|
||||||
};
|
}
|
||||||
|
|
||||||
prepend(view){
|
prepend(view){
|
||||||
this._views.unshift(view);
|
this._views.unshift(view);
|
||||||
|
@ -46,7 +46,7 @@ class Views {
|
||||||
}
|
}
|
||||||
this.length++;
|
this.length++;
|
||||||
return view;
|
return view;
|
||||||
};
|
}
|
||||||
|
|
||||||
insert(view, index) {
|
insert(view, index) {
|
||||||
this._views.splice(index, 0, view);
|
this._views.splice(index, 0, view);
|
||||||
|
@ -61,7 +61,7 @@ class Views {
|
||||||
|
|
||||||
this.length++;
|
this.length++;
|
||||||
return view;
|
return view;
|
||||||
};
|
}
|
||||||
|
|
||||||
remove(view) {
|
remove(view) {
|
||||||
var index = this._views.indexOf(view);
|
var index = this._views.indexOf(view);
|
||||||
|
@ -74,7 +74,7 @@ class Views {
|
||||||
this.destroy(view);
|
this.destroy(view);
|
||||||
|
|
||||||
this.length--;
|
this.length--;
|
||||||
};
|
}
|
||||||
|
|
||||||
destroy(view) {
|
destroy(view) {
|
||||||
if(view.displayed){
|
if(view.displayed){
|
||||||
|
@ -85,13 +85,13 @@ class Views {
|
||||||
this.container.removeChild(view.element);
|
this.container.removeChild(view.element);
|
||||||
}
|
}
|
||||||
view = null;
|
view = null;
|
||||||
};
|
}
|
||||||
|
|
||||||
// Iterators
|
// Iterators
|
||||||
|
|
||||||
each() {
|
each() {
|
||||||
return this._views.forEach.apply(this._views, arguments);
|
return this._views.forEach.apply(this._views, arguments);
|
||||||
};
|
}
|
||||||
|
|
||||||
clear(){
|
clear(){
|
||||||
// Remove all views
|
// Remove all views
|
||||||
|
@ -107,7 +107,7 @@ class Views {
|
||||||
|
|
||||||
this._views = [];
|
this._views = [];
|
||||||
this.length = 0;
|
this.length = 0;
|
||||||
};
|
}
|
||||||
|
|
||||||
find(section){
|
find(section){
|
||||||
|
|
||||||
|
@ -121,7 +121,7 @@ class Views {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
displayed(){
|
displayed(){
|
||||||
var displayed = [];
|
var displayed = [];
|
||||||
|
@ -135,7 +135,7 @@ class Views {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return displayed;
|
return displayed;
|
||||||
};
|
}
|
||||||
|
|
||||||
show(){
|
show(){
|
||||||
var view;
|
var view;
|
||||||
|
@ -148,7 +148,7 @@ class Views {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.hidden = false;
|
this.hidden = false;
|
||||||
};
|
}
|
||||||
|
|
||||||
hide(){
|
hide(){
|
||||||
var view;
|
var view;
|
||||||
|
@ -161,7 +161,7 @@ class Views {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.hidden = true;
|
this.hidden = true;
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Views;
|
export default Views;
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
import EventEmitter from 'event-emitter';
|
import EventEmitter from "event-emitter";
|
||||||
import {extend, borders, uuid, isNumber, bounds, defer} from '../../utils/core';
|
import {extend, borders, uuid, isNumber, bounds, defer} from "../../utils/core";
|
||||||
import EpubCFI from '../../epubcfi';
|
import EpubCFI from "../../epubcfi";
|
||||||
import Contents from '../../contents';
|
import Contents from "../../contents";
|
||||||
|
|
||||||
class IframeView {
|
class IframeView {
|
||||||
constructor(section, options) {
|
constructor(section, options) {
|
||||||
this.settings = extend({
|
this.settings = extend({
|
||||||
ignoreClass : '',
|
ignoreClass : "",
|
||||||
axis: 'vertical',
|
axis: "vertical",
|
||||||
width: 0,
|
width: 0,
|
||||||
height: 0,
|
height: 0,
|
||||||
layout: undefined,
|
layout: undefined,
|
||||||
|
@ -36,10 +36,10 @@ class IframeView {
|
||||||
this.layout = this.settings.layout;
|
this.layout = this.settings.layout;
|
||||||
// Dom events to listen for
|
// Dom events to listen for
|
||||||
// this.listenedEvents = ["keydown", "keyup", "keypressed", "mouseup", "mousedown", "click", "touchend", "touchstart"];
|
// this.listenedEvents = ["keydown", "keyup", "keypressed", "mouseup", "mousedown", "click", "touchend", "touchstart"];
|
||||||
};
|
}
|
||||||
|
|
||||||
container(axis) {
|
container(axis) {
|
||||||
var element = document.createElement('div');
|
var element = document.createElement("div");
|
||||||
|
|
||||||
element.classList.add("epub-view");
|
element.classList.add("epub-view");
|
||||||
|
|
||||||
|
@ -55,7 +55,7 @@ class IframeView {
|
||||||
}
|
}
|
||||||
|
|
||||||
return element;
|
return element;
|
||||||
};
|
}
|
||||||
|
|
||||||
create() {
|
create() {
|
||||||
|
|
||||||
|
@ -67,7 +67,7 @@ class IframeView {
|
||||||
this.element = this.createContainer();
|
this.element = this.createContainer();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.iframe = document.createElement('iframe');
|
this.iframe = document.createElement("iframe");
|
||||||
this.iframe.id = this.id;
|
this.iframe.id = this.id;
|
||||||
this.iframe.scrolling = "no"; // Might need to be removed: breaks ios width calculations
|
this.iframe.scrolling = "no"; // Might need to be removed: breaks ios width calculations
|
||||||
this.iframe.style.overflow = "hidden";
|
this.iframe.style.overflow = "hidden";
|
||||||
|
@ -102,14 +102,14 @@ class IframeView {
|
||||||
// Firefox has trouble with baseURI and srcdoc
|
// Firefox has trouble with baseURI and srcdoc
|
||||||
// TODO: Disable for now in firefox
|
// TODO: Disable for now in firefox
|
||||||
|
|
||||||
if(!!("srcdoc" in this.iframe)) {
|
if(!("srcdoc" in this.iframe)) {
|
||||||
this.supportsSrcdoc = true;
|
this.supportsSrcdoc = true;
|
||||||
} else {
|
} else {
|
||||||
this.supportsSrcdoc = false;
|
this.supportsSrcdoc = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.iframe;
|
return this.iframe;
|
||||||
};
|
}
|
||||||
|
|
||||||
render(request, show) {
|
render(request, show) {
|
||||||
|
|
||||||
|
@ -163,11 +163,10 @@ class IframeView {
|
||||||
|
|
||||||
}.bind(this))
|
}.bind(this))
|
||||||
.catch(function(e){
|
.catch(function(e){
|
||||||
console.error(e);
|
|
||||||
this.emit("loaderror", e);
|
this.emit("loaderror", e);
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
// Determine locks base on settings
|
// Determine locks base on settings
|
||||||
size(_width, _height) {
|
size(_width, _height) {
|
||||||
|
@ -182,7 +181,7 @@ class IframeView {
|
||||||
this.lock("width", width, height);
|
this.lock("width", width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
// Lock an axis to element dimensions, taking borders into account
|
// Lock an axis to element dimensions, taking borders into account
|
||||||
lock(what, width, height) {
|
lock(what, width, height) {
|
||||||
|
@ -224,7 +223,7 @@ class IframeView {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
// Resize a single axis based on content dimensions
|
// Resize a single axis based on content dimensions
|
||||||
expand(force) {
|
expand(force) {
|
||||||
|
@ -286,7 +285,7 @@ class IframeView {
|
||||||
}
|
}
|
||||||
|
|
||||||
this._expanding = false;
|
this._expanding = false;
|
||||||
};
|
}
|
||||||
|
|
||||||
contentWidth(min) {
|
contentWidth(min) {
|
||||||
var prev;
|
var prev;
|
||||||
|
@ -302,7 +301,7 @@ class IframeView {
|
||||||
// Reset iframe size back
|
// Reset iframe size back
|
||||||
this.iframe.style.width = prev;
|
this.iframe.style.width = prev;
|
||||||
return width;
|
return width;
|
||||||
};
|
}
|
||||||
|
|
||||||
contentHeight(min) {
|
contentHeight(min) {
|
||||||
var prev;
|
var prev;
|
||||||
|
@ -314,7 +313,7 @@ class IframeView {
|
||||||
|
|
||||||
this.iframe.style.height = prev;
|
this.iframe.style.height = prev;
|
||||||
return height;
|
return height;
|
||||||
};
|
}
|
||||||
|
|
||||||
|
|
||||||
resize(width, height) {
|
resize(width, height) {
|
||||||
|
@ -335,7 +334,7 @@ class IframeView {
|
||||||
|
|
||||||
this.reframe(this.iframeBounds.width, this.iframeBounds.height);
|
this.reframe(this.iframeBounds.width, this.iframeBounds.height);
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
reframe(width, height) {
|
reframe(width, height) {
|
||||||
var size;
|
var size;
|
||||||
|
@ -367,7 +366,7 @@ class IframeView {
|
||||||
|
|
||||||
this.emit("resized", size);
|
this.emit("resized", size);
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
|
|
||||||
load(contents) {
|
load(contents) {
|
||||||
|
@ -403,7 +402,7 @@ class IframeView {
|
||||||
}
|
}
|
||||||
|
|
||||||
return loaded;
|
return loaded;
|
||||||
};
|
}
|
||||||
|
|
||||||
onLoad(event, promise) {
|
onLoad(event, promise) {
|
||||||
|
|
||||||
|
@ -431,7 +430,7 @@ class IframeView {
|
||||||
});
|
});
|
||||||
|
|
||||||
promise.resolve(this.contents);
|
promise.resolve(this.contents);
|
||||||
};
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -458,25 +457,25 @@ class IframeView {
|
||||||
|
|
||||||
setLayout(layout) {
|
setLayout(layout) {
|
||||||
this.layout = layout;
|
this.layout = layout;
|
||||||
};
|
}
|
||||||
|
|
||||||
setAxis(axis) {
|
setAxis(axis) {
|
||||||
this.settings.axis = axis;
|
this.settings.axis = axis;
|
||||||
};
|
}
|
||||||
|
|
||||||
resizeListenters() {
|
resizeListenters() {
|
||||||
// Test size again
|
// Test size again
|
||||||
clearTimeout(this.expanding);
|
clearTimeout(this.expanding);
|
||||||
this.expanding = setTimeout(this.expand.bind(this), 350);
|
this.expanding = setTimeout(this.expand.bind(this), 350);
|
||||||
};
|
}
|
||||||
|
|
||||||
addListeners() {
|
addListeners() {
|
||||||
//TODO: Add content listeners for expanding
|
//TODO: Add content listeners for expanding
|
||||||
};
|
}
|
||||||
|
|
||||||
removeListeners(layoutFunc) {
|
removeListeners(layoutFunc) {
|
||||||
//TODO: remove content listeners for expanding
|
//TODO: remove content listeners for expanding
|
||||||
};
|
}
|
||||||
|
|
||||||
display(request) {
|
display(request) {
|
||||||
var displayed = new defer();
|
var displayed = new defer();
|
||||||
|
@ -499,7 +498,7 @@ class IframeView {
|
||||||
|
|
||||||
|
|
||||||
return displayed.promise;
|
return displayed.promise;
|
||||||
};
|
}
|
||||||
|
|
||||||
show() {
|
show() {
|
||||||
|
|
||||||
|
@ -510,7 +509,7 @@ class IframeView {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.emit("shown", this);
|
this.emit("shown", this);
|
||||||
};
|
}
|
||||||
|
|
||||||
hide() {
|
hide() {
|
||||||
// this.iframe.style.display = "none";
|
// this.iframe.style.display = "none";
|
||||||
|
@ -519,11 +518,11 @@ class IframeView {
|
||||||
|
|
||||||
this.stopExpanding = true;
|
this.stopExpanding = true;
|
||||||
this.emit("hidden", this);
|
this.emit("hidden", this);
|
||||||
};
|
}
|
||||||
|
|
||||||
position() {
|
position() {
|
||||||
return this.element.getBoundingClientRect();
|
return this.element.getBoundingClientRect();
|
||||||
};
|
}
|
||||||
|
|
||||||
locationOf(target) {
|
locationOf(target) {
|
||||||
var parentPos = this.iframe.getBoundingClientRect();
|
var parentPos = this.iframe.getBoundingClientRect();
|
||||||
|
@ -533,22 +532,22 @@ class IframeView {
|
||||||
"left": window.scrollX + parentPos.left + targetPos.left,
|
"left": window.scrollX + parentPos.left + targetPos.left,
|
||||||
"top": window.scrollY + parentPos.top + targetPos.top
|
"top": window.scrollY + parentPos.top + targetPos.top
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
|
|
||||||
onDisplayed(view) {
|
onDisplayed(view) {
|
||||||
// Stub, override with a custom functions
|
// Stub, override with a custom functions
|
||||||
};
|
}
|
||||||
|
|
||||||
onResize(view, e) {
|
onResize(view, e) {
|
||||||
// Stub, override with a custom functions
|
// Stub, override with a custom functions
|
||||||
};
|
}
|
||||||
|
|
||||||
bounds() {
|
bounds() {
|
||||||
if(!this.elementBounds) {
|
if(!this.elementBounds) {
|
||||||
this.elementBounds = bounds(this.element);
|
this.elementBounds = bounds(this.element);
|
||||||
}
|
}
|
||||||
return this.elementBounds;
|
return this.elementBounds;
|
||||||
};
|
}
|
||||||
|
|
||||||
destroy() {
|
destroy() {
|
||||||
|
|
||||||
|
@ -569,7 +568,7 @@ class IframeView {
|
||||||
}
|
}
|
||||||
// this.element.style.height = "0px";
|
// this.element.style.height = "0px";
|
||||||
// this.element.style.width = "0px";
|
// this.element.style.width = "0px";
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
EventEmitter(IframeView.prototype);
|
EventEmitter(IframeView.prototype);
|
||||||
|
|
|
@ -1,14 +1,13 @@
|
||||||
import EventEmitter from 'event-emitter';
|
import EventEmitter from "event-emitter";
|
||||||
import {extend, borders, uuid, isNumber, bounds, defer} from '../../utils/core';
|
import {extend, borders, uuid, isNumber, bounds, defer, qs, parse} from "../../utils/core";
|
||||||
import EpubCFI from '../../epubcfi';
|
import EpubCFI from "../../epubcfi";
|
||||||
import Contents from '../../contents';
|
import Contents from "../../contents";
|
||||||
// import URI from 'urijs';
|
|
||||||
|
|
||||||
class InlineView {
|
class InlineView {
|
||||||
constructor(section, options) {
|
constructor(section, options) {
|
||||||
this.settings = extend({
|
this.settings = extend({
|
||||||
ignoreClass : '',
|
ignoreClass : "",
|
||||||
axis: 'vertical',
|
axis: "vertical",
|
||||||
width: 0,
|
width: 0,
|
||||||
height: 0,
|
height: 0,
|
||||||
layout: undefined,
|
layout: undefined,
|
||||||
|
@ -38,10 +37,10 @@ class InlineView {
|
||||||
// Dom events to listen for
|
// Dom events to listen for
|
||||||
// this.listenedEvents = ["keydown", "keyup", "keypressed", "mouseup", "mousedown", "click", "touchend", "touchstart"];
|
// this.listenedEvents = ["keydown", "keyup", "keypressed", "mouseup", "mousedown", "click", "touchend", "touchstart"];
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
container(axis) {
|
container(axis) {
|
||||||
var element = document.createElement('div');
|
var element = document.createElement("div");
|
||||||
|
|
||||||
element.classList.add("epub-view");
|
element.classList.add("epub-view");
|
||||||
|
|
||||||
|
@ -62,7 +61,7 @@ class InlineView {
|
||||||
}
|
}
|
||||||
|
|
||||||
return element;
|
return element;
|
||||||
};
|
}
|
||||||
|
|
||||||
create() {
|
create() {
|
||||||
|
|
||||||
|
@ -74,7 +73,7 @@ class InlineView {
|
||||||
this.element = this.createContainer();
|
this.element = this.createContainer();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.frame = document.createElement('div');
|
this.frame = document.createElement("div");
|
||||||
this.frame.id = this.id;
|
this.frame.id = this.id;
|
||||||
this.frame.style.overflow = "hidden";
|
this.frame.style.overflow = "hidden";
|
||||||
this.frame.style.wordSpacing = "initial";
|
this.frame.style.wordSpacing = "initial";
|
||||||
|
@ -103,7 +102,7 @@ class InlineView {
|
||||||
this.elementBounds = bounds(this.element);
|
this.elementBounds = bounds(this.element);
|
||||||
|
|
||||||
return this.frame;
|
return this.frame;
|
||||||
};
|
}
|
||||||
|
|
||||||
render(request, show) {
|
render(request, show) {
|
||||||
|
|
||||||
|
@ -156,7 +155,7 @@ class InlineView {
|
||||||
this.emit("loaderror", e);
|
this.emit("loaderror", e);
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
// Determine locks base on settings
|
// Determine locks base on settings
|
||||||
size(_width, _height) {
|
size(_width, _height) {
|
||||||
|
@ -172,7 +171,7 @@ class InlineView {
|
||||||
this.lock("width", width, height);
|
this.lock("width", width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
// Lock an axis to element dimensions, taking borders into account
|
// Lock an axis to element dimensions, taking borders into account
|
||||||
lock(what, width, height) {
|
lock(what, width, height) {
|
||||||
|
@ -205,7 +204,7 @@ class InlineView {
|
||||||
this.resize(this.lockedWidth, this.lockedHeight);
|
this.resize(this.lockedWidth, this.lockedHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
// Resize a single axis based on content dimensions
|
// Resize a single axis based on content dimensions
|
||||||
expand(force) {
|
expand(force) {
|
||||||
|
@ -233,16 +232,15 @@ class InlineView {
|
||||||
}
|
}
|
||||||
|
|
||||||
this._expanding = false;
|
this._expanding = false;
|
||||||
};
|
}
|
||||||
|
|
||||||
contentWidth(min) {
|
contentWidth(min) {
|
||||||
return this.frame.scrollWidth;
|
return this.frame.scrollWidth;
|
||||||
};
|
}
|
||||||
|
|
||||||
contentHeight(min) {
|
contentHeight(min) {
|
||||||
console.log(this.frame.scrollHeight);
|
|
||||||
return this.frame.scrollHeight;
|
return this.frame.scrollHeight;
|
||||||
};
|
}
|
||||||
|
|
||||||
|
|
||||||
resize(width, height) {
|
resize(width, height) {
|
||||||
|
@ -263,7 +261,7 @@ class InlineView {
|
||||||
|
|
||||||
this.elementBounds = bounds(this.element);
|
this.elementBounds = bounds(this.element);
|
||||||
|
|
||||||
size = {
|
let size = {
|
||||||
width: this.elementBounds.width,
|
width: this.elementBounds.width,
|
||||||
height: this.elementBounds.height,
|
height: this.elementBounds.height,
|
||||||
widthDelta: this.elementBounds.width - this.prevBounds.width,
|
widthDelta: this.elementBounds.width - this.prevBounds.width,
|
||||||
|
@ -274,7 +272,7 @@ class InlineView {
|
||||||
|
|
||||||
this.emit("resized", size);
|
this.emit("resized", size);
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
|
|
||||||
load(contents) {
|
load(contents) {
|
||||||
|
@ -283,10 +281,12 @@ class InlineView {
|
||||||
var doc = parse(contents, "text/html");
|
var doc = parse(contents, "text/html");
|
||||||
var body = qs(doc, "body");
|
var body = qs(doc, "body");
|
||||||
|
|
||||||
|
/*
|
||||||
var srcs = doc.querySelectorAll("[src]");
|
var srcs = doc.querySelectorAll("[src]");
|
||||||
|
|
||||||
Array.prototype.slice.call(srcs)
|
Array.prototype.slice.call(srcs)
|
||||||
.forEach(function(item) {
|
.forEach(function(item) {
|
||||||
var src = item.getAttribute('src');
|
var src = item.getAttribute("src");
|
||||||
var assetUri = URI(src);
|
var assetUri = URI(src);
|
||||||
var origin = assetUri.origin();
|
var origin = assetUri.origin();
|
||||||
var absoluteUri;
|
var absoluteUri;
|
||||||
|
@ -296,7 +296,7 @@ class InlineView {
|
||||||
item.src = absoluteUri;
|
item.src = absoluteUri;
|
||||||
}
|
}
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
*/
|
||||||
this.frame.innerHTML = body.innerHTML;
|
this.frame.innerHTML = body.innerHTML;
|
||||||
|
|
||||||
this.document = this.frame.ownerDocument;
|
this.document = this.frame.ownerDocument;
|
||||||
|
@ -310,26 +310,26 @@ class InlineView {
|
||||||
|
|
||||||
|
|
||||||
return loaded;
|
return loaded;
|
||||||
};
|
}
|
||||||
|
|
||||||
setLayout(layout) {
|
setLayout(layout) {
|
||||||
this.layout = layout;
|
this.layout = layout;
|
||||||
};
|
}
|
||||||
|
|
||||||
|
|
||||||
resizeListenters() {
|
resizeListenters() {
|
||||||
// Test size again
|
// Test size again
|
||||||
// clearTimeout(this.expanding);
|
// clearTimeout(this.expanding);
|
||||||
// this.expanding = setTimeout(this.expand.bind(this), 350);
|
// this.expanding = setTimeout(this.expand.bind(this), 350);
|
||||||
};
|
}
|
||||||
|
|
||||||
addListeners() {
|
addListeners() {
|
||||||
//TODO: Add content listeners for expanding
|
//TODO: Add content listeners for expanding
|
||||||
};
|
}
|
||||||
|
|
||||||
removeListeners(layoutFunc) {
|
removeListeners(layoutFunc) {
|
||||||
//TODO: remove content listeners for expanding
|
//TODO: remove content listeners for expanding
|
||||||
};
|
}
|
||||||
|
|
||||||
display(request) {
|
display(request) {
|
||||||
var displayed = new defer();
|
var displayed = new defer();
|
||||||
|
@ -353,7 +353,7 @@ class InlineView {
|
||||||
|
|
||||||
|
|
||||||
return displayed.promise;
|
return displayed.promise;
|
||||||
};
|
}
|
||||||
|
|
||||||
show() {
|
show() {
|
||||||
|
|
||||||
|
@ -364,7 +364,7 @@ class InlineView {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.emit("shown", this);
|
this.emit("shown", this);
|
||||||
};
|
}
|
||||||
|
|
||||||
hide() {
|
hide() {
|
||||||
// this.frame.style.display = "none";
|
// this.frame.style.display = "none";
|
||||||
|
@ -373,11 +373,11 @@ class InlineView {
|
||||||
|
|
||||||
this.stopExpanding = true;
|
this.stopExpanding = true;
|
||||||
this.emit("hidden", this);
|
this.emit("hidden", this);
|
||||||
};
|
}
|
||||||
|
|
||||||
position() {
|
position() {
|
||||||
return this.element.getBoundingClientRect();
|
return this.element.getBoundingClientRect();
|
||||||
};
|
}
|
||||||
|
|
||||||
locationOf(target) {
|
locationOf(target) {
|
||||||
var parentPos = this.frame.getBoundingClientRect();
|
var parentPos = this.frame.getBoundingClientRect();
|
||||||
|
@ -387,22 +387,22 @@ class InlineView {
|
||||||
"left": window.scrollX + parentPos.left + targetPos.left,
|
"left": window.scrollX + parentPos.left + targetPos.left,
|
||||||
"top": window.scrollY + parentPos.top + targetPos.top
|
"top": window.scrollY + parentPos.top + targetPos.top
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
|
|
||||||
onDisplayed(view) {
|
onDisplayed(view) {
|
||||||
// Stub, override with a custom functions
|
// Stub, override with a custom functions
|
||||||
};
|
}
|
||||||
|
|
||||||
onResize(view, e) {
|
onResize(view, e) {
|
||||||
// Stub, override with a custom functions
|
// Stub, override with a custom functions
|
||||||
};
|
}
|
||||||
|
|
||||||
bounds() {
|
bounds() {
|
||||||
if(!this.elementBounds) {
|
if(!this.elementBounds) {
|
||||||
this.elementBounds = bounds(this.element);
|
this.elementBounds = bounds(this.element);
|
||||||
}
|
}
|
||||||
return this.elementBounds;
|
return this.elementBounds;
|
||||||
};
|
}
|
||||||
|
|
||||||
destroy() {
|
destroy() {
|
||||||
|
|
||||||
|
@ -423,7 +423,7 @@ class InlineView {
|
||||||
}
|
}
|
||||||
// this.element.style.height = "0px";
|
// this.element.style.height = "0px";
|
||||||
// this.element.style.width = "0px";
|
// this.element.style.width = "0px";
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
EventEmitter(InlineView.prototype);
|
EventEmitter(InlineView.prototype);
|
||||||
|
|
|
@ -1,16 +1,16 @@
|
||||||
import EpubCFI from './epubcfi';
|
import EpubCFI from "./epubcfi";
|
||||||
|
|
||||||
class Mapping {
|
class Mapping {
|
||||||
constructor(layout) {
|
constructor(layout) {
|
||||||
this.layout = layout;
|
this.layout = layout;
|
||||||
};
|
}
|
||||||
|
|
||||||
section(view) {
|
section(view) {
|
||||||
var ranges = this.findRanges(view);
|
var ranges = this.findRanges(view);
|
||||||
var map = this.rangeListToCfiList(view.section.cfiBase, ranges);
|
var map = this.rangeListToCfiList(view.section.cfiBase, ranges);
|
||||||
|
|
||||||
return map;
|
return map;
|
||||||
};
|
}
|
||||||
|
|
||||||
page(contents, cfiBase, start, end) {
|
page(contents, cfiBase, start, end) {
|
||||||
var root = contents && contents.document ? contents.document.body : false;
|
var root = contents && contents.document ? contents.document.body : false;
|
||||||
|
@ -23,7 +23,7 @@ class Mapping {
|
||||||
start: this.findStart(root, start, end),
|
start: this.findStart(root, start, end),
|
||||||
end: this.findEnd(root, start, end)
|
end: this.findEnd(root, start, end)
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
walk(root, func) {
|
walk(root, func) {
|
||||||
//var treeWalker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT + NodeFilter.SHOW_TEXT, null, false);
|
//var treeWalker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT + NodeFilter.SHOW_TEXT, null, false);
|
||||||
|
@ -44,7 +44,7 @@ class Mapping {
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
};
|
}
|
||||||
|
|
||||||
findRanges(view){
|
findRanges(view){
|
||||||
var columns = [];
|
var columns = [];
|
||||||
|
@ -64,7 +64,7 @@ class Mapping {
|
||||||
}
|
}
|
||||||
|
|
||||||
return columns;
|
return columns;
|
||||||
};
|
}
|
||||||
|
|
||||||
findStart(root, start, end){
|
findStart(root, start, end){
|
||||||
var stack = [root];
|
var stack = [root];
|
||||||
|
@ -111,7 +111,7 @@ class Mapping {
|
||||||
|
|
||||||
// Return last element
|
// Return last element
|
||||||
return this.findTextStartRange($prev, start, end);
|
return this.findTextStartRange($prev, start, end);
|
||||||
};
|
}
|
||||||
|
|
||||||
findEnd(root, start, end){
|
findEnd(root, start, end){
|
||||||
var stack = [root];
|
var stack = [root];
|
||||||
|
@ -161,12 +161,11 @@ class Mapping {
|
||||||
|
|
||||||
// end of chapter
|
// end of chapter
|
||||||
return this.findTextEndRange($prev, start, end);
|
return this.findTextEndRange($prev, start, end);
|
||||||
};
|
}
|
||||||
|
|
||||||
|
|
||||||
findTextStartRange(node, start, end){
|
findTextStartRange(node, start, end){
|
||||||
var ranges = this.splitTextNodeIntoRanges(node);
|
var ranges = this.splitTextNodeIntoRanges(node);
|
||||||
var prev;
|
|
||||||
var range;
|
var range;
|
||||||
var pos;
|
var pos;
|
||||||
|
|
||||||
|
@ -179,12 +178,12 @@ class Mapping {
|
||||||
return range;
|
return range;
|
||||||
}
|
}
|
||||||
|
|
||||||
prev = range;
|
// prev = range;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ranges[0];
|
return ranges[0];
|
||||||
};
|
}
|
||||||
|
|
||||||
findTextEndRange(node, start, end){
|
findTextEndRange(node, start, end){
|
||||||
var ranges = this.splitTextNodeIntoRanges(node);
|
var ranges = this.splitTextNodeIntoRanges(node);
|
||||||
|
@ -210,15 +209,13 @@ class Mapping {
|
||||||
// Ends before limit
|
// Ends before limit
|
||||||
return ranges[ranges.length-1];
|
return ranges[ranges.length-1];
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
splitTextNodeIntoRanges(node, _splitter){
|
splitTextNodeIntoRanges(node, _splitter){
|
||||||
var ranges = [];
|
var ranges = [];
|
||||||
var textContent = node.textContent || "";
|
var textContent = node.textContent || "";
|
||||||
var text = textContent.trim();
|
var text = textContent.trim();
|
||||||
var range;
|
var range;
|
||||||
var rect;
|
|
||||||
var list;
|
|
||||||
var doc = node.ownerDocument;
|
var doc = node.ownerDocument;
|
||||||
var splitter = _splitter || " ";
|
var splitter = _splitter || " ";
|
||||||
|
|
||||||
|
@ -257,7 +254,7 @@ class Mapping {
|
||||||
}
|
}
|
||||||
|
|
||||||
return ranges;
|
return ranges;
|
||||||
};
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -279,11 +276,11 @@ class Mapping {
|
||||||
end: endCfi
|
end: endCfi
|
||||||
};
|
};
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
rangeListToCfiList(cfiBase, columns){
|
rangeListToCfiList(cfiBase, columns){
|
||||||
var map = [];
|
var map = [];
|
||||||
var rangePair, cifPair;
|
var cifPair;
|
||||||
|
|
||||||
for (var i = 0; i < columns.length; i++) {
|
for (var i = 0; i < columns.length; i++) {
|
||||||
cifPair = this.rangePairToCfiPair(cfiBase, columns[i]);
|
cifPair = this.rangePairToCfiPair(cfiBase, columns[i]);
|
||||||
|
@ -293,7 +290,7 @@ class Mapping {
|
||||||
}
|
}
|
||||||
|
|
||||||
return map;
|
return map;
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Mapping;
|
export default Mapping;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import {qs, qsa, querySelectorByType} from './utils/core';
|
import {qs, qsa, querySelectorByType} from "./utils/core";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Navigation Parser
|
* Navigation Parser
|
||||||
|
@ -13,7 +13,7 @@ class Navigation {
|
||||||
if (xml) {
|
if (xml) {
|
||||||
this.parse(xml);
|
this.parse(xml);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse out the navigation items
|
* Parse out the navigation items
|
||||||
|
@ -30,7 +30,7 @@ class Navigation {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.unpack(this.toc);
|
this.unpack(this.toc);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unpack navigation items
|
* Unpack navigation items
|
||||||
|
@ -46,7 +46,7 @@ class Navigation {
|
||||||
this.tocById[item.id] = i;
|
this.tocById[item.id] = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get an item from the navigation
|
* Get an item from the navigation
|
||||||
|
@ -67,7 +67,7 @@ class Navigation {
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.toc[index];
|
return this.toc[index];
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse from a Epub > 3.0 Nav
|
* Parse from a Epub > 3.0 Nav
|
||||||
|
@ -98,7 +98,7 @@ class Navigation {
|
||||||
}
|
}
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a navItem
|
* Create a navItem
|
||||||
|
@ -107,16 +107,16 @@ class Navigation {
|
||||||
* @return {object} navItem
|
* @return {object} navItem
|
||||||
*/
|
*/
|
||||||
navItem(item){
|
navItem(item){
|
||||||
var id = item.getAttribute('id') || false,
|
var id = item.getAttribute("id") || false,
|
||||||
content = qs(item, "a"),
|
content = qs(item, "a"),
|
||||||
src = content.getAttribute('href') || '',
|
src = content.getAttribute("href") || "",
|
||||||
text = content.textContent || "",
|
text = content.textContent || "",
|
||||||
subitems = [],
|
subitems = [],
|
||||||
parentNode = item.parentNode,
|
parentNode = item.parentNode,
|
||||||
parent;
|
parent;
|
||||||
|
|
||||||
if(parentNode && parentNode.nodeName === "navPoint") {
|
if(parentNode && parentNode.nodeName === "navPoint") {
|
||||||
parent = parentNode.getAttribute('id');
|
parent = parentNode.getAttribute("id");
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -126,7 +126,7 @@ class Navigation {
|
||||||
"subitems" : subitems,
|
"subitems" : subitems,
|
||||||
"parent" : parent
|
"parent" : parent
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse from a Epub > 3.0 NC
|
* Parse from a Epub > 3.0 NC
|
||||||
|
@ -156,7 +156,7 @@ class Navigation {
|
||||||
}
|
}
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a ncxItem
|
* Create a ncxItem
|
||||||
|
@ -165,9 +165,9 @@ class Navigation {
|
||||||
* @return {object} ncxItem
|
* @return {object} ncxItem
|
||||||
*/
|
*/
|
||||||
ncxItem(item){
|
ncxItem(item){
|
||||||
var id = item.getAttribute('id') || false,
|
var id = item.getAttribute("id") || false,
|
||||||
content = qs(item, "content"),
|
content = qs(item, "content"),
|
||||||
src = content.getAttribute('src'),
|
src = content.getAttribute("src"),
|
||||||
navLabel = qs(item, "navLabel"),
|
navLabel = qs(item, "navLabel"),
|
||||||
text = navLabel.textContent ? navLabel.textContent : "",
|
text = navLabel.textContent ? navLabel.textContent : "",
|
||||||
subitems = [],
|
subitems = [],
|
||||||
|
@ -175,7 +175,7 @@ class Navigation {
|
||||||
parent;
|
parent;
|
||||||
|
|
||||||
if(parentNode && parentNode.nodeName === "navPoint") {
|
if(parentNode && parentNode.nodeName === "navPoint") {
|
||||||
parent = parentNode.getAttribute('id');
|
parent = parentNode.getAttribute("id");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -186,7 +186,7 @@ class Navigation {
|
||||||
"subitems" : subitems,
|
"subitems" : subitems,
|
||||||
"parent" : parent
|
"parent" : parent
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* forEach pass through
|
* forEach pass through
|
||||||
|
@ -195,7 +195,7 @@ class Navigation {
|
||||||
*/
|
*/
|
||||||
forEach(fn) {
|
forEach(fn) {
|
||||||
return this.toc.forEach(fn);
|
return this.toc.forEach(fn);
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Navigation;
|
export default Navigation;
|
||||||
|
|
141
src/packaging.js
141
src/packaging.js
|
@ -1,5 +1,4 @@
|
||||||
import {qs, qsa, qsp} from './utils/core';
|
import {qs, qsa, qsp} from "./utils/core";
|
||||||
import EpubCFI from './epubcfi';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Open Packaging Format Parser
|
* Open Packaging Format Parser
|
||||||
|
@ -11,7 +10,7 @@ class Packaging {
|
||||||
if (packageDocument) {
|
if (packageDocument) {
|
||||||
this.parse(packageDocument);
|
this.parse(packageDocument);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse OPF XML
|
* Parse OPF XML
|
||||||
|
@ -22,26 +21,22 @@ class Packaging {
|
||||||
var metadataNode, manifestNode, spineNode;
|
var metadataNode, manifestNode, spineNode;
|
||||||
|
|
||||||
if(!packageDocument) {
|
if(!packageDocument) {
|
||||||
console.error("Package File Not Found");
|
throw new Error("Package File Not Found");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
metadataNode = qs(packageDocument, "metadata");
|
metadataNode = qs(packageDocument, "metadata");
|
||||||
if(!metadataNode) {
|
if(!metadataNode) {
|
||||||
console.error("No Metadata Found");
|
throw new Error("No Metadata Found");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
manifestNode = qs(packageDocument, "manifest");
|
manifestNode = qs(packageDocument, "manifest");
|
||||||
if(!manifestNode) {
|
if(!manifestNode) {
|
||||||
console.error("No Manifest Found");
|
throw new Error("No Manifest Found");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
spineNode = qs(packageDocument, "spine");
|
spineNode = qs(packageDocument, "spine");
|
||||||
if(!spineNode) {
|
if(!spineNode) {
|
||||||
console.error("No Spine Found");
|
throw new Error("No Spine Found");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.manifest = this.parseManifest(manifestNode);
|
this.manifest = this.parseManifest(manifestNode);
|
||||||
|
@ -59,15 +54,15 @@ class Packaging {
|
||||||
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'metadata' : this.metadata,
|
"metadata" : this.metadata,
|
||||||
'spine' : this.spine,
|
"spine" : this.spine,
|
||||||
'manifest' : this.manifest,
|
"manifest" : this.manifest,
|
||||||
'navPath' : this.navPath,
|
"navPath" : this.navPath,
|
||||||
'ncxPath' : this.ncxPath,
|
"ncxPath" : this.ncxPath,
|
||||||
'coverPath': this.coverPath,
|
"coverPath": this.coverPath,
|
||||||
'spineNodeIndex' : this.spineNodeIndex
|
"spineNodeIndex" : this.spineNodeIndex
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse Metadata
|
* Parse Metadata
|
||||||
|
@ -78,28 +73,28 @@ class Packaging {
|
||||||
parseMetadata(xml){
|
parseMetadata(xml){
|
||||||
var metadata = {};
|
var metadata = {};
|
||||||
|
|
||||||
metadata.title = this.getElementText(xml, 'title');
|
metadata.title = this.getElementText(xml, "title");
|
||||||
metadata.creator = this.getElementText(xml, 'creator');
|
metadata.creator = this.getElementText(xml, "creator");
|
||||||
metadata.description = this.getElementText(xml, 'description');
|
metadata.description = this.getElementText(xml, "description");
|
||||||
|
|
||||||
metadata.pubdate = this.getElementText(xml, 'date');
|
metadata.pubdate = this.getElementText(xml, "date");
|
||||||
|
|
||||||
metadata.publisher = this.getElementText(xml, 'publisher');
|
metadata.publisher = this.getElementText(xml, "publisher");
|
||||||
|
|
||||||
metadata.identifier = this.getElementText(xml, "identifier");
|
metadata.identifier = this.getElementText(xml, "identifier");
|
||||||
metadata.language = this.getElementText(xml, "language");
|
metadata.language = this.getElementText(xml, "language");
|
||||||
metadata.rights = this.getElementText(xml, "rights");
|
metadata.rights = this.getElementText(xml, "rights");
|
||||||
|
|
||||||
metadata.modified_date = this.getPropertyText(xml, 'dcterms:modified');
|
metadata.modified_date = this.getPropertyText(xml, "dcterms:modified");
|
||||||
|
|
||||||
metadata.layout = this.getPropertyText(xml, "rendition:layout");
|
metadata.layout = this.getPropertyText(xml, "rendition:layout");
|
||||||
metadata.orientation = this.getPropertyText(xml, 'rendition:orientation');
|
metadata.orientation = this.getPropertyText(xml, "rendition:orientation");
|
||||||
metadata.flow = this.getPropertyText(xml, 'rendition:flow');
|
metadata.flow = this.getPropertyText(xml, "rendition:flow");
|
||||||
metadata.viewport = this.getPropertyText(xml, 'rendition:viewport');
|
metadata.viewport = this.getPropertyText(xml, "rendition:viewport");
|
||||||
// metadata.page_prog_dir = packageXml.querySelector("spine").getAttribute("page-progression-direction");
|
// metadata.page_prog_dir = packageXml.querySelector("spine").getAttribute("page-progression-direction");
|
||||||
|
|
||||||
return metadata;
|
return metadata;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse Manifest
|
* Parse Manifest
|
||||||
|
@ -117,23 +112,23 @@ class Packaging {
|
||||||
|
|
||||||
//-- Create an object with the id as key
|
//-- Create an object with the id as key
|
||||||
items.forEach(function(item){
|
items.forEach(function(item){
|
||||||
var id = item.getAttribute('id'),
|
var id = item.getAttribute("id"),
|
||||||
href = item.getAttribute('href') || '',
|
href = item.getAttribute("href") || "",
|
||||||
type = item.getAttribute('media-type') || '',
|
type = item.getAttribute("media-type") || "",
|
||||||
properties = item.getAttribute('properties') || '';
|
properties = item.getAttribute("properties") || "";
|
||||||
|
|
||||||
manifest[id] = {
|
manifest[id] = {
|
||||||
'href' : href,
|
"href" : href,
|
||||||
// 'url' : href,
|
// "url" : href,
|
||||||
'type' : type,
|
"type" : type,
|
||||||
'properties' : properties.length ? properties.split(' ') : []
|
"properties" : properties.length ? properties.split(" ") : []
|
||||||
};
|
};
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return manifest;
|
return manifest;
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse Spine
|
* Parse Spine
|
||||||
|
@ -144,46 +139,46 @@ class Packaging {
|
||||||
parseSpine(spineXml, manifest){
|
parseSpine(spineXml, manifest){
|
||||||
var spine = [];
|
var spine = [];
|
||||||
|
|
||||||
var selected = spineXml.getElementsByTagName("itemref"),
|
var selected = spineXml.getElementsByTagName("itemref");
|
||||||
items = Array.prototype.slice.call(selected);
|
var items = Array.prototype.slice.call(selected);
|
||||||
|
|
||||||
var epubcfi = new EpubCFI();
|
// var epubcfi = new EpubCFI();
|
||||||
|
|
||||||
//-- Add to array to mantain ordering and cross reference with manifest
|
//-- Add to array to mantain ordering and cross reference with manifest
|
||||||
items.forEach(function(item, index){
|
items.forEach(function(item, index){
|
||||||
var idref = item.getAttribute('idref');
|
var idref = item.getAttribute("idref");
|
||||||
// var cfiBase = epubcfi.generateChapterComponent(spineNodeIndex, index, Id);
|
// var cfiBase = epubcfi.generateChapterComponent(spineNodeIndex, index, Id);
|
||||||
var props = item.getAttribute('properties') || '';
|
var props = item.getAttribute("properties") || "";
|
||||||
var propArray = props.length ? props.split(' ') : [];
|
var propArray = props.length ? props.split(" ") : [];
|
||||||
// var manifestProps = manifest[Id].properties;
|
// var manifestProps = manifest[Id].properties;
|
||||||
// var manifestPropArray = manifestProps.length ? manifestProps.split(' ') : [];
|
// var manifestPropArray = manifestProps.length ? manifestProps.split(" ") : [];
|
||||||
|
|
||||||
var itemref = {
|
var itemref = {
|
||||||
'idref' : idref,
|
"idref" : idref,
|
||||||
'linear' : item.getAttribute('linear') || '',
|
"linear" : item.getAttribute("linear") || "",
|
||||||
'properties' : propArray,
|
"properties" : propArray,
|
||||||
// 'href' : manifest[Id].href,
|
// "href" : manifest[Id].href,
|
||||||
// 'url' : manifest[Id].url,
|
// "url" : manifest[Id].url,
|
||||||
'index' : index
|
"index" : index
|
||||||
// 'cfiBase' : cfiBase
|
// "cfiBase" : cfiBase
|
||||||
};
|
};
|
||||||
spine.push(itemref);
|
spine.push(itemref);
|
||||||
});
|
});
|
||||||
|
|
||||||
return spine;
|
return spine;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find TOC NAV
|
* Find TOC NAV
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
findNavPath(manifestNode){
|
findNavPath(manifestNode){
|
||||||
// Find item with property 'nav'
|
// Find item with property "nav"
|
||||||
// Should catch nav irregardless of order
|
// Should catch nav irregardless of order
|
||||||
// var node = manifestNode.querySelector("item[properties$='nav'], item[properties^='nav '], item[properties*=' nav ']");
|
// var node = manifestNode.querySelector("item[properties$='nav'], item[properties^='nav '], item[properties*=' nav ']");
|
||||||
var node = qsp(manifestNode, "item", {"properties":"nav"});
|
var node = qsp(manifestNode, "item", {"properties":"nav"});
|
||||||
return node ? node.getAttribute('href') : false;
|
return node ? node.getAttribute("href") : false;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find TOC NCX
|
* Find TOC NCX
|
||||||
|
@ -206,8 +201,8 @@ class Packaging {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return node ? node.getAttribute('href') : false;
|
return node ? node.getAttribute("href") : false;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find the Cover Path
|
* Find the Cover Path
|
||||||
|
@ -218,15 +213,15 @@ class Packaging {
|
||||||
*/
|
*/
|
||||||
findCoverPath(packageXml){
|
findCoverPath(packageXml){
|
||||||
var pkg = qs(packageXml, "package");
|
var pkg = qs(packageXml, "package");
|
||||||
var epubVersion = pkg.getAttribute('version');
|
var epubVersion = pkg.getAttribute("version");
|
||||||
|
|
||||||
if (epubVersion === '2.0') {
|
if (epubVersion === "2.0") {
|
||||||
var metaCover = qsp(packageXml, 'meta', {'name':'cover'});
|
var metaCover = qsp(packageXml, "meta", {"name":"cover"});
|
||||||
if (metaCover) {
|
if (metaCover) {
|
||||||
var coverId = metaCover.getAttribute('content');
|
var coverId = metaCover.getAttribute("content");
|
||||||
// var cover = packageXml.querySelector("item[id='" + coverId + "']");
|
// var cover = packageXml.querySelector("item[id='" + coverId + "']");
|
||||||
var cover = packageXml.getElementById(coverId);
|
var cover = packageXml.getElementById(coverId);
|
||||||
return cover ? cover.getAttribute('href') : '';
|
return cover ? cover.getAttribute("href") : "";
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return false;
|
return false;
|
||||||
|
@ -234,10 +229,10 @@ class Packaging {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// var node = packageXml.querySelector("item[properties='cover-image']");
|
// var node = packageXml.querySelector("item[properties='cover-image']");
|
||||||
var node = qsp(packageXml, 'item', {'properties':'cover-image'});
|
var node = qsp(packageXml, "item", {"properties":"cover-image"});
|
||||||
return node ? node.getAttribute('href') : '';
|
return node ? node.getAttribute("href") : "";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get text of a namespaced element
|
* Get text of a namespaced element
|
||||||
|
@ -247,10 +242,10 @@ class Packaging {
|
||||||
* @return {string} text
|
* @return {string} text
|
||||||
*/
|
*/
|
||||||
getElementText(xml, tag){
|
getElementText(xml, tag){
|
||||||
var found = xml.getElementsByTagNameNS("http://purl.org/dc/elements/1.1/", tag),
|
var found = xml.getElementsByTagNameNS("http://purl.org/dc/elements/1.1/", tag);
|
||||||
el;
|
var el;
|
||||||
|
|
||||||
if(!found || found.length === 0) return '';
|
if(!found || found.length === 0) return "";
|
||||||
|
|
||||||
el = found[0];
|
el = found[0];
|
||||||
|
|
||||||
|
@ -258,9 +253,9 @@ class Packaging {
|
||||||
return el.childNodes[0].nodeValue;
|
return el.childNodes[0].nodeValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
return '';
|
return "";
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get text by property
|
* Get text by property
|
||||||
|
@ -276,8 +271,8 @@ class Packaging {
|
||||||
return el.childNodes[0].nodeValue;
|
return el.childNodes[0].nodeValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
return '';
|
return "";
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Packaging;
|
export default Packaging;
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
import EpubCFI from './epubcfi';
|
import EpubCFI from "./epubcfi";
|
||||||
import {
|
import {
|
||||||
qs,
|
qs,
|
||||||
qsa,
|
qsa,
|
||||||
querySelectorByType,
|
querySelectorByType,
|
||||||
indexOfSorted,
|
indexOfSorted,
|
||||||
locationOf
|
locationOf
|
||||||
} from './utils/core';
|
} from "./utils/core";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Page List Parser
|
* Page List Parser
|
||||||
|
@ -24,7 +24,7 @@ class PageList {
|
||||||
if(this.pageList && this.pageList.length) {
|
if(this.pageList && this.pageList.length) {
|
||||||
this.process(this.pageList);
|
this.process(this.pageList);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse PageList Xml
|
* Parse PageList Xml
|
||||||
|
@ -32,7 +32,7 @@ class PageList {
|
||||||
*/
|
*/
|
||||||
parse(xml) {
|
parse(xml) {
|
||||||
var html = qs(xml, "html");
|
var html = qs(xml, "html");
|
||||||
// var ncx = qs(xml, "ncx");
|
var ncx = qs(xml, "ncx");
|
||||||
|
|
||||||
if(html) {
|
if(html) {
|
||||||
this.toc = this.parseNav(xml);
|
this.toc = this.parseNav(xml);
|
||||||
|
@ -41,7 +41,7 @@ class PageList {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse a Nav PageList
|
* Parse a Nav PageList
|
||||||
|
@ -54,7 +54,6 @@ class PageList {
|
||||||
var navItems = navElement ? qsa(navElement, "li") : [];
|
var navItems = navElement ? qsa(navElement, "li") : [];
|
||||||
var length = navItems.length;
|
var length = navItems.length;
|
||||||
var i;
|
var i;
|
||||||
var toc = {};
|
|
||||||
var list = [];
|
var list = [];
|
||||||
var item;
|
var item;
|
||||||
|
|
||||||
|
@ -66,7 +65,7 @@ class PageList {
|
||||||
}
|
}
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Page List Item
|
* Page List Item
|
||||||
|
@ -75,9 +74,8 @@ class PageList {
|
||||||
* @return {object} pageListItem
|
* @return {object} pageListItem
|
||||||
*/
|
*/
|
||||||
item(item){
|
item(item){
|
||||||
var id = item.getAttribute('id') || false,
|
var content = qs(item, "a"),
|
||||||
content = qs(item, "a"),
|
href = content.getAttribute("href") || "",
|
||||||
href = content.getAttribute('href') || '',
|
|
||||||
text = content.textContent || "",
|
text = content.textContent || "",
|
||||||
page = parseInt(text),
|
page = parseInt(text),
|
||||||
isCfi = href.indexOf("epubcfi"),
|
isCfi = href.indexOf("epubcfi"),
|
||||||
|
@ -101,7 +99,7 @@ class PageList {
|
||||||
"page" : page
|
"page" : page
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Process pageList items
|
* Process pageList items
|
||||||
|
@ -118,7 +116,7 @@ class PageList {
|
||||||
this.firstPage = parseInt(this.pages[0]);
|
this.firstPage = parseInt(this.pages[0]);
|
||||||
this.lastPage = parseInt(this.pages[this.pages.length-1]);
|
this.lastPage = parseInt(this.pages[this.pages.length-1]);
|
||||||
this.totalPages = this.lastPage - this.firstPage;
|
this.totalPages = this.lastPage - this.firstPage;
|
||||||
};
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -159,7 +157,7 @@ class PageList {
|
||||||
}
|
}
|
||||||
|
|
||||||
return deferred.promise;
|
return deferred.promise;
|
||||||
};
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -198,7 +196,7 @@ class PageList {
|
||||||
|
|
||||||
}
|
}
|
||||||
return pg;
|
return pg;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get an EpubCFI from a Page List Item
|
* Get an EpubCFI from a Page List Item
|
||||||
|
@ -220,7 +218,7 @@ class PageList {
|
||||||
}
|
}
|
||||||
// TODO: handle pages not in the list
|
// TODO: handle pages not in the list
|
||||||
return cfi;
|
return cfi;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a Page from Book percentage
|
* Get a Page from Book percentage
|
||||||
|
@ -230,7 +228,7 @@ class PageList {
|
||||||
pageFromPercentage(percent){
|
pageFromPercentage(percent){
|
||||||
var pg = Math.round(this.totalPages * percent);
|
var pg = Math.round(this.totalPages * percent);
|
||||||
return pg;
|
return pg;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a value between 0 - 1 corresponding to the location of a page
|
* Returns a value between 0 - 1 corresponding to the location of a page
|
||||||
|
@ -240,7 +238,7 @@ class PageList {
|
||||||
percentageFromPage(pg){
|
percentageFromPage(pg){
|
||||||
var percentage = (pg - this.firstPage) / this.totalPages;
|
var percentage = (pg - this.firstPage) / this.totalPages;
|
||||||
return Math.round(percentage * 1000) / 1000;
|
return Math.round(percentage * 1000) / 1000;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a value between 0 - 1 corresponding to the location of a cfi
|
* Returns a value between 0 - 1 corresponding to the location of a cfi
|
||||||
|
@ -251,7 +249,7 @@ class PageList {
|
||||||
var pg = this.pageFromCfi(cfi);
|
var pg = this.pageFromCfi(cfi);
|
||||||
var percentage = this.percentageFromPage(pg);
|
var percentage = this.percentageFromPage(pg);
|
||||||
return percentage;
|
return percentage;
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default PageList;
|
export default PageList;
|
||||||
|
|
|
@ -1,13 +1,12 @@
|
||||||
import EventEmitter from 'event-emitter';
|
import EventEmitter from "event-emitter";
|
||||||
import { extend, defer, isFloat } from './utils/core';
|
import { extend, defer, isFloat } from "./utils/core";
|
||||||
import {replaceLinks} from './replacements';
|
import {replaceLinks} from "./utils/replacements";
|
||||||
import Hook from './hook';
|
import Hook from "./utils/hook";
|
||||||
import EpubCFI from './epubcfi';
|
import EpubCFI from "./epubcfi";
|
||||||
import Queue from './queue';
|
import Queue from "./utils/queue";
|
||||||
import Layout from './layout';
|
import Layout from "./layout";
|
||||||
import Mapping from './mapping';
|
import Mapping from "./mapping";
|
||||||
import Themes from './themes';
|
import Themes from "./themes";
|
||||||
import Path from './utils/path';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* [Rendition description]
|
* [Rendition description]
|
||||||
|
@ -29,7 +28,7 @@ class Rendition {
|
||||||
this.settings = extend(this.settings || {}, {
|
this.settings = extend(this.settings || {}, {
|
||||||
width: null,
|
width: null,
|
||||||
height: null,
|
height: null,
|
||||||
ignoreClass: '',
|
ignoreClass: "",
|
||||||
manager: "default",
|
manager: "default",
|
||||||
view: "iframe",
|
view: "iframe",
|
||||||
flow: null,
|
flow: null,
|
||||||
|
@ -84,7 +83,7 @@ class Rendition {
|
||||||
this.starting = new defer();
|
this.starting = new defer();
|
||||||
this.started = this.starting.promise;
|
this.started = this.starting.promise;
|
||||||
this.q.enqueue(this.start);
|
this.q.enqueue(this.start);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the manager function
|
* Set the manager function
|
||||||
|
@ -92,7 +91,7 @@ class Rendition {
|
||||||
*/
|
*/
|
||||||
setManager(manager) {
|
setManager(manager) {
|
||||||
this.manager = manager;
|
this.manager = manager;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Require the manager from passed string, or as a function
|
* Require the manager from passed string, or as a function
|
||||||
|
@ -106,14 +105,14 @@ class Rendition {
|
||||||
// or require included managers directly
|
// or require included managers directly
|
||||||
if (typeof manager === "string") {
|
if (typeof manager === "string") {
|
||||||
// Use global or require
|
// Use global or require
|
||||||
viewManager = typeof ePub != "undefined" ? ePub.ViewManagers[manager] : undefined; //require('./managers/'+manager);
|
viewManager = typeof ePub != "undefined" ? ePub.ViewManagers[manager] : undefined; //require("./managers/"+manager);
|
||||||
} else {
|
} else {
|
||||||
// otherwise, assume we were passed a function
|
// otherwise, assume we were passed a function
|
||||||
viewManager = manager
|
viewManager = manager;
|
||||||
}
|
}
|
||||||
|
|
||||||
return viewManager;
|
return viewManager;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Require the view from passed string, or as a function
|
* Require the view from passed string, or as a function
|
||||||
|
@ -124,14 +123,14 @@ class Rendition {
|
||||||
var View;
|
var View;
|
||||||
|
|
||||||
if (typeof view == "string") {
|
if (typeof view == "string") {
|
||||||
View = typeof ePub != "undefined" ? ePub.Views[view] : undefined; //require('./views/'+view);
|
View = typeof ePub != "undefined" ? ePub.Views[view] : undefined; //require("./views/"+view);
|
||||||
} else {
|
} else {
|
||||||
// otherwise, assume we were passed a function
|
// otherwise, assume we were passed a function
|
||||||
View = view
|
View = view;
|
||||||
}
|
}
|
||||||
|
|
||||||
return View;
|
return View;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start the rendering
|
* Start the rendering
|
||||||
|
@ -168,14 +167,14 @@ class Rendition {
|
||||||
this.manager.on("scroll", this.reportLocation.bind(this));
|
this.manager.on("scroll", this.reportLocation.bind(this));
|
||||||
|
|
||||||
|
|
||||||
this.on('displayed', this.reportLocation.bind(this));
|
this.on("displayed", this.reportLocation.bind(this));
|
||||||
|
|
||||||
// Trigger that rendering has started
|
// Trigger that rendering has started
|
||||||
this.emit("started");
|
this.emit("started");
|
||||||
|
|
||||||
// Start processing queue
|
// Start processing queue
|
||||||
this.starting.resolve();
|
this.starting.resolve();
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Call to attach the container to an element in the dom
|
* Call to attach the container to an element in the dom
|
||||||
|
@ -198,7 +197,7 @@ class Rendition {
|
||||||
|
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Display a point in the book
|
* Display a point in the book
|
||||||
|
@ -212,7 +211,7 @@ class Rendition {
|
||||||
|
|
||||||
return this.q.enqueue(this._display, target);
|
return this.q.enqueue(this._display, target);
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tells the manager what to display immediately
|
* Tells the manager what to display immediately
|
||||||
|
@ -229,9 +228,7 @@ class Rendition {
|
||||||
|
|
||||||
// Check if this is a book percentage
|
// Check if this is a book percentage
|
||||||
if (this.book.locations.length && isFloat(target)) {
|
if (this.book.locations.length && isFloat(target)) {
|
||||||
console.log("percentage", target);
|
target = this.book.locations.cfiFromPercentage(target);
|
||||||
target = book.locations.cfiFromPercentage(target);
|
|
||||||
console.log("cfi", target);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
section = this.book.spine.get(target);
|
section = this.book.spine.get(target);
|
||||||
|
@ -257,7 +254,7 @@ class Rendition {
|
||||||
// this.emit("displayed", section);
|
// this.emit("displayed", section);
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
render(view, show) {
|
render(view, show) {
|
||||||
|
@ -301,7 +298,7 @@ class Rendition {
|
||||||
this.trigger("loaderror", e);
|
this.trigger("loaderror", e);
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
};
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -313,7 +310,7 @@ class Rendition {
|
||||||
this.hooks.content.trigger(view, this);
|
this.hooks.content.trigger(view, this);
|
||||||
this.emit("rendered", view.section);
|
this.emit("rendered", view.section);
|
||||||
this.reportLocation();
|
this.reportLocation();
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Report resize events and display the last seen location
|
* Report resize events and display the last seen location
|
||||||
|
@ -330,7 +327,7 @@ class Rendition {
|
||||||
height: size.height
|
height: size.height
|
||||||
});
|
});
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Move the Rendition to a specific offset
|
* Move the Rendition to a specific offset
|
||||||
|
@ -339,7 +336,7 @@ class Rendition {
|
||||||
*/
|
*/
|
||||||
moveTo(offset){
|
moveTo(offset){
|
||||||
this.manager.moveTo(offset);
|
this.manager.moveTo(offset);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Go to the next "page" in the rendition
|
* Go to the next "page" in the rendition
|
||||||
|
@ -348,7 +345,7 @@ class Rendition {
|
||||||
next(){
|
next(){
|
||||||
return this.q.enqueue(this.manager.next.bind(this.manager))
|
return this.q.enqueue(this.manager.next.bind(this.manager))
|
||||||
.then(this.reportLocation.bind(this));
|
.then(this.reportLocation.bind(this));
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Go to the previous "page" in the rendition
|
* Go to the previous "page" in the rendition
|
||||||
|
@ -357,7 +354,7 @@ class Rendition {
|
||||||
prev(){
|
prev(){
|
||||||
return this.q.enqueue(this.manager.prev.bind(this.manager))
|
return this.q.enqueue(this.manager.prev.bind(this.manager))
|
||||||
.then(this.reportLocation.bind(this));
|
.then(this.reportLocation.bind(this));
|
||||||
};
|
}
|
||||||
|
|
||||||
//-- http://www.idpf.org/epub/301/spec/epub-publications.html#meta-properties-rendering
|
//-- http://www.idpf.org/epub/301/spec/epub-publications.html#meta-properties-rendering
|
||||||
/**
|
/**
|
||||||
|
@ -389,7 +386,7 @@ class Rendition {
|
||||||
};
|
};
|
||||||
|
|
||||||
return properties;
|
return properties;
|
||||||
};
|
}
|
||||||
|
|
||||||
// applyLayoutProperties(){
|
// applyLayoutProperties(){
|
||||||
// var settings = this.determineLayoutProperties(this.book.package.metadata);
|
// var settings = this.determineLayoutProperties(this.book.package.metadata);
|
||||||
|
@ -421,7 +418,7 @@ class Rendition {
|
||||||
if (this.manager) {
|
if (this.manager) {
|
||||||
this.manager.updateFlow(_flow);
|
this.manager.updateFlow(_flow);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adjust the layout of the rendition to reflowable or pre-paginated
|
* Adjust the layout of the rendition to reflowable or pre-paginated
|
||||||
|
@ -440,7 +437,7 @@ class Rendition {
|
||||||
}
|
}
|
||||||
|
|
||||||
return this._layout;
|
return this._layout;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adjust if the rendition uses spreads
|
* Adjust if the rendition uses spreads
|
||||||
|
@ -454,7 +451,7 @@ class Rendition {
|
||||||
if (this.manager.isRendered()) {
|
if (this.manager.isRendered()) {
|
||||||
this.manager.updateLayout();
|
this.manager.updateLayout();
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Report the current location
|
* Report the current location
|
||||||
|
@ -463,7 +460,7 @@ class Rendition {
|
||||||
reportLocation(){
|
reportLocation(){
|
||||||
return this.q.enqueue(function(){
|
return this.q.enqueue(function(){
|
||||||
var location = this.manager.currentLocation();
|
var location = this.manager.currentLocation();
|
||||||
if (location && location.then && typeof location.then === 'function') {
|
if (location && location.then && typeof location.then === "function") {
|
||||||
location.then(function(result) {
|
location.then(function(result) {
|
||||||
this.location = result;
|
this.location = result;
|
||||||
|
|
||||||
|
@ -485,7 +482,7 @@ class Rendition {
|
||||||
}
|
}
|
||||||
|
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the Current Location CFI
|
* Get the Current Location CFI
|
||||||
|
@ -493,7 +490,7 @@ class Rendition {
|
||||||
*/
|
*/
|
||||||
currentLocation(){
|
currentLocation(){
|
||||||
var location = this.manager.currentLocation();
|
var location = this.manager.currentLocation();
|
||||||
if (location && location.then && typeof location.then === 'function') {
|
if (location && location.then && typeof location.then === "function") {
|
||||||
location.then(function(result) {
|
location.then(function(result) {
|
||||||
var percentage = this.book.locations.percentageFromCfi(result);
|
var percentage = this.book.locations.percentageFromCfi(result);
|
||||||
if (percentage != null) {
|
if (percentage != null) {
|
||||||
|
@ -508,7 +505,7 @@ class Rendition {
|
||||||
}
|
}
|
||||||
return location;
|
return location;
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove and Clean Up the Rendition
|
* Remove and Clean Up the Rendition
|
||||||
|
@ -518,7 +515,7 @@ class Rendition {
|
||||||
this.q.clear();
|
this.q.clear();
|
||||||
|
|
||||||
this.manager.destroy();
|
this.manager.destroy();
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pass the events from a view
|
* Pass the events from a view
|
||||||
|
@ -531,7 +528,7 @@ class Rendition {
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
view.on("selected", this.triggerSelectedEvent.bind(this));
|
view.on("selected", this.triggerSelectedEvent.bind(this));
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Emit events passed by a view
|
* Emit events passed by a view
|
||||||
|
@ -540,7 +537,7 @@ class Rendition {
|
||||||
*/
|
*/
|
||||||
triggerViewEvent(e){
|
triggerViewEvent(e){
|
||||||
this.emit(e.type, e);
|
this.emit(e.type, e);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Emit a selection event's CFI Range passed from a a view
|
* Emit a selection event's CFI Range passed from a a view
|
||||||
|
@ -549,7 +546,7 @@ class Rendition {
|
||||||
*/
|
*/
|
||||||
triggerSelectedEvent(cfirange){
|
triggerSelectedEvent(cfirange){
|
||||||
this.emit("selected", cfirange);
|
this.emit("selected", cfirange);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a Range from a Visible CFI
|
* Get a Range from a Visible CFI
|
||||||
|
@ -567,7 +564,7 @@ class Rendition {
|
||||||
if (found.length) {
|
if (found.length) {
|
||||||
return found[0].range(_cfi, ignoreClass);
|
return found[0].range(_cfi, ignoreClass);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hook to adjust images to fit in columns
|
* Hook to adjust images to fit in columns
|
||||||
|
@ -587,11 +584,11 @@ class Rendition {
|
||||||
resolve();
|
resolve();
|
||||||
}, 1);
|
}, 1);
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
getContents () {
|
getContents () {
|
||||||
return this.manager ? this.manager.getContents() : [];
|
return this.manager ? this.manager.getContents() : [];
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//-- Enable binding events to Renderer
|
//-- Enable binding events to Renderer
|
||||||
|
|
|
@ -1,21 +1,21 @@
|
||||||
import {substitute} from './replacements';
|
import {substitute} from "./utils/replacements";
|
||||||
import {createBase64Url, createBlobUrl} from './utils/core';
|
import {createBase64Url, createBlobUrl} from "./utils/core";
|
||||||
import Path from './utils/path';
|
import Path from "./utils/path";
|
||||||
import path from 'path-webpack';
|
import path from "path-webpack";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle Package Resources
|
* Handle Package Resources
|
||||||
* @class
|
* @class
|
||||||
* @param {Manifest} manifest
|
* @param {Manifest} manifest
|
||||||
* @param {[object]} options
|
* @param {[object]} options
|
||||||
* @param {[string='base64']} options.replacements
|
* @param {[string="base64"]} options.replacements
|
||||||
* @param {[Archive]} options.archive
|
* @param {[Archive]} options.archive
|
||||||
* @param {[method]} options.resolver
|
* @param {[method]} options.resolver
|
||||||
*/
|
*/
|
||||||
class Resources {
|
class Resources {
|
||||||
constructor(manifest, options) {
|
constructor(manifest, options) {
|
||||||
this.settings = {
|
this.settings = {
|
||||||
replacements: (options && options.replacements) || 'base64',
|
replacements: (options && options.replacements) || "base64",
|
||||||
archive: (options && options.archive),
|
archive: (options && options.archive),
|
||||||
resolver: (options && options.resolver)
|
resolver: (options && options.resolver)
|
||||||
};
|
};
|
||||||
|
@ -62,7 +62,7 @@ class Resources {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert split resources into Urls
|
* Convert split resources into Urls
|
||||||
|
@ -81,7 +81,7 @@ class Resources {
|
||||||
return item.href;
|
return item.href;
|
||||||
});
|
});
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create blob urls for all the assets
|
* Create blob urls for all the assets
|
||||||
|
@ -94,24 +94,24 @@ class Resources {
|
||||||
resolver = resolver || this.settings.resolver;
|
resolver = resolver || this.settings.resolver;
|
||||||
|
|
||||||
if (this.settings.replacements === "none") {
|
if (this.settings.replacements === "none") {
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve) {
|
||||||
resolve(this.urls);
|
resolve(this.urls);
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
var replacements = this.urls.
|
var replacements = this.urls.
|
||||||
map(function(url) {
|
map( (url) => {
|
||||||
var absolute = resolver(url);
|
var absolute = resolver(url);
|
||||||
|
|
||||||
return archive.createUrl(absolute, {"base64": (this.settings.replacements === "base64")});
|
return archive.createUrl(absolute, {"base64": (this.settings.replacements === "base64")});
|
||||||
}.bind(this))
|
});
|
||||||
|
|
||||||
return Promise.all(replacements)
|
return Promise.all(replacements)
|
||||||
.then(function(replacementUrls) {
|
.then( (replacementUrls) => {
|
||||||
this.replacementUrls = replacementUrls;
|
this.replacementUrls = replacementUrls;
|
||||||
return replacementUrls;
|
return replacementUrls;
|
||||||
}.bind(this));
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Replace URLs in CSS resources
|
* Replace URLs in CSS resources
|
||||||
|
@ -137,7 +137,7 @@ class Resources {
|
||||||
replaced.push(replacement);
|
replaced.push(replacement);
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
return Promise.all(replaced);
|
return Promise.all(replaced);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new CSS file with the replaced URLs
|
* Create a new CSS file with the replaced URLs
|
||||||
|
@ -149,13 +149,12 @@ class Resources {
|
||||||
*/
|
*/
|
||||||
createCssFile(href, archive, resolver){
|
createCssFile(href, archive, resolver){
|
||||||
var newUrl;
|
var newUrl;
|
||||||
var indexInUrls;
|
|
||||||
archive = archive || this.settings.archive;
|
archive = archive || this.settings.archive;
|
||||||
resolver = resolver || this.settings.resolver;
|
resolver = resolver || this.settings.resolver;
|
||||||
|
|
||||||
if (path.isAbsolute(href)) {
|
if (path.isAbsolute(href)) {
|
||||||
return new Promise(function(resolve, reject){
|
return new Promise(function(resolve){
|
||||||
resolve(urls, replacementUrls);
|
resolve();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -164,28 +163,28 @@ class Resources {
|
||||||
// Get the text of the css file from the archive
|
// Get the text of the css file from the archive
|
||||||
var textResponse = archive.getText(absolute);
|
var textResponse = archive.getText(absolute);
|
||||||
// Get asset links relative to css file
|
// Get asset links relative to css file
|
||||||
var relUrls = this.urls.map(function(assetHref) {
|
var relUrls = this.urls.map( (assetHref) => {
|
||||||
var resolved = resolver(assetHref);
|
var resolved = resolver(assetHref);
|
||||||
var relative = new Path(absolute).relative(resolved);
|
var relative = new Path(absolute).relative(resolved);
|
||||||
|
|
||||||
return relative;
|
return relative;
|
||||||
}.bind(this));
|
});
|
||||||
|
|
||||||
return textResponse.then(function (text) {
|
return textResponse.then( (text) => {
|
||||||
// Replacements in the css text
|
// Replacements in the css text
|
||||||
text = substitute(text, relUrls, this.replacementUrls);
|
text = substitute(text, relUrls, this.replacementUrls);
|
||||||
|
|
||||||
// Get the new url
|
// Get the new url
|
||||||
if (this.settings.replacements === "base64") {
|
if (this.settings.replacements === "base64") {
|
||||||
newUrl = createBase64Url(text, 'text/css');
|
newUrl = createBase64Url(text, "text/css");
|
||||||
} else {
|
} else {
|
||||||
newUrl = createBlobUrl(text, 'text/css');
|
newUrl = createBlobUrl(text, "text/css");
|
||||||
}
|
}
|
||||||
|
|
||||||
return newUrl;
|
return newUrl;
|
||||||
}.bind(this));
|
});
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolve all resources URLs relative to an absolute URL
|
* Resolve all resources URLs relative to an absolute URL
|
||||||
|
@ -203,7 +202,7 @@ class Resources {
|
||||||
var relative = new Path(absolute).relative(resolved);
|
var relative = new Path(absolute).relative(resolved);
|
||||||
return relative;
|
return relative;
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a URL for a resource
|
* Get a URL for a resource
|
||||||
|
@ -220,8 +219,8 @@ class Resources {
|
||||||
resolve(this.replacementUrls[indexInUrls]);
|
resolve(this.replacementUrls[indexInUrls]);
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
} else {
|
} else {
|
||||||
return archive.createUrl(absolute,
|
return this.archive.createUrl(path,
|
||||||
{"base64": (this.settings.replacements === "base64")})
|
{"base64": (this.settings.replacements === "base64")});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -240,7 +239,7 @@ class Resources {
|
||||||
relUrls = this.urls;
|
relUrls = this.urls;
|
||||||
}
|
}
|
||||||
return substitute(content, relUrls, this.replacementUrls);
|
return substitute(content, relUrls, this.replacementUrls);
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Resources;
|
export default Resources;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { defer } from './utils/core';
|
import { defer } from "./utils/core";
|
||||||
import EpubCFI from './epubcfi';
|
import EpubCFI from "./epubcfi";
|
||||||
import Hook from './hook';
|
import Hook from "./utils/hook";
|
||||||
import Url from './utils/url';
|
import { replaceBase } from "./utils/replacements";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a Section of the Book
|
* Represents a Section of the Book
|
||||||
|
@ -29,8 +29,7 @@ class Section {
|
||||||
this.hooks.serialize = new Hook(this);
|
this.hooks.serialize = new Hook(this);
|
||||||
this.hooks.content = new Hook(this);
|
this.hooks.content = new Hook(this);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load the section from its url
|
* Load the section from its url
|
||||||
|
@ -38,7 +37,7 @@ class Section {
|
||||||
* @return {document} a promise with the xml document
|
* @return {document} a promise with the xml document
|
||||||
*/
|
*/
|
||||||
load(_request){
|
load(_request){
|
||||||
var request = _request || this.request || require('./request');
|
var request = _request || this.request || require("./utils/request");
|
||||||
var loading = new defer();
|
var loading = new defer();
|
||||||
var loaded = loading.promise;
|
var loaded = loading.promise;
|
||||||
|
|
||||||
|
@ -47,8 +46,7 @@ class Section {
|
||||||
} else {
|
} else {
|
||||||
request(this.url)
|
request(this.url)
|
||||||
.then(function(xml){
|
.then(function(xml){
|
||||||
var base;
|
// var directory = new Url(this.url).directory;
|
||||||
var directory = new Url(this.url).directory;
|
|
||||||
|
|
||||||
this.document = xml;
|
this.document = xml;
|
||||||
this.contents = xml.documentElement;
|
this.contents = xml.documentElement;
|
||||||
|
@ -64,33 +62,15 @@ class Section {
|
||||||
}
|
}
|
||||||
|
|
||||||
return loaded;
|
return loaded;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a base tag for resolving urls in the section
|
* Adds a base tag for resolving urls in the section
|
||||||
* @private
|
* @private
|
||||||
* @param {document} _document
|
|
||||||
*/
|
*/
|
||||||
base(_document){
|
base(){
|
||||||
var task = new defer();
|
return replaceBase(this.document, this);
|
||||||
var base = _document.createElement("base"); // TODO: check if exists
|
|
||||||
var head;
|
|
||||||
|
|
||||||
base.setAttribute("href", window.location.origin + "/" +this.url);
|
|
||||||
|
|
||||||
if(_document) {
|
|
||||||
head = _document.querySelector("head");
|
|
||||||
}
|
}
|
||||||
if(head) {
|
|
||||||
head.insertBefore(base, head.firstChild);
|
|
||||||
task.resolve();
|
|
||||||
} else {
|
|
||||||
task.reject(new Error("No head to insert into"));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return task.promise;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render the contents of a section
|
* Render the contents of a section
|
||||||
|
@ -108,7 +88,7 @@ class Section {
|
||||||
var Serializer;
|
var Serializer;
|
||||||
|
|
||||||
if (typeof XMLSerializer === "undefined") {
|
if (typeof XMLSerializer === "undefined") {
|
||||||
Serializer = require('xmldom').XMLSerializer;
|
Serializer = require("xmldom").XMLSerializer;
|
||||||
} else {
|
} else {
|
||||||
Serializer = XMLSerializer;
|
Serializer = XMLSerializer;
|
||||||
}
|
}
|
||||||
|
@ -127,7 +107,7 @@ class Section {
|
||||||
});
|
});
|
||||||
|
|
||||||
return rendered;
|
return rendered;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find a string in a section
|
* Find a string in a section
|
||||||
|
@ -135,9 +115,9 @@ class Section {
|
||||||
* @param {string} query [description]
|
* @param {string} query [description]
|
||||||
* @return {[type]} [description]
|
* @return {[type]} [description]
|
||||||
*/
|
*/
|
||||||
find(query){
|
find(){
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reconciles the current chapters layout properies with
|
* Reconciles the current chapters layout properies with
|
||||||
|
@ -155,7 +135,7 @@ class Section {
|
||||||
|
|
||||||
//-- Get the chapter's display type
|
//-- Get the chapter's display type
|
||||||
this.properties.forEach(function(prop){
|
this.properties.forEach(function(prop){
|
||||||
var rendition = prop.replace("rendition:", '');
|
var rendition = prop.replace("rendition:", "");
|
||||||
var split = rendition.indexOf("-");
|
var split = rendition.indexOf("-");
|
||||||
var property, value;
|
var property, value;
|
||||||
|
|
||||||
|
@ -167,7 +147,7 @@ class Section {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return settings;
|
return settings;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a CFI from a Range in the Section
|
* Get a CFI from a Range in the Section
|
||||||
|
@ -176,7 +156,7 @@ class Section {
|
||||||
*/
|
*/
|
||||||
cfiFromRange(_range) {
|
cfiFromRange(_range) {
|
||||||
return new EpubCFI(_range, this.cfiBase).toString();
|
return new EpubCFI(_range, this.cfiBase).toString();
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a CFI from an Element in the Section
|
* Get a CFI from an Element in the Section
|
||||||
|
@ -185,7 +165,7 @@ class Section {
|
||||||
*/
|
*/
|
||||||
cfiFromElement(el) {
|
cfiFromElement(el) {
|
||||||
return new EpubCFI(el, this.cfiBase).toString();
|
return new EpubCFI(el, this.cfiBase).toString();
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Section;
|
export default Section;
|
||||||
|
|
32
src/spine.js
32
src/spine.js
|
@ -1,8 +1,7 @@
|
||||||
import core from './utils/core';
|
import EpubCFI from "./epubcfi";
|
||||||
import EpubCFI from './epubcfi';
|
import Hook from "./utils/hook";
|
||||||
import Hook from './hook';
|
import Section from "./section";
|
||||||
import Section from './section';
|
import {replaceBase, replaceCanonical} from "./utils/replacements";
|
||||||
import {replaceBase, replaceCanonical} from './replacements';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A collection of Spine Items
|
* A collection of Spine Items
|
||||||
|
@ -24,7 +23,7 @@ class Spine {
|
||||||
this.epubcfi = new EpubCFI();
|
this.epubcfi = new EpubCFI();
|
||||||
|
|
||||||
this.loaded = false;
|
this.loaded = false;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unpack items from a opf into spine items
|
* Unpack items from a opf into spine items
|
||||||
|
@ -36,11 +35,10 @@ class Spine {
|
||||||
this.items = _package.spine;
|
this.items = _package.spine;
|
||||||
this.manifest = _package.manifest;
|
this.manifest = _package.manifest;
|
||||||
this.spineNodeIndex = _package.spineNodeIndex;
|
this.spineNodeIndex = _package.spineNodeIndex;
|
||||||
this.baseUrl = _package.baseUrl || _package.basePath || '';
|
this.baseUrl = _package.baseUrl || _package.basePath || "";
|
||||||
this.length = this.items.length;
|
this.length = this.items.length;
|
||||||
|
|
||||||
this.items.forEach(function(item, index){
|
this.items.forEach( (item, index) => {
|
||||||
var href, url;
|
|
||||||
var manifestItem = this.manifest[item.idref];
|
var manifestItem = this.manifest[item.idref];
|
||||||
var spineItem;
|
var spineItem;
|
||||||
|
|
||||||
|
@ -63,10 +61,10 @@ class Spine {
|
||||||
this.append(spineItem);
|
this.append(spineItem);
|
||||||
|
|
||||||
|
|
||||||
}.bind(this));
|
});
|
||||||
|
|
||||||
this.loaded = true;
|
this.loaded = true;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get an item from the spine
|
* Get an item from the spine
|
||||||
|
@ -94,7 +92,7 @@ class Spine {
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.spineItems[index] || null;
|
return this.spineItems[index] || null;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Append a Section to the Spine
|
* Append a Section to the Spine
|
||||||
|
@ -111,7 +109,7 @@ class Spine {
|
||||||
this.spineById[section.idref] = index;
|
this.spineById[section.idref] = index;
|
||||||
|
|
||||||
return index;
|
return index;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prepend a Section to the Spine
|
* Prepend a Section to the Spine
|
||||||
|
@ -119,7 +117,7 @@ class Spine {
|
||||||
* @param {Section} section
|
* @param {Section} section
|
||||||
*/
|
*/
|
||||||
prepend(section) {
|
prepend(section) {
|
||||||
var index = this.spineItems.unshift(section);
|
// var index = this.spineItems.unshift(section);
|
||||||
this.spineByHref[section.href] = 0;
|
this.spineByHref[section.href] = 0;
|
||||||
this.spineById[section.idref] = 0;
|
this.spineById[section.idref] = 0;
|
||||||
|
|
||||||
|
@ -129,7 +127,7 @@ class Spine {
|
||||||
});
|
});
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
};
|
}
|
||||||
|
|
||||||
// insert(section, index) {
|
// insert(section, index) {
|
||||||
//
|
//
|
||||||
|
@ -149,7 +147,7 @@ class Spine {
|
||||||
|
|
||||||
return this.spineItems.splice(index, 1);
|
return this.spineItems.splice(index, 1);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loop over the Sections in the Spine
|
* Loop over the Sections in the Spine
|
||||||
|
@ -157,7 +155,7 @@ class Spine {
|
||||||
*/
|
*/
|
||||||
each() {
|
each() {
|
||||||
return this.spineItems.forEach.apply(this.spineItems, arguments);
|
return this.spineItems.forEach.apply(this.spineItems, arguments);
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Spine;
|
export default Spine;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import Url from './utils/url';
|
import Url from "./utils/url";
|
||||||
|
|
||||||
class Themes {
|
class Themes {
|
||||||
constructor(rendition) {
|
constructor(rendition) {
|
||||||
|
@ -7,7 +7,7 @@ class Themes {
|
||||||
"default" : {
|
"default" : {
|
||||||
"rules" : [],
|
"rules" : [],
|
||||||
"url" : "",
|
"url" : "",
|
||||||
"serialized" : ''
|
"serialized" : ""
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
this._overrides = {};
|
this._overrides = {};
|
||||||
|
@ -34,7 +34,7 @@ class Themes {
|
||||||
if (arguments.length === 2 && typeof(arguments[1]) === "object") {
|
if (arguments.length === 2 && typeof(arguments[1]) === "object") {
|
||||||
return this.registerRules(arguments[0], arguments[1]);
|
return this.registerRules(arguments[0], arguments[1]);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
default (theme) {
|
default (theme) {
|
||||||
if (!theme) {
|
if (!theme) {
|
||||||
|
@ -46,7 +46,7 @@ class Themes {
|
||||||
if (typeof(theme) === "object") {
|
if (typeof(theme) === "object") {
|
||||||
return this.registerRules("default", theme);
|
return this.registerRules("default", theme);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
registerThemes (themes) {
|
registerThemes (themes) {
|
||||||
for (var theme in themes) {
|
for (var theme in themes) {
|
||||||
|
@ -58,17 +58,17 @@ class Themes {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
registerUrl (name, input) {
|
registerUrl (name, input) {
|
||||||
var url = new Url(input);
|
var url = new Url(input);
|
||||||
this._themes[name] = { "url": url.toString() };
|
this._themes[name] = { "url": url.toString() };
|
||||||
};
|
}
|
||||||
|
|
||||||
registerRules (name, rules) {
|
registerRules (name, rules) {
|
||||||
this._themes[name] = { "rules": rules };
|
this._themes[name] = { "rules": rules };
|
||||||
// TODO: serialize css rules
|
// TODO: serialize css rules
|
||||||
};
|
}
|
||||||
|
|
||||||
apply (name) {
|
apply (name) {
|
||||||
var prev = this._current;
|
var prev = this._current;
|
||||||
|
@ -78,18 +78,18 @@ class Themes {
|
||||||
this.update(name);
|
this.update(name);
|
||||||
|
|
||||||
contents = this.rendition.getContents();
|
contents = this.rendition.getContents();
|
||||||
contents.forEach(function (content) {
|
contents.forEach( (content) => {
|
||||||
content.removeClass(prev);
|
content.removeClass(prev);
|
||||||
content.addClass(name);
|
content.addClass(name);
|
||||||
}.bind(this));
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
update (name) {
|
update (name) {
|
||||||
var contents = this.rendition.getContents();
|
var contents = this.rendition.getContents();
|
||||||
contents.forEach(function (content) {
|
contents.forEach( (content) => {
|
||||||
this.add(name, content);
|
this.add(name, content);
|
||||||
}.bind(this));
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
inject (view) {
|
inject (view) {
|
||||||
var links = [];
|
var links = [];
|
||||||
|
@ -108,7 +108,7 @@ class Themes {
|
||||||
if(this._current) {
|
if(this._current) {
|
||||||
view.contents.addClass(this._current);
|
view.contents.addClass(this._current);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
add (name, contents) {
|
add (name, contents) {
|
||||||
var theme = this._themes[name];
|
var theme = this._themes[name];
|
||||||
|
@ -125,33 +125,32 @@ class Themes {
|
||||||
contents.addStylesheetRules(theme.rules);
|
contents.addStylesheetRules(theme.rules);
|
||||||
theme.injected = true;
|
theme.injected = true;
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
override (name, value) {
|
override (name, value) {
|
||||||
var contents = this.rendition.getContents();
|
var contents = this.rendition.getContents();
|
||||||
|
|
||||||
this._overrides[name] = value;
|
this._overrides[name] = value;
|
||||||
|
|
||||||
contents.forEach(function (content) {
|
contents.forEach( (content) => {
|
||||||
content.css(name, this._overrides[name]);
|
content.css(name, this._overrides[name]);
|
||||||
}.bind(this));
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
overrides (view) {
|
overrides (view) {
|
||||||
var contents = view.contents;
|
var contents = view.contents;
|
||||||
var overrides = this._overrides;
|
var overrides = this._overrides;
|
||||||
var rules = [];
|
|
||||||
|
|
||||||
for (var rule in overrides) {
|
for (var rule in overrides) {
|
||||||
if (overrides.hasOwnProperty(rule)) {
|
if (overrides.hasOwnProperty(rule)) {
|
||||||
contents.css(rule, overrides[rule]);
|
contents.css(rule, overrides[rule]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
fontSize (size) {
|
fontSize (size) {
|
||||||
this.override("font-size", size);
|
this.override("font-size", size);
|
||||||
};
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,32 +1,19 @@
|
||||||
export const requestAnimationFrame = (typeof window != 'undefined') ? (window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame) : false;
|
export const requestAnimationFrame = (typeof window != "undefined") ? (window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame) : false;
|
||||||
|
|
||||||
export function isElement(obj) {
|
export function isElement(obj) {
|
||||||
return !!(obj && obj.nodeType == 1);
|
return !!(obj && obj.nodeType == 1);
|
||||||
};
|
}
|
||||||
|
|
||||||
// http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
|
// http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
|
||||||
export function uuid() {
|
export function uuid() {
|
||||||
var d = new Date().getTime();
|
var d = new Date().getTime();
|
||||||
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
var uuid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
|
||||||
var r = (d + Math.random()*16)%16 | 0;
|
var r = (d + Math.random()*16)%16 | 0;
|
||||||
d = Math.floor(d/16);
|
d = Math.floor(d/16);
|
||||||
return (c=='x' ? r : (r&0x7|0x8)).toString(16);
|
return (c=="x" ? r : (r&0x7|0x8)).toString(16);
|
||||||
});
|
});
|
||||||
return uuid;
|
return uuid;
|
||||||
};
|
|
||||||
|
|
||||||
// From Lodash
|
|
||||||
export function values(object) {
|
|
||||||
var index = -1,
|
|
||||||
props = Object.keys(object),
|
|
||||||
length = props.length,
|
|
||||||
result = Array(length);
|
|
||||||
|
|
||||||
while (++index < length) {
|
|
||||||
result[index] = object[props[index]];
|
|
||||||
}
|
}
|
||||||
return result;
|
|
||||||
};
|
|
||||||
|
|
||||||
export function documentHeight() {
|
export function documentHeight() {
|
||||||
return Math.max(
|
return Math.max(
|
||||||
|
@ -36,34 +23,34 @@ export function documentHeight() {
|
||||||
document.body.offsetHeight,
|
document.body.offsetHeight,
|
||||||
document.documentElement.offsetHeight
|
document.documentElement.offsetHeight
|
||||||
);
|
);
|
||||||
};
|
}
|
||||||
|
|
||||||
export function isNumber(n) {
|
export function isNumber(n) {
|
||||||
return !isNaN(parseFloat(n)) && isFinite(n);
|
return !isNaN(parseFloat(n)) && isFinite(n);
|
||||||
};
|
}
|
||||||
|
|
||||||
export function isFloat(n) {
|
export function isFloat(n) {
|
||||||
return isNumber(n) && (Math.floor(n) !== n);
|
return isNumber(n) && (Math.floor(n) !== n);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function prefixed(unprefixed) {
|
export function prefixed(unprefixed) {
|
||||||
var vendors = ["Webkit", "Moz", "O", "ms" ],
|
var vendors = ["Webkit", "Moz", "O", "ms" ];
|
||||||
prefixes = ['-Webkit-', '-moz-', '-o-', '-ms-'],
|
// var prefixes = ["-Webkit-", "-moz-", "-o-", "-ms-"];
|
||||||
upper = unprefixed[0].toUpperCase() + unprefixed.slice(1),
|
var upper = unprefixed[0].toUpperCase() + unprefixed.slice(1);
|
||||||
length = vendors.length;
|
var length = vendors.length;
|
||||||
|
|
||||||
if (typeof(document) === 'undefined' || typeof(document.body.style[unprefixed]) != 'undefined') {
|
if (typeof(document) === "undefined" || typeof(document.body.style[unprefixed]) != "undefined") {
|
||||||
return unprefixed;
|
return unprefixed;
|
||||||
}
|
}
|
||||||
|
|
||||||
for ( var i=0; i < length; i++ ) {
|
for ( var i=0; i < length; i++ ) {
|
||||||
if (typeof(document.body.style[vendors[i] + upper]) != 'undefined') {
|
if (typeof(document.body.style[vendors[i] + upper]) != "undefined") {
|
||||||
return vendors[i] + upper;
|
return vendors[i] + upper;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return unprefixed;
|
return unprefixed;
|
||||||
};
|
}
|
||||||
|
|
||||||
export function defaults(obj) {
|
export function defaults(obj) {
|
||||||
for (var i = 1, length = arguments.length; i < length; i++) {
|
for (var i = 1, length = arguments.length; i < length; i++) {
|
||||||
|
@ -73,7 +60,7 @@ export function defaults(obj) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return obj;
|
return obj;
|
||||||
};
|
}
|
||||||
|
|
||||||
export function extend(target) {
|
export function extend(target) {
|
||||||
var sources = [].slice.call(arguments, 1);
|
var sources = [].slice.call(arguments, 1);
|
||||||
|
@ -84,7 +71,7 @@ export function extend(target) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
return target;
|
return target;
|
||||||
};
|
}
|
||||||
|
|
||||||
// Fast quicksort insert for sorted array -- based on:
|
// Fast quicksort insert for sorted array -- based on:
|
||||||
// http://stackoverflow.com/questions/1344500/efficient-way-to-insert-a-number-into-a-sorted-array-of-numbers
|
// http://stackoverflow.com/questions/1344500/efficient-way-to-insert-a-number-into-a-sorted-array-of-numbers
|
||||||
|
@ -93,7 +80,8 @@ export function insert(item, array, compareFunction) {
|
||||||
array.splice(location, 0, item);
|
array.splice(location, 0, item);
|
||||||
|
|
||||||
return location;
|
return location;
|
||||||
};
|
}
|
||||||
|
|
||||||
// Returns where something would fit in
|
// Returns where something would fit in
|
||||||
export function locationOf(item, array, compareFunction, _start, _end) {
|
export function locationOf(item, array, compareFunction, _start, _end) {
|
||||||
var start = _start || 0;
|
var start = _start || 0;
|
||||||
|
@ -104,7 +92,7 @@ export function locationOf(item, array, compareFunction, _start, _end) {
|
||||||
compareFunction = function(a, b) {
|
compareFunction = function(a, b) {
|
||||||
if(a > b) return 1;
|
if(a > b) return 1;
|
||||||
if(a < b) return -1;
|
if(a < b) return -1;
|
||||||
if(a = b) return 0;
|
if(a == b) return 0;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
if(end-start <= 0) {
|
if(end-start <= 0) {
|
||||||
|
@ -124,7 +112,8 @@ export function locationOf(item, array, compareFunction, _start, _end) {
|
||||||
} else{
|
} else{
|
||||||
return locationOf(item, array, compareFunction, start, pivot);
|
return locationOf(item, array, compareFunction, start, pivot);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
// Returns -1 of mpt found
|
// Returns -1 of mpt found
|
||||||
export function indexOfSorted(item, array, compareFunction, _start, _end) {
|
export function indexOfSorted(item, array, compareFunction, _start, _end) {
|
||||||
var start = _start || 0;
|
var start = _start || 0;
|
||||||
|
@ -135,7 +124,7 @@ export function indexOfSorted(item, array, compareFunction, _start, _end) {
|
||||||
compareFunction = function(a, b) {
|
compareFunction = function(a, b) {
|
||||||
if(a > b) return 1;
|
if(a > b) return 1;
|
||||||
if(a < b) return -1;
|
if(a < b) return -1;
|
||||||
if(a = b) return 0;
|
if(a == b) return 0;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
if(end-start <= 0) {
|
if(end-start <= 0) {
|
||||||
|
@ -154,7 +143,7 @@ export function indexOfSorted(item, array, compareFunction, _start, _end) {
|
||||||
} else{
|
} else{
|
||||||
return indexOfSorted(item, array, compareFunction, start, pivot);
|
return indexOfSorted(item, array, compareFunction, start, pivot);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
export function bounds(el) {
|
export function bounds(el) {
|
||||||
|
|
||||||
|
@ -178,7 +167,7 @@ export function bounds(el) {
|
||||||
width: width
|
width: width
|
||||||
};
|
};
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
export function borders(el) {
|
export function borders(el) {
|
||||||
|
|
||||||
|
@ -202,7 +191,7 @@ export function borders(el) {
|
||||||
width: width
|
width: width
|
||||||
};
|
};
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
export function windowBounds() {
|
export function windowBounds() {
|
||||||
|
|
||||||
|
@ -218,23 +207,23 @@ export function windowBounds() {
|
||||||
height: height
|
height: height
|
||||||
};
|
};
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
//https://stackoverflow.com/questions/13482352/xquery-looking-for-text-with-single-quote/13483496#13483496
|
//-- https://stackoverflow.com/questions/13482352/xquery-looking-for-text-with-single-quote/13483496#13483496
|
||||||
export function cleanStringForXpath(str) {
|
export function cleanStringForXpath(str) {
|
||||||
var parts = str.match(/[^'"]+|['"]/g);
|
var parts = str.match(/[^'"]+|['"]/g);
|
||||||
parts = parts.map(function(part){
|
parts = parts.map(function(part){
|
||||||
if (part === "'") {
|
if (part === "'") {
|
||||||
return '\"\'\"'; // output "'"
|
return "\"\'\""; // output "'"
|
||||||
}
|
}
|
||||||
|
|
||||||
if (part === '"') {
|
if (part === "\"") {
|
||||||
return "\'\"\'"; // output '"'
|
return "\'\"\'"; // output '"'
|
||||||
}
|
}
|
||||||
return "\'" + part + "\'";
|
return `\'${part}\'`;
|
||||||
});
|
});
|
||||||
return "concat(\'\'," + parts.join(",") + ")";
|
return `concat(\'\',${ parts.join(",") })`;
|
||||||
};
|
}
|
||||||
|
|
||||||
export function indexOfTextNode(textNode){
|
export function indexOfTextNode(textNode){
|
||||||
var parent = textNode.parentNode;
|
var parent = textNode.parentNode;
|
||||||
|
@ -250,17 +239,15 @@ export function indexOfTextNode(textNode){
|
||||||
}
|
}
|
||||||
|
|
||||||
return index;
|
return index;
|
||||||
};
|
}
|
||||||
|
|
||||||
export function isXml(ext) {
|
export function isXml(ext) {
|
||||||
return ['xml', 'opf', 'ncx'].indexOf(ext) > -1;
|
return ["xml", "opf", "ncx"].indexOf(ext) > -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createBlob(content, mime){
|
export function createBlob(content, mime){
|
||||||
var blob = new Blob([content], {type : mime });
|
return new Blob([content], {type : mime });
|
||||||
|
}
|
||||||
return blob;
|
|
||||||
};
|
|
||||||
|
|
||||||
export function createBlobUrl(content, mime){
|
export function createBlobUrl(content, mime){
|
||||||
var _URL = window.URL || window.webkitURL || window.mozURL;
|
var _URL = window.URL || window.webkitURL || window.mozURL;
|
||||||
|
@ -270,10 +257,9 @@ export function createBlobUrl(content, mime){
|
||||||
tempUrl = _URL.createObjectURL(blob);
|
tempUrl = _URL.createObjectURL(blob);
|
||||||
|
|
||||||
return tempUrl;
|
return tempUrl;
|
||||||
};
|
}
|
||||||
|
|
||||||
export function createBase64Url(content, mime){
|
export function createBase64Url(content, mime){
|
||||||
var string;
|
|
||||||
var data;
|
var data;
|
||||||
var datauri;
|
var datauri;
|
||||||
|
|
||||||
|
@ -287,7 +273,7 @@ export function createBase64Url(content, mime){
|
||||||
datauri = "data:" + mime + ";base64," + data;
|
datauri = "data:" + mime + ";base64," + data;
|
||||||
|
|
||||||
return datauri;
|
return datauri;
|
||||||
};
|
}
|
||||||
|
|
||||||
export function type(obj){
|
export function type(obj){
|
||||||
return Object.prototype.toString.call(obj).slice(8, -1);
|
return Object.prototype.toString.call(obj).slice(8, -1);
|
||||||
|
@ -298,7 +284,7 @@ export function parse(markup, mime, forceXMLDom) {
|
||||||
var Parser;
|
var Parser;
|
||||||
|
|
||||||
if (typeof DOMParser === "undefined" || forceXMLDom) {
|
if (typeof DOMParser === "undefined" || forceXMLDom) {
|
||||||
Parser = require('xmldom').DOMParser;
|
Parser = require("xmldom").DOMParser;
|
||||||
} else {
|
} else {
|
||||||
Parser = DOMParser;
|
Parser = DOMParser;
|
||||||
}
|
}
|
||||||
|
@ -312,7 +298,7 @@ export function parse(markup, mime, forceXMLDom) {
|
||||||
export function qs(el, sel) {
|
export function qs(el, sel) {
|
||||||
var elements;
|
var elements;
|
||||||
if (!el) {
|
if (!el) {
|
||||||
throw new Error('No Element Provided');
|
throw new Error("No Element Provided");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof el.querySelector != "undefined") {
|
if (typeof el.querySelector != "undefined") {
|
||||||
|
@ -337,11 +323,11 @@ export function qsa(el, sel) {
|
||||||
export function qsp(el, sel, props) {
|
export function qsp(el, sel, props) {
|
||||||
var q, filtered;
|
var q, filtered;
|
||||||
if (typeof el.querySelector != "undefined") {
|
if (typeof el.querySelector != "undefined") {
|
||||||
sel += '[';
|
sel += "[";
|
||||||
for (var prop in props) {
|
for (var prop in props) {
|
||||||
sel += prop + "='" + props[prop] + "'";
|
sel += prop + "='" + props[prop] + "'";
|
||||||
}
|
}
|
||||||
sel += ']';
|
sel += "]";
|
||||||
return el.querySelector(sel);
|
return el.querySelector(sel);
|
||||||
} else {
|
} else {
|
||||||
q = el.getElementsByTagName(sel);
|
q = el.getElementsByTagName(sel);
|
||||||
|
@ -409,10 +395,15 @@ export function walk(node,callback){
|
||||||
if(callback(node)){
|
if(callback(node)){
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if(node = node.firstChild){
|
node = node.firstChild;
|
||||||
|
if(node){
|
||||||
do{
|
do{
|
||||||
if(walk(node,callback)){return true}
|
let walked = walk(node,callback);
|
||||||
}while(node=node.nextSibling)
|
if(walked){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
node = node.nextSibling;
|
||||||
|
} while(node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -421,7 +412,7 @@ export function blob2base64(blob, cb) {
|
||||||
reader.readAsDataURL(blob);
|
reader.readAsDataURL(blob);
|
||||||
reader.onloadend = function() {
|
reader.onloadend = function() {
|
||||||
cb(reader.result);
|
cb(reader.result);
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// From: https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm/Deferred#backwards_forwards_compatible
|
// From: https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm/Deferred#backwards_forwards_compatible
|
||||||
|
@ -459,7 +450,7 @@ export function defer() {
|
||||||
export function querySelectorByType(html, element, type){
|
export function querySelectorByType(html, element, type){
|
||||||
var query;
|
var query;
|
||||||
if (typeof html.querySelector != "undefined") {
|
if (typeof html.querySelector != "undefined") {
|
||||||
query = html.querySelector(element+'[*|type="'+type+'"]');
|
query = html.querySelector(`${element}[*|type="${type}"]`);
|
||||||
}
|
}
|
||||||
// Handle IE not supporting namespaced epub:type in querySelector
|
// Handle IE not supporting namespaced epub:type in querySelector
|
||||||
if(!query || query.length === 0) {
|
if(!query || query.length === 0) {
|
||||||
|
@ -482,6 +473,6 @@ export function findChildren(el) {
|
||||||
if (node.nodeType === 1) {
|
if (node.nodeType === 1) {
|
||||||
result.push(node);
|
result.push(node);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ class Hook {
|
||||||
constructor(context){
|
constructor(context){
|
||||||
this.context = context || this;
|
this.context = context || this;
|
||||||
this.hooks = [];
|
this.hooks = [];
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a function to be run before a hook completes
|
* Adds a function to be run before a hook completes
|
||||||
|
@ -26,7 +26,7 @@ class Hook {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Triggers a hook to run all functions
|
* Triggers a hook to run all functions
|
||||||
|
@ -37,7 +37,7 @@ class Hook {
|
||||||
var context = this.context;
|
var context = this.context;
|
||||||
var promises = [];
|
var promises = [];
|
||||||
|
|
||||||
this.hooks.forEach(function(task, i) {
|
this.hooks.forEach(function(task) {
|
||||||
var executing = task.apply(context, args);
|
var executing = task.apply(context, args);
|
||||||
|
|
||||||
if(executing && typeof executing["then"] === "function") {
|
if(executing && typeof executing["then"] === "function") {
|
||||||
|
@ -49,15 +49,15 @@ class Hook {
|
||||||
|
|
||||||
|
|
||||||
return Promise.all(promises);
|
return Promise.all(promises);
|
||||||
};
|
}
|
||||||
|
|
||||||
// Adds a function to be run before a hook completes
|
// Adds a function to be run before a hook completes
|
||||||
list(){
|
list(){
|
||||||
return this.hooks;
|
return this.hooks;
|
||||||
};
|
}
|
||||||
|
|
||||||
clear(){
|
clear(){
|
||||||
return this.hooks = [];
|
return this.hooks = [];
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
export default Hook;
|
export default Hook;
|
|
@ -1,4 +1,4 @@
|
||||||
import {defer, requestAnimationFrame} from './utils/core';
|
import {defer, requestAnimationFrame} from "./core";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Queue for handling tasks one at a time
|
* Queue for handling tasks one at a time
|
||||||
|
@ -12,7 +12,7 @@ class Queue {
|
||||||
this.tick = requestAnimationFrame;
|
this.tick = requestAnimationFrame;
|
||||||
this.running = false;
|
this.running = false;
|
||||||
this.paused = false;
|
this.paused = false;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add an item to the queue
|
* Add an item to the queue
|
||||||
|
@ -29,7 +29,7 @@ class Queue {
|
||||||
// args = [args];
|
// args = [args];
|
||||||
// }
|
// }
|
||||||
if(!task) {
|
if(!task) {
|
||||||
return console.error("No Task Provided");
|
throw new Error("No Task Provided");
|
||||||
}
|
}
|
||||||
|
|
||||||
if(typeof task === "function"){
|
if(typeof task === "function"){
|
||||||
|
@ -63,7 +63,7 @@ class Queue {
|
||||||
}
|
}
|
||||||
|
|
||||||
return queued.promise;
|
return queued.promise;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run one item
|
* Run one item
|
||||||
|
@ -84,7 +84,7 @@ class Queue {
|
||||||
// Task is a function that returns a promise
|
// Task is a function that returns a promise
|
||||||
return result.then(function(){
|
return result.then(function(){
|
||||||
inwait.deferred.resolve.apply(this.context, arguments);
|
inwait.deferred.resolve.apply(this.context, arguments);
|
||||||
}.bind(this), function(reason) {
|
}.bind(this), function() {
|
||||||
inwait.deferred.reject.apply(this.context, arguments);
|
inwait.deferred.reject.apply(this.context, arguments);
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
} else {
|
} else {
|
||||||
|
@ -106,14 +106,14 @@ class Queue {
|
||||||
return inwait.promise;
|
return inwait.promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
// Run All Immediately
|
// Run All Immediately
|
||||||
dump(){
|
dump(){
|
||||||
while(this._q.length) {
|
while(this._q.length) {
|
||||||
this.dequeue();
|
this.dequeue();
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run all tasks sequentially, at convince
|
* Run all tasks sequentially, at convince
|
||||||
|
@ -126,7 +126,7 @@ class Queue {
|
||||||
this.defered = new defer();
|
this.defered = new defer();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.tick.call(window, function() {
|
this.tick.call(window, () => {
|
||||||
|
|
||||||
if(this._q.length) {
|
if(this._q.length) {
|
||||||
|
|
||||||
|
@ -140,7 +140,7 @@ class Queue {
|
||||||
this.running = undefined;
|
this.running = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
}.bind(this));
|
});
|
||||||
|
|
||||||
// Unpause
|
// Unpause
|
||||||
if(this.paused == true) {
|
if(this.paused == true) {
|
||||||
|
@ -148,7 +148,7 @@ class Queue {
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.defered.promise;
|
return this.defered.promise;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Flush all, as quickly as possible
|
* Flush all, as quickly as possible
|
||||||
|
@ -170,7 +170,7 @@ class Queue {
|
||||||
return this.running;
|
return this.running;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clear all items in wait
|
* Clear all items in wait
|
||||||
|
@ -178,7 +178,7 @@ class Queue {
|
||||||
clear(){
|
clear(){
|
||||||
this._q = [];
|
this._q = [];
|
||||||
this.running = false;
|
this.running = false;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the number of tasks in the queue
|
* Get the number of tasks in the queue
|
||||||
|
@ -186,14 +186,14 @@ class Queue {
|
||||||
*/
|
*/
|
||||||
length(){
|
length(){
|
||||||
return this._q.length;
|
return this._q.length;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pause a running queue
|
* Pause a running queue
|
||||||
*/
|
*/
|
||||||
pause(){
|
pause(){
|
||||||
this.paused = true;
|
this.paused = true;
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -212,22 +212,27 @@ class Task {
|
||||||
return function(){
|
return function(){
|
||||||
var toApply = arguments || [];
|
var toApply = arguments || [];
|
||||||
|
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise( (resolve, reject) => {
|
||||||
var callback = function(value){
|
var callback = function(value, err){
|
||||||
|
if (!value && err) {
|
||||||
|
reject(err);
|
||||||
|
} else {
|
||||||
resolve(value);
|
resolve(value);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
// Add the callback to the arguments list
|
// Add the callback to the arguments list
|
||||||
toApply.push(callback);
|
toApply.push(callback);
|
||||||
|
|
||||||
// Apply all arguments to the functions
|
// Apply all arguments to the functions
|
||||||
task.apply(this, toApply);
|
task.apply(context || this, toApply);
|
||||||
|
|
||||||
}.bind(this));
|
});
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export default Queue;
|
export default Queue;
|
||||||
|
export { Task };
|
|
@ -1,6 +1,5 @@
|
||||||
// import URI from 'urijs';
|
import { qs } from "./core";
|
||||||
import { qs } from './utils/core';
|
import Url from "./url";
|
||||||
import Url from './utils/url';
|
|
||||||
|
|
||||||
export function replaceBase(doc, section){
|
export function replaceBase(doc, section){
|
||||||
var base;
|
var base;
|
||||||
|
@ -96,12 +95,12 @@ export function replaceLinks(view, renderer) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
export function substitute(content, urls, replacements) {
|
export function substitute(content, urls, replacements) {
|
||||||
urls.forEach(function(url, i){
|
urls.forEach(function(url, i){
|
||||||
if (url && replacements[i]) {
|
if (url && replacements[i]) {
|
||||||
content = content.replace(new RegExp(url, 'g'), replacements[i]);
|
content = content.replace(new RegExp(url, "g"), replacements[i]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return content;
|
return content;
|
|
@ -1,10 +1,9 @@
|
||||||
import {defer, isXml, parse} from './utils/core';
|
import {defer, isXml, parse} from "./core";
|
||||||
import Path from './utils/path';
|
import Path from "./path";
|
||||||
|
|
||||||
function request(url, type, withCredentials, headers) {
|
function request(url, type, withCredentials, headers) {
|
||||||
var supportsURL = (typeof window != "undefined") ? window.URL : false; // TODO: fallback for url if window isn't defined
|
var supportsURL = (typeof window != "undefined") ? window.URL : false; // TODO: fallback for url if window isn't defined
|
||||||
var BLOB_RESPONSE = supportsURL ? "blob" : "arraybuffer";
|
var BLOB_RESPONSE = supportsURL ? "blob" : "arraybuffer";
|
||||||
var uri;
|
|
||||||
|
|
||||||
var deferred = new defer();
|
var deferred = new defer();
|
||||||
|
|
||||||
|
@ -16,12 +15,13 @@ function request(url, type, withCredentials, headers) {
|
||||||
|
|
||||||
var header;
|
var header;
|
||||||
|
|
||||||
if (!('overrideMimeType' in xhrPrototype)) {
|
if (!("overrideMimeType" in xhrPrototype)) {
|
||||||
// IE10 might have response, but not overrideMimeType
|
// IE10 might have response, but not overrideMimeType
|
||||||
Object.defineProperty(xhrPrototype, 'overrideMimeType', {
|
Object.defineProperty(xhrPrototype, "overrideMimeType", {
|
||||||
value: function xmlHttpRequestOverrideMimeType(mimeType) {}
|
value: function xmlHttpRequestOverrideMimeType() {}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if(withCredentials) {
|
if(withCredentials) {
|
||||||
xhr.withCredentials = true;
|
xhr.withCredentials = true;
|
||||||
}
|
}
|
||||||
|
@ -39,26 +39,26 @@ function request(url, type, withCredentials, headers) {
|
||||||
xhr.setRequestHeader("Accept", "application/json");
|
xhr.setRequestHeader("Accept", "application/json");
|
||||||
}
|
}
|
||||||
|
|
||||||
// If type isn't set, determine it from the file extension
|
// If type isn"t set, determine it from the file extension
|
||||||
if(!type) {
|
if(!type) {
|
||||||
type = new Path(url).extension;
|
type = new Path(url).extension;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(type == 'blob'){
|
if(type == "blob"){
|
||||||
xhr.responseType = BLOB_RESPONSE;
|
xhr.responseType = BLOB_RESPONSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if(isXml(type)) {
|
if(isXml(type)) {
|
||||||
// xhr.responseType = "document";
|
// xhr.responseType = "document";
|
||||||
xhr.overrideMimeType('text/xml'); // for OPF parsing
|
xhr.overrideMimeType("text/xml"); // for OPF parsing
|
||||||
}
|
}
|
||||||
|
|
||||||
if(type == 'xhtml') {
|
if(type == "xhtml") {
|
||||||
// xhr.responseType = "document";
|
// xhr.responseType = "document";
|
||||||
}
|
}
|
||||||
|
|
||||||
if(type == 'html' || type == 'htm') {
|
if(type == "html" || type == "htm") {
|
||||||
// xhr.responseType = "document";
|
// xhr.responseType = "document";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,7 +69,6 @@ function request(url, type, withCredentials, headers) {
|
||||||
xhr.send();
|
xhr.send();
|
||||||
|
|
||||||
function err(e) {
|
function err(e) {
|
||||||
console.error(e);
|
|
||||||
deferred.reject(e);
|
deferred.reject(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,7 +76,7 @@ function request(url, type, withCredentials, headers) {
|
||||||
if (this.readyState === XMLHttpRequest.DONE) {
|
if (this.readyState === XMLHttpRequest.DONE) {
|
||||||
var responseXML = false;
|
var responseXML = false;
|
||||||
|
|
||||||
if(this.responseType === '' || this.responseType === "document") {
|
if(this.responseType === "" || this.responseType === "document") {
|
||||||
responseXML = this.responseXML;
|
responseXML = this.responseXML;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,20 +106,20 @@ function request(url, type, withCredentials, headers) {
|
||||||
r = this.responseXML;
|
r = this.responseXML;
|
||||||
} else
|
} else
|
||||||
if(isXml(type)){
|
if(isXml(type)){
|
||||||
// xhr.overrideMimeType('text/xml'); // for OPF parsing
|
// xhr.overrideMimeType("text/xml"); // for OPF parsing
|
||||||
// If this.responseXML wasn't set, try to parse using a DOMParser from text
|
// If this.responseXML wasn't set, try to parse using a DOMParser from text
|
||||||
r = parse(this.response, "text/xml");
|
r = parse(this.response, "text/xml");
|
||||||
}else
|
}else
|
||||||
if(type == 'xhtml'){
|
if(type == "xhtml"){
|
||||||
r = parse(this.response, "application/xhtml+xml");
|
r = parse(this.response, "application/xhtml+xml");
|
||||||
}else
|
}else
|
||||||
if(type == 'html' || type == 'htm'){
|
if(type == "html" || type == "htm"){
|
||||||
r = parse(this.response, "text/html");
|
r = parse(this.response, "text/html");
|
||||||
}else
|
}else
|
||||||
if(type == 'json'){
|
if(type == "json"){
|
||||||
r = JSON.parse(this.response);
|
r = JSON.parse(this.response);
|
||||||
}else
|
}else
|
||||||
if(type == 'blob'){
|
if(type == "blob"){
|
||||||
|
|
||||||
if(supportsURL) {
|
if(supportsURL) {
|
||||||
r = this.response;
|
r = this.response;
|
||||||
|
@ -147,6 +146,6 @@ function request(url, type, withCredentials, headers) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return deferred.promise;
|
return deferred.promise;
|
||||||
};
|
}
|
||||||
|
|
||||||
export default request;
|
export default request;
|
|
@ -1,4 +1,4 @@
|
||||||
import Path from "./path"
|
import Path from "./path";
|
||||||
import path from "path-webpack";
|
import path from "path-webpack";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue