-
Notifications
You must be signed in to change notification settings - Fork 59
/
Soy.js
246 lines (225 loc) · 7.14 KB
/
Soy.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
'use strict';
import 'metal-soy-bundle';
import {ComponentRegistry} from 'metal-component';
import {isFunction, isObject, isString, object} from 'metal';
import {validators, Config} from 'metal-state';
import IncrementalDomRenderer, {HTML2IncDom} from 'metal-incremental-dom';
import SoyAop from './SoyAop';
// The injected data that will be passed to soy templates.
let ijData = {};
/**
* Soy Renderer
*/
class Soy extends IncrementalDomRenderer.constructor {
/**
* Adds the template params to the component's state, if they don't exist yet.
* @param {!Component} component
* @return {Object}
*/
getExtraDataConfig(component) {
let elementTemplate = component.constructor.TEMPLATE;
if (!isFunction(elementTemplate)) {
return;
}
elementTemplate = SoyAop.getOriginalFn(elementTemplate);
this.soyParamTypes_ = elementTemplate.types || {};
const keys = elementTemplate.params || [];
const configs = {};
for (let i = 0; i < keys.length; i++) {
if (!component[keys[i]]) {
configs[keys[i]] = {};
}
}
return configs;
}
/**
* Copies the component's state to an object so it can be passed as it's
* template call's data. The copying needs to be done because, if the component
* itself is passed directly, some problems occur when soy tries to merge it
* with other data, due to property getters and setters. This is safer.
* Also calls the component's "prepareStateForRender" to let it change the
* data passed to the template.
* @param {!Component} component
* @param {!Array<string>} params The params used by this template.
* @return {!Object}
* @protected
*/
buildTemplateData_(component, params) {
const data = object.mixin({}, this.getConfig(component));
component.getStateKeys().forEach(key => {
let value = component[key];
if (this.isHtmlParam_(component, key)) {
value = soyRenderer_.toIncDom(value);
}
data[key] = value;
});
for (let i = 0; i < params.length; i++) {
if (!data[params[i]] && isFunction(component[params[i]])) {
data[params[i]] = component[params[i]].bind(component);
}
}
if (isFunction(component.prepareStateForRender)) {
return component.prepareStateForRender(data) || data;
} else {
return data;
}
}
/**
* Returns the requested template function. This function will be wrapped in
* another though, just to defer the requirement of the template's module
* being ready until the function is actually called.
* @param {string} namespace The soy template's namespace.
* @param {string} templateName The name of the template function.
* @return {!function()}
*/
getTemplate(namespace, templateName) {
return function(data, ignored, ijData) {
if (!goog.loadedModules_[namespace]) {
throw new Error(
`No template with namespace "${
namespace
}" has been loaded yet.`
);
}
return goog.loadedModules_[namespace][templateName](
data,
ignored,
ijData
);
};
}
/**
* Handles an intercepted soy template call. If the call is for a component's
* main template, then it will be replaced with a call that incremental dom
* can use for both handling an instance of that component and rendering it.
* @param {!function()} originalFn The original template function that was
* intercepted.
* @param {Object} data The data the template was called with.
* @protected
*/
handleInterceptedCall_(originalFn, data = {}) {
const args = [originalFn.componentCtor, null, []];
for (let key in data) {
if (Object.prototype.hasOwnProperty.call(data, key)) {
args.push(key, data[key]);
}
}
IncrementalDOM.elementVoid.apply(null, args);
}
/**
* Checks if the given param type is html.
* @param {!Component} component
* @param {string} name
* @protected
* @return {boolean}
*/
isHtmlParam_(component, name) {
const state = component.getDataManager().getStateInstance(component);
if (state.getStateKeyConfig(name).isHtml) {
return true;
}
const elementTemplate = SoyAop.getOriginalFn(
component.constructor.TEMPLATE
);
const type = (elementTemplate.types || {})[name] || '';
return type.split('|').indexOf('html') !== -1;
}
/**
* Registers the given templates to be used by `Soy` for the specified
* component constructor.
* @param {!Function} componentCtor The constructor of the component that
* should use the given templates.
* @param {!Object} templates Object containing soy template functions.
* @param {string=} mainTemplate The name of the main template that should be
* used to render the component. Defaults to "render".
*/
register(componentCtor, templates, mainTemplate = 'render') {
componentCtor.RENDERER = soyRenderer_;
componentCtor.TEMPLATE = SoyAop.getOriginalFn(templates[mainTemplate]);
componentCtor.TEMPLATE.componentCtor = componentCtor;
SoyAop.registerForInterception(templates, mainTemplate);
ComponentRegistry.register(componentCtor);
}
/**
* Overrides the default method from `IncrementalDomRenderer` so the component's
* soy template can be used for rendering.
* @param {!Component} component
* @param {!Object} data Data passed to the component when rendering it.
* @override
*/
renderIncDom(component) {
let elementTemplate = component.constructor.TEMPLATE;
if (isFunction(elementTemplate) && !component.render) {
elementTemplate = SoyAop.getOriginalFn(elementTemplate);
SoyAop.startInterception(this.handleInterceptedCall_);
const data = this.buildTemplateData_(
component,
elementTemplate.params || []
);
elementTemplate(data, null, ijData);
SoyAop.stopInterception();
} else {
super.renderIncDom(component);
}
}
/**
* Sets the injected data object that should be passed to templates.
* @param {Object} data
*/
setInjectedData(data) {
ijData = data || {};
}
/**
* Overrides the original `IncrementalDomRenderer` method so that only
* state keys used by the main template can cause updates.
* @param {!Component} component
* @param {Object} changes
* @return {boolean}
*/
shouldUpdate(component, changes) {
const should = super.shouldUpdate(component, changes);
if (!should || component.shouldUpdate) {
return should;
}
const fn = component.constructor.TEMPLATE;
const params = fn ? SoyAop.getOriginalFn(fn).params : [];
for (let i = 0; i < params.length; i++) {
if (changes.props[params[i]]) {
return true;
}
}
return false;
}
/**
* Converts the given incremental dom function into an html string.
* @param {!function()} incDomFn
* @return {string}
*/
toHtmlString(incDomFn) {
const element = document.createElement('div');
IncrementalDOM.patch(element, incDomFn);
return element.innerHTML;
}
/**
* Converts the given html string into an incremental dom function.
* @param {string|{contentKind: string, content: string}} value
* @return {!function()}
*/
toIncDom(value) {
if (
isObject(value) &&
isString(value.content) &&
value.contentKind === 'HTML'
) {
value = value.content;
}
if (isString(value)) {
value = HTML2IncDom.buildFn(value);
}
return value;
}
}
const soyRenderer_ = new Soy();
soyRenderer_.RENDERER_NAME = 'soy';
export default soyRenderer_;
export {Config, soyRenderer_ as Soy, SoyAop, validators};