2ElmRuntime.Render.Element = function() {
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);
17 if (props.tag !== '') { e.id = props.tag; }
18 if (props.href !== '') {
19 var a = newElement('a');
24 if (props.hover.ctor !== 'Tuple0') {
26 e.addEventListener('mouseover', function() {
27 if (overCount++ > 0) return;
30 e.addEventListener('mouseout', function(evt) {
31 if (e.contains(evt.toElement || evt.relatedTarget)) return;
39function image(props, img) {
40 switch (img._0.ctor) {
41 case 'Plain': return plainImage(img._3);
42 case 'Fitted': return fittedImage(props.width, props.height, img._3);
43 case 'Cropped': return croppedImage(img,props.width,props.height,img._3);
44 case 'Tiled': return tiledImage(img._3);
48function plainImage(src) {
49 var img = newElement('img');
52 img.style.display = "block";
56function tiledImage(src) {
57 var div = newElement('div');
58 div.style.backgroundImage = 'url(' + src + ')';
62function fittedImage(w, h, src) {
63 var div = newElement('div');
64 div.style.background = 'url(' + src + ') no-repeat center';
65 div.style.webkitBackgroundSize = 'cover';
66 div.style.MozBackgroundSize = 'cover';
67 div.style.OBackgroundSize = 'cover';
68 div.style.backgroundSize = 'cover';
72function croppedImage(elem, w, h, src) {
74 var e = newElement('div');
75 e.style.overflow = "hidden";
77 var img = newElement('img');
78 img.onload = function() {
79 var sw = w / elem._1, sh = h / elem._2;
80 img.style.width = ((this.width * sw)|0) + 'px';
81 img.style.height = ((this.height * sh)|0) + 'px';
82 img.style.marginLeft = ((- pos._0 * sw)|0) + 'px';
83 img.style.marginTop = ((- pos._1 * sh)|0) + 'px';
91function goIn(e) { e.style.position = 'absolute'; return e; }
92function goDown(e) { return e }
93function goRight(e) { e.style.styleFloat = e.style.cssFloat = "left"; return e; }
94function flowWith(f, array) {
95 var container = newElement('div');
96 for (var i = array.length; i--; ) {
97 container.appendChild(f(render(array[i])));
102function flow(dir,elist) {
103 var array = fromList(elist);
105 case "DDown": array.reverse();
106 case "DUp": return flowWith(goDown,array);
107 case "DRight": array.reverse();
108 case "DLeft": return flowWith(goRight,array);
109 case "DOut": array.reverse();
110 case "DIn": return flowWith(goIn,array);
116 case "Absolute": return pos._0 + "px";
117 case "Relative": return (pos._0 * 100) + "%";
121function setPos(pos,w,h,e) {
122 e.style.position = 'absolute';
123 e.style.margin = 'auto';
125 switch(pos.horizontal.ctor) {
126 case 'P': e.style.right = toPos(pos.x); break;
127 case 'Z': transform = 'translateX(' + ((-w/2)|0) + 'px) ';
128 case 'N': e.style.left = toPos(pos.x); break;
130 switch(pos.vertical.ctor) {
131 case 'N': e.style.bottom = toPos(pos.y); break;
132 case 'Z': transform += 'translateY(' + ((-h/2)|0) + 'px)';
133 case 'P': e.style.top = toPos(pos.y); break;
135 if (transform !== '') addTransform(e.style, transform);
139function container(pos,elem) {
140 var e = render(elem);
141 setPos(pos, elem.props.width, elem.props.height, e);
142 var div = newElement('div');
143 div.style.position = 'relative';
144 div.style.overflow = 'hidden';
149function rawHtml(html) {
150 var e = newElement('div');
155function render(elem) { return setProps(elem.props, makeElement(elem)); }
156function makeElement(e) {
157 var elem = e.element;
159 case 'Image': return image(e.props, elem);
160 case 'Flow': return flow(elem._0, elem._1);
161 case 'Container': return container(elem._0, elem._1);
162 case 'Spacer': return newElement('div');
163 case 'RawHtml': return rawHtml(elem._0);
164 case 'Custom': return elem.render(elem.model);
168function update(node, curr, next) {
169 if (node.tagName === 'A') { node = node.firstChild; }
170 if (curr.props.id === next.props.id) return updateProps(node, curr, next);
171 if (curr.element.ctor !== next.element.ctor) {
172 node.parentNode.replaceChild(render(next),node);
175 var nextE = next.element, currE = curr.element;
177 case "Spacer": break;
179 if (nextE._0 !== currE._0) node.innerHTML = nextE._0;
182 if (nextE._0.ctor === 'Plain') {
183 if (nextE._3 !== currE._3) node.src = nextE._3;
184 } else if (!eq(nextE,currE) ||
185 next.props.width !== curr.props.width ||
186 next.props.height !== curr.props.height) {
187 node.parentNode.replaceChild(render(next),node);
192 var arr = fromList(nextE._1);
193 for (var i = arr.length; i--; ) { arr[i] = arr[i].element.ctor; }
194 if (nextE._0.ctor !== currE._0.ctor) {
195 node.parentNode.replaceChild(render(next),node);
198 var nexts = fromList(nextE._1);
199 var kids = node.childNodes;
200 if (nexts.length !== kids.length) {
201 node.parentNode.replaceChild(render(next),node);
204 var currs = fromList(currE._1);
205 var goDir = function(x) { return x; };
206 switch(nextE._0.ctor) {
207 case "DDown": case "DUp": goDir = goDown; break;
208 case "DRight": case "DLeft": goDir = goRight; break;
209 case "DOut": case "DIn": goDir = goIn; break;
211 for (var i = kids.length; i-- ;) {
212 update(kids[i],currs[i],nexts[i]);
217 var inner = node.firstChild;
218 if (!update(inner, currE._1, nextE._1)) {
219 if (nextE._0.horizontal.ctor !== currE._0.horizontal.ctor) {
220 inner.style.left = inner.style.right = 'none';
221 removeTransform(inner.style);
223 if (nextE._0.vertical.ctor !== currE._0.vertical.ctor) {
224 inner.style.top = inner.style.bottom = 'none';
225 removeTransform(inner.style);
228 setPos(nextE._0, nextE._1.props.width, nextE._1.props.height, inner);
231 if (currE.type === nextE.type) {
232 var done = nextE.update(node, currE.model, nextE.model);
235 return node.parentNode.replaceChild(render(next), node);
238 updateProps(node, curr, next);
241function updateProps(node, curr, next) {
242 var props = next.props, currP = curr.props, e = node;
243 if (props.width !== currP.width) e.style.width = (props.width |0) + 'px';
244 if (props.height !== currP.height) e.style.height = (props.height|0) + 'px';
245 if (props.opacity !== 1 && props.opacity !== currP.opacity) {
246 e.style.opacity = props.opacity;
248 var nextColor = (props.color.ctor === 'Just' ?
249 extract(props.color._0) : 'transparent');
250 if (e.style.backgroundColor !== nextColor) {
251 e.style.backgroundColor = nextColor;
253 if (props.tag !== currP.tag) { e.id = props.tag; }
254 if (props.href !== currP.href) {
255 if (currP.href === '') {
256 var a = newElement('a');
259 e.parentNode.replaceChild(a,e);
261 node.parentNode.href = props.href;
266return { render:render, update:update };
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list