|
| 1 | +import {stringify, parse} from './sfPath'; |
| 2 | +import canonicalTitleMap from './canonicalTitleMap'; |
| 3 | + |
| 4 | + |
| 5 | +//export function merge(schema, form, schemaDefaultTypes, ignore, options, readonly, asyncTemplates) { |
| 6 | +export function merge(lookup, form, options, readonly, asyncTemplates) { |
| 7 | + form = form || []; |
| 8 | + options = options || {}; |
| 9 | + |
| 10 | + //ok let's merge! |
| 11 | + //We look at the supplied form and extend it with schema standards |
| 12 | + return form.map((obj) => { |
| 13 | + |
| 14 | + //handle the shortcut with just a name |
| 15 | + if (typeof obj === 'string') { |
| 16 | + obj = {key: obj}; |
| 17 | + } |
| 18 | + |
| 19 | + if (obj.key) { |
| 20 | + if (typeof obj.key === 'string') { |
| 21 | + obj.key = parse(obj.key); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + //If it has a titleMap make sure it's a list |
| 26 | + if (obj.titleMap) { |
| 27 | + obj.titleMap = canonicalTitleMap(obj.titleMap); |
| 28 | + } |
| 29 | + |
| 30 | + //extend with std form from schema. |
| 31 | + if (obj.key) { |
| 32 | + const strid = stringify(obj.key); |
| 33 | + if (lookup[strid]) { |
| 34 | + const schemaDefaults = lookup[strid]; |
| 35 | + if (schemaDefaults) { |
| 36 | + Object.keys(schemaDefaults).forEach((attr) => { |
| 37 | + if (obj[attr] === undefined) { |
| 38 | + obj[attr] = schemaDefaults[attr]; |
| 39 | + } |
| 40 | + }); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + // Are we inheriting readonly? |
| 46 | + if (readonly === true) { // Inheriting false is not cool. |
| 47 | + obj.readonly = true; |
| 48 | + } |
| 49 | + |
| 50 | + //if it's a type with items, merge 'em! |
| 51 | + if (obj.items) { |
| 52 | + obj.items = merge(lookup, obj.items, options, obj.readonly, asyncTemplates); |
| 53 | + } |
| 54 | + |
| 55 | + //if its has tabs, merge them also! |
| 56 | + if (obj.tabs) { |
| 57 | + obj.tabs.forEach((tab) => { |
| 58 | + if (tab.items) { |
| 59 | + tab.items = merge(lookup, tab.items, options, obj.readonly, asyncTemplates); |
| 60 | + } |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + // Special case: checkbox |
| 65 | + // Since have to ternary state we need a default |
| 66 | + if (obj.type === 'checkbox' && obj.schema['default'] === undefined) { |
| 67 | + obj.schema['default'] = false; |
| 68 | + } |
| 69 | + |
| 70 | + // Special case: template type with tempplateUrl that's needs to be loaded before rendering |
| 71 | + // TODO: this is not a clean solution. Maybe something cleaner can be made when $ref support |
| 72 | + // is introduced since we need to go async then anyway |
| 73 | + if (asyncTemplates && obj.type === 'template' && !obj.template && obj.templateUrl) { |
| 74 | + asyncTemplates.push(obj); |
| 75 | + } |
| 76 | + |
| 77 | + return obj; |
| 78 | + }); |
| 79 | +} |
0 commit comments