aboutsummaryrefslogtreecommitdiff
path: root/point/data/renderSlide.js
blob: e149f410f012d76d555b2d41508d86d9704c565a (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 *	This file is part of "What's The Point" <https://github.com/Pacien/WhatsThePoint>
 *	Copyright (C) 2014  Pacien TRAN-GIRARD
 *
 *	"What's The Point" is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU Affero General Public License as
 *	published by the Free Software Foundation, either version 3 of the
 *	License, or (at your option) any later version.
 *
 *	"What's The Point" is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU Affero General Public License for more details.
 *
 *	You should have received a copy of the GNU Affero General Public License
 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

define(function () {

	var renderSlide = {

		init: function () {
			this.lastTitle = undefined;
		},

		appendNode: function (parent, tag, content, appendMethod) {
			var node = document.createElement(tag);
			appendMethod(node, content);
			parent.appendChild(node);
			return parent;
		},

		appendHtmlNode: function (parent, tag, htmlContent) {
			return this.appendNode(parent, tag, htmlContent, function (parent, htmlContent) {
				parent.innerHTML = htmlContent;
			});
		},

		appendObjectNode: function (parent, tag, node) {
			return this.appendNode(parent, tag, node, function (parent, node) {
				parent.appendChild(node);
			});
		},

		appendTextNode: function (parent, tag, text) {
			return this.appendObjectNode(parent, tag, document.createTextNode(text));
		},

		appendContentNode: function (parent, tag, content) {
			if (typeof (content) === "string") {
				this.appendHtmlNode(parent, tag, content);
			} else {
				this.appendObjectNode(parent, tag, content);
			}
		},

		renderTitle: function (slideData) {
			var title = slideData.title;
			var subtitle = slideData.subtitle;

			if (title !== undefined) {
				this.lastTitle = title;
			} else {
				if (subtitle !== undefined) {
					title = this.lastTitle;
				} else {
					this.lastTitle = undefined;
				}
			}

			var titleNode;
			if (title !== undefined || subtitle !== undefined) {
				titleNode = document.createElement("s-title");
			} else {
				titleNode = document.createDocumentFragment("s-title");
			}

			if (title !== undefined) {
				this.appendTextNode(titleNode, "h1", title);
			}

			if (subtitle !== undefined) {
				this.appendTextNode(titleNode, "h2", subtitle);
			}

			return titleNode;
		},

		renderBody: function (slideData) {
			var bodyNode = document.createDocumentFragment();
			//var bodyNode = document.createElement("s-body");

			if (slideData.content !== undefined) {
				this.appendContentNode(bodyNode, "s-content", slideData.content);
			}

			var notes = slideData.notes === undefined ? "" : slideData.notes;
			this.appendContentNode(bodyNode, "s-notes", notes);

			return bodyNode;
		},

		renderSlide: function (slideData, index) {
			var slide = document.createElement("p-slide");
			slide.setAttribute("id", index + 1);
			slide.appendChild(this.renderTitle(slideData));
			slide.appendChild(this.renderBody(slideData));
			return slide;
		},

		renderSlides: function (slidesData) {
			var slides = document.createDocumentFragment();
			for (var i = 0; i < slidesData.length; i++) {
				slides.appendChild(this.renderSlide(slidesData[i], i));
			}
			return slides;
		},

		render: function (slidesData, callback) {
			callback(this.renderSlides(slidesData));
		},
	};

	return renderSlide;

});