| |
| |
| |
| |
| document.addEventListener('DOMContentLoaded', onDOMContentLoaded); |
| |
| |
| |
| |
| function onDOMContentLoaded() { |
| const infileInput = document.getElementById('infile'); |
| infileInput.onchange = onInputDICOMFile; |
| const inrulesfileInput = document.getElementById('inrulesfile'); |
| inrulesfileInput.onchange = onInputRulesFile; |
| const jsonlintButton = document.getElementById('jsonlint'); |
| jsonlintButton.onclick = launchJSONLint; |
| const saveButton = document.getElementById('save'); |
| saveButton.onclick = saveRules; |
| const generateButton = document.getElementById('generate'); |
| generateButton.onclick = generate; |
| |
| |
| dwv.logger.level = dwv.logger.levels.DEBUG; |
| } |
| |
| |
| let _rulesFile = null; |
| |
| let _dicomFile = null; |
| |
| let _dicomElements = null; |
| |
| |
| |
| |
| |
| |
| function onLoadDICOMFile(event) { |
| |
| const parser = new dwv.DicomParser(); |
| parser.parse(event.target.result); |
| |
| _dicomElements = parser.getDicomElements(); |
| |
| const element = document.getElementById('generate'); |
| element.className = 'button button-active'; |
| } |
| |
| |
| |
| |
| function generate() { |
| |
| if (!isValidRules()) { |
| return; |
| } |
| |
| const writer = new dwv.DicomWriter(); |
| const addMissingTags = true; |
| writer.setRules( |
| JSON.parse(document.getElementById('rules').value), |
| addMissingTags); |
| let dicomBuffer = null; |
| try { |
| dicomBuffer = writer.getBuffer(_dicomElements); |
| } catch (error) { |
| console.error(error); |
| alert(error.message); |
| } |
| |
| const blob = new Blob([dicomBuffer], {type: 'application/dicom'}); |
| |
| const element = document.getElementById('generate'); |
| element.href = URL.createObjectURL(blob); |
| element.download = 'anonym-' + _dicomFile.name; |
| } |
| |
| |
| |
| |
| function saveRules() { |
| |
| if (!isValidRules()) { |
| return; |
| } |
| |
| const text = document.getElementById('rules').value; |
| |
| const blob = new Blob([text], {type: 'text/plain'}); |
| |
| const element = document.getElementById('save'); |
| element.download = (_rulesFile === null ? 'rules.json' : _rulesFile.name); |
| element.href = URL.createObjectURL(blob); |
| } |
| |
| |
| |
| |
| |
| |
| function isValidRules() { |
| try { |
| JSON.parse(document.getElementById('rules').value); |
| } catch { |
| alert('The JSON is not valid, please check it with JSONLint.'); |
| return false; |
| } |
| return true; |
| } |
| |
| |
| |
| |
| function launchJSONLint() { |
| const text = document.getElementById('rules').value; |
| const link = 'http://jsonlint.com/?json=' + encodeURIComponent(text); |
| window.open(link); |
| } |
| |
| |
| |
| |
| |
| |
| function onInputDICOMFile(event) { |
| if (event.target.files.length === 0) { |
| return; |
| } |
| _dicomFile = event.target.files[0]; |
| const reader = new FileReader(); |
| reader.onload = onLoadDICOMFile; |
| reader.readAsArrayBuffer(_dicomFile); |
| } |
| |
| |
| |
| |
| |
| |
| function onInputRulesFile(event) { |
| if (event.target.files.length === 0) { |
| return; |
| } |
| _rulesFile = event.target.files[0]; |
| const reader = new FileReader(); |
| reader.onload = function (event) { |
| document.getElementById('rules').value = event.target.result; |
| }; |
| reader.readAsText(_rulesFile); |
| } |