This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
compiler.js
377 lines (330 loc) · 13.4 KB
/
compiler.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
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
/**
* @ngdoc module
* @name material.core.compiler
* @description
* AngularJS Material template and element compiler.
*/
angular
.module('material.core')
.provider('$mdCompiler', MdCompilerProvider);
MdCompilerProvider.$inject = ['$compileProvider'];
function MdCompilerProvider() {
this.$get = ["$q", "$templateRequest", "$injector", "$compile", "$controller",
function($q, $templateRequest, $injector, $compile, $controller) {
return new MdCompilerService($q, $templateRequest, $injector, $compile, $controller);
}];
/**
* @ngdoc service
* @name $mdCompiler
* @module material.core.compiler
* @description
* The $mdCompiler service is an abstraction of AngularJS's compiler, that allows developers
* to compile an element with options like in a Directive Definition Object.
*
* > The compiler powers a lot of components inside of AngularJS Material.
* > Like the `$mdPanel` or `$mdDialog` services.
*
* @usage
*
* Basic Usage with a template
*
* <hljs lang="js">
* $mdCompiler.compile({
* templateUrl: 'modal.html',
* controller: 'ModalCtrl',
* locals: {
* modal: myModalInstance;
* }
* }).then(function (compileData) {
* compileData.element; // Compiled DOM element
* compileData.link(myScope); // Instantiate controller and link element to scope.
* });
* </hljs>
*
* Example with a content element
*
* <hljs lang="js">
*
* // Create a virtual element and link it manually.
* // The compiler doesn't need to recompile the element each time.
* var myElement = $compile('<span>Test</span>')(myScope);
*
* $mdCompiler.compile({
* contentElement: myElement
* }).then(function (compileData) {
* compileData.element // Content Element (same as above)
* compileData.link // This does nothing when using a contentElement.
* });
* </hljs>
*
* > Content Element is a significant performance improvement when the developer already knows
* > that the compiled element will be always the same and the scope will not change either.
*
* The `contentElement` option also supports DOM elements which will be temporary removed and
* restored at its old position.
*
* <hljs lang="js">
* var domElement = document.querySelector('#myElement');
*
* $mdCompiler.compile({
* contentElement: myElement
* }).then(function (compileData) {
* compileData.element // Content Element (same as above)
* compileData.link // This does nothing when using a contentElement.
* });
* </hljs>
*
* The `$mdCompiler` can also query for the element in the DOM itself.
*
* <hljs lang="js">
* $mdCompiler.compile({
* contentElement: '#myElement'
* }).then(function (compileData) {
* compileData.element // Content Element (same as above)
* compileData.link // This does nothing when using a contentElement.
* });
* </hljs>
*
*/
function MdCompilerService($q, $templateRequest, $injector, $compile, $controller) {
/**
* @private @const
* @type {!IQService}
*/
this.$q = $q;
/**
* @private @const
* @type {!ITemplateRequestService}
*/
this.$templateRequest = $templateRequest;
/**
* @private @const
* @type {!IInjectorService}
*/
this.$injector = $injector;
/**
* @private @const
* @type{!ICompileService}
*/
this.$compile = $compile;
/**
* @private @const
* @type {!IControllerService}
*/
this.$controller = $controller;
}
/**
* @ngdoc method
* @name $mdCompiler#compile
* @description
*
* A method to compile a HTML template with the AngularJS compiler.
* The `$mdCompiler` is wrapper around the AngularJS compiler and provides extra functionality
* like controller instantiation or async resolves.
*
* @param {!Object} options An options object, with the following properties:
*
* - `controller` - `{string|function}` Controller fn that should be associated with
* newly created scope or the name of a registered controller if passed as a string.
* - `controllerAs` - `{string=}` A controller alias name. If present the controller will be
* published to scope under the `controllerAs` name.
* - `contentElement` - `{string|Element}`: Instead of using a template, which will be
* compiled each time, you can also use a DOM element.<br/>
* - `template` - `{string=}` An html template as a string.
* - `templateUrl` - `{string=}` A path to an html template.
* - `transformTemplate` - `{function(template)=}` A function which transforms the template after
* it is loaded. It will be given the template string as a parameter, and should
* return a a new string representing the transformed template.
* - `resolve` - `{Object.<string, function>=}` - An optional map of dependencies which should
* be injected into the controller. If any of these dependencies are promises, the compiler
* will wait for them all to be resolved, or if one is rejected before the controller is
* instantiated `compile()` will fail..
* * `key` - `{string}`: a name of a dependency to be injected into the controller.
* * `factory` - `{string|function}`: If `string` then it is an alias for a service.
* Otherwise if function, then it is injected and the return value is treated as the
* dependency. If the result is a promise, it is resolved before its value is
* injected into the controller.
*
* @returns {Q.Promise<{element: JQLite, link: Function, locals: Object, cleanup: any,
* controller: Object=}>} promise A promise, which will be resolved with a `compileData` object.
* `compileData` has the following properties:
*
* - `element` - `{JQLite}`: an uncompiled element matching the provided template.
* - `link` - `{function(scope)}`: A link function, which, when called, will compile
* the element and instantiate the provided controller (if given).
* - `locals` - `{Object}`: The locals which will be passed into the controller once `link` is
* called. If `bindToController` is true, they will be copied to the ctrl instead
*/
MdCompilerService.prototype.compile = function(options) {
if (options.contentElement) {
return this._prepareContentElement(options);
} else {
return this._compileTemplate(options);
}
};
/**
* Instead of compiling any template, the compiler just fetches an existing HTML element from the
* DOM and provides a restore function to put the element back it old DOM position.
* @param {!Object} options Options to be used for the compiler.
* @returns {Q.Promise<{element: JQLite, link: Function, locals: Object, cleanup: any}>}
*/
MdCompilerService.prototype._prepareContentElement = function(options) {
var contentElement = this._fetchContentElement(options);
return this.$q.resolve({
element: contentElement.element,
cleanup: contentElement.restore,
locals: {},
link: function() {
return contentElement.element;
}
});
};
/**
* Compiles a template by considering all options and waiting for all resolves to be ready.
* @param {!Object} options Compile options
* @returns {!Q.Promise<{element: JQLite, link: Function, locals: Object, cleanup: any}>} Compile
* data with link function.
*/
MdCompilerService.prototype._compileTemplate = function(options) {
var self = this;
var templateUrl = options.templateUrl;
var template = options.template || '';
var resolve = angular.extend({}, options.resolve);
var locals = angular.extend({}, options.locals);
var transformTemplate = options.transformTemplate || angular.identity;
// Take resolve values and invoke them.
// Resolves can either be a string (value: 'MyRegisteredAngularConst'),
// or an invokable 'factory' of sorts: (value: function ValueGetter($dependency) {})
angular.forEach(resolve, function(value, key) {
if (angular.isString(value)) {
resolve[key] = self.$injector.get(value);
} else {
resolve[key] = self.$injector.invoke(value);
}
});
// Add the locals, which are just straight values to inject
// eg locals: { three: 3 }, will inject three into the controller
angular.extend(resolve, locals);
if (templateUrl) {
resolve.$$ngTemplate = this.$templateRequest(templateUrl);
} else {
resolve.$$ngTemplate = this.$q.when(template);
}
// Wait for all the resolves to finish if they are promises
return this.$q.all(resolve).then(function(locals) {
var template = transformTemplate(locals.$$ngTemplate, options);
var element = options.element || angular.element('<div>').html(template.trim()).contents();
return self._compileElement(locals, element, options);
});
};
/**
* Method to compile an element with the given options.
* @param {!Object} locals Locals to be injected to the controller if present
* @param {!JQLite} element Element to be compiled and linked
* @param {!Object} options Options to be used for linking.
* @returns {!{element: JQLite, link: Function, locals: Object, cleanup: any, controller: Object}} Compile data with link function.
*/
MdCompilerService.prototype._compileElement = function(locals, element, options) {
var self = this;
var ngLinkFn = this.$compile(element);
var compileData = {
element: element,
cleanup: element.remove.bind(element),
locals: locals,
link: linkFn
};
function linkFn(scope) {
locals.$scope = scope;
// Instantiate controller if the developer provided one.
if (options.controller) {
var injectLocals = angular.extend({}, locals, {
$element: element
});
// Create the specified controller instance.
var ctrl = self._createController(options, injectLocals, locals);
// Registering extra $destroy listeners should be avoided.
// Only register the listener if the controller implements a $onDestroy hook.
if (angular.isFunction(ctrl.$onDestroy)) {
scope.$on('$destroy', function() {
// Call the $onDestroy hook if it's present on the controller.
angular.isFunction(ctrl.$onDestroy) && ctrl.$onDestroy();
});
}
// Unique identifier for AngularJS Route ngView controllers.
element.data('$ngControllerController', ctrl);
element.children().data('$ngControllerController', ctrl);
// Expose the instantiated controller to the compile data
compileData.controller = ctrl;
}
// Invoke the AngularJS $compile link function.
return ngLinkFn(scope);
}
return compileData;
};
/**
* Creates and instantiates a new controller with the specified options.
* @param {!Object} options Options that include the controller function or string.
* @param {!Object} injectLocals Locals to to be provided in the controller DI.
* @param {!Object} locals Locals to be injected to the controller.
* @returns {!Object} Created controller instance.
*/
MdCompilerService.prototype._createController = function(options, injectLocals, locals) {
var ctrl = this.$controller(options.controller, injectLocals);
if (options.bindToController) {
angular.extend(ctrl, locals);
}
if (options.controllerAs) {
injectLocals.$scope[options.controllerAs] = ctrl;
}
// Call the $onInit hook if it's present on the controller.
angular.isFunction(ctrl.$onInit) && ctrl.$onInit();
return ctrl;
};
/**
* Fetches an element removing it from the DOM and using it temporary for the compiler.
* Elements which were fetched will be restored after use.
* @param {!Object} options Options to be used for the compilation.
* @returns {{element: !JQLite, restore: !function}}
*/
MdCompilerService.prototype._fetchContentElement = function(options) {
var contentEl = options.contentElement;
var restoreFn;
if (angular.isString(contentEl)) {
contentEl = document.querySelector(contentEl);
restoreFn = createRestoreFn(contentEl);
} else {
contentEl = contentEl[0] || contentEl;
// When the element is visible in the DOM, then we restore it at close of the dialog.
// Otherwise it will be removed from the DOM after close.
if (document.contains(contentEl)) {
restoreFn = createRestoreFn(contentEl);
} else {
restoreFn = function() {
if (contentEl.parentNode) {
contentEl.parentNode.removeChild(contentEl);
}
};
}
}
return {
element: angular.element(contentEl),
restore: restoreFn
};
function createRestoreFn(element) {
var parent = element.parentNode;
var nextSibling = element.nextElementSibling;
return function() {
if (!nextSibling) {
// When the element didn't had any sibling, then it can be simply appended to the
// parent, because it plays no role, which index it had before.
parent.appendChild(element);
} else {
// When the element had a sibling, which marks the previous position of the element
// in the DOM, we insert it correctly before the sibling, to have the same index as
// before.
parent.insertBefore(element, nextSibling);
}
};
}
};
}