on 3cd940f help sign in
elk/runtime/Render/Element.js
374 lines 12 kB history raw
2ElmRuntime.Render.Element = function() {
3'use strict';
5var Utils = ElmRuntime.use(ElmRuntime.Render.Utils);
6var newElement = Utils.newElement, extract = Utils.extract,
7 addTransform = Utils.addTransform, removeTransform = Utils.removeTransform,
8 fromList = Utils.fromList, eq = Utils.eq;
10function setProps(elem, e) {
11 var props = elem.props;
12 var element = elem.element;
13 if (element.adjustWidth) {
14 props.width -= element.adjustWidth;
15 }
16 if (element.adjustHeight) {
17 props.height -= element.adjustHeight;
18 }
19 e.style.width = (props.width |0) + 'px';
20 e.style.height = (props.height|0) + 'px';
21 if (props.opacity !== 1) { e.style.opacity = props.opacity; }
22 if (props.color.ctor === 'Just') {
23 e.style.backgroundColor = extract(props.color._0);
24 }
25 if (props.tag !== '') { e.id = props.tag; }
26 if (props.href !== '') {
27 var a = newElement('a');
28 a.href = props.href;
29 a.style.width = '100%';
30 a.style.height = '100%';
31 a.style.top = 0;
32 a.style.left = 0;
33 a.style.display = 'block';
34 a.style.position = 'absolute';
35 e.style.position = 'relative';
36 e.appendChild(a);
37 }
38 if (props.hover.ctor !== '_Tuple0') {
39 e.style.pointerEvents = 'auto';
40 e.elm_hover_handler = props.hover;
41 e.elm_hover_count = 0;
42 e.addEventListener('mouseover', function() {
43 if (e.elm_hover_count++ > 0) return;
44 var handler = e.elm_hover_handler;
45 if (handler !== null) {
46 handler(true);
47 }
48 });
49 e.addEventListener('mouseout', function(evt) {
50 if (e.contains(evt.toElement || evt.relatedTarget)) return;
51 e.elm_hover_count = 0;
52 var handler = e.elm_hover_handler;
53 if (handler !== null) {
54 handler(false);
55 }
56 });
57 }
58 if (props.click.ctor !== '_Tuple0') {
59 e.style.pointerEvents = 'auto';
60 e.elm_click_handler = props.click;
61 e.addEventListener('click', function() {
62 e.elm_click_handler(Tuple0);
63 });
64 }
65 return e;
68function image(props, img) {
69 switch (img._0.ctor) {
70 case 'Plain': return plainImage(img._3);
71 case 'Fitted': return fittedImage(props.width, props.height, img._3);
72 case 'Cropped': return croppedImage(img,props.width,props.height,img._3);
73 case 'Tiled': return tiledImage(img._3);
74 }
77function plainImage(src) {
78 var img = newElement('img');
79 img.src = src;
80 img.name = src;
81 img.style.display = "block";
82 return img;
85function tiledImage(src) {
86 var div = newElement('div');
87 div.style.backgroundImage = 'url(' + src + ')';
88 return div;
91function fittedImage(w, h, src) {
92 var div = newElement('div');
93 div.style.background = 'url(' + src + ') no-repeat center';
94 div.style.webkitBackgroundSize = 'cover';
95 div.style.MozBackgroundSize = 'cover';
96 div.style.OBackgroundSize = 'cover';
97 div.style.backgroundSize = 'cover';
98 return div;
101function croppedImage(elem, w, h, src) {
102 var pos = elem._0._0;
103 var e = newElement('div');
104 e.style.overflow = "hidden";
106 var img = newElement('img');
107 img.onload = function() {
108 var sw = w / elem._1, sh = h / elem._2;
109 img.style.width = ((this.width * sw)|0) + 'px';
110 img.style.height = ((this.height * sh)|0) + 'px';
111 img.style.marginLeft = ((- pos._0 * sw)|0) + 'px';
112 img.style.marginTop = ((- pos._1 * sh)|0) + 'px';
113 };
114 img.src = src;
115 img.name = src;
116 e.appendChild(img);
117 return e;
120function goIn(e) { e.style.position = 'absolute'; return e; }
121function goDown(e) { return e }
122function goRight(e) { e.style.styleFloat = e.style.cssFloat = "left"; return e; }
123function flowWith(f, array) {
124 var container = newElement('div');
125 if (f == goIn) container.style.pointerEvents = 'none';
127 for (var i = array.length; i--; ) {
128 container.appendChild(f(render(array[i])));
129 }
130 return container;
133function flow(dir,elist) {
134 var array = fromList(elist);
135 switch(dir.ctor) {
136 case "DDown": array.reverse();
137 case "DUp": return flowWith(goDown,array);
138 case "DRight": array.reverse();
139 case "DLeft": return flowWith(goRight,array);
140 case "DOut": array.reverse();
141 case "DIn": return flowWith(goIn,array);
142 }
145function toPos(pos) {
146 switch(pos.ctor) {
147 case "Absolute": return pos._0 + "px";
148 case "Relative": return (pos._0 * 100) + "%";
149 }
152// must clear right, left, top, bottom, and transform
153// before calling this function
154function setPos(pos,w,h,e) {
155 e.style.position = 'absolute';
156 e.style.margin = 'auto';
157 var transform = '';
158 switch(pos.horizontal.ctor) {
159 case 'P': e.style.right = toPos(pos.x); e.style.removeProperty('left'); break;
160 case 'Z': transform = 'translateX(' + ((-w/2)|0) + 'px) ';
161 case 'N': e.style.left = toPos(pos.x); e.style.removeProperty('right'); break;
162 }
163 switch(pos.vertical.ctor) {
164 case 'N': e.style.bottom = toPos(pos.y); e.style.removeProperty('top'); break;
165 case 'Z': transform += 'translateY(' + ((-h/2)|0) + 'px)';
166 case 'P': e.style.top = toPos(pos.y); e.style.removeProperty('bottom'); break;
167 }
168 if (transform !== '') addTransform(e.style, transform);
169 return e;
172function container(pos,elem) {
173 var e = render(elem);
174 setPos(pos, elem.props.width, elem.props.height, e);
175 var div = newElement('div');
176 div.style.position = 'relative';
177 div.style.overflow = 'hidden';
178 div.appendChild(e);
179 return div;
182function rawHtml(elem) {
183 var html = elem.html;
184 var args = elem.args;
185 var guid = elem.guid;
186 var align = elem.align;
188 var div = newElement('div');
189 div.innerHTML = html;
190 div.style.visibility = "hidden";
191 if (align) div.style.textAlign = align;
192 document.body.appendChild(div);
194 for (var i = args.length; i--; ) {
195 var arg = args[i];
196 var span = document.getElementById('md-' + guid + '-' + i);
197 if (arg.isText) {
198 span.innerHTML = arg;
199 } else {
200 span.style.display = 'block';
201 span.style.width = arg.props.width + 'px';
202 span.style.height = arg.props.height + 'px';
203 span.appendChild(render(arg));
204 }
205 }
206 document.body.removeChild(div);
207 div.style.visibility = 'visible';
208 return div;
211function render(elem) { return setProps(elem, makeElement(elem)); }
212function makeElement(e) {
213 var elem = e.element;
214 switch(elem.ctor) {
215 case 'Image': return image(e.props, elem);
216 case 'Flow': return flow(elem._0, elem._1);
217 case 'Container': return container(elem._0, elem._1);
218 case 'Spacer': return newElement('div');
219 case 'RawHtml': return rawHtml(elem);
220 case 'Custom': return elem.render(elem.model);
221 }
224function update(node, curr, next) {
225 if (node.tagName === 'A') { node = node.firstChild; }
226 if (curr.props.id === next.props.id) return updateProps(node, curr, next);
227 if (curr.element.ctor !== next.element.ctor) {
228 node.parentNode.replaceChild(render(next),node);
229 return true;
230 }
231 var nextE = next.element, currE = curr.element;
232 switch(nextE.ctor) {
233 case "Spacer": break;
234 case "RawHtml":
235 // only markdown blocks have guids, so this must be a text block
236 if (nextE.guid === null) {
237 if(currE.html.valueOf() !== nextE.html.valueOf()) {
238 node.innerHTML = nextE.html;
239 }
240 break;
241 }
242 if (nextE.guid !== currE.guid) {
243 node.parentNode.replaceChild(render(next),node);
244 return true;
245 }
246 var nargs = nextE.args;
247 var cargs = currE.args;
248 for (var i = nargs.length; i--; ) {
249 var narg = nargs[i];
250 var carg = cargs[i]
251 if (narg == carg) continue;
252 var span = document.getElementById('md-' + currE.guid + '-' + i);
253 if (narg.isElement) {
254 if (carg.isElement) {
255 update(span, carg, narg);
256 } else {
257 span.style.display = 'block';
258 var e = render(narg);
259 span.innerHTML = '';
260 span.appendChild(e);
261 }
262 } else {
263 span.style.display = 'inline';
264 span.innerHTML = narg;
265 }
266 }
267 break;
268 case "Image":
269 if (nextE._0.ctor === 'Plain') {
270 if (nextE._3 !== currE._3) node.src = nextE._3;
271 } else if (!eq(nextE,currE) ||
272 next.props.width !== curr.props.width ||
273 next.props.height !== curr.props.height) {
274 node.parentNode.replaceChild(render(next),node);
275 return true;
276 }
277 break;
278 case "Flow":
279 var arr = fromList(nextE._1);
280 for (var i = arr.length; i--; ) { arr[i] = arr[i].element.ctor; }
281 if (nextE._0.ctor !== currE._0.ctor) {
282 node.parentNode.replaceChild(render(next),node);
283 return true;
284 }
285 var nexts = fromList(nextE._1);
286 var kids = node.childNodes;
287 if (nexts.length !== kids.length) {
288 node.parentNode.replaceChild(render(next),node);
289 return true;
290 }
291 var currs = fromList(currE._1);
292 var goDir = function(x) { return x; };
293 switch(nextE._0.ctor) {
294 case "DDown": case "DUp": goDir = goDown; break;
295 case "DRight": case "DLeft": goDir = goRight; break;
296 case "DOut": case "DIn": goDir = goIn; break;
297 }
298 for (var i = kids.length; i-- ;) {
299 update(kids[i],currs[i],nexts[i]);
300 goDir(kids[i]);
301 }
302 break;
303 case "Container":
304 update(node.firstChild, currE._1, nextE._1);
305 setPos(nextE._0, nextE._1.props.width, nextE._1.props.height, node.firstChild);
306 break;
307 case "Custom":
308 if (currE.type === nextE.type) {
309 var done = nextE.update(node, currE.model, nextE.model);
310 if (done) return;
311 } else {
312 return node.parentNode.replaceChild(render(next), node);
313 }
314 }
315 updateProps(node, curr, next);
318function updateProps(node, curr, next) {
319 var props = next.props;
320 var currP = curr.props;
321 var e = node;
322 var element = next.element;
323 if (element.adjustWidth) {
324 props.width -= element.adjustWidth;
325 }
326 if (element.adjustHeight) {
327 props.height -= element.adjustHeight;
328 }
329 if (props.width !== currP.width) e.style.width = (props.width |0) + 'px';
330 if (props.height !== currP.height) e.style.height = (props.height|0) + 'px';
331 if (props.opacity !== 1 && props.opacity !== currP.opacity) {
332 e.style.opacity = props.opacity;
333 }
334 var nextColor = (props.color.ctor === 'Just' ?
335 extract(props.color._0) : '');
336 if (e.style.backgroundColor !== nextColor) {
337 e.style.backgroundColor = (nextColor === '' ? 'transparent' : nextColor);
338 }
339 if (props.tag !== currP.tag) { e.id = props.tag; }
340 if (props.href !== currP.href) {
341 if (currP.href === '') {
342 var a = newElement('a');
343 a.href = props.href;
344 a.style.width = '100%';
345 a.style.height = '100%';
346 a.style.top = 0;
347 a.style.left = 0;
348 a.style.display = 'block';
349 a.style.position = 'absolute';
350 e.style.position = 'relative';
351 e.appendChild(a);
352 } else {
353 node.lastNode.href = props.href;
354 }
355 }
357 // update hover handlers
358 if (props.hover.ctor !== '_Tuple0') {
359 e.elm_hover_handler = props.hover;
360 } else if (e.elm_hover_handler) {
361 e.elm_hover_handler = null;
362 }
364 // update click handlers
365 if (props.click.ctor !== '_Tuple0') {
366 e.elm_click_handler = props.click;
367 } else if (e.elm_click_handler) {
368 e.elm_click_handler = null;
369 }
372return { render:render, update:update };
374};