//////////////////////////////////////////////////////////////////////////////// // // Copyright (c) 2013-2017 Dawson Dean // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included // in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // //////////////////////////////////////////////////////////////////////////////// // // This is the top-level module. It initiates the entire UI and also does all // the layout of HTML elements in the UI. This layout is done initially when the // UI opens, but also may change the UI elements dynamically in response to user // actions. // ///////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// // // [StartNBFiles] // //////////////////////////////////////////////////////////////////////////////// function StartNBFiles() { } // StartNBFiles //////////////////////////////////////////////////////////////////////////////// // // [OnImportFile] // // fileList is of type "FileList" https://developer.mozilla.org/en-US/docs/Web/API/FileList // fineInfo is of type "File" https://developer.mozilla.org/en-US/docs/Web/API/File //////////////////////////////////////////////////////////////////////////////// function OnImportFile(fileList) { //LogEvent("OnImportFile"); var fileNum; var fileInfo; var fileReader; var numFiles = fileList.length; //LogEvent("OnImportFile. numFiles=" + numFiles); if (numFiles <= 0) { return; } fileInfo = fileList[0]; //LogEvent("OnImportFile. fileInfo=" + fileInfo); fileReader = new FileReader(); fileReader.onabort = function() { OnFileReadEvent("onabort", fileReader); }; fileReader.onerror = function() { OnFileReadEvent("onerror", fileReader); }; fileReader.onload = function() { OnFileReadEvent("onload", fileReader); }; fileReader.onloadend = function() { OnFileReadEvent("onloadend", fileReader); }; fileReader.onloadstart = function() { OnFileReadEvent("onloadstart", fileReader); }; fileReader.onprogress = function() { OnFileReadEvent("onprogress", fileReader); }; //LogEvent("OnImportFile. fileReader=" + fileReader); fileReader.readAsText(fileInfo); } // OnImportFile //////////////////////////////////////////////////////////////////////////////// // // [OnFileReadEvent] // //////////////////////////////////////////////////////////////////////////////// function OnFileReadEvent(msg, fileReader) { //LogEvent("OnFileReadEvent. msg=" + msg); var xmlDoc = null; var rootElementList; var patientChart; if ("onload" != msg) { return; } //LogEvent("OnFileReadEvent. fileReader.error=" + fileReader.error); //LogEvent("OnFileReadEvent. fileReader.readyState=" + fileReader.readyState); if ((2 != fileReader.readyState) || (fileReader.error)) { return; } LogEvent("OnFileReadEvent. fileReader.result=" + fileReader.result); xmlDoc = Util_ParseStringIntoXML(fileReader.result); if (!xmlDoc) { return; } rootElementList = xmlDoc.getElementsByTagName("NBDoc"); if (!rootElementList) { return; } //LogEvent("OnImportFile. rootElementList.length=" + rootElementList.length); for (index = 0; index < rootElementList.length; index++) { patientChart = rootElementList[index]; LogEvent("OnImportFile. patientChart=" + patientChart); } } // OnFileReadEvent //////////////////////////////////////////////////////////////////////////////// // // [Util_ParseStringIntoXML] // //////////////////////////////////////////////////////////////////////////////// function Util_ParseStringIntoXML(xmlTextString) { var xmlParser = null; var xmlDoc = null; // This is in descending order, from most preferred to least preferred. // We stop looking through the list when one is available var MicrosoftDOMObjectNameList = [ "Msxml2.DOMDocument.6.0", "Msxml2.DOMDocument.5.0", "Msxml2.DOMDocument.4.0", "Msxml2.DOMDocument.3.0", "MSXML2.DOMDocument", "MSXML.DOMDocument" ]; var currentObjectName; //LogEvent("Util_ParseStringIntoXML. xmlTextString=" + xmlTextString); //LogEvent("typedef = " + typeof(ActiveXObject)); // This should work for all browsers except old versions of Internet Explorer. if (window.DOMParser) { //LogEvent("Util_ParseStringIntoXML. Try window.DOMParser"); xmlParser = new DOMParser(); if (xmlParser) { try { xmlDoc = xmlParser.parseFromString(xmlTextString, "text/xml"); } catch (e) {xmlDoc = null;}; if (xmlDoc) { LogEvent("Util_ParseStringIntoXML succeeded parsing as text/xml"); return(xmlDoc); } try { xmlDoc = xmlParser.parseFromString(xmlTextString, "application/xml"); } catch (e) {xmlDoc = null;}; if (xmlDoc) { LogEvent("Util_ParseStringIntoXML succeeded parsing as application/xml"); return(xmlDoc); } try { xmlDoc = xmlParser.parseFromString(xmlTextString, "text/html"); } catch (e) {xmlDoc = null;}; if (xmlDoc) { LogEvent("Util_ParseStringIntoXML succeeded parsing as text/html"); return(xmlDoc); } } } // if (window.DOMParser) // If we did not succeed, then this may be because we are running in an // old version of Internet Explorer. In that case, try to load an older // Microsoft-specific version of the DOM. for (var index = 0; index < MicrosoftDOMObjectNameList.length; index++) { currentObjectName = MicrosoftDOMObjectNameList[index]; try { xmlDoc = new ActiveXObject(currentObjectName); } catch(e) {xmlDoc = null;}; if (xmlDoc) { //LogEvent("Util_ParseStringIntoXML succeeded with " + currentObjectName); xmlDoc.loadXML(xmlTextString); return(xmlDoc); } } return(null); } // Util_ParseStringIntoXML //Save by creating a download link with a data URI //https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs //TEST // var pom = document.createElement('a'); // pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text)); // pom.setAttribute('download', filename); // if (document.createEvent) { // var event = document.createEvent('MouseEvents'); // event.initEvent('click', true, true); // pom.dispatchEvent(event); // } else { // pom.click(); // } //Chrome (as of version 28): works with 2 097 152 Bytes, which is exactly 2 MB //Firefox (as of version 26): works with 1 040 000 Bytes, which is probably 1 MB