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