-
Notifications
You must be signed in to change notification settings - Fork 200
/
bookdata.js
59 lines (55 loc) · 1.48 KB
/
bookdata.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// 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];
},
data: data
}
}
// 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)] }
}
});
}
// 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 }; }
});
}