|
"use strict"; |
|
|
|
const ReferenceInput = document.querySelector('#reference'); |
|
const PredictionInput = document.querySelector('#prediction'); |
|
|
|
const ReferenceMaxLengthCount = document.querySelector("#counter_ref"); |
|
const PredictionMaxLengthCount = document.querySelector("#counter_pred"); |
|
|
|
const areaNormalShadow = "0 0 10px var(--white-main)"; |
|
const areaSuccessShadow = "0 0 10px green"; |
|
const areaErrorShadow = "0 0 10px red"; |
|
|
|
const areaPlaceHolderMsgError = "Only raw text format (.txt) are valid, please retry."; |
|
const referencePlaceHolder = "Drag & Drop or Paste the expected transcription..."; |
|
const predictionPlaceHolder = "Drag & Drop or Paste the transcription produced by the model you wish to evaluate..."; |
|
|
|
const LIMIT_CHAR_SIZE = 7000; |
|
|
|
|
|
[ReferenceInput, PredictionInput].forEach(item => { |
|
item.addEventListener('drop', event => { |
|
dropHandler(event); |
|
}); |
|
item.addEventListener('keydown', function () { |
|
limitText(item, chooseCorectCounter(item.id), LIMIT_CHAR_SIZE); |
|
}) |
|
item.addEventListener('keyup', function () { |
|
limitText(item, chooseCorectCounter(item.id), LIMIT_CHAR_SIZE); |
|
}) |
|
item.addEventListener('input', function () { |
|
AreaStateShadow(item, 'normal'); |
|
AreaPlaceHolderState(item, item.id, 'normal') |
|
if (item.value !== "") { |
|
AreaStateShadow(item, 'success'); |
|
} |
|
}); |
|
}) |
|
|
|
|
|
function chooseCorectCounter(id){ |
|
return (id === "reference") ? ReferenceMaxLengthCount : PredictionMaxLengthCount |
|
} |
|
|
|
|
|
function limitText(limitField, limitCount, limitNum) { |
|
if (limitField.value.length > limitNum) { |
|
limitField.value = limitField.value.substring(0, limitNum); |
|
limitCount.value = limitNum - limitField.value.length; |
|
} else { |
|
limitCount.value = limitNum - limitField.value.length; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function AreaStateShadow(element, type) { |
|
element.style["boxShadow"] = areaNormalShadow; |
|
if (type === 'error') { |
|
element.style["boxShadow"] = areaErrorShadow; |
|
} |
|
if (type === 'success') { |
|
element.style["boxShadow"] = areaSuccessShadow; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function AreaPlaceHolderState(element, id, type = 'normal') { |
|
if (type === 'normal') { |
|
if (id === 'reference') { |
|
element.placeholder = referencePlaceHolder; |
|
} |
|
if (id === 'prediction') { |
|
element.placeholder = predictionPlaceHolder; |
|
} |
|
} |
|
if (type === 'error') { |
|
element.value = ''; |
|
element.placeholder = areaPlaceHolderMsgError; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
function dropHandler(event) { |
|
|
|
event.preventDefault(); |
|
|
|
|
|
let dropId = event.target.id; |
|
let textArea = document.getElementById(dropId); |
|
|
|
|
|
const reader = new FileReader(); |
|
|
|
|
|
let dropEvent = event.dataTransfer.items; |
|
let dropEventMimetype = dropEvent[0].type; |
|
let dropEventFormat = dropEvent[0].kind; |
|
|
|
|
|
|
|
reader.addEventListener("load", (e) => { |
|
|
|
|
|
|
|
|
|
|
|
textArea.value = e.target.result; |
|
limitText(textArea, chooseCorectCounter(textArea.id), LIMIT_CHAR_SIZE); |
|
}, false); |
|
|
|
|
|
if (dropEvent) { |
|
if (dropEventFormat === 'file' && dropEventMimetype === 'text/plain') { |
|
let file = dropEvent[0].getAsFile(); |
|
reader.readAsText(file); |
|
AreaStateShadow(textArea, 'success') |
|
AreaPlaceHolderState(textArea, dropId, 'normal') |
|
} else { |
|
AreaStateShadow(textArea, 'error') |
|
AreaPlaceHolderState(textArea, dropId, 'error') |
|
} |
|
} |
|
} |
|
|