-
Notifications
You must be signed in to change notification settings - Fork 44
/
core.coffee
503 lines (426 loc) · 13.9 KB
/
core.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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# init
alight.version = '{{{version}}}'
alight.debug =
scan: 0
directive: false
watch: false
watchText: false
parser: false
alight.directivePreprocessor = directivePreprocessor = (attrName, args) ->
# html prefix data
if attrName[0..4] is 'data-'
name = attrName[5..]
else
name = attrName
j = name.indexOf '-'
if j < 0
return { noNs: true }
ns = name.substring 0, j
name = name.substring(j+1).replace /(-\w)/g, (m) ->
m.substring(1).toUpperCase()
raw = null
if args.scope.$ns and args.scope.$ns.directives
path = args.scope.$ns.directives[ns]
if path
raw = path[name]
if not raw
if not args.scope.$ns.inheritGlobal
return { noDirective: true }
else
if not args.scope.$ns.inheritGlobal
return { noNs: true }
if not raw
path = alight.directives[ns]
if not path
return { noNs: true }
raw = path[name]
if not raw
return { noDirective: true }
dir = {}
if f$.isFunction raw
dir.init = raw
else if f$.isObject raw
for k, v of raw
dir[k] = v
else throw 'Wrong directive: ' + ns + '.' + name
dir.priority = raw.priority or 0
dir.restrict = raw.restrict or 'A'
if dir.restrict.indexOf(args.attr_type) < 0
throw 'Directive has wrong binding (attribute/element): ' + name
dir.$init = (element, expression, scope, env) ->
doProcess = ->
l = dscope.procLine
for dp, i in l
dp.fn.call dscope
if dscope.isDeferred
dscope.procLine = l[i+1..]
break
null
dscope =
element: element
expression: expression
scope: scope
env: env
ns: ns
name: name
args: args
directive: dir
result: {}
isDeferred: false
procLine: alight.hooks.directive
makeDeferred: ->
dscope.isDeferred = true
dscope.result.owner = true # stop binding
dscope.doBinding = true # continue binding
->
dscope.isDeferred = false
doProcess()
doProcess()
dscope.result
dir
do ->
directivePreprocessor.ext = ext = alight.hooks.directive
ext.push
code: 'init'
fn: ->
if @.directive.init
@.result = @.directive.init(@.element, @.expression, @.scope, @.env) or {}
if not f$.isObject(@.result)
@.result = {}
ext.push
code: 'templateUrl'
fn: ->
ds = @
if @.directive.templateUrl
callback = @.makeDeferred()
f$.ajax
cache: true
url: @.directive.templateUrl
success: (html) ->
ds.directive.template = html
callback()
error: callback
ext.push
code: 'template'
fn: ->
if @.directive.template
if @.element.nodeType is 1
f$.html @.element, @.directive.template
else if @.element.nodeType is 8
el = document.createElement 'p'
el.innerHTML = @.directive.template.trimLeft()
el = el.firstChild
f$.after @.element, el
@.element = el
if not @.directive.scope
@.directive.scope = true
ext.push
code: 'scope'
fn: ->
if @.directive.scope
parentScope = @.scope
if @.directive.scope is 'root'
@.scope = parentScope.$new 'root'
else
@.scope = parentScope.$new(@.directive.scope is 'isolate')
@.result.owner = true
@.doBinding = true
ext.push
code: 'link'
fn: ->
if @.directive.link
@.directive.link(@.element, @.expression, @.scope, @.env)
ext.push
code: 'scopeBinding'
fn: ->
if @.doBinding
alight.applyBindings @.scope, @.element, { skip_attr:@.env.skippedAttr() }
testDirective = do ->
addAttr = (attrName, args, base) ->
if args.attr_type is 'A'
attr = base or {}
attr.priority = -5
attr.is_attr = true
attr.name = attrName
attr.attrName = attrName
attr.element = args.element
args.list.push attr
(attrName, args) ->
if args.skip_attr.indexOf(attrName) >= 0
return addAttr attrName, args, { skip:true }
directive = alight.directivePreprocessor attrName, args
if directive.noNs
return addAttr attrName, args
if directive.noDirective
return addAttr attrName, args, { noDirective:true }
args.list.push
name: attrName
directive: directive
priority: directive.priority
attrName: attrName
sortByPriority = (a, b) ->
if a.priority == b.priority
return 0
if a.priority > b.priority
return -1
else
return 1
attrBinding = (element, value, scope, attrName) ->
text = value
if text.indexOf(alight.utils.pars_start_tag) < 0
return
setter = (result) ->
f$.attr element, attrName, result
'$scanNoChanges'
w = scope.$watchText text, setter
setter w.value
bindText = (scope, node) ->
text = node.data
if text.indexOf(alight.utils.pars_start_tag) < 0
return
setter = (result) ->
node.nodeValue = result
'$scanNoChanges'
w = scope.$watchText text, setter
setter w.value
bindComment = (scope, element) ->
text = element.nodeValue.trimLeft()
if text[0..9] isnt 'directive:'
return
text = text[10..].trimLeft()
i = text.indexOf ' '
if i >= 0
dirName = text[0..i-1]
value = text[i+1..]
else
dirName = text
value = ''
args =
list: list = []
element: element
attr_type: 'M'
scope: scope
skip_attr: []
testDirective dirName, args
d = list[0]
if d.noDirective
throw "Directive not found: #{d.name}"
directive = d.directive
env =
element: element
attrName: dirName
attributes: []
skippedAttr: ->
[]
if alight.debug.directive
console.log 'bind', d.attrName, value, d
try
result = directive.$init element, value, scope, env
if result and result.start
result.start()
catch e
alight.exceptionHandler e, 'Error in directive: ' + d.name,
value: value
env: env
scope: scope
element: element
bindElement = do ->
takeAttr = (name, skip) ->
if arguments.length is 1
skip = true
for attr in @.attributes
if attr.attrName isnt name
continue
if skip
attr.skip = true
value = f$.attr @.element, name
return value or true
skippedAttr = ->
for attr in @.attributes
if not attr.skip
continue
attr.attrName
(scope, element, config) ->
config = config || {}
skip_children = false
skip_attr = config.skip_attr or []
if not (skip_attr instanceof Array)
skip_attr = [skip_attr]
if !config.skip_top
args =
list: list = []
element: element
skip_attr: skip_attr
attr_type: 'E'
scope: scope
attrName = element.nodeName.toLowerCase()
testDirective attrName, args
args.attr_type = 'A'
attrs = f$.getAttributes element
for attrName, attr_value of attrs
testDirective attrName, args
# sort by priority
list = list.sort sortByPriority
for d in list
if d.skip
continue
if d.noDirective
throw "Directive not found: #{d.name}"
d.skip = true
value = f$.attr element, d.attrName
if d.is_attr
attrBinding element, value, scope, d.attrName
else
directive = d.directive
env =
element: element
attrName: d.attrName
attributes: list
takeAttr: takeAttr
skippedAttr: skippedAttr
if alight.debug.directive
console.log 'bind', d.attrName, value, d
try
result = directive.$init element, value, scope, env
if result and result.start
result.start()
catch e
alight.exceptionHandler e, 'Error in directive: ' + d.attrName,
value: value
env: env
scope: scope
element: element
if result and result.owner
skip_children = true
break
if !skip_children
# text bindings
for node in f$.childNodes element
if not node
continue
bindNode scope, node
null
nodeTypeBind =
1: bindElement # element
3: bindText # text
8: bindComment # comment
bindNode = (scope, node, option) ->
if alight.utils.getData node, 'skipBinding'
return
if alight.hooks.binding.length
for h in alight.hooks.binding
r = h.fn scope, node, option
if r and r.owner # take control
return
fn = nodeTypeBind[node.nodeType]
if fn
fn scope, node, option
alight.nextTick = do ->
timer = null
list = []
exec = ->
timer = null
dlist = list.slice()
list.length = 0
for it in dlist
callback = it[0]
self = it[1]
try
callback.call self
catch e
alight.exceptionHandler e, '$nextTick, error in function',
fn: callback
null
(callback) ->
list.push [callback, @]
if timer
return
timer = setTimeout exec, 0
alight.getController = (name, scope) ->
error = false
if scope.$ns and scope.$ns.controllers
ctrl = scope.$ns.controllers[name]
if not ctrl and not scope.$ns.inheritGlobal
error = true
if not ctrl and not error
ctrl = alight.controllers[name] or (enableGlobalControllers and window[name])
if not ctrl
throw 'Controller isn\'t found: ' + name
if not (ctrl instanceof Function)
throw 'Wrong controller: ' + name
ctrl
alight.getFilter = (name, scope, param) ->
error = false
if scope.$ns and scope.$ns.filters
filter = scope.$ns.filters[name]
if not filter and not scope.$ns.inheritGlobal
error = true
if not filter and not error
filter = alight.filters[name]
if not filter
throw 'Filter not found: ' + name
filter
alight.applyBindings = (scope, element, config) ->
if not element
throw 'No element'
if not scope
scope = alight.Scope()
root = scope.$system.root
finishBinding = not root.finishBinding_lock
if finishBinding
root.finishBinding_lock = true
config = config or {}
bindNode scope, element, config
if finishBinding
root.finishBinding_lock = false
lst = root.watchers.finishBinding.slice()
root.watchers.finishBinding.length = 0
for cb in lst
cb()
null
alight.bootstrap = (input) ->
if not input
input = f$.find document, '[al-app]'
if f$.isElement input
input = [input]
if f$.isArray(input) or typeof(input.length) is 'number'
for element in input
if element.ma_bootstrapped
continue
element.ma_bootstrapped = true
attr = f$.attr element, 'al-app'
if attr
if attr[0] is '#'
t = attr.split ' '
tag = t[0].substring(1)
ctrlName = t[1]
scope = alight.apps[tag]
if scope
if ctrlName
console.error "New controller on exists scope: al-app=\"#{attr}\""
else
alight.apps[tag] = scope = alight.Scope()
if ctrlName
ctrl = alight.getController ctrlName, scope
ctrl scope
else
scope = alight.Scope()
ctrl = alight.getController attr, scope
ctrl scope
else
scope = alight.Scope()
alight.applyBindings scope, element, { skip_attr: 'al-app' }
else
if f$.isObject(input) and input.$el
scope = alight.Scope
prototype: input
if f$.isElement input.$el
alight.applyBindings scope, input.$el
else
for el in f$.find(document.body, input.$el)
alight.applyBindings scope, el
return scope
else
alight.exceptionHandler 'Error in bootstrap', 'Error in bootstrap',
input: input
null