-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdustPlugins.coffee
More file actions
229 lines (200 loc) · 7.95 KB
/
dustPlugins.coffee
File metadata and controls
229 lines (200 loc) · 7.95 KB
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
define [
'cord!helpers/TimeoutStubHelper'
'cord!utils/DomInfo'
'cord!utils/Future'
'cord!errors'
'underscore'
], (TimeoutStubHelper, DomInfo, Future, errors, _) ->
###
Set of dustjs plugin functions supporting CordJS templates special setup and structuring
###
widget: (chunk, context, bodies, params) ->
###
{#widget/} block handling
###
tmplWidget = context.get('_ownerWidget')
tmplWidget.childWidgetAdd(params.type)
if params.type.substr(0, 2) == './'
params.type = "//#{tmplWidget.constructor.relativeDir}#{params.type.substr(1)}"
contextDomInfo = tmplWidget._domInfo
chunk.map (chunk) ->
normalizedName = if params.name then params.name.trim() else undefined
normalizedName = undefined if not normalizedName
timeout = if params.timeout? then parseInt(params.timeout) else -1
hasTimeout = CORD_IS_BROWSER and timeout >= 0
timeoutDomInfo = if hasTimeout then new DomInfo(tmplWidget.debug('#widget::timeout')) else contextDomInfo
tmplWidget.getStructTemplate().then (tmpl) ->
# creating widget from the structured template or not depending on it's existence and name
# btw getting and pushing futher timeout template name from the structure template if there is one
if tmpl.isEmpty() or not normalizedName
tmplWidget.widgetRepo.createWidget(params.type, tmplWidget, normalizedName, tmplWidget.getBundle())
# tuple result is expected below, so we need to convert to an array
.then (widget) -> [widget, null]
else if normalizedName
tmpl.getWidgetByName(normalizedName).then (widget) ->
[widget, tmpl.getWidgetInfoByName(normalizedName).timeoutTemplate]
.catch ->
tmplWidget.widgetRepo.createWidget(params.type, tmplWidget, normalizedName, tmplWidget.getBundle())
# tuple result is expected below, so we need to convert to an array
.then (widget) -> [widget, null]
# else impossible
.spread (widget, timeoutTemplate) ->
complete = false
tmplWidget.resolveParamRefs(widget, params).then (resolvedParams) ->
widget.setModifierClass(params.class)
widget.show(resolvedParams, timeoutDomInfo)
.then (out) ->
if not complete
complete = true
timeoutDomInfo.completeWith(contextDomInfo) if hasTimeout
tmplWidget.childWidgetComplete(params.type)
chunk.end(widget.renderRootTag(out))
return
else
TimeoutStubHelper.replaceStub(out, widget, contextDomInfo).then ($newRoot) ->
timeoutDomInfo.setDomRoot($newRoot)
timeoutDomInfo.domInserted().when(contextDomInfo.domInserted())
return
.catchIf (err) ->
err instanceof errors.WidgetDropped or err instanceof errors.WidgetSentenced
.catch (err) ->
if not complete
complete = true
tmplWidget.childWidgetFailed(params.type, err)
throw err
.catch (err) ->
# we can not merge this catch to previous one, because childWidgetFailed method can throw it's own error
chunk.setError(err)
return
if hasTimeout
setTimeout ->
# if the widget has not been rendered within given timeout, render stub template from the {:timeout} block
if not complete
complete = true
widget.setDelayedRender()
TimeoutStubHelper.getTimeoutHtml(tmplWidget, timeoutTemplate, widget).then (out) ->
tmplWidget.childWidgetComplete(params.type)
chunk.end(widget.renderRootTag(out))
return
.catch (err) ->
tmplWidget.childWidgetFailed(params.type, err)
throw err
.catch (err) ->
# we can not merge this catch to previous one, because childWidgetFailed method can throw it's own error
chunk.setError(err)
return
, timeout
.catch (err) ->
tmplWidget.childWidgetFailed(params.type, err)
throw err
.catch (err) ->
# we can not merge this catch to previous one, because childWidgetFailed method can throw it's own error
chunk.setError(err)
return
deferred: (chunk, context, bodies, params) ->
###
{#deferred/} block handling
###
if bodies.block?
tmplWidget = context.get('_ownerWidget')
deferredId = tmplWidget._deferredBlockCounter++
deferredKeys = params.params.split(/[, ]/)
needToWait = (name for name in deferredKeys when tmplWidget.ctx.isDeferred(name))
type = 'deferred_'+deferredId
promise = new Future(tmplWidget.debug('deferred'))
for name in needToWait
promise.when(tmplWidget.ctx.getPromise(name))
tmplWidget.childWidgetAdd(type)
chunk.map (chunk) ->
promise.then ->
TimeoutStubHelper.renderTemplateFile(tmplWidget, "__deferred_#{deferredId}")
.then (out) ->
tmplWidget.childWidgetComplete(type)
chunk.end(out)
return
.catch (err) ->
tmplWidget.childWidgetFailed(type, err)
throw err
.catch (err) ->
# we can not merge this catch to previous one, because childWidgetFailed method can throw it's own error
tmplWidget.logger.error "Error on widget #{ tmplWidget.debug() } #deferred rendering:", err
chunk.setError(err)
return
else
''
placeholder: (chunk, context, bodies, params) ->
###
{#placeholder/} block handling
Placeholder - point of extension of the widget.
###
tmplWidget = context.get('_ownerWidget')
type = _.uniqueId('placeholder_')
tmplWidget.childWidgetAdd(type)
chunk.map (chunk) ->
name = params?.name ? 'default'
if params and params.class
tmplWidget._placeholdersClasses[name] = params.class
tmplWidget._renderPlaceholder(name, tmplWidget._domInfo).spread (out) ->
tmplWidget.childWidgetComplete(type)
chunk.end(tmplWidget.renderPlaceholderTag(name, out))
return
.catch (err) ->
tmplWidget.childWidgetFailed(type, err)
throw err
.catch (err) ->
chunk.setError(err)
return
i18n: (chunk, context, bodies, params) ->
###
{#i18n text="" [context=""] [wrapped="true"] /}
###
tmplWidget = context.get('_ownerWidget')
text = params.text or ''
delete(params.text)
if tmplWidget.ctx.i18nHelper
chunk.write(tmplWidget.ctx.i18nHelper(text, params))
else
chunk.write(text)
url: (chunk, context, bodies, params) ->
###
{#url routeId="" [param1=""...] /}
###
tmplWidget = context.get('_ownerWidget')
routeId = params.routeId
if not routeId
throw new Error tmplWidget.debug("RouteId is require for #url")
delete(params.routeId)
chunk.write(tmplWidget.router.urlTo(routeId, params))
widgetInitializer: (chunk, context) ->
###
Widget initialization script generator.
Should be inserted to the bottom of the body-tag in the top-most layout widget.
###
tmplWidget = context.get('_ownerWidget')
if tmplWidget.widgetRepo._initEnd
''
else
chunk.map (chunk) ->
tmplWidget._childWidgetCompletePromise.then ->
chunk.end(tmplWidget.widgetRepo.getTemplateCode())
return
.catch (error) ->
chunk.setError(error)
return
css: (chunk, context) ->
###
Inserts css-link tags for the required css-files of the widgets during server-side page generation.
###
tmplWidget = context.get('_ownerWidget')
chunk.map (chunk) ->
tmplWidget._childWidgetCompletePromise.then ->
tmplWidget.widgetRepo.getTemplateCss()
.then (html) ->
chunk.end(html)
return
.catch (error) ->
chunk.setError(error)
return
# Boolean literals to pass as widget parameter value
false: false
true: true