-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathadapter.js
222 lines (191 loc) · 7.9 KB
/
adapter.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
'use strict';
const Fractal = require('@frctl/core');
const _ = require('lodash');
const Path = require('path');
const utils = Fractal.utils;
const adapterUtils = require('./utils');
class TwigAdapter extends Fractal.Adapter {
constructor(Twig, source, app, config) {
super(Twig, source);
this._app = app;
this._config = config;
this._loaderName = `fractal-${source.name}`;
source.set('engine', '@frctl/twig');
let self = this;
Twig.extend(function (Twig) {
/*
* Register a Fractal template loader. Locations can be handles or paths.
*/
Twig.Templates.registerLoader(self._loaderName, function (location, params) {
if (params.precompiled) {
params.data = params.precompiled;
} else {
let view = adapterUtils.isHandle(location, self._config.handlePrefix)
? self.getView(location)
: _.find(self.views, { path: Path.join(source.fullPath, location) });
if (!view) {
throw new Error(`Template ${location} not found`);
}
params.data = view.content;
}
return new Twig.Template(params);
});
/*
* Monkey patch the render method to make sure that the _self variable
* always refers to the actual component/sub-component being rendered.
* Without this _self would always refer to the root component.
*/
const render = Twig.Template.prototype.render;
Twig.Template.prototype.render = function (context, params) {
if (!self._config.pristine && this.id) {
let handle = null;
if (adapterUtils.isHandle(this.id, self._config.handlePrefix)) {
handle = this.id;
} else {
let view = _.find(self.views, { path: Path.join(source.fullPath, this.id) });
if (view) {
handle = view.handle;
}
}
if (handle) {
let entity = source.find(adapterUtils.replaceHandlePrefix(handle, self._config.handlePrefix));
if (entity) {
entity = entity.isComponent ? entity.variants().default() : entity;
if (config.importContext) {
context = utils.defaultsDeep(_.cloneDeep(context), entity.getContext());
context._self = entity.toJSON();
setKeys(context);
}
}
}
}
/*
* Twig JS uses an internal _keys property on the context data
* which we need to regenerate every time we patch the context.
*/
function setKeys(obj) {
obj._keys = _.compact(
_.map(obj, (val, key) => {
return _.isString(key) && !key.startsWith('_') ? key : undefined;
})
);
_.each(obj, (val, key) => {
if (_.isPlainObject(val) && _.isString(key) && !key.startsWith('_')) {
setKeys(val);
}
});
}
return render.call(this, context, params);
};
/*
* Twig caching is enabled for better perf, so we need to
* manually update the cache when a template is updated or removed.
*/
Twig.cache = false;
self.on('view:updated', unCache);
self.on('view:removed', unCache);
self.on('wrapper:updated', unCache);
self.on('wrapper:removed', unCache);
function unCache(view) {
let path = Path.relative(source.fullPath, _.isString(view) ? view : view.path);
if (view.handle && Twig.Templates.registry[view.handle]) {
delete Twig.Templates.registry[view.handle];
}
if (Twig.Templates.registry[path]) {
delete Twig.Templates.registry[path];
}
}
});
}
get twig() {
return this._engine;
}
render(path, str, context, meta) {
let self = this;
meta = meta || {};
if (!this._config.pristine) {
setEnv('_self', meta.self, context);
setEnv('_target', meta.target, context);
setEnv('_env', meta.env, context);
setEnv('_config', this._app.config(), context);
}
return new Promise(function (resolve, reject) {
let tplPath = path ? Path.relative(self._source.fullPath, path) : undefined;
try {
let template = self.engine.twig({
method: self._config.method === 'fractal' ? self._loaderName : self._config.method,
async: false,
rethrow: true,
name:
self._config.method === 'fractal'
? meta.self
? `${self._config.handlePrefix}${meta.self.handle}`
: tplPath
: undefined,
path: path,
precompiled: str,
base: self._config.base,
strict_variables: self._config.strict_variables,
namespaces: self._config.namespaces,
});
resolve(template.render(context));
} catch (e) {
reject(new Error(e));
}
});
function setEnv(key, value, context) {
if (context[key] === undefined && value !== undefined) {
context[key] = value;
}
}
}
}
module.exports = function (config) {
config = _.defaults(config || {}, {
method: 'fractal',
pristine: false,
handlePrefix: '@',
importContext: false,
base: null,
strict_variables: false,
namespaces: {},
});
return {
register(source, app) {
const Twig = require('twig');
if (!config.pristine) {
_.each(require('./functions')(app) || {}, function (func, name) {
Twig.extendFunction(name, func);
});
_.each(require('./filters')(app), function (filter, name) {
Twig.extendFilter(name, filter);
});
_.each(require('./tests')(app), function (test, name) {
Twig.extendTest(name, test);
});
Twig.extend(function (Twig) {
_.each(require('./tags')(app, config), function (tag) {
Twig.exports.extendTag(tag(Twig));
});
});
}
_.each(config.functions || {}, function (func, name) {
Twig.extendFunction(name, func);
});
_.each(config.filters || {}, function (filter, name) {
Twig.extendFilter(name, filter);
});
_.each(config.tests || {}, function (test, name) {
Twig.extendTest(name, test);
});
Twig.extend(function (Twig) {
_.each(config.tags || {}, function (tag) {
Twig.exports.extendTag(tag(Twig));
});
});
const adapter = new TwigAdapter(Twig, source, app, config);
adapter.setHandlePrefix(config.handlePrefix);
return adapter;
},
};
};