From 16558c2eefbcc61cce647f51d30f87b178b3ab77 Mon Sep 17 00:00:00 2001 From: alexperez Date: Thu, 26 Oct 2023 02:13:36 -0300 Subject: [PATCH] chore: add comments --- src/ApiRequestEditorElement.js | 68 ++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 19 deletions(-) diff --git a/src/ApiRequestEditorElement.js b/src/ApiRequestEditorElement.js index 01fe58e..b8be950 100644 --- a/src/ApiRequestEditorElement.js +++ b/src/ApiRequestEditorElement.js @@ -601,39 +601,69 @@ export class ApiRequestEditorElement extends AmfHelperMixin( }); } + /** + * This function is called when the AMF model change and the element needs to update + * Given the current selection, it updates the selected operation + * + * To find the selected, this method searches the lexical value of the + * operation changed value and then searches this in the new AMF model. + * + * When not found operation return previous selection. + * @param {AMF} amf + * @param {object} operationChanged + * @return {string} The new selected operation + * @example '#14' + **/ _computeSelected(amf, operationChanged) { if (operationChanged && amf && amf[0]) { - const opKey = this._getAmfKey(this.ns.aml.vocabularies.apiContract.supportedOperation); - const lexicalKey = this._getAmfKey(this.ns.aml.vocabularies.docSourceMaps.lexical); - const lexicalValueKey = this._getAmfKey(this.ns.aml.vocabularies.docSourceMaps.value); - const sourceKey = this._getAmfKey(this.ns.aml.vocabularies.docSourceMaps.sources); - + // get required keys + const opKey = this._getAmfKey( + this.ns.aml.vocabularies.apiContract.supportedOperation + ); + const lexicalKey = this._getAmfKey( + this.ns.aml.vocabularies.docSourceMaps.lexical + ); + const lexicalValueKey = this._getAmfKey( + this.ns.aml.vocabularies.docSourceMaps.value + ); + const sourceKey = this._getAmfKey( + this.ns.aml.vocabularies.docSourceMaps.sources + ); + + // search the lexical value of the operation changed const sourcesOld = this._getValueArray(operationChanged, sourceKey); - const lexicalsOld = this._getValueArray(sourcesOld[0], lexicalKey) - const lexicalValueOld = this._getValue(lexicalsOld[0], lexicalValueKey) + const lexicalsOld = this._getValueArray(sourcesOld[0], lexicalKey); + const lexicalValueOld = this._getValue(lexicalsOld[0], lexicalValueKey); + // get enpoints from the new AMF model const encodes = this._computeEncodes(amf); const endpoints = this._computeEndpoints(encodes); - let newOperationSelected = null - endpoints.forEach((endpoint) => { + let newOperationSelected = null; + + // loop throught a list of endpoints and find the operation with the same lexical value + endpoints.forEach((endpoint) => { const supportedOperation = this._ensureArray(endpoint[opKey]); + // if the operation is not supported by the endpoint, skip it if (!supportedOperation) { - newOperationSelected = undefined + newOperationSelected = undefined; + } + // if the operation is supported by the endpoint, find the operation with the same lexical value + else { + newOperationSelected = supportedOperation.find((operation) => { + const sources = this._getValueArray(operation, sourceKey); + const lexicals = this._getValueArray(sources[0], lexicalKey); + const lexicalValue = this._getValue(lexicals[0], lexicalValueKey); + return lexicalValue === lexicalValueOld; + }); } - newOperationSelected = supportedOperation.find((operation) => { - const sources = this._getValueArray(operation, sourceKey); - const lexicals = this._getValueArray(sources[0], lexicalKey) - const lexicalValue = this._getValue(lexicals[0], lexicalValueKey) - return lexicalValue === lexicalValueOld - }); - }); + // if the operation is not found, return the previous selection if (!newOperationSelected) { return this.selected; } - - return newOperationSelected['@id']; + // if the operation is found, return the new selection + return newOperationSelected["@id"]; } }