|
| 1 | + |
| 2 | +import * as CONST from "../constants" |
| 3 | +import {getProperties} from "../FrameHelpers" |
| 4 | +import {getDataType} from "../dataTypeFrames/helpers" |
| 5 | + |
| 6 | +// retrieves layout of sys unit data type one ofs |
| 7 | +function getSysUnitTypeLayout(choice) { |
| 8 | + |
| 9 | + let layout = { |
| 10 | + "title": choice, |
| 11 | + "properties": { |
| 12 | + [choice]: { |
| 13 | + "type": "string", |
| 14 | + "title": choice, |
| 15 | + "default": "[]" |
| 16 | + } |
| 17 | + }, |
| 18 | + } |
| 19 | + |
| 20 | + let uiSchema = {"ui:widget": "hidden"} |
| 21 | + return {layout, uiSchema} |
| 22 | +} |
| 23 | + |
| 24 | +// retrieves layout of normal data type one ofs |
| 25 | +function getNormalDataTypeLayout(choice, dataType) { |
| 26 | + |
| 27 | + let layout = { |
| 28 | + "title": choice, |
| 29 | + "properties": { |
| 30 | + [choice]: { |
| 31 | + "type": getDataType(dataType), |
| 32 | + "title": choice |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + return layout |
| 37 | +} |
| 38 | + |
| 39 | +// return type and title from extractedProperties |
| 40 | +function extractPropertyFrames (extractedProperties) { |
| 41 | + let extractedLayout={} |
| 42 | + for(let props in extractedProperties) { |
| 43 | + if(extractedProperties[props]["type"] === CONST.STRING_TYPE) { |
| 44 | + extractedLayout[props] = { |
| 45 | + type: extractedProperties[props]["type"], |
| 46 | + title: extractedProperties[props]["title"] |
| 47 | + } |
| 48 | + } |
| 49 | + else if(extractedProperties[props]["type"] === CONST.OBJECT_TYPE) { |
| 50 | + extractedLayout[props] = { |
| 51 | + type: extractedProperties[props]["type"], |
| 52 | + title: extractedProperties[props]["title"], |
| 53 | + properties: extractedProperties[props]["properties"] |
| 54 | + } |
| 55 | + } |
| 56 | + // pass in default type here |
| 57 | + if(props === "@type") extractedLayout[props]["default"]=extractedProperties[props]["default"] |
| 58 | + } |
| 59 | + return extractedLayout |
| 60 | +} |
| 61 | + |
| 62 | +// retrieves layout of object data types one ofs |
| 63 | +function getDocumentLayout(choiceDocument, choiceDocumentFrames, oneOfLinkedClassName, args) { |
| 64 | + let {fullFrame, item, frame, uiFrame, mode, formData, onTraverse, onSelect, documentation, setChainedData} = args |
| 65 | + |
| 66 | + let choiceSubDocument=choiceDocumentFrames.hasOwnProperty("@class") ? choiceDocumentFrames["@class"] : choiceDocumentFrames |
| 67 | + let choiceSubDocumentFrame = fullFrame[choiceSubDocument] |
| 68 | + |
| 69 | + // get documentation from frame |
| 70 | + let extractedDocumentation= choiceSubDocumentFrame.hasOwnProperty(CONST.DOCUMENTATION) ? choiceSubDocumentFrame[CONST.DOCUMENTATION] : [] |
| 71 | + let exractedProperties = getProperties(fullFrame, item, choiceSubDocumentFrame, uiFrame, mode, formData, onTraverse, onSelect, extractedDocumentation, setChainedData) |
| 72 | + |
| 73 | + console.log("exractedProperties sub", exractedProperties) |
| 74 | + exractedProperties.properties["@type"]={ |
| 75 | + type: "string", |
| 76 | + title: choiceSubDocument, |
| 77 | + default: choiceSubDocument |
| 78 | + } |
| 79 | + |
| 80 | + // hide @type field |
| 81 | + exractedProperties.uiSchema["@type"]={"ui:widget": "hidden"} |
| 82 | + let uiSchema=exractedProperties.uiSchema |
| 83 | + |
| 84 | + let layout= { |
| 85 | + title: choiceDocument, |
| 86 | + properties: { |
| 87 | + [choiceDocument]: { |
| 88 | + title: choiceDocument, |
| 89 | + properties: extractPropertyFrames(exractedProperties.properties) |
| 90 | + }, |
| 91 | + "@type": { |
| 92 | + type: "string", |
| 93 | + title: oneOfLinkedClassName, |
| 94 | + default: oneOfLinkedClassName |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + let layout_o = { |
| 100 | + //title: choiceSubDocument, |
| 101 | + title: choiceDocument, |
| 102 | + properties: extractPropertyFrames(exractedProperties.properties) |
| 103 | + } |
| 104 | + return {layout, uiSchema} |
| 105 | +} |
| 106 | + |
| 107 | + |
| 108 | +export function getAnyOfSchema(args) { |
| 109 | + let { frame, fullFrame, item, mode, formData }=args |
| 110 | + let anyOfArray=[], anyOfUiSchema={} |
| 111 | + |
| 112 | + let oneOfLinkedClassName=frame[item]["@class"] |
| 113 | + let oneOfLinkedClassFrame=fullFrame[oneOfLinkedClassName] |
| 114 | + |
| 115 | + if(!frame.hasOwnProperty(item)) { |
| 116 | + throw new Error (`Expected frames to include ${item}, but received undefined ... `) |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + /** get all definitions of choices by looping over frame array */ |
| 121 | + let oneOfChoices=oneOfLinkedClassFrame["@oneOf"][0] // one of will have only array length 1 |
| 122 | + for(let choice in oneOfChoices) { |
| 123 | + if(typeof oneOfChoices[choice] === CONST.OBJECT_TYPE && |
| 124 | + oneOfChoices[choice].hasOwnProperty("@class")) { |
| 125 | + let {layout, uiSchema} =getDocumentLayout(choice, oneOfChoices[choice], oneOfLinkedClassName, args) |
| 126 | + anyOfUiSchema[choice]=uiSchema |
| 127 | + anyOfArray.push(layout) |
| 128 | + } |
| 129 | + else if(typeof oneOfChoices[choice] === CONST.STRING_TYPE) { |
| 130 | + if(fullFrame.hasOwnProperty(oneOfChoices[choice])) { |
| 131 | + let {layout, uiSchema} =getDocumentLayout(choice, oneOfChoices[choice], oneOfLinkedClassName, args) |
| 132 | + anyOfUiSchema=uiSchema |
| 133 | + anyOfArray.push(layout) |
| 134 | + } |
| 135 | + else if(oneOfChoices[choice] === CONST.SYS_UNIT_DATA_TYPE) { |
| 136 | + let {layout, uiSchema} = getSysUnitTypeLayout(choice) |
| 137 | + anyOfUiSchema[choice]=uiSchema |
| 138 | + anyOfArray.push(layout) |
| 139 | + } |
| 140 | + else anyOfArray.push(getNormalDataTypeLayout(choice, oneOfChoices[choice])) |
| 141 | + } |
| 142 | + |
| 143 | + } |
| 144 | + //console.log("anyOfArray", anyOfArray, anyOfUiSchema) |
| 145 | + return {anyOfArray, anyOfUiSchema} |
| 146 | +} |
| 147 | + |
| 148 | +/** |
| 149 | + * |
| 150 | + * @param {*} frame - any of choice document frames |
| 151 | + * @param {*} item - property |
| 152 | + * @param {*} formData - data to be displayed |
| 153 | + * @returns a filled array items with any of choice document only with filled info |
| 154 | + */ |
| 155 | +export function getOneOfTypeArrayItems (frame, item, formData) { |
| 156 | + let arrayItems=[] |
| 157 | + |
| 158 | + if(!formData) return arrayItems |
| 159 | + if(!Array.isArray(formData)) return arrayItems |
| 160 | + |
| 161 | + formData.map(data => { |
| 162 | + // get the choice which have been added |
| 163 | + //let choice = data["@type"] |
| 164 | + for(let props in data) { |
| 165 | + if(props === "@id") continue |
| 166 | + else if(props === "@type") continue |
| 167 | + else { |
| 168 | + let choice = props |
| 169 | + frame.properties[item].anyOf.map( any => { |
| 170 | + if(any.title === choice) { |
| 171 | + let chosenAnyOfFrame = any |
| 172 | + |
| 173 | + // new Item layout |
| 174 | + let newItemLayout = { |
| 175 | + type: CONST.OBJECT_TYPE, |
| 176 | + info: CONST.ONEOFVALUES, |
| 177 | + title: choice, |
| 178 | + anyOf: [chosenAnyOfFrame] |
| 179 | + } |
| 180 | + arrayItems.push(newItemLayout) |
| 181 | + } |
| 182 | + }) |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + }) |
| 187 | + |
| 188 | + return arrayItems |
| 189 | +} |
| 190 | + |
0 commit comments