Repository navigation
Expand file tree
/
Copy pathplugins.js
More file actions
118 lines (99 loc) · 2.78 KB
/
Copy pathplugins.js
File metadata and controls
118 lines (99 loc) · 2.78 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
import * as LogManager from 'aurelia-logging';
import {Metadata} from 'aurelia-metadata';
var logger = LogManager.getLogger('aurelia');
function loadPlugin(aurelia, loader, info){
logger.debug(`Loading plugin ${info.moduleId}.`);
aurelia.currentPluginId = info.moduleId;
var baseUrl = info.moduleId.startsWith('./') ? undefined : "";
return loader.loadModule(info.moduleId, baseUrl).then(exportedValue => {
if('install' in exportedValue){
var result = exportedValue.install(aurelia, info.config || {});
if(result){
return result.then(() =>{
aurelia.currentPluginId = null;
logger.debug(`Installed plugin ${info.moduleId}.`);
});
}else{
logger.debug(`Installed plugin ${info.moduleId}.`);
}
}else{
logger.debug(`Loaded plugin ${info.moduleId}.`);
}
aurelia.currentPluginId = null;
});
}
/**
* Manages loading and installing plugins.
*
* @class Plugins
* @constructor
* @param {Aurelia} aurelia An instance of Aurelia.
*/
export class Plugins {
constructor(aurelia){
this.aurelia = aurelia;
this.info = [];
this.processed = false;
}
/**
* Installs a plugin before Aurelia starts.
*
* @method plugin
* @param {moduleId} moduleId The ID of the module to install.
* @param {config} config The configuration for the specified module.
* @return {Plugins} Returns the current Plugins instance.
*/
plugin(moduleId, config){
var plugin = {moduleId:moduleId, config:config || {}};
if(this.processed){
loadPlugin(this.aurelia, this.aurelia.loader, plugin);
}else{
this.info.push(plugin);
}
return this;
}
/**
* Installs special support for ES5 authoring.
*
* @method es5
* @return {Plugins} Returns the current Plugins instance.
*/
es5(){
Function.prototype.computed = function(computedProperties){
for(var key in computedProperties){
if(computedProperties.hasOwnProperty(key)){
Object.defineProperty(this.prototype, key, { get: computedProperties[key], enumerable: true });
}
}
}
return this;
}
/**
* Installs special support for AtScript authoring.
*
* @method atscript
* @return {Plugins} Returns the current Plugins instance.
*/
atscript(){
this.aurelia.container.supportAtScript();
Metadata.configure.locator(fn => fn['annotate'] || fn['annotations']);
return this;
}
_process(){
var aurelia = this.aurelia,
loader = aurelia.loader,
info = this.info,
current;
if(this.processed){
return;
}
var next = () => {
if(current = info.shift()){
return loadPlugin(aurelia, loader, current).then(next);
}
this.processed = true;
return Promise.resolve();
};
return next();
}
}