forked from cordjs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBehaviour.coffee
521 lines (429 loc) · 18.6 KB
/
Behaviour.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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
define [
'cord!errors'
'cord!Model'
'cord!utils/Defer'
'cord!utils/DomHelper'
'cord!utils/DomInfo'
'cord!utils/Future'
'cord!utils/profiler/profiler'
'cord!Module'
'jquery'
'postal'
], (errors, Model, Defer, DomHelper, DomInfo, Future, pr, Module, $, postal) ->
checkIsSentenced = (widget, message = '') ->
###
Utility DRY function to check if the given widget is sentenced and trow special exception about it.
@param {Widget} widget the checking widget
@param optional{String} message additional message to be included to the exception
@throws Error
###
if widget.isSentenced()
throw new errors.WidgetSentenced(
"Widget #{widget.debug()} is sentenced!#{ if message then " (#{message})" else ''}"
)
class Behaviour extends Module
# jQuery aggregate of all DOM-roots of the widget
# (widget can have multiple DOM-roots when it has several inline-blocks)
$rootEls: null
_widgetSubscriptions: null
_modelBindings: null
constructor: (widget, $domRoot) ->
###
@param Widget widget
@param (optional)jQuery $domRoot prepared root element of the widget or of some widget's parent
###
@_widgetSubscriptions = []
@_modelBindings = {}
@widget = widget
@id = widget.ctx.id
if $domRoot
if $domRoot.attr('id') == @id
@el = $domRoot
else
@el = $('#' + @id, $domRoot)
else
@el = $('#' + @id)
@$el = @el
@$rootEls = if @el.length == 1 then @el else $()
if widget.ctx[':inlines']?
@$rootEls = @$rootEls.add('#'+info.id, $domRoot) for inlineName, info of widget.ctx[':inlines']
@elements = @constructor.elements unless @elements
@elements = @elements() if _.isFunction @elements
@_elementSelectors = {} if @elements # needed to support '@element'-like selectors for events
@events = @constructor.events unless @events
@events = @events() if _.isFunction @events
@widgetEvents = @constructor.widgetEvents unless @widgetEvents
@widgetEvents = @widgetEvents() if _.isFunction @widgetEvents
@refreshElements() if @elements
@delegateEvents(@events) if @events
@initWidgetEvents(@widgetEvents) if @widgetEvents
@_callbacks = []
pr.timer "#{@constructor.name}::init", =>
@init()
if @show?
@widget.shown().done @getCallback =>
@show()
init: ->
destroy: ->
$: (selector) ->
###
Creates jQuery object with the given selector in the context of this widget.
Multiple root element of the widget (when there are several inlines) are also supported transparently.
@param String selector jquery selector
@return jQuery
###
if @$rootEls.length > 0
$(selector, @$rootEls)
else
$(selector)
addClass: (value) ->
###
Adds the specified class(es) to the root element(s) of the widget.
This change will be preserved on the widget re-render.
@param String value One or more space-separated CSS classes
###
@_clsPrepare value, (classes, ctx) ->
addClasses = _.difference(classes, ctx.__cord_dyn_classes__)
@_clsAdd(addClasses, ctx)
removeClass: (value) ->
###
Removes the specified class(es) from the root element(s) of the widget.
This change will be preserved on the widget re-render.
@param String value One or more space-separated CSS classes
###
@_clsPrepare value, (classes, ctx) ->
removeClasses = _.intersection(classes, ctx.__cord_dyn_classes__)
@_clsRemove(removeClasses, ctx)
toggleClass: (value, state) ->
###
Adds or removes one or more classes from the root element(s) of the widget,
depending on either the class's presence or the value of the `state` argument.
If the state value is not set the given classes existence is inverted.
This change will be preserved on the widget re-render.
@param String value One or more space-separated CSS classes
@param Boolean state A boolean value to determine whether the class should be added or removed
###
if state?
state = !!state
if state
@addClass(value)
else
@removeClass(value)
else
@_clsPrepare value, (classes, ctx) ->
addClasses = _.difference(classes, ctx.__cord_dyn_classes__)
removeClasses = _.intersection(classes, ctx.__cord_dyn_classes__)
@_clsRemove(removeClasses, ctx)
@_clsAdd(addClasses, ctx)
_clsPrepare: (value, cb) ->
###
DRY for (add|remove|toggle)Class
###
if @widget
ctx = @widget.ctx
ctx.__cord_dyn_classes__ ?= []
classes = value.split(/\s/).filter((x) -> x != '')
# calling in the context of the behaviour to avoid necessity of using fat arrow
cb.call(this, classes, ctx)
_clsAdd: (addClasses, ctx) ->
###
DRY for (add|toggle)Class
###
ctx.__cord_dyn_classes__ = ctx.__cord_dyn_classes__.concat(addClasses)
root = if @el.length == 1 then @el else @$rootEls
root.addClass(addClasses.join(' ')) if addClasses.length > 0
_clsRemove: (removeClasses, ctx) ->
###
DRY for (remove|toggle)Class
###
ctx.__cord_dyn_classes__ = _.difference(ctx.__cord_dyn_classes__, removeClasses)
root = if @el.length == 1 then @el else @$rootEls
root.removeClass(removeClasses.join(' ')) if removeClasses.length > 0
addSubscription: (subscription, callback = null) ->
if callback and _.isString subscription
subscription = postal.subscribe
topic: subscription
callback: callback
@_widgetSubscriptions.push subscription
subscription
getCallback: (callback) ->
###
Register callback and clear it in case of object destruction or clearCallbacks invocation
Need to be used, when reference to the widget object (@) is used inside a callback, for instance:
api.get Url, Params, @getCallback (result) =>
@ctx.set 'apiResult', result
###
that = this
makeSafeCallback = (callback) ->
result = -> callback.apply(null, arguments) if not result.cleared and that.widget and not that.widget.isSentenced()
result.cleared = false
result
safeCallback = makeSafeCallback(callback)
@_callbacks.push safeCallback
safeCallback
clearCallbacks: ->
callback.cleared = true for callback in @_callbacks
@_callbacks = []
delegateEvents: (events) ->
if typeof window.zone != 'undefined'
tmpZone = window.zone
window.zone = tmpZone.constructor.rootZone
for key, method of events
method = @_getEventMethod(method, key)
match = key.match(/^(\S+)\s*(.*)$/)
eventName = match[1]
selector = match[2]
do (method, selector) =>
if selector is ''
@$rootEls.on(eventName, method)
else
# special helper selector ##
# ##someId is replaced by #{widgets id}-someId
# in widget template it should look like id="{id}-someId"
if selector.substr(0, 2) == '##'
selector = '#' + @widget.ctx.id + '-' + selector.substr(2)
# support for @elementName like selectors
if selector[0] == '@' and this[selector.substr(1)]
selector = @_elementSelectors[selector.substr(1)]
if eventName == 'scroll'
# scroll event is not bubbling up, so it have to be bound without event delegation feature
# right to the element
$(selector, @$rootEls).on(eventName, method)
else
root = if @el.length == 1 then @el else @$rootEls
root.on(eventName, selector, method)
if typeof window.zone != 'undefined'
window.zone = tmpZone
initWidgetEvents: (events) ->
for fieldName, method of events
onChangeMethod = @_getWidgetEventMethod(fieldName, method)
subscription = postal.subscribe
topic: "widget.#{ @id }.change.#{ fieldName }"
callback: onChangeMethod
@_widgetSubscriptions.push(subscription)
@_registerModelBinding(@widget.ctx[fieldName], fieldName, onChangeMethod)
_getEventMethod: (method, eventDesc) ->
m = @_getHandlerFunction(method)
that = this
->
origArgs = arguments
pr.timer "#{that.constructor.__name}::DOM('#{eventDesc}')", ->
m.apply(that, origArgs) if that.widget and not that.widget.isSentenced()
true
_getWidgetEventMethod: (fieldName, method) ->
m = @_getHandlerFunction(method)
onChangeMethod = =>
data = arguments[0]
ctxVersionBorder = @widget._behaviourContextBorderVersion
# if data.version is undefined than it's model-emitted event and need not version check
versionOk = (not ctxVersionBorder? or not data.version? or data.version > ctxVersionBorder)
if not @widget.isSentenced() and data.value != ':deferred' and versionOk
duplicate = false
if data.cursor
if @widget._eventCursors[data.cursor]
delete @widget._eventCursors[data.cursor]
duplicate = true
else
@widget._eventCursors[data.cursor] = true
if not duplicate
@_registerModelBinding(data.value, fieldName, onChangeMethod)
m.apply(this, arguments)
_registerModelBinding: (value, fieldName, onChangeMethod) ->
if @_modelBindings[fieldName]?
mb = @_modelBindings[fieldName]
if value != mb.model
mb.subscription.unsubscribe() if mb.subscription?
delete @_modelBindings[fieldName]
if value instanceof Model and not (@_modelBindings[fieldName]? and value == mb.model)
@_modelBindings[fieldName] ?= {}
@_modelBindings[fieldName].model = value
@_modelBindings[fieldName].subscription = value.on 'change', (model) =>
onChangeMethod
name: fieldName
value: model
oldValue: value
_getHandlerFunction: (method) ->
if typeof(method) is 'function'
result = method
else
throw new Error("#{method} doesn't exist") unless @[method]
result = @[method]
result
refreshElements: ->
for key, value of @elements
this[value] = @$(key)
@_elementSelectors[value] = key
clean: ->
@destroy()
subscription.unsubscribe() for subscription in @_widgetSubscriptions
@_widgetSubscriptions = []
for name, mb of @_modelBindings
mb.subscription?.unsubscribe()
@_modelBindings = {}
@widget = null
@el.off()
@el = @$el = null
@clearCallbacks()
render: ->
###
Fully re-renders and replaces all widget's contents by killing all child widgets and re-rendering own template.
Works using defer async in order to collapse several simultaneous calls of render into one.
@return Future[Behaviour] new behaviour instance
###
@widget.sentenceChildrenToDeath()
# re-render shouldn't be performed before the widget is shown due to possibility of wrong DOM root element
# state and replacing the DOM node in wrong place after re-render
# this is pretty dangerous change and should attract attention when re-render isn't performed when it should be
@widget.shown().then =>
if not @_renderAggregatePromise?
@_renderAggregatePromise = Future.single(@debug('renderAggregate'))
Defer.nextTick =>
@_renderAggregatePromise.when(@_render0())
@_renderAggregatePromise = null
@_renderAggregatePromise
.catchIf (err) ->
err instanceof errors.WidgetDropped
.failAloud(@debug('render'))
_render0: ->
###
Actually re-render code. Should be used only from public render() method.
###
if @widget?
# renderTemplate will clean this behaviour, so we must save links...
widget = @widget
$rootEl = @el
domInfo = new DomInfo(@debug('render'))
# harakiri: this is need to avoid interference of subsequent async calls of the @render() for the same widget
widget._cleanBehaviour()
# dirty hack to prevent interfered browserInit() triggered by concurrently running Widget::inject()
widget._delayedRender = true
widget.renderTemplate(domInfo).then (out) ->
$newWidgetRoot = $(widget.renderRootTag(out))
domInfo.setDomRoot($newWidgetRoot)
# unlocking flag to allow browserInit to proceed (see comment above)
widget._delayedRender = false
widget.browserInit($newWidgetRoot).then ->
$newWidgetRoot.attr('style', $rootEl.attr('style'))
DomHelper.replace($rootEl, $newWidgetRoot)
.then ->
domInfo.markShown()
widget.markShown()
widget.emit 're-render.complete'
widget.behaviour
else
Future.rejected(new Error("Behaviour [#{@constructor.__name}] is already cleaned!"))
renderInline: (name) ->
###
Re-renders inline with the given name
@param String name inline's name to render
@deprecated inlines re-rendering should be performed by render() method
###
@widget._behaviourContextBorderVersion = null
@widget._resetWidgetReady()
domInfo = new DomInfo(@debug('renderInline'))
@widget.renderInline(name, domInfo).then (out) =>
@widget.renderInlineTag(name, out)
.then (wrappedOut) =>
$newInlineRoot = $(wrappedOut)
domInfo.setDomRoot($newInlineRoot)
id = @widget.ctx[':inlines'][name].id
$oldInlineRoot = $('#'+id)
DomHelper.replace($oldInlineRoot, $newInlineRoot).then =>
domInfo.markShown()
@widget.browserInit()
.failAloud(@debug('renderInline'))
_renderNewWidget: (widget, params) ->
###
Renders (via show method) the given widget with the given params, inserts it into DOM and initialtes.
Returns jquery-object referring to the widget's root element via callback argument.
@param Widget widget widget object
@param Object params key-value params for the widget
@return Future[jQuery] jQuery element of the created widget
###
domInfo = new DomInfo("#{ @debug('renderNewWidget') } -> #{ widget.debug() }")
widget.show(params, domInfo).then (out) ->
checkIsSentenced(widget, 'after widget.show')
$el = $(widget.renderRootTag(out))
domInfo.setDomRoot($el)
domInfo.domInserted().when(widget.shown())
widget.browserInit($el).then ->
checkIsSentenced(widget, 'after browserInit')
$el
initChildWidget: (type, name, params, callback) ->
###
Creates and initiates new child widget with the given type and params.
Returns jquery-object referring to the widget's root element via callback argument for further inserting the
widget to the right place in the DOM.
@param String type widget type in canonical format (absolute or in context of the current widget)
@param (optional)String name optional name for the new widget
@param Object params key-value params for the widget
@param Function(jquery) callback callback which is called with the resulting jquery element and created object of widget
###
if _.isObject(name)
callback = params
params = name
name = null
try
checkIsSentenced(@widget)
@widget.createChildWidget(type, name).then (newWidget) =>
checkIsSentenced(newWidget, 'before _renderNewWidget')
@_renderNewWidget(newWidget, params).done ($el) ->
callback?($el, newWidget)
.then ($el) ->
[[$el, newWidget]]
catch err
Future.rejected(err)
insertChildWidget: (type, params = {}) ->
###
Creates and correctly inserts a new child widget with the given params into the given place in the DOM.
By default the root element of the newly created widget is appended to the end of the root element of this widget.
Unlike `initChildWidget()` this method correctly performs `markShown()` for the inserted widget.
@param {String} type widget type in canonical format (absolute or in context of the current widget)
@param (optional){Object} params new widget's params and special positioning params
Positioning params:
':position': 'append'(default)|'prepend'|'replace'
':context': DOM (jQuery) element into which should be inserted
or which should be replaced
default - root element of this widget
@return Future[Array[jQuery, Widget]]
###
result = Future.single(@debug("insertChildWidget -> #{type}"))
widgetParams = {}
name = undefined
insertPosition = 'append'
insertContext = @el
for key, val of params
switch key
when 'name' then name = val
when ':position' then insertPosition = val
when ':context' then insertContext = val
else widgetParams[key] = val
if insertPosition == 'replace' and insertContext == @el
return Future.rejected(new Error("Child widget cannot replace parent\'s root element (#{@debug()})!"))
@initChildWidget(type, name, widgetParams).spread ($el, newWidget) ->
(switch insertPosition
when 'append' then DomHelper.append(insertContext, $el)
when 'prepend' then DomHelper.prepend(insertContext, $el)
when 'replace' then DomHelper.replace(insertContext, $el)
else throw new Error("Invalid insert position: #{insertPosition}!")
).then ->
newWidget.markShown()
[[$el, newWidget]]
.then (res) -> result.resolve(res)
.catch (err) ->
result.reject(err)
# preventing reporting of unhandled rejection in case of fast page switching
result.failOk() if err instanceof errors.WidgetSentenced
result
dropChildWidget: (widget) ->
@widget.dropChild(widget.ctx.id)
getServiceContainer: ->
@widget.getServiceContainer()
debug: (method) ->
###
Return identification string of the current widget for debug purposes
@param (optional) String method include optional "::method" suffix to the result
@return String
###
methodStr = if method? then "::#{ method }" else ''
"#{ @widget.getPath() }Behaviour(#{ @widget.ctx.id })#{ methodStr }"