aglio-theme-goose
Version:
Custom theme for the Aglio API Blueprint renderer
93 lines (86 loc) • 2.93 kB
JavaScript
// Generated by CoffeeScript 2.2.1
(function() {
// Handle MSON inheritance. This is interesting because certain attributes,
// items, members, etc can be overridden. For example, the `id` property is
// overridden to be any valid `string` below:
// # My Type
// + id (number)
// + name (string)
// # Another Type (My Type)
// + id (string)
// Make sure all members are unique, removing all duplicates before the last
// occurence of the member key name.
var uniqueMembers,
hasProp = {}.hasOwnProperty;
uniqueMembers = function(content) {
var i, key, known, results;
known = [];
i = content.length - 1;
results = [];
while (i >= 0) {
if (content[i].element === 'member') {
key = content[i].content.key.content;
if (known.indexOf(key) !== -1) {
content.splice(i, 1);
continue;
}
known.push(key);
}
results.push(i--);
}
return results;
};
// Have `element` inherit from `base`.
module.exports = function(base, element) {
var combined, item, j, key, len, ref, ref1, ref2, ref3, ref4, value;
// First, we do a deep copy of the base (parent) element
combined = JSON.parse(JSON.stringify(base));
// Next, we copy or overwrite any metadata and attributes
if (element.meta) {
if (combined.meta == null) {
combined.meta = {};
}
ref = element.meta;
for (key in ref) {
if (!hasProp.call(ref, key)) continue;
value = ref[key];
combined.meta[key] = value;
}
}
if (element.attributes) {
if (combined.attributes == null) {
combined.attributes = {};
}
ref1 = element.attributes;
for (key in ref1) {
if (!hasProp.call(ref1, key)) continue;
value = ref1[key];
combined.attributes[key] = value;
}
}
// Lastly, we combine the content if we can. For simple types, this means
// overwriting the content. For arrays it adds to the content list and for
// objects is adds *or* overwrites (if an existing key already exists).
if (element.content) {
if (((ref2 = combined.content) != null ? ref2.push : void 0) || ((ref3 = element.content) != null ? ref3.push : void 0)) {
// This could be an object or array
if (combined.content == null) {
combined.content = [];
}
ref4 = element.content;
for (j = 0, len = ref4.length; j < len; j++) {
item = ref4[j];
combined.content.push(item);
}
if (combined.content.length && combined.content[0].element === 'member') {
// This is probably an object - remove duplicate keys!
uniqueMembers(combined.content);
}
} else {
// Not an array or object, just overwrite the content
combine.content = element.content;
}
}
return combined;
};
}).call(this);