Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions js/smartdocs_integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,38 @@
Drupal.behaviors.smartdocsFieldFormatter = {
attach: function (context) {
let specMap = {};
let pendingRequests = 0;

for (let fieldName in drupalSettings.smartdocsFieldFormatter) {
if (drupalSettings.smartdocsFieldFormatter.hasOwnProperty(fieldName)) {
const field = drupalSettings.smartdocsFieldFormatter[fieldName];
for (let fieldDelta = 0; fieldDelta < field.openApiFiles.length; fieldDelta++) {
// Each openApiFile var has the spec url and file extension.
const fileUrl = field.openApiFiles[fieldDelta].fileUrl;
const fileExtention = field.openApiFiles[fieldDelta].fileExtension;

// Track pending AJAX requests
pendingRequests++;

// Get the OpenAPI spec file from the server.
$.get(fileUrl, function(data, status) {
if (fileExtention === 'json') {
// JSON files do not need any conversion.
specMap[field.entityId] = data;
$.ajax({
url: fileUrl,
async: false, // Make synchronous to ensure data loads before SmartDocs initializes
success: function(data, status) {
if (fileExtention === 'json') {
// JSON files do not need any conversion.
specMap[field.entityId] = data;
}
else {
// YAML files need to be parsed into javascript object.
specMap[field.entityId] = jsyaml.load(data);
}
// Store the spec into session storage.
sessionStorage.setItem('specs', JSON.stringify(specMap));
},
complete: function() {
pendingRequests--;
}
else {
// YAML files need to be parsed into javascript object.
specMap[field.entityId] = jsyaml.load(data);
}
// Store the spec into session storage.
sessionStorage.setItem('specs', JSON.stringify(specMap));
});
}
}
Expand Down
Loading