This repository has been archived by the owner on Jun 7, 2024. It is now read-only.
forked from speced/respec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
include-config.js
50 lines (49 loc) · 1.5 KB
/
include-config.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
// Module core/include-config
// Inject's the document's configuration into the head as JSON.
define(
[],
function () {
'use strict';
return {
run: function (conf, doc, cb, msg) {
msg.pub('start', 'core/include-config');
var initialUserConfig;
try {
if (Object.assign) {
initialUserConfig = Object.assign({}, conf);
} else {
initialUserConfig = JSON.parse(JSON.stringify(conf));
}
} catch (err) {
initialUserConfig = {};
}
msg.sub('end-all', function () {
var script = doc.createElement('script');
script.id = 'initialUserConfig';
var confFilter = function (key, val) {
// DefinitionMap contains array of DOM elements that aren't serializable
// we replace them by their id
if (key === 'definitionMap') {
var ret = {};
Object
.keys(val)
.forEach(function (k) {
ret[k] = val[k].map(function (d) {
return d[0].id;
});
});
return ret;
}
return val;
};
script.innerHTML = JSON.stringify(initialUserConfig, confFilter, 2);
script.type = 'application/json';
doc.head.appendChild(script);
conf.initialUserConfig = initialUserConfig;
});
msg.pub('end', 'core/include-config');
cb();
}
};
}
);