Update version of monocle in tree to 2.0.0

This commit is contained in:
Kovid Goyal 2011-06-15 12:21:39 -06:00
parent bfcbaa645c
commit 935bbe502d

View File

@ -1,5 +1,5 @@
Monocle = { Monocle = {
VERSION: "1.0.0" VERSION: "2.0.0"
}; };
@ -170,7 +170,8 @@ Monocle.Browser.has.iframeTouchBug = Monocle.Browser.iOSVersionBelow("4.2");
Monocle.Browser.has.selectThruBug = Monocle.Browser.iOSVersionBelow("4.2"); Monocle.Browser.has.selectThruBug = Monocle.Browser.iOSVersionBelow("4.2");
Monocle.Browser.has.mustScrollSheaf = Monocle.Browser.is.MobileSafari; Monocle.Browser.has.mustScrollSheaf = Monocle.Browser.is.MobileSafari;
Monocle.Browser.has.iframeDoubleWidthBug = Monocle.Browser.has.mustScrollSheaf; Monocle.Browser.has.iframeDoubleWidthBug =
Monocle.Browser.has.mustScrollSheaf || Monocle.Browser.on.Kindle3;
Monocle.Browser.has.floatColumnBug = Monocle.Browser.is.WebKit; Monocle.Browser.has.floatColumnBug = Monocle.Browser.is.WebKit;
@ -181,6 +182,11 @@ Monocle.Browser.has.jumpFlickerBug =
Monocle.Browser.on.MacOSX && Monocle.Browser.is.WebKit; Monocle.Browser.on.MacOSX && Monocle.Browser.is.WebKit;
Monocle.Browser.has.columnOverflowPaintBug = Monocle.Browser.is.WebKit &&
!Monocle.Browser.is.MobileSafari &&
navigator.userAgent.indexOf("AppleWebKit/534") > 0;
if (typeof window.console == "undefined") { if (typeof window.console == "undefined") {
window.console = { window.console = {
messages: [], messages: [],
@ -241,6 +247,7 @@ Monocle.Factory = function (element, label, index, reader) {
function initialize() { function initialize() {
if (!p.label) { return; }
var node = p.reader.properties.graph; var node = p.reader.properties.graph;
node[p.label] = node[p.label] || []; node[p.label] = node[p.label] || [];
if (typeof p.index == 'undefined' && node[p.label][p.index]) { if (typeof p.index == 'undefined' && node[p.label][p.index]) {
@ -274,7 +281,11 @@ Monocle.Factory = function (element, label, index, reader) {
function make(tagName, oLabel, index_or_options, or_options) { function make(tagName, oLabel, index_or_options, or_options) {
var oIndex, options; var oIndex, options;
if (arguments.length == 2) { if (arguments.length == 1) {
oLabel = null,
oIndex = 0;
options = {};
} else if (arguments.length == 2) {
oIndex = 0; oIndex = 0;
options = {}; options = {};
} else if (arguments.length == 4) { } else if (arguments.length == 4) {
@ -376,6 +387,22 @@ Monocle.pieceLoaded('factory');
Monocle.Events = {} Monocle.Events = {}
Monocle.Events.dispatch = function (elem, evtType, data, cancelable) {
if (!document.createEvent) {
return true;
}
var evt = document.createEvent("Events");
evt.initEvent(evtType, false, cancelable || false);
evt.m = data;
try {
return elem.dispatchEvent(evt);
} catch(e) {
console.warn("Failed to dispatch event: "+evtType);
return false;
}
}
Monocle.Events.listen = function (elem, evtType, fn, useCapture) { Monocle.Events.listen = function (elem, evtType, fn, useCapture) {
if (elem.addEventListener) { if (elem.addEventListener) {
return elem.addEventListener(evtType, fn, useCapture || false); return elem.addEventListener(evtType, fn, useCapture || false);
@ -405,7 +432,7 @@ Monocle.Events.listenForContact = function (elem, fns, options) {
pageY: ci.pageY pageY: ci.pageY
}; };
var target = evt.target || window.srcElement; var target = evt.target || evt.srcElement;
while (target.nodeType != 1 && target.parentNode) { while (target.nodeType != 1 && target.parentNode) {
target = target.parentNode; target = target.parentNode;
} }
@ -527,13 +554,18 @@ Monocle.Events.deafenForContact = function (elem, listeners) {
} }
Monocle.Events.listenForTap = function (elem, fn) { Monocle.Events.listenForTap = function (elem, fn, activeClass) {
var startPos; var startPos;
if (Monocle.Browser.on.Kindle3) { if (Monocle.Browser.on.Kindle3) {
Monocle.Events.listen(elem, 'click', function () {}); Monocle.Events.listen(elem, 'click', function () {});
} }
var annul = function () {
startPos = null;
if (activeClass && elem.dom) { elem.dom.removeClass(activeClass); }
}
var annulIfOutOfBounds = function (evt) { var annulIfOutOfBounds = function (evt) {
if (evt.type.match(/^mouse/)) { if (evt.type.match(/^mouse/)) {
return; return;
@ -545,7 +577,7 @@ Monocle.Events.listenForTap = function (elem, fn) {
evt.m.registrantX < 0 || evt.m.registrantX > elem.offsetWidth || evt.m.registrantX < 0 || evt.m.registrantX > elem.offsetWidth ||
evt.m.registrantY < 0 || evt.m.registrantY > elem.offsetHeight evt.m.registrantY < 0 || evt.m.registrantY > elem.offsetHeight
) { ) {
startPos = null; annul();
} else { } else {
evt.preventDefault(); evt.preventDefault();
} }
@ -557,6 +589,7 @@ Monocle.Events.listenForTap = function (elem, fn) {
start: function (evt) { start: function (evt) {
startPos = [evt.m.pageX, evt.m.pageY]; startPos = [evt.m.pageX, evt.m.pageY];
evt.preventDefault(); evt.preventDefault();
if (activeClass && elem.dom) { elem.dom.addClass(activeClass); }
}, },
move: annulIfOutOfBounds, move: annulIfOutOfBounds,
end: function (evt) { end: function (evt) {
@ -565,10 +598,9 @@ Monocle.Events.listenForTap = function (elem, fn) {
evt.m.startOffset = startPos; evt.m.startOffset = startPos;
fn(evt); fn(evt);
} }
annul();
}, },
cancel: function (evt) { cancel: annul
startPos = null;
}
}, },
{ {
useCapture: false useCapture: false
@ -997,6 +1029,9 @@ Monocle.Reader = function (node, bookData, options, onLoadCallback) {
createReaderElements(); createReaderElements();
p.defaultStyles = addPageStyles(k.DEFAULT_STYLE_RULES, false); p.defaultStyles = addPageStyles(k.DEFAULT_STYLE_RULES, false);
if (options.stylesheet) {
p.initialStyles = addPageStyles(options.stylesheet, false);
}
primeFrames(options.primeURL, function () { primeFrames(options.primeURL, function () {
applyStyles(); applyStyles();
@ -1077,6 +1112,7 @@ Monocle.Reader = function (node, bookData, options, onLoadCallback) {
if (Monocle.Browser.is.WebKit) { if (Monocle.Browser.is.WebKit) {
frame.contentDocument.documentElement.style.overflow = "hidden"; frame.contentDocument.documentElement.style.overflow = "hidden";
} }
dispatchEvent('monocle:frameprimed', { frame: frame, pageIndex: pageCount });
if ((pageCount += 1) == pageMax) { if ((pageCount += 1) == pageMax) {
Monocle.defer(callback); Monocle.defer(callback);
} }
@ -1131,6 +1167,7 @@ Monocle.Reader = function (node, bookData, options, onLoadCallback) {
var pageCount = 0; var pageCount = 0;
if (typeof callback == 'function') { if (typeof callback == 'function') {
var watcher = function (evt) { var watcher = function (evt) {
dispatchEvent('monocle:firstcomponentchange', evt.m);
if ((pageCount += 1) == p.flipper.pageCount) { if ((pageCount += 1) == p.flipper.pageCount) {
deafen('monocle:componentchange', watcher); deafen('monocle:componentchange', watcher);
callback(); callback();
@ -1239,7 +1276,7 @@ Monocle.Reader = function (node, bookData, options, onLoadCallback) {
page.appendChild(runner); page.appendChild(runner);
ctrlData.elements.push(runner); ctrlData.elements.push(runner);
} }
} else if (cType == "modal" || cType == "popover") { } else if (cType == "modal" || cType == "popover" || cType == "hud") {
ctrlElem = ctrl.createControlElements(overlay); ctrlElem = ctrl.createControlElements(overlay);
overlay.appendChild(ctrlElem); overlay.appendChild(ctrlElem);
ctrlData.elements.push(ctrlElem); ctrlData.elements.push(ctrlElem);
@ -1312,24 +1349,33 @@ Monocle.Reader = function (node, bookData, options, onLoadCallback) {
var controlData = dataForControl(ctrl); var controlData = dataForControl(ctrl);
if (!controlData) { if (!controlData) {
console.warn("No data for control: " + ctrl); console.warn("No data for control: " + ctrl);
return; return false;
} }
if (controlData.hidden == false) {
return; if (showingControl(ctrl)) {
return false;
} }
var overlay = dom.find('overlay');
if (controlData.usesOverlay && controlData.controlType != "hud") {
for (var i = 0, ii = p.controls.length; i < ii; ++i) {
if (p.controls[i].usesOverlay && !p.controls[i].hidden) {
return false;
}
}
overlay.style.display = "block";
}
for (var i = 0; i < controlData.elements.length; ++i) { for (var i = 0; i < controlData.elements.length; ++i) {
controlData.elements[i].style.display = "block"; controlData.elements[i].style.display = "block";
} }
var overlay = dom.find('overlay');
if (controlData.usesOverlay) {
overlay.style.display = "block";
}
if (controlData.controlType == "popover") { if (controlData.controlType == "popover") {
overlay.listeners = Monocle.Events.listenForContact( overlay.listeners = Monocle.Events.listenForContact(
overlay, overlay,
{ {
start: function (evt) { start: function (evt) {
obj = evt.target || window.event.srcElement; var obj = evt.target || window.event.srcElement;
do { do {
if (obj == controlData.elements[0]) { return true; } if (obj == controlData.elements[0]) { return true; }
} while (obj && (obj = obj.parentNode)); } while (obj && (obj = obj.parentNode));
@ -1346,22 +1392,18 @@ Monocle.Reader = function (node, bookData, options, onLoadCallback) {
ctrl.properties.hidden = false; ctrl.properties.hidden = false;
} }
dispatchEvent('controlshow', ctrl, false); dispatchEvent('controlshow', ctrl, false);
return true;
}
function showingControl(ctrl) {
var controlData = dataForControl(ctrl);
return controlData.hidden == false;
} }
function dispatchEvent(evtType, data, cancelable) { function dispatchEvent(evtType, data, cancelable) {
if (!document.createEvent) { return Monocle.Events.dispatch(dom.find('box'), evtType, data, cancelable);
return true;
}
var evt = document.createEvent("Events");
evt.initEvent(evtType, false, cancelable || false);
evt.m = data;
try {
return dom.find('box').dispatchEvent(evt);
} catch(e) {
console.warn("Failed to dispatch event: " + evtType);
return false;
}
} }
@ -1502,6 +1544,7 @@ Monocle.Reader = function (node, bookData, options, onLoadCallback) {
API.addControl = addControl; API.addControl = addControl;
API.hideControl = hideControl; API.hideControl = hideControl;
API.showControl = showControl; API.showControl = showControl;
API.showingControl = showingControl;
API.dispatchEvent = dispatchEvent; API.dispatchEvent = dispatchEvent;
API.listen = listen; API.listen = listen;
API.deafen = deafen; API.deafen = deafen;
@ -1527,22 +1570,32 @@ Monocle.Reader.DEFAULT_CLASS_PREFIX = 'monelem_'
Monocle.Reader.FLIPPER_DEFAULT_CLASS = "Slider"; Monocle.Reader.FLIPPER_DEFAULT_CLASS = "Slider";
Monocle.Reader.FLIPPER_LEGACY_CLASS = "Legacy"; Monocle.Reader.FLIPPER_LEGACY_CLASS = "Legacy";
Monocle.Reader.DEFAULT_STYLE_RULES = [ Monocle.Reader.DEFAULT_STYLE_RULES = [
"html * {" + "html#RS\\:monocle * {" +
"-webkit-font-smoothing: subpixel-antialiased;" +
"text-rendering: auto !important;" + "text-rendering: auto !important;" +
"word-wrap: break-word !important;" + "word-wrap: break-word !important;" +
"overflow: visible !important;" +
(Monocle.Browser.has.floatColumnBug ? "float: none !important;" : "") + (Monocle.Browser.has.floatColumnBug ? "float: none !important;" : "") +
"}" + "}",
"body {" + "html#RS\\:monocle body {" +
"margin: 0 !important;" + "margin: 0 !important;" +
"padding: 0 !important;" + "padding: 0 !important;" +
"-webkit-text-size-adjust: none;" + "-webkit-text-size-adjust: none;" +
"}" + "}",
"table, img {" + "html#RS\\:monocle body * {" +
"max-width: 100% !important;" + "max-width: 100% !important;" +
"max-height: 90% !important;" + "}",
"html#RS\\:monocle img, html#RS\\:monocle video, html#RS\\:monocle object {" +
"max-height: 95% !important;" +
"}" "}"
] ]
if (Monocle.Browser.has.columnOverflowPaintBug) {
Monocle.Reader.DEFAULT_STYLE_RULES.push(
"::-webkit-scrollbar { width: 0; height: 0; }"
)
}
Monocle.pieceLoaded('reader'); Monocle.pieceLoaded('reader');
/* BOOK */ /* BOOK */
@ -1586,6 +1639,16 @@ Monocle.Book = function (dataSource) {
locus.load = true; locus.load = true;
locus.componentId = p.componentIds[0]; locus.componentId = p.componentIds[0];
return locus; return locus;
} else if (
cIndex < 0 &&
locus.componentId &&
currComponent.properties.id != locus.componentId
) {
pageDiv.m.reader.dispatchEvent(
"monocle:notfound",
{ href: locus.componentId }
);
return null;
} else if (cIndex < 0) { } else if (cIndex < 0) {
component = currComponent; component = currComponent;
locus.componentId = pageDiv.m.activeFrame.m.component.properties.id; locus.componentId = pageDiv.m.activeFrame.m.component.properties.id;
@ -1619,6 +1682,8 @@ Monocle.Book = function (dataSource) {
result.page += locus.direction; result.page += locus.direction;
} else if (typeof(locus.anchor) == "string") { } else if (typeof(locus.anchor) == "string") {
result.page = component.pageForChapter(locus.anchor, pageDiv); result.page = component.pageForChapter(locus.anchor, pageDiv);
} else if (typeof(locus.xpath) == "string") {
result.page = component.pageForXPath(locus.xpath, pageDiv);
} else if (typeof(locus.position) == "string") { } else if (typeof(locus.position) == "string") {
if (locus.position == "start") { if (locus.position == "start") {
result.page = 1; result.page = 1;
@ -1638,6 +1703,7 @@ Monocle.Book = function (dataSource) {
if (result.page < 1) { if (result.page < 1) {
if (cIndex == 0) { if (cIndex == 0) {
result.page = 1; result.page = 1;
result.boundarystart = true;
} else { } else {
result.load = true; result.load = true;
result.componentId = p.componentIds[cIndex - 1]; result.componentId = p.componentIds[cIndex - 1];
@ -1647,6 +1713,7 @@ Monocle.Book = function (dataSource) {
} else if (result.page > lastPageNum['new']) { } else if (result.page > lastPageNum['new']) {
if (cIndex == p.lastCIndex) { if (cIndex == p.lastCIndex) {
result.page = lastPageNum['new']; result.page = lastPageNum['new'];
result.boundaryend = true;
} else { } else {
result.load = true; result.load = true;
result.componentId = p.componentIds[cIndex + 1]; result.componentId = p.componentIds[cIndex + 1];
@ -1660,7 +1727,13 @@ Monocle.Book = function (dataSource) {
function setPageAt(pageDiv, locus) { function setPageAt(pageDiv, locus) {
locus = pageNumberAt(pageDiv, locus); locus = pageNumberAt(pageDiv, locus);
if (!locus.load) { if (locus && !locus.load) {
var evtData = { locus: locus, page: pageDiv }
if (locus.boundarystart) {
pageDiv.m.reader.dispatchEvent('monocle:boundarystart', evtData);
} else if (locus.boundaryend) {
pageDiv.m.reader.dispatchEvent('monocle:boundaryend', evtData);
} else {
var component = p.components[p.componentIds.indexOf(locus.componentId)]; var component = p.components[p.componentIds.indexOf(locus.componentId)];
pageDiv.m.place = pageDiv.m.place || new Monocle.Place(); pageDiv.m.place = pageDiv.m.place || new Monocle.Place();
pageDiv.m.place.setPlace(component, locus.page); pageDiv.m.place.setPlace(component, locus.page);
@ -1673,6 +1746,7 @@ Monocle.Book = function (dataSource) {
} }
pageDiv.m.reader.dispatchEvent("monocle:pagechange", evtData); pageDiv.m.reader.dispatchEvent("monocle:pagechange", evtData);
} }
}
return locus; return locus;
} }
@ -1683,6 +1757,10 @@ Monocle.Book = function (dataSource) {
locus = pageNumberAt(pageDiv, locus); locus = pageNumberAt(pageDiv, locus);
} }
if (!locus) {
return;
}
if (!locus.load) { if (!locus.load) {
callback(locus); callback(locus);
return; return;
@ -1690,7 +1768,9 @@ Monocle.Book = function (dataSource) {
var findPageNumber = function () { var findPageNumber = function () {
locus = setPageAt(pageDiv, locus); locus = setPageAt(pageDiv, locus);
if (locus.load) { if (!locus) {
return;
} else if (locus.load) {
loadPageAt(pageDiv, locus, callback, progressCallback) loadPageAt(pageDiv, locus, callback, progressCallback)
} else { } else {
callback(locus); callback(locus);
@ -1715,10 +1795,12 @@ Monocle.Book = function (dataSource) {
} }
function setOrLoadPageAt(pageDiv, locus, callback, progressCallback) { function setOrLoadPageAt(pageDiv, locus, callback, onProgress, onFail) {
locus = setPageAt(pageDiv, locus); locus = setPageAt(pageDiv, locus);
if (locus.load) { if (!locus) {
loadPageAt(pageDiv, locus, callback, progressCallback); if (onFail) { onFail(); }
} else if (locus.load) {
loadPageAt(pageDiv, locus, callback, onProgress);
} else { } else {
callback(locus); callback(locus);
} }
@ -1864,13 +1946,18 @@ Monocle.Place = function () {
} }
function percentageThrough() { function percentAtTopOfPage() {
return p.percent - 1.0 / p.component.lastPageNumber();
}
function percentAtBottomOfPage() {
return p.percent; return p.percent;
} }
function pageAtPercentageThrough(pc) { function pageAtPercentageThrough(percent) {
return Math.max(Math.round(p.component.lastPageNumber() * pc), 1); return Math.max(Math.round(p.component.lastPageNumber() * percent), 1);
} }
@ -1911,6 +1998,8 @@ Monocle.Place = function () {
} }
if (options.direction) { if (options.direction) {
locus.page += options.direction; locus.page += options.direction;
} else {
locus.percent = percentAtBottomOfPage();
} }
return locus; return locus;
} }
@ -1942,7 +2031,9 @@ Monocle.Place = function () {
API.setPlace = setPlace; API.setPlace = setPlace;
API.setPercentageThrough = setPercentageThrough; API.setPercentageThrough = setPercentageThrough;
API.componentId = componentId; API.componentId = componentId;
API.percentageThrough = percentageThrough; API.percentAtTopOfPage = percentAtTopOfPage;
API.percentAtBottomOfPage = percentAtBottomOfPage;
API.percentageThrough = percentAtBottomOfPage;
API.pageAtPercentageThrough = pageAtPercentageThrough; API.pageAtPercentageThrough = pageAtPercentageThrough;
API.pageNumber = pageNumber; API.pageNumber = pageNumber;
API.chapterInfo = chapterInfo; API.chapterInfo = chapterInfo;
@ -2158,11 +2249,13 @@ Monocle.Component = function (book, id, index, chapters, source) {
if (p.chapters[0] && typeof p.chapters[0].percent == "number") { if (p.chapters[0] && typeof p.chapters[0].percent == "number") {
return; return;
} }
var doc = pageDiv.m.activeFrame.contentDocument;
for (var i = 0; i < p.chapters.length; ++i) { for (var i = 0; i < p.chapters.length; ++i) {
var chp = p.chapters[i]; var chp = p.chapters[i];
chp.percent = 0; chp.percent = 0;
if (chp.fragment) { if (chp.fragment) {
chp.percent = pageDiv.m.dimensions.percentageThroughOfId(chp.fragment); var node = doc.getElementById(chp.fragment);
chp.percent = pageDiv.m.dimensions.percentageThroughOfNode(node);
} }
} }
return p.chapters; return p.chapters;
@ -2187,14 +2280,37 @@ Monocle.Component = function (book, id, index, chapters, source) {
if (!fragment) { if (!fragment) {
return 1; return 1;
} }
var pc2pn = function (pc) { return Math.floor(pc * p.pageLength) + 1 }
for (var i = 0; i < p.chapters.length; ++i) { for (var i = 0; i < p.chapters.length; ++i) {
if (p.chapters[i].fragment == fragment) { if (p.chapters[i].fragment == fragment) {
return pc2pn(p.chapters[i].percent); return percentToPageNumber(p.chapters[i].percent);
} }
} }
var percent = pageDiv.m.dimensions.percentageThroughOfId(fragment); var doc = pageDiv.m.activeFrame.contentDocument;
return pc2pn(percent); var node = doc.getElementById(fragment);
var percent = pageDiv.m.dimensions.percentageThroughOfNode(node);
return percentToPageNumber(percent);
}
function pageForXPath(xpath, pageDiv) {
var doc = pageDiv.m.activeFrame.contentDocument;
var percent = 0;
if (typeof doc.evaluate == "function") {
var node = doc.evaluate(
xpath,
doc,
null,
9,
null
).singleNodeValue;
var percent = pageDiv.m.dimensions.percentageThroughOfNode(node);
}
return percentToPageNumber(percent);
}
function percentToPageNumber(pc) {
return Math.floor(pc * p.pageLength) + 1;
} }
@ -2207,6 +2323,7 @@ Monocle.Component = function (book, id, index, chapters, source) {
API.updateDimensions = updateDimensions; API.updateDimensions = updateDimensions;
API.chapterForPage = chapterForPage; API.chapterForPage = chapterForPage;
API.pageForChapter = pageForChapter; API.pageForChapter = pageForChapter;
API.pageForXPath = pageForXPath;
API.lastPageNumber = lastPageNumber; API.lastPageNumber = lastPageNumber;
return API; return API;
@ -2415,9 +2532,11 @@ Monocle.Dimensions.Vert = function (pageDiv) {
} }
function percentageThroughOfId(id) { function percentageThroughOfNode(target) {
if (!target) {
return 0;
}
var doc = p.page.m.activeFrame.contentDocument; var doc = p.page.m.activeFrame.contentDocument;
var target = doc.getElementById(id);
var offset = 0; var offset = 0;
if (target.getBoundingClientRect) { if (target.getBoundingClientRect) {
offset = target.getBoundingClientRect().top; offset = target.getBoundingClientRect().top;
@ -2456,7 +2575,7 @@ Monocle.Dimensions.Vert = function (pageDiv) {
API.hasChanged = hasChanged; API.hasChanged = hasChanged;
API.measure = measure; API.measure = measure;
API.pages = pages; API.pages = pages;
API.percentageThroughOfId = percentageThroughOfId; API.percentageThroughOfNode = percentageThroughOfNode;
API.locusToOffset = locusToOffset; API.locusToOffset = locusToOffset;
initialize(); initialize();
@ -2713,8 +2832,7 @@ Monocle.Dimensions.Columns = function (pageDiv) {
(!p.measurements) || (!p.measurements) ||
(p.measurements.width != newMeasurements.width) || (p.measurements.width != newMeasurements.width) ||
(p.measurements.height != newMeasurements.height) || (p.measurements.height != newMeasurements.height) ||
(p.measurements.scrollWidth != newMeasurements.scrollWidth) || (p.measurements.scrollWidth != newMeasurements.scrollWidth)
(p.measurements.fontSize != newMeasurements.fontSize)
); );
} }
@ -2736,12 +2854,18 @@ Monocle.Dimensions.Columns = function (pageDiv) {
if (!lc || !lc.getBoundingClientRect) { if (!lc || !lc.getBoundingClientRect) {
console.warn('Empty document for page['+p.page.m.pageIndex+']'); console.warn('Empty document for page['+p.page.m.pageIndex+']');
p.measurements.scrollWidth = p.measurements.width; p.measurements.scrollWidth = p.measurements.width;
} else if (lc.getBoundingClientRect().bottom > p.measurements.height) { } else {
var bcr = lc.getBoundingClientRect();
if (
bcr.right > p.measurements.width ||
bcr.bottom > p.measurements.height
) {
p.measurements.scrollWidth = p.measurements.width * 2; p.measurements.scrollWidth = p.measurements.width * 2;
} else { } else {
p.measurements.scrollWidth = p.measurements.width; p.measurements.scrollWidth = p.measurements.width;
} }
} }
}
p.length = Math.ceil(p.measurements.scrollWidth / p.measurements.width); p.length = Math.ceil(p.measurements.scrollWidth / p.measurements.width);
p.dirty = false; p.dirty = false;
@ -2758,12 +2882,11 @@ Monocle.Dimensions.Columns = function (pageDiv) {
} }
function percentageThroughOfId(id) { function percentageThroughOfNode(target) {
var doc = p.page.m.activeFrame.contentDocument;
var target = doc.getElementById(id);
if (!target) { if (!target) {
return 0; return 0;
} }
var doc = p.page.m.activeFrame.contentDocument;
var offset = 0; var offset = 0;
if (target.getBoundingClientRect) { if (target.getBoundingClientRect) {
offset = target.getBoundingClientRect().left; offset = target.getBoundingClientRect().left;
@ -2785,20 +2908,30 @@ Monocle.Dimensions.Columns = function (pageDiv) {
function componentChanged(evt) { function componentChanged(evt) {
if (evt.m['page'] != p.page) { return; } if (evt.m['page'] != p.page) { return; }
var doc = evt.m['document']; var doc = evt.m['document'];
if (Monocle.Browser.has.columnOverflowPaintBug) {
var div = doc.createElement('div');
Monocle.Styles.applyRules(div, k.BODY_STYLES);
div.style.cssText += "overflow: scroll !important;";
while (doc.body.childNodes.length) {
div.appendChild(doc.body.firstChild);
}
doc.body.appendChild(div);
} else {
Monocle.Styles.applyRules(doc.body, k.BODY_STYLES); Monocle.Styles.applyRules(doc.body, k.BODY_STYLES);
if (Monocle.Browser.is.WebKit) { if (Monocle.Browser.is.WebKit) {
doc.documentElement.style.overflow = 'hidden'; doc.documentElement.style.overflow = 'hidden';
} }
}
p.dirty = true; p.dirty = true;
} }
function setColumnWidth() { function setColumnWidth() {
var cw = p.page.m.sheafDiv.clientWidth; var cw = p.page.m.sheafDiv.clientWidth;
var doc = p.page.m.activeFrame.contentDocument;
if (currBodyStyleValue('column-width') != cw+"px") { if (currBodyStyleValue('column-width') != cw+"px") {
Monocle.Styles.affix(doc.body, 'column-width', cw+"px"); Monocle.Styles.affix(columnedElement(), 'column-width', cw+"px");
p.dirty = true; p.dirty = true;
} }
} }
@ -2809,8 +2942,7 @@ Monocle.Dimensions.Columns = function (pageDiv) {
return { return {
width: sheaf.clientWidth, width: sheaf.clientWidth,
height: sheaf.clientHeight, height: sheaf.clientHeight,
scrollWidth: scrollerWidth(), scrollWidth: scrollerWidth()
fontSize: currBodyStyleValue('font-size')
} }
} }
@ -2819,16 +2951,24 @@ Monocle.Dimensions.Columns = function (pageDiv) {
if (Monocle.Browser.has.mustScrollSheaf) { if (Monocle.Browser.has.mustScrollSheaf) {
return p.page.m.sheafDiv; return p.page.m.sheafDiv;
} else { } else {
return p.page.m.activeFrame.contentDocument.body; return columnedElement();
} }
} }
function columnedElement() {
var elem = p.page.m.activeFrame.contentDocument.body;
return Monocle.Browser.has.columnOverflowPaintBug ? elem.firstChild : elem;
}
function scrollerWidth() { function scrollerWidth() {
var bdy = p.page.m.activeFrame.contentDocument.body; var bdy = p.page.m.activeFrame.contentDocument.body;
if (Monocle.Browser.has.iframeDoubleWidthBug) { if (Monocle.Browser.has.iframeDoubleWidthBug) {
if (Monocle.Browser.on.Android) { if (Monocle.Browser.on.Kindle3) {
return bdy.scrollWidth * 1.5; // I actually have no idea why 1.5. return scrollerElement().scrollWidth;
} else if (Monocle.Browser.on.Android) {
return bdy.scrollWidth;
} else if (Monocle.Browser.iOSVersion < "4.1") { } else if (Monocle.Browser.iOSVersion < "4.1") {
var hbw = bdy.scrollWidth / 2; var hbw = bdy.scrollWidth / 2;
var sew = scrollerElement().scrollWidth; var sew = scrollerElement().scrollWidth;
@ -2838,15 +2978,18 @@ Monocle.Dimensions.Columns = function (pageDiv) {
var hbw = bdy.scrollWidth / 2; var hbw = bdy.scrollWidth / 2;
return hbw; return hbw;
} }
} else if (Monocle.Browser.is.Gecko) { } else if (bdy.getBoundingClientRect) {
var lc = bdy.lastChild; var elems = bdy.getElementsByTagName('*');
while (lc && lc.nodeType != 1) { var bdyRect = bdy.getBoundingClientRect();
lc = lc.previousSibling; var l = bdyRect.left, r = bdyRect.right;
} for (var i = elems.length - 1; i >= 0; --i) {
if (lc && lc.getBoundingClientRect) { var rect = elems[i].getBoundingClientRect();
return lc.getBoundingClientRect().right; l = Math.min(l, rect.left);
r = Math.max(r, rect.right);
} }
return Math.abs(l) + Math.abs(r);
} }
return scrollerElement().scrollWidth; return scrollerElement().scrollWidth;
} }
@ -2867,8 +3010,14 @@ Monocle.Dimensions.Columns = function (pageDiv) {
function translateToLocus(locus) { function translateToLocus(locus) {
var offset = locusToOffset(locus); var offset = locusToOffset(locus);
p.page.m.offset = 0 - offset;
if (k.SETX && !Monocle.Browser.has.columnOverflowPaintBug) {
var bdy = p.page.m.activeFrame.contentDocument.body; var bdy = p.page.m.activeFrame.contentDocument.body;
Monocle.Styles.affix(bdy, "transform", "translateX("+offset+"px)"); Monocle.Styles.affix(bdy, "transform", "translateX("+offset+"px)");
} else {
var scrElem = scrollerElement();
scrElem.scrollLeft = 0 - offset;
}
return offset; return offset;
} }
@ -2876,7 +3025,7 @@ Monocle.Dimensions.Columns = function (pageDiv) {
API.hasChanged = hasChanged; API.hasChanged = hasChanged;
API.measure = measure; API.measure = measure;
API.pages = pages; API.pages = pages;
API.percentageThroughOfId = percentageThroughOfId; API.percentageThroughOfNode = percentageThroughOfNode;
API.locusToOffset = locusToOffset; API.locusToOffset = locusToOffset;
API.translateToLocus = translateToLocus; API.translateToLocus = translateToLocus;
@ -2898,6 +3047,8 @@ Monocle.Dimensions.Columns.BODY_STYLES = {
"column-fill": "auto" "column-fill": "auto"
} }
Monocle.Dimensions.Columns.SETX = true; // Set to false for scrollLeft.
if (Monocle.Browser.has.iframeDoubleWidthBug) { if (Monocle.Browser.has.iframeDoubleWidthBug) {
Monocle.Dimensions.Columns.BODY_STYLES["min-width"] = "200%"; Monocle.Dimensions.Columns.BODY_STYLES["min-width"] = "200%";
} else { } else {
@ -2924,6 +3075,8 @@ Monocle.Flippers.Slider = function (reader) {
function addPage(pageDiv) { function addPage(pageDiv) {
pageDiv.m.dimensions = new Monocle.Dimensions.Columns(pageDiv); pageDiv.m.dimensions = new Monocle.Dimensions.Columns(pageDiv);
Monocle.Styles.setX(pageDiv, "0px");
} }
@ -2963,6 +3116,7 @@ Monocle.Flippers.Slider = function (reader) {
function interactiveMode(bState) { function interactiveMode(bState) {
p.reader.dispatchEvent('monocle:interactive:'+(bState ? 'on' : 'off'));
if (!Monocle.Browser.has.selectThruBug) { if (!Monocle.Browser.has.selectThruBug) {
return; return;
} }
@ -2994,10 +3148,10 @@ Monocle.Flippers.Slider = function (reader) {
function moveTo(locus, callback) { function moveTo(locus, callback) {
var fn = function () { var fn = function () {
prepareNextPage(announceTurn); prepareNextPage(function () {
if (typeof callback == "function") { if (typeof callback == "function") { callback(); }
callback(); announceTurn();
} });
} }
setPage(upperPage(), locus, fn); setPage(upperPage(), locus, fn);
} }
@ -3045,12 +3199,26 @@ Monocle.Flippers.Slider = function (reader) {
if (dir == k.FORWARDS) { if (dir == k.FORWARDS) {
if (getPlace().onLastPageOfBook()) { if (getPlace().onLastPageOfBook()) {
p.reader.dispatchEvent(
'monocle:boundaryend',
{
locus: getPlace().getLocus({ direction : dir }),
page: upperPage()
}
);
resetTurnData(); resetTurnData();
return; return;
} }
onGoingForward(boxPointX); onGoingForward(boxPointX);
} else if (dir == k.BACKWARDS) { } else if (dir == k.BACKWARDS) {
if (getPlace().onFirstPageOfBook()) { if (getPlace().onFirstPageOfBook()) {
p.reader.dispatchEvent(
'monocle:boundarystart',
{
locus: getPlace().getLocus({ direction : dir }),
page: upperPage()
}
);
resetTurnData(); resetTurnData();
return; return;
} }
@ -3215,14 +3383,14 @@ Monocle.Flippers.Slider = function (reader) {
function announceTurn() { function announceTurn() {
hideWaitControl(upperPage());
hideWaitControl(lowerPage());
p.reader.dispatchEvent('monocle:turn'); p.reader.dispatchEvent('monocle:turn');
resetTurnData(); resetTurnData();
} }
function resetTurnData() { function resetTurnData() {
hideWaitControl(upperPage());
hideWaitControl(lowerPage());
p.turnData = {}; p.turnData = {};
} }
@ -3268,7 +3436,7 @@ Monocle.Flippers.Slider = function (reader) {
(new Date()).getTime() - stamp > duration || (new Date()).getTime() - stamp > duration ||
Math.abs(currX - finalX) <= Math.abs((currX + step) - finalX) Math.abs(currX - finalX) <= Math.abs((currX + step) - finalX)
) { ) {
clearTimeout(elem.setXTransitionInterval) clearTimeout(elem.setXTransitionInterval);
Monocle.Styles.setX(elem, finalX); Monocle.Styles.setX(elem, finalX);
if (elem.setXTCB) { if (elem.setXTCB) {
elem.setXTCB(); elem.setXTCB();
@ -3366,13 +3534,17 @@ Monocle.Flippers.Slider = function (reader) {
function jumpIn(pageDiv, callback) { function jumpIn(pageDiv, callback) {
var dur = Monocle.Browser.has.jumpFlickerBug ? 1 : 0; var dur = Monocle.Browser.has.jumpFlickerBug ? 1 : 0;
Monocle.defer(function () {
setX(pageDiv, 0, { duration: dur }, callback); setX(pageDiv, 0, { duration: dur }, callback);
});
} }
function jumpOut(pageDiv, callback) { function jumpOut(pageDiv, callback) {
var dur = Monocle.Browser.has.jumpFlickerBug ? 1 : 0; var dur = Monocle.Browser.has.jumpFlickerBug ? 1 : 0;
Monocle.defer(function () {
setX(pageDiv, 0 - pageDiv.offsetWidth, { duration: dur }, callback); setX(pageDiv, 0 - pageDiv.offsetWidth, { duration: dur }, callback);
});
} }
@ -3382,7 +3554,9 @@ Monocle.Flippers.Slider = function (reader) {
duration: k.durations.SLIDE, duration: k.durations.SLIDE,
timing: 'ease-in' timing: 'ease-in'
}; };
Monocle.defer(function () {
setX(upperPage(), 0, slideOpts, callback); setX(upperPage(), 0, slideOpts, callback);
});
} }
@ -3391,7 +3565,9 @@ Monocle.Flippers.Slider = function (reader) {
duration: k.durations.SLIDE, duration: k.durations.SLIDE,
timing: 'ease-in' timing: 'ease-in'
}; };
Monocle.defer(function () {
setX(upperPage(), 0 - upperPage().offsetWidth, slideOpts, callback); setX(upperPage(), 0 - upperPage().offsetWidth, slideOpts, callback);
});
} }
@ -3418,13 +3594,13 @@ Monocle.Flippers.Slider = function (reader) {
function showWaitControl(page) { function showWaitControl(page) {
var ctrl = p.reader.dom.find('flippers_slider_wait', page.m.pageIndex); var ctrl = p.reader.dom.find('flippers_slider_wait', page.m.pageIndex);
ctrl.style.opacity = 0.5; ctrl.style.visibility = "visible";
} }
function hideWaitControl(page) { function hideWaitControl(page) {
var ctrl = p.reader.dom.find('flippers_slider_wait', page.m.pageIndex); var ctrl = p.reader.dom.find('flippers_slider_wait', page.m.pageIndex);
ctrl.style.opacity = 0; ctrl.style.visibility = "hidden";
} }
API.pageCount = p.pageCount; API.pageCount = p.pageCount;