diff --git a/dist/temme.js b/dist/temme.js index caecdbb..f5fde17 100644 --- a/dist/temme.js +++ b/dist/temme.js @@ -169,7 +169,10 @@ function _typeof(obj) { default: { ref: '', mode: 'append', - allowChildren: false + children: { + allow: false, + placement: 'after' + } }, type: 'object', keys: { @@ -188,18 +191,31 @@ function _typeof(obj) { return mode != null && typeof mode === 'string'; } }, - allowChildren: { - default: false, - type: 'boolean', - isValid: function isValid(allowChildren) { - return allowChildren != null && typeof allowChildren === 'boolean'; - } - }, - childrenFirst: { - default: false, - type: 'boolean', - isValid: function isValid(childrenFirst) { - return childrenFirst != null && typeof childrenFirst === 'boolean'; + children: { + default: { + allow: false, + placement: 'after' + }, + type: 'object', + keys: { + allow: { + default: false, + type: 'boolean', + isValid: function isValid(allow) { + return allow != null && typeof allow === 'boolean'; + } + }, + placement: { + default: 'after', + type: 'string', + values: ['after', 'before'], + isValid: function isValid(placement) { + return placement != null && typeof placement === 'string'; + } + } + }, + isValid: function isValid(children) { + return children != null && !Array.isArray(children) && _typeof(children) === 'object'; } } }, @@ -272,12 +288,23 @@ function _typeof(obj) { hierarchy['temmeIds'] = Array.from(temmeIds); hierarchy['temmeIds'].push(generateTemmeId()); // Populating empty options with defaults. - for (var _key in options) { - if (!(_key in hierarchy)) { - if (options[_key].type === 'object') { - hierarchy[_key] = Object.create(options[_key].default); + for (var _key2 in options) { + if (!(_key2 in hierarchy)) { + if (options[_key2].type === 'object') { + hierarchy[_key2] = Object.create(options[_key2].default); + } else { + hierarchy[_key2] = options[_key2].default; + } + } else if (_key2 === 'from') { + if (!('children' in hierarchy['from'])) { + hierarchy['from']['children'] = Object.create(options['from']['keys']['children'].default); } else { - hierarchy[_key] = options[_key].default; + // Checking of the object lacks any `from.children` keys and setting the default values. + for (var _key in options['from']['keys']['children']['keys']) { + if (!(_key in hierarchy['from']['children'])) { + hierarchy['from']['children'][_key] = options['from']['keys']['children']['keys'][_key].default; + } + } } } } // Checking if the element has children. @@ -370,7 +397,7 @@ function _typeof(obj) { { var _loop = function _loop(k) { // Avoiding inheriting the `from`, `name` options. - if (!['from', 'ref', 'id', 'name', 'temmeIds', hierarchy['from']['allowChildren'] !== true ? 'children' : ''].includes(k)) { + if (!['from', 'ref', 'id', 'name', 'temmeIds', hierarchy['from']['children']['allow'] !== true ? 'children' : ''].includes(k)) { switch (options[k].type) { case 'array': { @@ -385,17 +412,32 @@ function _typeof(obj) { hierarchy[k] = sanitizedClasses; } else { - // If the children append mode is on `childrenFirst`, meaning - // The referenced object's children should could before the - // referencing object's. Otherwise, the should come after. - if (hierarchy['from']['childrenFirst']) { - var _hierarchy$k; + // If the children's placement is set to `after`, meaning + // The referenced object's children should come after the + // referencing object's. Otherwise, the should come before. + switch (hierarchy['from']['children']['placement']) { + case 'after': + { + var _hierarchy$k; + + (_hierarchy$k = hierarchy[k]).push.apply(_hierarchy$k, _toConsumableArray(reference.refElement[k])); + + break; + } + + case 'before': + { + var _hierarchy$k2; + + (_hierarchy$k2 = hierarchy[k]).unshift.apply(_hierarchy$k2, _toConsumableArray(reference.refElement[k])); - (_hierarchy$k = hierarchy[k]).unshift.apply(_hierarchy$k, _toConsumableArray(reference.refElement[k])); - } else { - var _hierarchy$k2; + break; + } - (_hierarchy$k2 = hierarchy[k]).push.apply(_hierarchy$k2, _toConsumableArray(reference.refElement[k])); + default: + { + throw new TemmeError('InvalidReferencingObject', "\u201C".concat(hierarchy['from']['children']['placement'], "\u201D is an invalid placement.")); + } } } @@ -435,7 +477,7 @@ function _typeof(obj) { // looping through all the referenced object's options. for (var k in reference.refElement) { // Avoiding inheriting the `from` and `name. - if (!['from', 'ref', 'name', 'temmeIds', hierarchy['from']['allowChildren'] !== true ? 'children' : ''].includes(k)) { + if (!['from', 'ref', 'name', 'temmeIds', hierarchy['from']['children']['allow'] !== true ? 'children' : ''].includes(k)) { switch (options[k].type) { case 'object': { @@ -515,8 +557,8 @@ function _typeof(obj) { })[0]; } - if (hierarchy['from']['allowChildren'] === true && hierarchy['temmeIds'].includes(reference.refElement['temmeIds'][reference.refElement['temmeIds'].length - 1])) { - throw new TemmeError('InvalidReference', "Elements cannot reference their parents while “allowChildren” is set to “true”."); + if (hierarchy['from']['children']['allow'] === true && hierarchy['temmeIds'].includes(reference.refElement['temmeIds'][reference.refElement['temmeIds'].length - 1])) { + throw new TemmeError('InvalidReference', "Elements cannot reference their parents while “from.children.allow” is set to “true”."); } if (typeof reference !== 'undefined') { diff --git a/dist/temme.min.js b/dist/temme.min.js index 6174817..3287c33 100644 --- a/dist/temme.min.js +++ b/dist/temme.min.js @@ -1 +1 @@ -"use strict";function _toConsumableArray(e){return _arrayWithoutHoles(e)||_iterableToArray(e)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance")}function _iterableToArray(e){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e))return Array.from(e)}function _arrayWithoutHoles(e){if(Array.isArray(e)){for(var t=0,r=new Array(e.length);t mode != null && typeof mode === 'string' }, - allowChildren: { - default: false, - type: 'boolean', - isValid: (allowChildren) => allowChildren != null && typeof allowChildren === 'boolean' - }, - childrenFirst: { - default: false, - type: 'boolean', - isValid: (childrenFirst) => childrenFirst != null && typeof childrenFirst === 'boolean' + children: { + default: { allow: false, placement: 'after' }, + type: 'object', + keys: { + allow: { + default: false, + type: 'boolean', + isValid: (allow) => allow != null && typeof allow === 'boolean' + }, + placement: { + default: 'after', + type: 'string', + values: ['after', 'before'], + isValid: (placement) => placement != null && typeof placement === 'string' + } + }, + isValid: (children) => children != null && !Array.isArray(children) && typeof children === 'object' } }, isValid: (from) => from != null && !Array.isArray(from) && typeof from === 'object' @@ -209,13 +217,25 @@ hierarchy['temmeIds'].push(generateTemmeId()); // Populating empty options with defaults. - for (let key in options) { + for (const key in options) { if (!(key in hierarchy)) { if (options[key].type === 'object') { hierarchy[key] = Object.create(options[key].default); } else { hierarchy[key] = options[key].default; } + } else if (key === 'from') { + if (!('children' in hierarchy['from'])) { + hierarchy['from']['children'] = Object.create(options['from']['keys']['children'].default); + } else { + + // Checking of the object lacks any `from.children` keys and setting the default values. + for (const _key in options['from']['keys']['children']['keys']) { + if (!(_key in hierarchy['from']['children'])) { + hierarchy['from']['children'][_key] = options['from']['keys']['children']['keys'][_key].default; + } + } + } } } @@ -359,8 +379,8 @@ .sort((refA, refB) => refB.depth - refA.depth)[0]; } - if (hierarchy['from']['allowChildren'] === true && hierarchy['temmeIds'].includes(reference.refElement['temmeIds'][reference.refElement['temmeIds'].length - 1])) { - throw new TemmeError('InvalidReference', "Elements cannot reference their parents while “allowChildren” is set to “true”."); + if (hierarchy['from']['children']['allow'] === true && hierarchy['temmeIds'].includes(reference.refElement['temmeIds'][reference.refElement['temmeIds'].length - 1])) { + throw new TemmeError('InvalidReference', "Elements cannot reference their parents while “from.children.allow” is set to “true”."); } if (typeof reference !== 'undefined') { @@ -388,7 +408,7 @@ for (let k in reference.refElement) { // Avoiding inheriting the `from`, `name` options. - if (!['from', 'ref', 'id', 'name', 'temmeIds', (hierarchy['from']['allowChildren'] !== true ? 'children' : '')].includes(k)) { + if (!['from', 'ref', 'id', 'name', 'temmeIds', (hierarchy['from']['children']['allow'] !== true ? 'children' : '')].includes(k)) { switch (options[k].type) { case 'array': { @@ -396,21 +416,33 @@ // Removing any duplicate classes. const filteredClasses = reference.refElement[k].filter((cls, index) => !hierarchy[k].includes(cls) && reference.refElement[k].indexOf(cls) === index && cls.trim().length > 0); - + // Sorting and concatinating the classes. const sanitizedClasses = ([...hierarchy[k], ...filteredClasses]).sort(); - + // Assigning the classes. hierarchy[k] = sanitizedClasses; } else { - // If the children append mode is on `childrenFirst`, meaning - // The referenced object's children should could before the - // referencing object's. Otherwise, the should come after. - if (hierarchy['from']['childrenFirst']) { - hierarchy[k].unshift(...reference.refElement[k]); - } else { - hierarchy[k].push(...reference.refElement[k]); + // If the children's placement is set to `after`, meaning + // The referenced object's children should come after the + // referencing object's. Otherwise, the should come before. + switch (hierarchy['from']['children']['placement']) { + case 'after': { + hierarchy[k].push(...reference.refElement[k]); + + break; + } + + case 'before': { + hierarchy[k].unshift(...reference.refElement[k]); + + break; + } + + default: { + throw new TemmeError('InvalidReferencingObject', `“${hierarchy['from']['children']['placement']}” is an invalid placement.`); + } } } @@ -445,7 +477,7 @@ for (let k in reference.refElement) { // Avoiding inheriting the `from` and `name. - if (!['from', 'ref', 'name', 'temmeIds', (hierarchy['from']['allowChildren'] !== true ? 'children' : '')].includes(k)) { + if (!['from', 'ref', 'name', 'temmeIds', (hierarchy['from']['children']['allow'] !== true ? 'children' : '')].includes(k)) { switch (options[k].type) { case 'object': { diff --git a/tests/sandbox/index.html b/tests/sandbox/index.html index 603941b..263f2af 100644 --- a/tests/sandbox/index.html +++ b/tests/sandbox/index.html @@ -1,7 +1,7 @@