forked from apostrophecms-legacy/apostrophe-override-options
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
225 lines (207 loc) · 8.03 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
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
var _ = require('lodash');
module.exports = {
moogBundle: {
modules: [ 'apostrophe-override-options-module', 'apostrophe-override-options-pages', 'apostrophe-override-options-doc-type-manager', 'apostrophe-override-options-pieces-pages' ],
directory: 'lib/modules'
},
construct: function(self, options) {
self.modulesReady = function() {
self.compileLocaleTree();
},
// Populates `req.aposOptions` with a version of
// the options object for each module that has been
// overridden by settings related to the current page,
// per the documentation of this module. Invoked
// as soon as possible by an override of
// `apos.pages.serveLoaders` and also in certain
// other contexts such as routes relating to the
// editing of page settings.
self.calculateOverrides = function(req) {
req.aposOptions = {};
var workflow = self.apos.modules['apostrophe-workflow'];
_.each(self.apos.modules, function(module, name) {
// This is not a deep clone. Deep cloning is used
// in overrideKey below for the primaries (top level options)
// that are altered. This leaves options.apos alone,
// for instance, and potentially other expensive/large
// objects as well
req.aposOptions[name] = _.cloneWith(module.options, cloneCustom);
req.aposOptions[name].__clonedPrimaries = {};
if (workflow) {
var locale = workflow.liveify(req.locale);
_.each(self.localeAncestors[locale] || [], function(locale) {
_.each((module.options.localized && module.options.localized[locale]) || {}, function(val, key) {
self.overrideLocalKey(req, module, key, val);
});
});
}
});
self.applyOverridesFromDoc(req, req.data.global || {});
// clone so we are not modifying bestPage._ancestors
var ancestors = _.clone((req.data.bestPage && req.data.bestPage._ancestors) || []);
if (req.data.bestPage) {
ancestors.push(req.data.bestPage);
}
_.each(ancestors, function(ancestor) {
self.applyOverridesFromDoc(req, ancestor);
});
};
self.overrideLocalKey = function(req, module, key, val) {
return self.overrideKey(req, 'apos.' + module.__meta.name + '.' + key, val);
};
self.overrideKey = function(req, key, val) {
var path = key.split(/\./);
var primary;
if (path[0] !== 'apos') {
return new Error('Key for fixed override must start with apos. and an instantiated module name or alias');
}
var module = self.apos.modules[path[1]];
if (!module) {
module = self.apos[path[1]];
if (!module || module.alias !== path[1]) {
// Something sneaky is going on
return new Error('Key for fixed override must start with apos. and an instantiated existing module name or alias');
}
}
if (!module) {
return new Error('Key for fixed override must start with apos. and an instantiated module name or alias');
}
primary = path[2];
if (primary === 'apos') {
// Cloning it deeply would be prohibitively expensive
return new Error('Option overrides may not alter the apos object passed to a module');
}
var name = module.__meta.name;
if (!req.aposOptions[name].__clonedPrimaries[primary]) {
req.aposOptions[name][primary] = _.cloneDeepWith(req.aposOptions[module.__meta.name][primary], cloneCustom);
req.aposOptions[name].__clonedPrimaries[primary] = true;
}
var array;
var added;
var moduleOptions = req.aposOptions[name];
var sliced = path.slice(2);
if (val && (typeof(val) === 'object')) {
if (val.$append) {
array = _.get(moduleOptions, sliced) || [];
_.set(moduleOptions, sliced, array.concat(val.$append));
} else if (val.$prepend) {
array = _.get(moduleOptions, sliced) || [];
_.set(moduleOptions, sliced, val.$prepend.concat(array));
} else if (val.$appendUnique) {
array = _.get(moduleOptions, sliced) || [];
added = _.differenceWith(val.$appendUnique, array, _.isEqual);
_.set(moduleOptions, sliced, array.concat(added));
} else if (val.$prependUnique) {
array = _.get(moduleOptions, sliced) || [];
added = _.differenceWith(val.$prependUnique, array, _.isEqual);
_.set(moduleOptions, sliced, added.concat(array || []));
} else if (val.$remove) {
array = _.get(moduleOptions, sliced);
array = _.differenceWith(array, val.$remove, _.isEqual);
_.set(moduleOptions, sliced, array);
} else if (val.$assign) {
// As an escape mechanism
_.set(moduleOptions, sliced, val.$assign);
} else {
_.set(moduleOptions, sliced, val);
}
} else if (val && (typeof(val) === 'function')) {
_.set(moduleOptions, sliced,
val(req, moduleOptions, sliced, _.get(moduleOptions, sliced))
);
} else {
_.set(moduleOptions, sliced, val);
}
};
// Apply option overrides based on a particular document's
// type and settings, per the `overrideOptions` configuration
// of its manager module or, if provided, the module passed
// as `optionsSource`.
self.applyOverridesFromDoc = function(req, doc, optionsSource) {
var manager;
if (!optionsSource) {
optionsSource = self.apos.docs.getManager(doc.type);
}
if (!optionsSource) {
return;
}
if (!optionsSource.options.overrideOptions) {
return;
}
var overrideOptions = optionsSource.options.overrideOptions;
var fixed = overrideOptions.fixed;
var localized = overrideOptions.localized;
var editable = overrideOptions.editable;
var workflow = self.apos.modules['apostrophe-workflow'];
var locale;
_.each(fixed || {}, function(val, key) {
self.overrideKey(req, key, val);
});
if (workflow && localized) {
locale = workflow.liveify(req.locale);
_.each(self.localeAncestors[locale] || [], function(locale) {
if (localized) {
_.each((localized[locale]) || {}, function(val, key) {
self.overrideKey(req, key, val);
});
}
});
}
_.each(editable || {}, function(field, key) {
var verb;
var object;
if (typeof(field) === 'object') {
// It's a command like $append. Build an
// object like { $append: [ 5 ] } from an
// object like { $append: 'fieldname' }.
// If the field is empty treat it as
// appending nothing. This code looks weird
// because I'm avoiding hardcoding this for
// every verb.
verb = _.keys(field)[0];
field = _.values(field)[0];
val = doc[field];
if (!Array.isArray(val)) {
if (val || (val === 0)) {
val = [ val ];
} else {
val = [];
}
}
object = {};
object[verb] = val;
val = object;
} else {
val = doc[field];
}
if (val || (val === 0)) {
self.overrideKey(req, key, val);
}
});
};
self.compileLocaleTree = function() {
var workflow = self.apos.modules['apostrophe-workflow'];
if (!workflow) {
return;
}
var locales = workflow.options.locales;
self.localeAncestors = {};
exploreLocales(locales, []);
function exploreLocales(locales, ancestors) {
_.each(locales, function(locale) {
self.localeAncestors[locale.name] = ancestors.concat([ locale.name ]);
if (locale.children) {
exploreLocales(locale.children, ancestors.concat([ locale.name ]));
}
});
}
};
// Make sure cloned options still include functions.
// _.clone handles most other types well.
function cloneCustom(item) {
if (typeof(item) === 'function') {
return item;
}
}
}
};