on df8df36 help sign in
elk/runtime/Render/Element.js
317 lines 10 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 var overCount = 0;
32 e.addEventListener('mouseover', function() {
33 if (overCount++ > 0) return;
34 props.hover(true);
35 });
36 e.addEventListener('mouseout', function(evt) {
37 if (e.contains(evt.toElement || evt.relatedTarget)) return;
38 overCount = 0;
39 props.hover(false);
40 });
41 }
42 return e;
45function image(props, img) {
46 switch (img._0.ctor) {
47 case 'Plain': return plainImage(img._3);
48 case 'Fitted': return fittedImage(props.width, props.height, img._3);
49 case 'Cropped': return croppedImage(img,props.width,props.height,img._3);
50 case 'Tiled': return tiledImage(img._3);
51 }
54function plainImage(src) {
55 var img = newElement('img');
56 img.src = src;
57 img.name = src;
58 img.style.display = "block";
59 return img;
62function tiledImage(src) {
63 var div = newElement('div');
64 div.style.backgroundImage = 'url(' + src + ')';
65 return div;
68function fittedImage(w, h, src) {
69 var div = newElement('div');
70 div.style.background = 'url(' + src + ') no-repeat center';
71 div.style.webkitBackgroundSize = 'cover';
72 div.style.MozBackgroundSize = 'cover';
73 div.style.OBackgroundSize = 'cover';
74 div.style.backgroundSize = 'cover';
75 return div;
78function croppedImage(elem, w, h, src) {
79 var pos = elem._0._0;
80 var e = newElement('div');
81 e.style.overflow = "hidden";
83 var img = newElement('img');
84 img.onload = function() {
85 var sw = w / elem._1, sh = h / elem._2;
86 img.style.width = ((this.width * sw)|0) + 'px';
87 img.style.height = ((this.height * sh)|0) + 'px';
88 img.style.marginLeft = ((- pos._0 * sw)|0) + 'px';
89 img.style.marginTop = ((- pos._1 * sh)|0) + 'px';
90 };
91 img.src = src;
92 img.name = src;
93 e.appendChild(img);
94 return e;
97function goIn(e) { e.style.position = 'absolute'; return e; }
98function goDown(e) { return e }
99function goRight(e) { e.style.styleFloat = e.style.cssFloat = "left"; return e; }
100function flowWith(f, array) {
101 var container = newElement('div');
102 for (var i = array.length; i--; ) {
103 container.appendChild(f(render(array[i])));
104 }
105 return container;
108function flow(dir,elist) {
109 var array = fromList(elist);
110 switch(dir.ctor) {
111 case "DDown": array.reverse();
112 case "DUp": return flowWith(goDown,array);
113 case "DRight": array.reverse();
114 case "DLeft": return flowWith(goRight,array);
115 case "DOut": array.reverse();
116 case "DIn": return flowWith(goIn,array);
117 }
120function toPos(pos) {
121 switch(pos.ctor) {
122 case "Absolute": return pos._0 + "px";
123 case "Relative": return (pos._0 * 100) + "%";
124 }
127// must clear right, left, top, bottom, and transform
128// before calling this function
129function setPos(pos,w,h,e) {
130 e.style.position = 'absolute';
131 e.style.margin = 'auto';
132 var transform = '';
133 switch(pos.horizontal.ctor) {
134 case 'P': e.style.right = toPos(pos.x); e.style.removeProperty('left'); break;
135 case 'Z': transform = 'translateX(' + ((-w/2)|0) + 'px) ';
136 case 'N': e.style.left = toPos(pos.x); e.style.removeProperty('right'); break;
137 }
138 switch(pos.vertical.ctor) {
139 case 'N': e.style.bottom = toPos(pos.y); e.style.removeProperty('top'); break;
140 case 'Z': transform += 'translateY(' + ((-h/2)|0) + 'px)';
141 case 'P': e.style.top = toPos(pos.y); e.style.removeProperty('bottom'); break;
142 }
143 if (transform !== '') addTransform(e.style, transform);
144 return e;
147function container(pos,elem) {
148 var e = render(elem);
149 setPos(pos, elem.props.width, elem.props.height, e);
150 var div = newElement('div');
151 div.style.position = 'relative';
152 div.style.overflow = 'hidden';
153 div.appendChild(e);
154 return div;
157function rawHtml(elem) {
158 var html = elem.html;
159 var args = elem.args;
160 var guid = elem.guid;
162 var div = newElement('div');
163 div.innerHTML = html;
164 div.style.visibility = "hidden";
165 document.body.appendChild(div);
167 for (var i = args.length; i--; ) {
168 var arg = args[i];
169 var span = document.getElementById('md-' + guid + '-' + i);
170 if (arg.isText) {
171 span.innerHTML = arg;
172 } else {
173 span.style.display = 'block';
174 span.style.width = arg.props.width + 'px';
175 span.style.height = arg.props.height + 'px';
176 span.appendChild(render(arg));
177 }
178 }
179 document.body.removeChild(div);
180 div.style.visibility = 'visible';
181 return div;
184function render(elem) { return setProps(elem.props, makeElement(elem)); }
185function makeElement(e) {
186 var elem = e.element;
187 switch(elem.ctor) {
188 case 'Image': return image(e.props, elem);
189 case 'Flow': return flow(elem._0, elem._1);
190 case 'Container': return container(elem._0, elem._1);
191 case 'Spacer': return newElement('div');
192 case 'RawHtml': return rawHtml(elem);
193 case 'Custom': return elem.render(elem.model);
194 }
197function update(node, curr, next) {
198 if (node.tagName === 'A') { node = node.firstChild; }
199 if (curr.props.id === next.props.id) return updateProps(node, curr, next);
200 if (curr.element.ctor !== next.element.ctor) {
201 node.parentNode.replaceChild(render(next),node);
202 return true;
203 }
204 var nextE = next.element, currE = curr.element;
205 switch(nextE.ctor) {
206 case "Spacer": break;
207 case "RawHtml":
208 if (nextE.guid !== currE.guid) {
209 node.parentNode.replaceChild(render(next),node);
210 return true;
211 }
212 var nargs = nextE.args;
213 var cargs = currE.args;
214 for (var i = nargs.length; i--; ) {
215 var narg = nargs[i];
216 var carg = cargs[i]
217 if (narg == carg) continue;
218 var span = document.getElementById('md-' + currE.guid + '-' + i);
219 if (narg.isElement) {
220 if (carg.isElement) {
221 update(span, carg, narg);
222 } else {
223 span.style.display = 'block';
224 var e = render(narg);
225 span.innerHTML = '';
226 span.appendChild(e);
227 }
228 } else {
229 span.style.display = 'inline';
230 span.innerHTML = narg;
231 }
232 }
233 break;
234 case "Image":
235 if (nextE._0.ctor === 'Plain') {
236 if (nextE._3 !== currE._3) node.src = nextE._3;
237 } else if (!eq(nextE,currE) ||
238 next.props.width !== curr.props.width ||
239 next.props.height !== curr.props.height) {
240 node.parentNode.replaceChild(render(next),node);
241 return true;
242 }
243 break;
244 case "Flow":
245 var arr = fromList(nextE._1);
246 for (var i = arr.length; i--; ) { arr[i] = arr[i].element.ctor; }
247 if (nextE._0.ctor !== currE._0.ctor) {
248 node.parentNode.replaceChild(render(next),node);
249 return true;
250 }
251 var nexts = fromList(nextE._1);
252 var kids = node.childNodes;
253 if (nexts.length !== kids.length) {
254 node.parentNode.replaceChild(render(next),node);
255 return true;
256 }
257 var currs = fromList(currE._1);
258 var goDir = function(x) { return x; };
259 switch(nextE._0.ctor) {
260 case "DDown": case "DUp": goDir = goDown; break;
261 case "DRight": case "DLeft": goDir = goRight; break;
262 case "DOut": case "DIn": goDir = goIn; break;
263 }
264 for (var i = kids.length; i-- ;) {
265 update(kids[i],currs[i],nexts[i]);
266 goDir(kids[i]);
267 }
268 break;
269 case "Container":
270 update(node.firstChild, currE._1, nextE._1)
271 setPos(nextE._0, nextE._1.props.width, nextE._1.props.height, node.firstChild);
272 break;
273 case "Custom":
274 if (currE.type === nextE.type) {
275 var done = nextE.update(node, currE.model, nextE.model);
276 if (done) return;
277 } else {
278 return node.parentNode.replaceChild(render(next), node);
279 }
280 }
281 updateProps(node, curr, next);
284function updateProps(node, curr, next) {
285 var props = next.props, currP = curr.props, e = node;
286 if (props.width !== currP.width) e.style.width = (props.width |0) + 'px';
287 if (props.height !== currP.height) e.style.height = (props.height|0) + 'px';
288 if (props.opacity !== 1 && props.opacity !== currP.opacity) {
289 e.style.opacity = props.opacity;
290 }
291 var nextColor = (props.color.ctor === 'Just' ?
292 extract(props.color._0) : '');
293 if (e.style.backgroundColor !== nextColor) {
294 e.style.backgroundColor = (nextColor === '' ? 'transparent' : nextColor);
295 }
296 if (props.tag !== currP.tag) { e.id = props.tag; }
297 if (props.href !== currP.href) {
298 if (currP.href === '') {
299 var a = newElement('a');
300 a.href = props.href;
301 a.style.width = '100%';
302 a.style.height = '100%';
303 a.style.top = 0;
304 a.style.left = 0;
305 a.style.display = 'block';
306 a.style.position = 'absolute';
307 e.style.position = 'relative';
308 e.appendChild(a);
309 } else {
310 node.lastNode.href = props.href;
311 }
312 }
315return { render:render, update:update };
317};