diff options
Diffstat (limited to 'js')
-rw-r--r-- | js/mv.js | 102 | ||||
-rw-r--r-- | js/util/progress.js | 37 |
2 files changed, 139 insertions, 0 deletions
diff --git a/js/mv.js b/js/mv.js new file mode 100644 index 0000000..d8e8529 --- /dev/null +++ b/js/mv.js @@ -0,0 +1,102 @@ +"use strict"; +/* + * Globals accessible via the agent console for debugging purposes + */ + +/* + * Processing + */ +(function() { + /* Set up handlers for file selector */ + document.getElementById('input').addEventListener('change', function(fevt) { + var reader = new FileReader(); + var loadbar = progress('loadbar'); + var filesbar = progress('filesbar'); + + reader.onerror = function(evt) { + switch(evt.target.error.code) { + case evt.target.error.NOT_FOUND_ERR: + alert('File Not Found!'); + break; + case evt.target.error.NOT_READABLE_ERR: + alert('File is not readable'); + break; + case evt.target.error.ABORT_ERR: + break; // noop + default: + alert('An error occurred reading this file.'); + }; + } + reader.onprogress = function(evt) { + if (evt.lengthComputable) { + loadbar.update(evt.loaded, evt.total); + } + } + reader.onabort = function(evt) { + alert('File load aborted!'); + } + reader.onloadstart = function(evt) { + loadbar.reset(); + } + reader.onload = function(evt) { + loadbar.complete(); + ++cur; + parseRecords(reader.result); + if (cur == fevt.target.files.length) { + filesbar.complete(); + /* TODO: Make this fade out nicely? */ + setTimeout(function() { + loadbar.hide(); + }, 2000); + makeHeap(); + filesbar.complete(); + setTimeout(function() { + filesbar.hide(); + }, 2000); + } else { + filesbar.update(cur, fevt.target.files.length); + nextFile(); + } + } + var cur = 0; + var lbase = loadbar.label; + loadbar.label = function() { + return lbase() == '100%' ? "Loaded '" + fevt.target.files[cur].name + "' - Done!" : "Loading '" + fevt.target.files[cur].name + "' - " + lbase(); + }; + var fbase = filesbar.label; + filesbar.label = function () { + return fbase() == '100%' ? "Loaded " + fevt.target.files.length + " file(s) - Done!" : "Loading file " + (cur + 1) + " of " + fevt.target.files.length + " - " + fbase(); + } + loadbar.show(); + filesbar.show(); + function nextFile() { + reader.readAsBinaryString(fevt.target.files[cur]); + } + nextFile(); + }, false); + var records = []; + function parseRecords(data) { + var spl = data.split(/\r?\n/); + spl.forEach(function(e) { + var d = e.match(/^(\d+\.\d+) PC(\d+) (\d+):(\d+),(\d+) GAINXP (\d+) (\d+) (\w+)/); + if (d == null) { + return; + } + records.push({ + time: d[1], + pc: d[2], + map: d[3], + x: d[4], + y: d[5], + e: d[6], + j: d[7], + type: d[8] + }); + }); + } + + /* The record files are set, do everything */ + function makeHeap() { + /* TODO */ + } +})(); diff --git a/js/util/progress.js b/js/util/progress.js new file mode 100644 index 0000000..060e6dc --- /dev/null +++ b/js/util/progress.js @@ -0,0 +1,37 @@ +function progress(root) { + var _progress = {}; + var container = document.getElementById(root); + var _percent = '0%'; + var bar = document.querySelector('#' + root + ' .percent'); + _progress.label = function() { + return _percent; + } + /* Updates the progress bar to display a specific percentage. No range checking performed. */ + _progress.setPercent = function(percent) { + _percent = percent; + bar.style.width = _percent; + bar.textContent = _progress.label(); + } + /* Updates the progress bar to display a percentage based on the current proportion of items done. */ + _progress.update = function(current, total) { + var percentLoaded = Math.min(100, Math.round((current / total) * 100)); + _progress.setPercent(percentLoaded + '%'); + }; + /* Resets the progress bar to display nothing done. */ + _progress.reset = function() { + _progress.setPercent('0%'); + } + /* Resets the progress bar to display everything done. */ + _progress.complete = function() { + _progress.setPercent('100%'); + } + /* Shows the progress bar. */ + _progress.show = function() { + container.className += ' loading'; + } + /* Hides the progress bar */ + _progress.hide = function() { + container.className = container.className.replace(/\bloading\b/, ''); + } + return _progress; +} |