diff options
Diffstat (limited to 'point/data/renderSlide.js')
-rw-r--r-- | point/data/renderSlide.js | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/point/data/renderSlide.js b/point/data/renderSlide.js new file mode 100644 index 0000000..e149f41 --- /dev/null +++ b/point/data/renderSlide.js | |||
@@ -0,0 +1,127 @@ | |||
1 | /* | ||
2 | * This file is part of "What's The Point" <https://github.com/Pacien/WhatsThePoint> | ||
3 | * Copyright (C) 2014 Pacien TRAN-GIRARD | ||
4 | * | ||
5 | * "What's The Point" is free software: you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU Affero General Public License as | ||
7 | * published by the Free Software Foundation, either version 3 of the | ||
8 | * License, or (at your option) any later version. | ||
9 | * | ||
10 | * "What's The Point" is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU Affero General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU Affero General Public License | ||
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | |||
19 | define(function () { | ||
20 | |||
21 | var renderSlide = { | ||
22 | |||
23 | init: function () { | ||
24 | this.lastTitle = undefined; | ||
25 | }, | ||
26 | |||
27 | appendNode: function (parent, tag, content, appendMethod) { | ||
28 | var node = document.createElement(tag); | ||
29 | appendMethod(node, content); | ||
30 | parent.appendChild(node); | ||
31 | return parent; | ||
32 | }, | ||
33 | |||
34 | appendHtmlNode: function (parent, tag, htmlContent) { | ||
35 | return this.appendNode(parent, tag, htmlContent, function (parent, htmlContent) { | ||
36 | parent.innerHTML = htmlContent; | ||
37 | }); | ||
38 | }, | ||
39 | |||
40 | appendObjectNode: function (parent, tag, node) { | ||
41 | return this.appendNode(parent, tag, node, function (parent, node) { | ||
42 | parent.appendChild(node); | ||
43 | }); | ||
44 | }, | ||
45 | |||
46 | appendTextNode: function (parent, tag, text) { | ||
47 | return this.appendObjectNode(parent, tag, document.createTextNode(text)); | ||
48 | }, | ||
49 | |||
50 | appendContentNode: function (parent, tag, content) { | ||
51 | if (typeof (content) === "string") { | ||
52 | this.appendHtmlNode(parent, tag, content); | ||
53 | } else { | ||
54 | this.appendObjectNode(parent, tag, content); | ||
55 | } | ||
56 | }, | ||
57 | |||
58 | renderTitle: function (slideData) { | ||
59 | var title = slideData.title; | ||
60 | var subtitle = slideData.subtitle; | ||
61 | |||
62 | if (title !== undefined) { | ||
63 | this.lastTitle = title; | ||
64 | } else { | ||
65 | if (subtitle !== undefined) { | ||
66 | title = this.lastTitle; | ||
67 | } else { | ||
68 | this.lastTitle = undefined; | ||
69 | } | ||
70 | } | ||
71 | |||
72 | var titleNode; | ||
73 | if (title !== undefined || subtitle !== undefined) { | ||
74 | titleNode = document.createElement("s-title"); | ||
75 | } else { | ||
76 | titleNode = document.createDocumentFragment("s-title"); | ||
77 | } | ||
78 | |||
79 | if (title !== undefined) { | ||
80 | this.appendTextNode(titleNode, "h1", title); | ||
81 | } | ||
82 | |||
83 | if (subtitle !== undefined) { | ||
84 | this.appendTextNode(titleNode, "h2", subtitle); | ||
85 | } | ||
86 | |||
87 | return titleNode; | ||
88 | }, | ||
89 | |||
90 | renderBody: function (slideData) { | ||
91 | var bodyNode = document.createDocumentFragment(); | ||
92 | //var bodyNode = document.createElement("s-body"); | ||
93 | |||
94 | if (slideData.content !== undefined) { | ||
95 | this.appendContentNode(bodyNode, "s-content", slideData.content); | ||
96 | } | ||
97 | |||
98 | var notes = slideData.notes === undefined ? "" : slideData.notes; | ||
99 | this.appendContentNode(bodyNode, "s-notes", notes); | ||
100 | |||
101 | return bodyNode; | ||
102 | }, | ||
103 | |||
104 | renderSlide: function (slideData, index) { | ||
105 | var slide = document.createElement("p-slide"); | ||
106 | slide.setAttribute("id", index + 1); | ||
107 | slide.appendChild(this.renderTitle(slideData)); | ||
108 | slide.appendChild(this.renderBody(slideData)); | ||
109 | return slide; | ||
110 | }, | ||
111 | |||
112 | renderSlides: function (slidesData) { | ||
113 | var slides = document.createDocumentFragment(); | ||
114 | for (var i = 0; i < slidesData.length; i++) { | ||
115 | slides.appendChild(this.renderSlide(slidesData[i], i)); | ||
116 | } | ||
117 | return slides; | ||
118 | }, | ||
119 | |||
120 | render: function (slidesData, callback) { | ||
121 | callback(this.renderSlides(slidesData)); | ||
122 | }, | ||
123 | }; | ||
124 | |||
125 | return renderSlide; | ||
126 | |||
127 | }); | ||