-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.coffee
executable file
·380 lines (279 loc) · 7.93 KB
/
base.coffee
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
{ $, toRedraw, redrawing, hazeling, ccss, css, vdom, contents, renderable, div } = require('./hazel.coffee')
{ fromHTML, create, diff, patch } = vdom
{ relative } = css
# Kaffa = require('../kaffa/dist/kaffa.js')
Kaffa = require('../kaffa/kaffa.coffee')
{ Class } = Kaffa
_isEvent = (name) ->
name.startsWith('@')
_getMixinKeys = (proto, name) ->
r = []
for k, v of proto
if k == name or k.startsWith(name + '.')
r.push(k)
return r
_getAttributes = (view, proto) ->
ps = proto._superclass
sv = if ps? then _getAttributes(view, ps) else []
v = []
for k in _getMixinKeys(proto, 'layout')
mv = if proto[k].attributes? then proto[k].attributes else []
v = _.union(mv, v)
_.union(sv, v)
_getStyle = (view, proto) ->
ps = proto._superclass
sv = if ps? then _getStyle(view, ps) else {}
v = {}
for k in _getMixinKeys(proto, 'layout')
mv = if proto[k].style? then proto[k].style.call(view) else {}
_.deepExtend(v, mv)
_.deepExtend({}, sv, v)
_getTemplate = (view, proto, args...) ->
# ps = proto._superclass
t = if proto.layout?.template? then proto.layout.template else null
# if ps?.layout?.template?
# ps.layout.template.call(view, t, args...)
# else if t?
if t?
t.call(view, args...)
else
(renderable -> span "").call(view, args...)
# string to type
_deserializeAttributeValue = (value) ->
if !value?
return false
try
i = parseFloat(value)
if !_.isNaN(i)
return i
catch e
return if value.toLowerCase() in ['true', 'false'] then value.toLowerCase() == 'true' else if value == '' then true else value
# type to string
_serializeAttributeValue = (value) ->
if !value?
return ""
if _.isBoolean(value)
return (if value then 'true' else 'false')
else if _.isString(value)
if value == ''
return 'false'
else
return value
else if value.toString?
return value.toString()
else
return ""
_setVProperties = (v) ->
if v.children?
_setVProperties(c) for c in v.children
if v.properties?
if !v.properties.attributes?
v.properties.attributes = {}
for key, value of v.properties
if !(key in ['dataset', 'id', 'class'])
v.properties.attributes[key] = value
delete v.properties[key]
BaseView = Class 'BaseView',
extends: HTMLElement
layout:
attributes: []
style: ->
':host':
position: relative
display: 'inline-block'
cursor: 'default'
createdCallback: ->
@_properties = []
for k, v of @
if k.startsWith('$') and !_.isFunction(v)
nk = k.substr(1)
@[nk] = @[k]
@_properties.push(nk)
root = @createShadowRoot()
@noTemplate = false
@isReady = false
@isAttached = false
@_vdom = null
@_vdom_style = null
@_observers = []
@$ = $(@)
@[0] = @
@length = 1
@cash = true
if @created?
@created()
@_prepare()
_bindInputs: ->
that = @
@$.find(':root /deep/ input').each((el) ->
el = $(el)
path = el.prop('bind')
if path? and _.valueForKeyPath(that, path)?
if !el.attr('type')?
el.attr('type', 'text')
switch el.attr('type').toLowerCase()
when 'checkbox'
el.on('change', (e) ->
_.setValueForKeyPath(that, path, el[0].checked)
)
el[0].checked = _.valueForKeyPath(that, path)
when 'radio'
el.on('change', (e) ->
_.setValueForKeyPath(that, path, el[0].checked)
)
el[0].checked = _.valueForKeyPath(that, path)
else
el.on('keyup', (e) ->
_.setValueForKeyPath(that, path, el[0].value)
)
el[0].value = _.valueForKeyPath(that, path)
)
_removeEvents: ->
@$.eachDeep((el) ->
$(el).off()
)
_createEvents: ->
for k, v of @__proto__
if _isEvent(k)
kk = k
k = k.substr(1)
p = k.split(' ')
if p.length > 1
eventType = _.first(p)
selector = _.rest(p).join(' ')
els = @$.find(':root /deep/ ' + selector.trim())
else
eventType = k.trim()
selector = null
els = @$
if v?
els.on(eventType, v)
else
els.off(eventType)
_observeProperty: (name) ->
o = Kaffa.observe(@, name, ((args) -> @refresh()))
o._el = @
@_observers.push(o)
_createIds: ->
that = @
@$.eachDeep((el) ->
if !_.isEmpty(el.id)
that[_.camelize(el.id) + '$'] = el
)
_propertiesToAttributes: ->
for key in _getAttributes(@, @__proto__)
if !key.startsWith('on-') and @[key]?
@setAttribute(key, _serializeAttributeValue(@[key]))
@_observeProperty(key)
_attributesToProperties: ->
for key in _getAttributes(@, @__proto__)
if !key.startsWith('on-') and @hasAttribute(key)
# if !@hasAttribute(key)
# @setAttribute(key, _serializeAttributeValue(@[key]))
if !@[key]?
# @[key] = null
@[key] = _deserializeAttributeValue(@getAttribute(key))
@_observeProperty(key)
_attributesToEvents: ->
for i in [[email protected]]
key = @attributes[i].name
value = @attributes[i].value
if key.startsWith('on-')
if !value?
@$.off(key.substr(3))
else if _.isFunction(value)
@$.on(key.substr(3), value)
else if _.isString(value)
if @[value]? and _.isFunction(@[value])
@$.on(key.substr(3), @[value])
else
@$.on(key.substr(3), new Function(['event'], value))
_prepare: ->
@_dom()
if @_el_style?
@shadowRoot.appendChild(@_el_style)
if @_el?
@shadowRoot.appendChild(@_el)
@_propertiesToAttributes()
attachedCallback: ->
@_propertiesToAttributes()
@_attributesToProperties()
if @ready?
@ready()
@isReady = true
@_removeEvents()
@_bindInputs()
@_attributesToEvents()
@_createEvents()
@_createIds()
for k in @_properties
@_observeProperty(k)
@redraw()
if @attached?
@attached()
# @refresh()
@isAttached = true
detachedCallback: ->
@_removeEvents()
for e in @_observers
e.close()
Kaffa.observers.splice(Kaffa.observers.indexOf(e), 1)
@_observers = []
if @detached?
@detached()
@isAttached = false
attributeChangedCallback: (name, oldValue, newValue) ->
# console.log "attributeChanged:", "#{@tagName.toLowerCase()}#{if !_.isEmpty(@id) then '#' + @id else ''}#{if !_.isEmpty(@className) then '.' + @className else ''}", name, oldValue, '->', newValue
if @isAttached
@refresh()
_dom: ->
return if hazeling()
try
i = parseInt(@textContent)
catch
i = NaN
if !_.isNaN(i)
content = contents[i]
else
content = null
st = '<style>' + ccss.compile(_getStyle(@, @__proto__)) + '</style>'
vs = fromHTML(st)
if !@_vdom_style?
@_el_style = create(vs)
else
patches = diff(@_vdom_style, vs);
@_el_style = patch(@_el_style, patches);
@_vdom_style = vs;
if @noTemplate == false
s = _getTemplate(@, @__proto__, content)
if _.isEmpty(s)
s = '<div></div>'
v = fromHTML(s)
if !@_vdom?
@_el = create(v)
else
patches = diff(@_vdom, v);
@_el = patch(@_el, patches);
@_vdom = v;
if @updated?
@updated()
redraw: ->
@_dom()
if _.contains(toRedraw, @)
_.remove(toRedraw, @)
# console.log "redraw", @
needsRedraw: ->
return _.contains(toRedraw, @)
refresh: ->
# console.log "refresh", @, toRedraw
if !_.contains(toRedraw, @)
toRedraw.push(@)
created: ->
ready: ->
attached: ->
detached: ->
updated: ->
for k of $.fn
if !(k in ['length', 'cash', 'init', 'extend']) and !BaseView.prototype[k]?
BaseView.prototype[k] = ( (fn) -> (args...) -> fn.call(@$, args...))($.fn[k])
module.exports.BaseView = BaseView