UNPKG

app-base-react

Version:
1 lines 11.8 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.defaultGetId=exports.mergeInOrder=exports.transformProps=exports.schemaToState=exports.newSchemaToOld=exports.oldSchemaToNew=exports.getParentProps=exports.flattenToData=exports.dataToFlatten=exports.dropItem=exports.addItem=exports.copyItem=exports.changeKeyFromUniqueId=exports.getKeyFromUniqueId=exports.isObject=void 0;var _slicedToArray=function(e,t){if(Array.isArray(e))return e;if(Symbol.iterator in Object(e)){var r=t,n=[],o=!0,t=!1,i=void 0;try{for(var a,s=e[Symbol.iterator]();!(o=(a=s.next()).done)&&(n.push(a.value),!r||n.length!==r);o=!0);}catch(e){t=!0,i=e}finally{try{!o&&s.return&&s.return()}finally{if(t)throw i}}return n}throw new TypeError("Invalid attempt to destructure non-iterable instance")},_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},_extends=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r,n=arguments[t];for(r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},_clone=(exports.isLooselyNumber=isLooselyNumber,exports.isCssLength=isCssLength,exports.isDeepEqual=isDeepEqual,exports.combineSchema=combineSchema,exports.isFunction=isFunction,exports.isFunctionSchema=isFunctionSchema,exports.flattenSchema=flattenSchema,exports.idToSchema=idToSchema,exports.looseJsonParse=looseJsonParse,exports.getChildren2=getChildren2,exports.getDataById=getDataById,exports.defaultGetValueFromEvent=defaultGetValueFromEvent,require("clone")),_clone2=_interopRequireDefault(_clone),_nanoid=require("nanoid");function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function _defineProperty(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function _objectWithoutProperties(e,t){var r,n={};for(r in e)0<=t.indexOf(r)||Object.prototype.hasOwnProperty.call(e,r)&&(n[r]=e[r]);return n}function stringContains(e,t){return-1<e.indexOf(t)}var isObject=exports.isObject=function(e){return stringContains(Object.prototype.toString.call(e),"Object")};function isLooselyNumber(e){return"number"==typeof e||"string"==typeof e&&!Number.isNaN(Number(e))}function isCssLength(e){return"string"==typeof e&&e.match(/^([0-9])*(%|px|rem|em)$/i)}function isDeepEqual(e,t){if(void 0!==e||void 0!==t){if(void 0===e||void 0===t)return!1;if(null!==e||null!==t){if(null===e||null===t)return!1;if(e.constructor!==t.constructor)return!1;if(e.constructor===Array){if(e.length!==t.length)return!1;for(var r=0;r<e.length;r++)if(e[r].constructor===Array||e[r].constructor===Object){if(!isDeepEqual(e[r],t[r]))return!1}else if(e[r]!==t[r])return!1}else if(e.constructor===Object){if(Object.keys(e).length!==Object.keys(t).length)return!1;for(var n=0;n<Object.keys(e).length;n++){var o=Object.keys(e)[n];if(!e[o]||"number"==typeof e[o]||e[o].constructor!==Array&&e[o].constructor!==Object){if(e[o]!==t[o])return!1}else if(!isDeepEqual(e[o],t[o]))return!1}}else if(e.constructor===String||e.constructor===Number)return e===t}}return!0}function combineSchema(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},a=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},t=getChildren(e).map(function(e){var t=e.name,r=e.schema,n=r.type,o=r.enum,i=r.properties,r=r.items,i="object"===n&&i,t=t&&a[e.name];return t?"array"===n&&r&&!o?(n=combineSchema(r,t.items||{}),_extends({},e,{schema:_extends({},e.schema,t,{items:n})})):i?(o=combineSchema(e.schema,t),_extends({},e,{schema:o})):_extends({},e,{schema:_extends({},e.schema,t)}):e}),r={},n=(t.forEach(function(e){r[e.name]=e.schema}),{});return Object.keys(a).forEach(function(e){"string"==typeof e&&"ui:"===e.substring(0,3)&&(n[e]=a[e])}),isEmpty(r)?_extends({},e,n):_extends({},e,n,{properties:r})}function isEmpty(e){return 0===Object.keys(e).length}function getChildren(e){var t,r,n;return e&&(t=e.properties,r=e.items,e=e.type,t||r)?(n="array"===e?r:"object"===e?t:{},Object.keys(n).map(function(e){return{schema:n[e],name:e}})):[]}function isFunction(e){return"function"==typeof e||("string"==typeof e&&"@"===e.substring(0,1)?e.substring(1):"string"==typeof e&&"{{"===e.substring(0,2)&&"}}"===e.substring(e.length-2,e.length)&&e.substring(2,e.length-2))}function isFunctionSchema(t){return Object.keys(t).some(function(e){return"function"==typeof t[e]||("string"==typeof t[e]?isFunction(t[e]):"object"===_typeof(t[e])&&isFunctionSchema(t[e]))})}function flattenSchema(e){var r=1<arguments.length&&void 0!==arguments[1]?arguments[1]:"#",t=arguments[2],n=3<arguments.length&&void 0!==arguments[3]?arguments[3]:{},e=(0,_clone2.default)(e),o=(e.$id||(e.$id=r),[]),i="object"===e.type&&e.properties,a="array"===e.type&&e.items&&e.items.properties;return i&&(Object.entries(e.properties).forEach(function(e){var e=_slicedToArray(e,2),t=e[0],e=e[1],t=r+"/"+t;o.push(t),flattenSchema(e,t,r,n)}),delete e.properties),a&&(Object.entries(e.items.properties).forEach(function(e){var e=_slicedToArray(e,2),t=e[0],e=e[1],t=r+"/"+t;o.push(t),flattenSchema(e,t,r,n)}),delete e.items.properties),e.type&&(n[r]={parent:t,schema:e,children:o}),n}var getKeyFromUniqueId=exports.getKeyFromUniqueId=function(){var e=(0<arguments.length&&void 0!==arguments[0]?arguments[0]:"#").split("/");return e[e.length-1]},changeKeyFromUniqueId=exports.changeKeyFromUniqueId=function(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"#",t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:"something",e=e.split("/");return"string"!=typeof t&&"number"!=typeof t||(e[e.length-1]=t),e.join("/")};function idToSchema(r){var e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:"#",n=2<arguments.length&&void 0!==arguments[2]&&arguments[2],o={},e=r[e],e=(0,_clone2.default)(e);return e&&(o=_extends({},e.schema),n&&o.$id&&delete o.$id,0<e.children.length?e.children.forEach(function(e){var t=e;if(r[e]){try{n&&(t=r[e].schema.$id)}catch(e){console.error(e,"catch")}t=getKeyFromUniqueId(t);"object"===o.type&&(o.properties||(o.properties={}),o.properties[t]=idToSchema(r,e,n)),"array"===o.type&&o.items&&"object"===o.items.type&&(o.items.properties||(o.items.properties={}),o.items.properties[t]=idToSchema(r,e,n))}}):("object"!==o.type||o.properties||(o.properties={}),"array"===o.type&&o.items&&"object"===o.items.type&&!o.items.properties&&(o.items.properties={}))),o}var copyItem=exports.copyItem=function(t,r,e){var n=_extends({},t);try{var o=t[r],i=e(r),a=n[o.parent].children,s=a.findIndex(function(e){return e===r});return a.splice(s+1,0,i),n[i]=(0,_clone2.default)(n[r]),n[i].schema.$id=i,[n,i]}catch(e){return console.error(e,"catcherror"),[t,r]}},addItem=exports.addItem=function(e){var t=e.selected,r=e.name,n=e.schema,o=e.flatten,i=e.fixedName,e=e.getId,a=t||"#",s=void 0;if(a&&"0"===a[0]||"#"===a){var c=_extends({},o);try{var u=a.substring(1),s="#"===a?"#/":u+"/";s+=i?r:e(r);c[u="#"===a?"#":u].children.push(s);var p={parent:u,schema:_extends({},n,{$id:s}),data:void 0,children:[]};c[s]=p}catch(e){console.error(e,"catch")}return{newId:s,newFlatten:c}}a=i?r:e(r),u=t.split("/"),u.pop(),u.push(a),s=u.join("/"),p=_extends({},o);try{var l=p[t],d=p[l.parent].children,f=d.findIndex(function(e){return e===t}),h=(d.splice(f+1,0,s),{parent:l.parent,schema:_extends({},n,{$id:s}),data:void 0,children:[]});p[s]=h}catch(e){console.error(e)}return{newId:s,newFlatten:p}},dropItem=exports.dropItem=function(e){var t=e.dragId,r=e.dragItem,n=e.dropId,o=e.position,e=e.flatten,o="#"===n?"inside":o,e=_extends({},e),i=(r&&(e[t]=r),r||e[t]),a=c=e[n];if("inside"!==o&&(a=e[c.parent]),-1<n.indexOf(t))return[e,t];c=t;try{var s=a.schema.$id,c=c.replace(i.parent,s)}catch(e){}try{var u=e[i.parent],p=u.children.indexOf(t);-1<p&&!r&&u.children.splice(p,1)}catch(e){console.error(e)}try{var l=a.children||[],d=l.indexOf(n);switch(o){case"up":l.splice(d,0,t);break;case"down":l.splice(d+1,0,t);break;default:l.push(t)}a.children=l}catch(e){console.error(e)}return i.parent=a.$id,[e,c]},dataToFlatten=exports.dataToFlatten=function(r,n){return r&&n?(Object.entries(r).forEach(function(e){var e=_slicedToArray(e,2),t=e[0],e=(e[1],getDataById(n,t));r[t].data=e}),r):{}},flattenToData=exports.flattenToData=function r(n){var o=1<arguments.length&&void 0!==arguments[1]?arguments[1]:"#";try{var i,a=n[o].data,e=Object.keys(n).filter(function(e){var t=o.split("/").length,r=e.split("/").length;return-1<e.indexOf(o)&&t<r});return e&&0<e.length&&(i=n[o].schema.type,void 0===a&&("object"===i?a={}:"array"===i&&(a=[{}])),e.forEach(function(e){var t=o.split("/").length;if(e.split("/").length===t+1){t=r(n,e),e=getKeyFromUniqueId(e);if(void 0===t)return a;"array"===i?a[0][e]=t:a[e]=t}})),a}catch(e){}},getParentProps=exports.getParentProps=function e(t,r,n){try{var o,i=n[r];return void 0!==i.schema[t]?i.schema[t]:i&&i.parent?void 0!==(o=n[i.parent].schema)[t]?o[t]:e(t,i.parent,n):void 0}catch(e){}};function looseJsonParse(e){return Function('"use strict";return ('+e+")")()}function getChildren2(e){var t,r,n;return e&&(t=e.properties,r=e.items,e=e.type,t||r)?(n="object"===e?t:{},"array"===e&&(n=r.properties),Object.keys(n).map(function(e){return{schema:n[e],name:e}})):[]}function getDataById(e,t){for(var r=0,n=(t=castPath(t,e)).length;null!=e&&r<n;){var o=toKey(t[r++]);e=Array.isArray(e)&&"object"===_typeof(e[0])?o?e[0][o]:e:o?e[o]:e}return r&&r==n?e:void 0}function castPath(e,t){return Array.isArray(e)?e:isKey(e,t)?[e]:e.match(/([^\.\/\[\]#"']+)/g)}function toKey(e){var t;return"string"==typeof e?e.replace(/^#\/?/,""):"0"==(t=""+e)&&1/e==-INFINITY?"-0":t}var reIsDeepProp=/#\/.+\//,reIsPlainProp=/^\w*$/;function isKey(e,t){var r;return!Array.isArray(e)&&("number"===(r=void 0===e?"undefined":_typeof(e))||"boolean"===r||null==e||reIsPlainProp.test(e)||!reIsDeepProp.test(e)||null!=t&&e in Object(t))}var oldSchemaToNew=function(e){var t,r;return e&&e.propsSchema?(t=e.propsSchema,r=_objectWithoutProperties(e,["propsSchema"]),_extends({schema:t},r)):e&&e.schema?e.schema:e&&"object"===e.type?e:_extends({type:"object",properties:{}},e)},newSchemaToOld=(exports.oldSchemaToNew=oldSchemaToNew,function(e){var t,r;return e&&e.schema?(t=e.schema,r=_objectWithoutProperties(e,["schema"]),_extends({propsSchema:t},r)):e}),schemaToState=(exports.newSchemaToOld=newSchemaToOld,exports.schemaToState=function(e){var r=oldSchemaToNew(e),t=Object.keys(r).reduce(function(e,t){return["type","properties"].includes(t)?e:_extends({},e,_defineProperty({},t,r[t]))},{}),e=!(e&&e.propsSchema);return{schema:r,frProps:t,formData:r.formData||{},isNewVersion:e}});function defaultGetValueFromEvent(e){var t=arguments.length<=1?void 0:arguments[1];return t&&t.target&&e in t.target?t.target[e]:t}var transformProps=function(e){function t(){for(var e=arguments.length,t=Array(e),r=0;r<e;r++)t[r]=arguments[r];var n=defaultGetValueFromEvent.apply(void 0,[u].concat(t));o(n)}var o=e.onChange,r=e.value,n=e.defaultValue,i=e.schema,e=_objectWithoutProperties(e,["onChange","value","defaultValue","schema"]),i=_extends({},i),a=i||{},s=a.trigger,a=a.valuePropName,c={},u="value",n=void 0===r?n:r,r=(a&&"string"==typeof a?c[u=a]=n:c.value=n,s&&"string"==typeof s?c[s]=t:c.onChange=t,{disabled:i.disabled||i["ui:disabled"],readOnly:i.readOnly||i["ui:readonly"]});return _extends({},c,{schema:i},r,e)},mergeInOrder=(exports.transformProps=transformProps,exports.mergeInOrder=function(){for(var e=arguments.length,t=Array(e),r=0;r<e;r++)t[r]=arguments[r];return t.reduce(function(e,r){return r?Object.keys(r).reduce(function(e,t){return e[t]&&delete e[t],_extends({},e,_defineProperty({},t,r[t]))},e):e},{})}),defaultGetId=exports.defaultGetId=function(t){return["name","sex","unit","position","mobile","email","idNo"].filter(function(e){return e===t}).length?t:t+"_"+(0,_nanoid.nanoid)(6)};