Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Sugar for creating bookdata objects.

* Monocle.bookData takes a hash.
* Monocle.bookDataFromIds takes an array of DOM element ids.
* Monocle.bookDataFromNodes takes an array of DOM elements.

See src/core/bookdata.js for details.
commit 9771dd7ea3db25702d875b01028db291acdac228 1 parent 928ce16
@joseph authored
View
24 src/core/book.js
@@ -419,26 +419,10 @@ Monocle.Book = function (dataSource) {
}
-// A shortcut for creating a book from an array of nodes.
-//
-// You could use this as follows, for eg:
-//
-// Monocle.Book.fromNodes([document.getElementById('content')]);
+// Legacy function. Deprecated.
//
Monocle.Book.fromNodes = function (nodes) {
- var bookData = {
- getComponents: function () {
- return ['anonymous'];
- },
- getContents: function () {
- return [];
- },
- getComponent: function (n) {
- return { 'nodes': nodes };
- },
- getMetaData: function (key) {
- }
- }
-
- return new Monocle.Book(bookData);
+ return new Monocle.Book(Monocle.Book.bookDataFromNodes(nodes));
}
+
+
View
58 src/core/bookdata.js
@@ -0,0 +1,58 @@
+// A shortcut for creating a bookdata object from a 'data' hash.
+//
+// eg:
+//
+// Monocle.bookData({ components: ['intro.html', 'ch1.html', 'ch2.html'] });
+//
+// All keys in the 'data' hash are optional:
+//
+// components: must be an array of component urls
+// chapters: must be an array of nested chapters (the usual bookdata structure)
+// metadata: must be a hash of key/values
+// getComponentFn: override the default way to fetch components via id
+//
+Monocle.bookData = function (data) {
+ return {
+ getComponents: function () {
+ return data.components || ['anonymous'];
+ },
+ getContents: function () {
+ return data.chapters || [];
+ },
+ getComponent: data.getComponent || function (id) {
+ return { url: id }
+ },
+ getMetaData: function (key) {
+ return (data.metadata || {})[key];
+ }
+ }
+}
+
+
+// A shortcut for creating a bookdata object from an array of element ids.
+//
+// eg:
+//
+// Monocle.bookDataFromIds(['part1', 'part2']);
+//
+Monocle.bookDataFromIds = function (elementIds) {
+ return Monocle.bookData({
+ components: elementIds,
+ getComponent: function (cmptId) {
+ return { nodes: [document.getElementById(cmptId).cloneNode(true)] }
+ }
+ });
+}
+
+
+// A shortcut for creating a bookdata object from an array of nodes.
+//
+// eg:
+//
+// Monocle.bookDataFromNodes([document.getElementById('content')]);
+//
+Monocle.bookDataFromNodes = function (nodes) {
+ return Monocle.bookData({
+ getComponent: function (n) { return { 'nodes': nodes }; }
+ });
+}
View
9 src/core/reader.js
@@ -97,12 +97,9 @@ Monocle.Reader = function (node, bookData, options, onLoadCallback) {
dispatchEvent("monocle:initializing");
- var bk;
- if (bookData) {
- bk = new Monocle.Book(bookData);
- } else {
- bk = Monocle.Book.fromNodes([box.cloneNode(true)]);
- }
+ bookData = bookData || Monocle.bookDataFromNodes([box.cloneNode(true)]);
+ var bk = new Monocle.Book(bookData);
+
box.innerHTML = "";
// Make sure the box div is absolutely or relatively positioned.
View
1  src/monocore.js
@@ -3,6 +3,7 @@
//= require "compat/css"
//= require "compat/stubs"
//= require "compat/browser"
+//= require "core/bookdata"
//= require "core/factory"
//= require "core/events"
//= require "core/styles"
View
40 test/embedded/embed.html
@@ -24,41 +24,15 @@
Monocle.DEBUG = true;
(function () {
- var bookData = {
- getComponents: function () {
- return ['content/1.html', 'content/2.html'];
- },
- getComponent: function (cmptId) {
- return { url: cmptId }
- },
- getContents: function () {
- return [
- {
- title: "Part I",
- src: "content/1.html"
- },
- {
- title: "Part II",
- src: "content/2.html"
- }
- ];
- },
- getMetaData: function(key) {
- return { title: "Lorem Ipsum", creator: "Mr Monocle" }[key];
- }
+ function init() {
+ var bookData = Monocle.bookData({
+ components: ['content/1.html', 'content/2.html']
+ });
+ var options = { panels: Monocle.Panels.IMode };
+ window.reader = Monocle.Reader('reader', bookData, options);
}
- // Initialize the reader element.
- var rdrOptions = {
- panels: Monocle.Panels.IMode
- }
- Monocle.Events.listen(
- window,
- 'load',
- function () {
- window.reader = Monocle.Reader('reader', bookData, rdrOptions);
- }
- );
+ Monocle.Events.listen(window, 'load', init);
})();
</script>
View
8 test/events/index.html
@@ -28,13 +28,7 @@
(function () {
Monocle.DEBUG = true;
- var bd = {
- getComponents: function () { return ['pt1.html', 'pt2.html']; },
- getComponent: function (cmptId) { return { url: cmptId } },
- getContents: function () { return []; },
- getMetaData: function (key) { return null; }
- }
-
+ var bd = Monocle.bookData({ components: ['pt1.html', 'pt2.html'] });
function listenFor(evtName) {
Monocle.Events.listen('reader', evtName, report);
View
30 test/flippers/index.html
@@ -28,35 +28,7 @@
<script type="text/javascript">
Monocle.DEBUG = true;
- var bookData = {
- getComponents: function () {
- return [
- 'part1',
- 'part2'
- ];
- },
- getContents: function () {
- return [
- {
- title: "I",
- src: 'part1'
- },
- {
- title: "II",
- src: 'part2'
- }
- ]
- },
- getComponent: function (cmptId) {
- return { nodes: [document.getElementById(cmptId).cloneNode(true)] };
- },
- getMetaData: function(key) {
- return {
- title: "Lorem Ipsum",
- creator: "Mr Monocle"
- }[key];
- }
- }
+ var bookData = Monocle.bookDataFromIds(['part1', 'part2']);
// Initialize the reader element.
Monocle.Events.listen(
View
8 test/locus-search/index.html
@@ -60,14 +60,8 @@
(function () {
Monocle.DEBUG = true;
- var bd = {
- getComponents: function () { return ['pt1.html', 'pt2.html']; },
- getComponent: function (cmptId) { return { url: cmptId } },
- getContents: function () { return []; },
- getMetaData: function (key) { return null; }
- }
-
function init() {
+ var bd = Monocle.bookData({ components: ['pt1.html', 'pt2.html'] });
var options = { stylesheet: ".hl { background: #FFC !important; }" }
window.reader = Monocle.Reader('reader', bd, options);
}
View
30 test/panels/index.html
@@ -28,35 +28,7 @@
<script type="text/javascript">
Monocle.DEBUG = true;
- var bookData = {
- getComponents: function () {
- return [
- 'part1',
- 'part2'
- ];
- },
- getContents: function () {
- return [
- {
- title: "I",
- src: 'part1'
- },
- {
- title: "II",
- src: 'part2'
- }
- ]
- },
- getComponent: function (cmptId) {
- return { nodes: [document.getElementById(cmptId).cloneNode(true)] };
- },
- getMetaData: function(key) {
- return {
- title: "Lorem Ipsum",
- creator: "Monocle"
- }[key];
- }
- }
+ var bookData = Monocle.bookDataFromIds(['part1', 'part2']);
// Initialize the reader element.
Monocle.Events.listen(
View
77 test/showcase/02-dickens/test.js
@@ -4,50 +4,41 @@ Monocle.DEBUG = true;
Monocle.Styles.container.right = "24px";
- var bookData = {
- getComponents: function () {
- return [
- 'components/1.html',
- 'components/2.html',
- 'components/3.html'
- ];
- },
- getContents: function () {
- return [
- {
- title: "The Signal Man",
- src: "components/1.html"
- },
- {
- title: "The Haunted House",
- src: "components/2.html",
- children: [
- {
- title: "Chapter I — The Mortals in the House",
- src: "components/2.html#p2ch1"
- },
- {
- title: "Chapter II — The Ghost in Master B.'s Room",
- src: "components/2.html#p2ch2"
- }
- ]
- },
- {
- title: "The Trial for Murder",
- src: "components/3.html"
- }
- ]
- },
- getComponent: function (componentId) {
- return { url: componentId }
- },
- getMetaData: function(key) {
- return {
- title: "Three Ghost Stories",
- creator: "Charles Dickens"
- }[key];
+ var bookData = Monocle.bookData({
+ components: [
+ 'components/1.html',
+ 'components/2.html',
+ 'components/3.html'
+ ],
+ chapters:[
+ {
+ title: "The Signal Man",
+ src: "components/1.html"
+ },
+ {
+ title: "The Haunted House",
+ src: "components/2.html",
+ children: [
+ {
+ title: "Chapter I — The Mortals in the House",
+ src: "components/2.html#p2ch1"
+ },
+ {
+ title: "Chapter II — The Ghost in Master B.'s Room",
+ src: "components/2.html#p2ch2"
+ }
+ ]
+ },
+ {
+ title: "The Trial for Murder",
+ src: "components/3.html"
+ }
+ ],
+ metadata: {
+ title: "Three Ghost Stories",
+ creator: "Charles Dickens"
}
- }
+ });
// Initialize the reader element.
Monocle.Events.listen(
View
48 test/stencil/test.js
@@ -1,15 +1,10 @@
Monocle.DEBUG = true;
(function () {
- var bookData = {
- getComponents: function () {
- return ['content/0.html', 'content/1/1.html', 'content/2/2.html'];
- },
- getComponent: function (cmptId) {
- return { url: cmptId }
- },
- getContents: function () {
- return [
+ function init() {
+ var bookData = Monocle.bookData({
+ components: ['content/0.html', 'content/1/1.html', 'content/2/2.html'],
+ chapters: [
{
title: "0",
src: "content/0.html"
@@ -26,30 +21,17 @@ Monocle.DEBUG = true;
title: "3 (does not exist)",
src: "content/3/3.html"
}
- ];
- },
- getMetaData: function(key) {
- return { title: "Lorem Ipsum", creator: "Mr Monocle" }[key];
- }
+ ],
+ metadata: { title: "Lorem Ipsum", creator: "Mr Monocle" }
+ });
+ var options = { panels: Monocle.Panels.IMode }
+ Monocle.Reader('reader', bookData, options, function (rdr) {
+ window.reader = rdr;
+ alertIfComponentNotFound(rdr);
+ createStencil(rdr);
+ });
}
- var rdrOptions = {
- panels: Monocle.Panels.IMode
- }
-
- // Initialize the reader element.
- Monocle.Events.listen(
- window,
- 'load',
- function () {
- Monocle.Reader('reader', bookData, rdrOptions, function (rdr) {
- window.reader = rdr;
- alertIfComponentNotFound(rdr);
- createStencil(rdr);
- });
- }
- );
-
function alertIfComponentNotFound(rdr) {
rdr.listen('monocle:notfound', function () { alert('Not found.') });
@@ -64,4 +46,8 @@ Monocle.DEBUG = true;
stencil.toggleHighlights();
}
+
+ // Initialize the reader element.
+ Monocle.Events.listen(window, 'load', init);
+
})();
Please sign in to comment.
Something went wrong with that request. Please try again.