Skip to content

Commit

Permalink
chore: add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpmule committed Oct 26, 2023
1 parent 7acc8e6 commit 16558c2
Showing 1 changed file with 49 additions and 19 deletions.
68 changes: 49 additions & 19 deletions src/ApiRequestEditorElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
}
}

Expand Down

0 comments on commit 16558c2

Please sign in to comment.