forked from evancz/elm-html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapper.js
405 lines (340 loc) · 10.9 KB
/
wrapper.js
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
var VNode = require('vtree/vnode');
var VText = require('vtree/vtext');
var diff = require('virtual-dom/diff');
var patch = require('virtual-dom/patch');
var createElement = require('virtual-dom/create-element');
var DataSet = require("data-set");
var Delegator = require("dom-delegator");
var isHook = require("vtree/is-vhook");
Elm.Native.Html = {};
Elm.Native.Html.make = function(elm) {
elm.Native = elm.Native || {};
elm.Native.Html = elm.Native.Html || {};
if (elm.Native.Html.values) return elm.Native.Html.values;
if ('values' in Elm.Native.Html)
return elm.Native.Html.values = Elm.Native.Html.values;
// This manages event listeners. Somehow...
var delegator = Delegator();
var RenderUtils = ElmRuntime.use(ElmRuntime.Render.Utils);
var newElement = Elm.Graphics.Element.make(elm).newElement;
var Utils = Elm.Native.Utils.make(elm);
var List = Elm.Native.List.make(elm);
var Maybe = Elm.Maybe.make(elm);
var eq = Elm.Native.Utils.make(elm).eq;
function createNode(tag, attrs, children) {
var key, namespace
// support keys
if ("key" in attrs) {
key = attrs.key
attrs.key = undefined
}
// support namespace
if ("namespace" in attrs) {
namespace = attrs.namespace
attrs.namespace = undefined
}
return new VNode(tag, attrs, children, key, namespace);
}
function listToObject(list) {
var object = {};
while (list.ctor !== '[]') {
var entry = list._0;
object[entry.key] = entry.value;
list = list._1;
}
return object;
}
function node(name, attributes, contents) {
var attrs = listToObject(attributes);
// ensure that setting text of an input does not move the cursor
var useSoftSet =
name === 'input'
&& 'value' in attrs
&& attrs.value !== undefined
&& !isHook(attrs.value);
if (useSoftSet) {
attrs.value = SoftSetHook(attrs.value);
}
return new createNode(name, attrs, List.toArray(contents));
}
var NORMAL_ATTRS_FOR_SVG = {
"style": true,
"namespace": true,
"key": true
}
var SVG_NAMESPACE = "http://www.w3.org/2000/svg"
function svg(name, attributes, contents) {
var attrs = listToObject(attributes);
attrs.namespace = SVG_NAMESPACE;
for (var key in attrs) {
if (!attrs.hasOwnProperty(key)) {
continue;
}
if (NORMAL_ATTRS_FOR_SVG[key]) {
continue;
}
// Is this needed for elm?
// var value = attrs[key]
// if (typeof value !== "string" &&
// typeof value !== "number" &&
// typeof value !== "boolean"
// ) {
// continue;
// }
attrs[key] = AttributeHook(attrs[key])
}
return new createNode(name, attrs, List.toArray(contents));
}
function pair(key, value) {
return {
key: key,
value: value
};
}
function style(properties) {
return pair('style', listToObject(properties));
}
function on(name, coerce) {
function createListener(handle, convert) {
delegator.listenTo(name);
function eventHandler(event) {
var value = coerce(event);
if (value.ctor === 'Just') {
elm.notify(handle.id, convert(value._0));
}
}
return pair(name, DataSetHook(eventHandler));
}
return F2(createListener);
}
function filterMap(f, getter) {
return function(event) {
var maybeValue = getter(event);
return maybeValue.ctor === 'Nothing' ? maybeValue : f(maybeValue._0);
};
}
function getMouseEvent(event) {
return !('button' in event) ?
Maybe.Nothing :
Maybe.Just({
_: {},
button: event.button,
altKey: event.altKey,
ctrlKey: event.ctrlKey,
metaKey: event.metaKey,
shiftKey: event.shiftKey
});
}
function getKeyboardEvent(event) {
return !('keyCode' in event) ?
Maybe.Nothing :
Maybe.Just({
_: {},
keyCode: event.keyCode,
altKey: event.altKey,
ctrlKey: event.ctrlKey,
metaKey: event.metaKey,
shiftKey: event.shiftKey
});
}
function getChecked(event) {
return 'checked' in event.target ?
Maybe.Just(event.target.checked) :
Maybe.Nothing;
}
function getValue(event) {
var node = event.target;
return 'value' in node ?
Maybe.Just(event.target.value) :
Maybe.Nothing;
}
function getValueAndSelection(event) {
var node = event.target;
return !('selectionStart' in node) ?
Maybe.Nothing :
Maybe.Just({
_: {},
value: node.value,
selection: {
start: node.selectionStart,
end: node.selectionEnd,
direction: {
ctor: node.selectionDirection === 'forward' ? 'Forward' : 'Backward'
}
}
});
}
function getAnything(event) {
return Maybe.Just(Utils._Tuple0);
}
function DataSetHook(value) {
if (!(this instanceof DataSetHook)) {
return new DataSetHook(value);
}
this.value = value;
}
DataSetHook.prototype.hook = function (node, propertyName) {
var ds = DataSet(node);
ds[propertyName] = this.value;
};
function SoftSetHook(value) {
if (!(this instanceof SoftSetHook)) {
return new SoftSetHook(value);
}
this.value = value;
}
SoftSetHook.prototype.hook = function (node, propertyName) {
if (node[propertyName] !== this.value) {
node[propertyName] = this.value;
}
};
function AttributeHook(value) {
if (!(this instanceof AttributeHook)) {
return new AttributeHook(value);
}
this.value = value;
}
AttributeHook.prototype.hook = function (node, prop, prev) {
if (prev && prev.value === this.value) {
return;
}
node.setAttributeNS(null, prop, this.value)
}
function text(string) {
return new VText(string);
}
function toElement(width, height, html) {
return A3(newElement, width, height,
{ ctor: 'Custom'
, type: 'evancz/elm-html'
, render: render
, update: update
, model: html
});
}
function render(model) {
var element = RenderUtils.newElement('div');
element.appendChild(createElement(model));
return element;
}
function update(node, oldModel, newModel) {
var patches = diff(oldModel, newModel);
var newNode = patch(node.firstChild, patches)
if (newNode !== node.firstChild) {
node.replaceChild(newNode, node.firstChild)
}
}
function lazyRef(fn, a) {
function thunk() {
return fn(a);
}
return new Thunk('ref', fn, [a], thunk, shouldUpdate_refEq);
}
function lazyRef2(fn, a, b) {
function thunk() {
return A2(fn, a, b);
}
return new Thunk('ref', fn, [a,b], thunk, shouldUpdate_refEq);
}
function lazyRef3(fn, a, b, c) {
function thunk() {
return A3(fn, a, b, c);
}
return new Thunk('ref', fn, [a,b,c], thunk, shouldUpdate_refEq);
}
function lazyStruct(fn, a) {
function thunk() {
return fn(a);
}
return new Thunk('struct', fn, [a], thunk, shouldUpdate_structEq);
}
function lazyStruct2(fn, a, b) {
function thunk() {
return A2(fn, a, b);
}
return new Thunk('struct', fn, [a,b], thunk, shouldUpdate_structEq);
}
function lazyStruct3(fn, a, b, c) {
function thunk() {
return A3(fn, a, b, c);
}
return new Thunk('struct', fn, [a,b,c], thunk, shouldUpdate_structEq);
}
function Thunk(kind, fn, args, thunk, shouldUpdate) {
this.fn = fn;
this.args = args;
this.vnode = null;
this.key = undefined;
this.thunk = thunk;
this.kind = kind;
this.shouldUpdate = shouldUpdate;
}
Thunk.prototype.type = "immutable-thunk";
Thunk.prototype.update = updateThunk;
Thunk.prototype.init = initThunk;
function shouldUpdate_refEq(current, previous) {
if (current.kind !== previous.kind || current.fn !== previous.fn) {
return true;
}
// if it's the same function, we know the number of args must match
var cargs = current.args;
var pargs = previous.args;
for (var i = cargs.length; i--; ) {
if (cargs[i] !== pargs[i]) {
return true;
}
}
return false;
}
function shouldUpdate_structEq(current, previous) {
if (current.kind !== previous.kind || current.fn !== previous.fn) {
return true;
}
// if it's the same function, we know the number of args must match
var cargs = current.args;
var pargs = previous.args;
for (var i = cargs.length; i--; ) {
if (eq(cargs[i], pargs[i])) {
return true;
}
}
return false;
}
function updateThunk(previous, domNode) {
if (!this.shouldUpdate(this, previous)) {
this.vnode = previous.vnode;
return;
}
if (!this.vnode) {
this.vnode = this.thunk();
}
var patches = diff(previous.vnode, this.vnode);
patch(domNode, patches);
}
function initThunk() {
this.vnode = this.thunk();
return createElement(this.vnode);
}
return Elm.Native.Html.values = {
node: F3(node),
svg: F3(svg),
text: text,
style: style,
on: F2(on),
pair: F2(pair),
getMouseEvent: getMouseEvent,
getKeyboardEvent: getKeyboardEvent,
getChecked: getChecked,
getValue: getValue,
getValueAndSelection: getValueAndSelection,
getAnything: getAnything,
filterMap: F2(filterMap),
lazyRef : F2(lazyRef ),
lazyRef2: F3(lazyRef2),
lazyRef3: F4(lazyRef3),
lazyStruct : F2(lazyStruct ),
lazyStruct2: F3(lazyStruct2),
lazyStruct3: F4(lazyStruct3),
toElement: F3(toElement)
};
};