|
| 1 | +/** |
| 2 | + * Create a SCR Scoring Object |
| 3 | + * |
| 4 | + * @param {Object} scrObject - Contains the definition of the SCR Score Object |
| 5 | + * @param {String} paneID - The shorthand of the page which will contain the object |
| 6 | + * @param {Object} scrInterfaceText - Contains all of the SCR relevant language interface |
| 7 | + * @returns a SCR Score object |
| 8 | + */ |
| 9 | +async function addSCRScoreObject(scrObject, paneID, scrInterfaceText) { |
| 10 | + // Create SCR Score Container |
| 11 | + let scrContainer = document.createElement('div'); |
| 12 | + scrContainer.setAttribute('id', `${paneID}-obj-${scrObject?.id}`); |
| 13 | + |
| 14 | + // Create the Input Heading |
| 15 | + let inputHeader = document.createElement('h3'); |
| 16 | + inputHeader.innerText = scrInterfaceText?.inputHeader; |
| 17 | + |
| 18 | + // Create the Input Container |
| 19 | + let scrInputContainer = document.createElement('div'); |
| 20 | + scrInputContainer.id = `${paneID}-obj-${scrObject?.id}-inputs`; |
| 21 | + |
| 22 | + // Create the Output Heading |
| 23 | + let outputHeader = document.createElement('h3'); |
| 24 | + outputHeader.innerText = scrInterfaceText?.outputHeader; |
| 25 | + |
| 26 | + // Create the Output Container |
| 27 | + let scrOutputContainer = document.createElement('div'); |
| 28 | + scrOutputContainer.id = `${paneID}-obj-${scrObject?.id}-outputs`; |
| 29 | + |
| 30 | + // Create the Score Button |
| 31 | + let scrScoreButton = document.createElement('button'); |
| 32 | + scrScoreButton.type = 'button'; |
| 33 | + scrScoreButton.id = `${paneID}-obj-${scrObject?.id}-score`; |
| 34 | + scrScoreButton.setAttribute('class', 'btn btn-primary'); |
| 35 | + scrScoreButton.innerText = scrInterfaceText?.scoreButton; |
| 36 | + scrScoreButton.onclick = async function () { |
| 37 | + this.disabled = true; |
| 38 | + this.innerHTML = `<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> ${scrInterfaceText?.runStatus}`; |
| 39 | + let submitForm = document.getElementById( |
| 40 | + `${paneID}-obj-${scrObject?.id}-inputs-form` |
| 41 | + ); |
| 42 | + const values = Array.from(submitForm.elements).map((x) => { |
| 43 | + return { |
| 44 | + name: x.id, |
| 45 | + value: isNaN(parseFloat(x.value)) |
| 46 | + ? x.value |
| 47 | + : parseFloat(x.value), |
| 48 | + }; |
| 49 | + }); |
| 50 | + |
| 51 | + let currentSCR = document.getElementById(`${paneID}-obj-${scrObject?.id}-endpoint`).value; |
| 52 | + let scrResponse = await scoreSCR(currentSCR, values); |
| 53 | + let scrResultTable = document.getElementById(`${paneID}-obj-${scrObject?.id}-outputs-table-table-root`); |
| 54 | + addRowToTable(scrResultTable, [Object.values(scrResponse?.data)]); |
| 55 | + this.disabled = false; |
| 56 | + this.innerText = scrInterfaceText?.scoreButton; |
| 57 | + } |
| 58 | + |
| 59 | + // Add the SCR Module Input Selector |
| 60 | + let scrEndpointInput = document.createElement('input'); |
| 61 | + scrEndpointInput.type = 'text'; |
| 62 | + scrEndpointInput.className = 'form-control'; |
| 63 | + scrEndpointInput.placeholder = 'https://example.com/SCR'; |
| 64 | + scrEndpointInput.id = `${paneID}-obj-${scrObject?.id}-endpoint`; |
| 65 | + scrEndpointInput.onblur = async function () { |
| 66 | + let scrDefinition = await getSCRMetadata(this.value); |
| 67 | + // Reset containers |
| 68 | + let scrInContainer = document.getElementById(`${paneID}-obj-${scrObject?.id}-inputs`); |
| 69 | + scrInContainer.innerHTML = ''; |
| 70 | + let scrOutContainer = document.getElementById(`${paneID}-obj-${scrObject?.id}-outputs`) |
| 71 | + scrOutContainer.innerHTML = ''; |
| 72 | + if(scrDefinition.length === 1) { |
| 73 | + let scrError = document.createElement('p'); |
| 74 | + scrError.style.color = 'red'; |
| 75 | + scrError.innerText = scrDefinition[0]; |
| 76 | + scrInContainer.appendChild(scrError); |
| 77 | + } else { |
| 78 | + // Create the inputs |
| 79 | + addAccordionBody(`${paneID}-obj-${scrObject?.id}`, 'inputs', 'input', scrDefinition[0]); |
| 80 | + // Create the outputs |
| 81 | + createTable(scrOutContainer, `${paneID}-obj-${scrObject?.id}-outputs-table`, Object.values(scrDefinition[1]).map(field => field.name), []); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // Add Label for input |
| 86 | + let scrEndpointInputLabel = document.createElement('label'); |
| 87 | + scrEndpointInputLabel.for = `${paneID}-obj-${scrObject?.id}-endpoint`; |
| 88 | + scrEndpointInputLabel.innerText = `${scrInterfaceText?.endpoint}:`; |
| 89 | + scrEndpointInputLabel.style.textTransform = 'capitalize'; |
| 90 | + |
| 91 | + // Create result |
| 92 | + scrContainer.appendChild(scrEndpointInputLabel); |
| 93 | + scrContainer.appendChild(scrEndpointInput); |
| 94 | + scrContainer.appendChild(document.createElement('br')); |
| 95 | + scrContainer.appendChild(inputHeader); |
| 96 | + scrContainer.appendChild(document.createElement('br')); |
| 97 | + scrContainer.appendChild(scrInputContainer); |
| 98 | + scrContainer.appendChild(document.createElement('br')); |
| 99 | + scrContainer.appendChild(scrScoreButton); |
| 100 | + scrContainer.appendChild(document.createElement('br')); |
| 101 | + scrContainer.appendChild(document.createElement('br')); |
| 102 | + scrContainer.appendChild(outputHeader); |
| 103 | + scrContainer.appendChild(document.createElement('br')); |
| 104 | + scrContainer.appendChild(scrOutputContainer); |
| 105 | + |
| 106 | + return scrContainer; |
| 107 | +} |
0 commit comments