-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
85 lines (65 loc) · 2.66 KB
/
index.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
/* jshint node: true */
'use strict';
module.exports = {
name: 'ember-merge-config',
getConstructorForEmberAppWithMergedConfig: function(EmberApp) {
var sel;
function EmberAppWithMergedConfig() {
var args = Array.prototype.slice.call(arguments);
return EmberApp.apply(this, args);
}
EmberAppWithMergedConfig.prototype = Object.create(EmberApp.prototype);
EmberAppWithMergedConfig.constructor = EmberAppWithMergedConfig;
if (typeof EmberApp.env === 'function') {
EmberAppWithMergedConfig.env = EmberApp.env;
}
if (EmberApp.prototype._contentForConfigModule) {
EmberAppWithMergedConfig.prototype._contentForConfigModule = function(content, config) {
var options = this.options && this.options['ember-merge-config'],
injectedContent = getInjectedContent(options),
returnIndex,
appConfigFromMeta;
EmberApp.prototype._contentForConfigModule.call(this, content, config);
appConfigFromMeta = content[content.length - 1].toString();
returnIndex = appConfigFromMeta.indexOf('return ');
appConfigFromMeta = appConfigFromMeta.substring(0, returnIndex) + injectedContent + appConfigFromMeta.substring(returnIndex, appConfigFromMeta.length);
content[content.length - 1] = appConfigFromMeta;
};
} else {
EmberAppWithMergedConfig.prototype.contentFor = function(config, match, type) {
var content, varName, isBase64, injectedContent, returnIndex, appConfigFromMeta,
options = this.options && this.options['ember-merge-config'];
if (type !== 'config-module') {
return EmberApp.prototype.contentFor.call(this, config, match, type);
} else {
injectedContent = getInjectedContent(options);
content = EmberApp.prototype.contentFor.call(this, config, match, type);
returnIndex = content.indexOf('return ');
content = content.substring(0, returnIndex) + injectedContent + content.substring(returnIndex, content.length);
return content;
}
};
}
return EmberAppWithMergedConfig;
}
};
function getInjectedContent(options) {
options = options || {};
var varName = options.globalVarName || 'configToMerge',
isBase64 = !!(options && options.isBase64),
injectedContent = [
'var parsedConfigToMerge;',
'\tif (window["' + varName + '"]) {',
'\t\ttry {',
'\t\t\tparsedConfigToMerge = window["' + varName + '"];',
isBase64 ? [
'\t\t\tif (typeof window["' + varName + '"] === "string") {',
'\t\t\t\tparsedConfigToMerge = JSON.parse(atob(window["' + varName + '"]));',
'\t\t\t}',
].join('\n') : '',
'\t\t\tEmber[\'default\'].$.extend(true, config, parsedConfigToMerge);',
'\t\t} catch (e) {}',
'\t}'
].join('\n') + '\n\t';
return injectedContent;
}