From 07ba404dcbdeebf02def41978a0520065bc99ead Mon Sep 17 00:00:00 2001 From: kanadeko Date: Sat, 14 Jan 2017 03:01:23 -0300 Subject: Frontend WIP --- public/js/envResolver.js | 83 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 public/js/envResolver.js (limited to 'public/js/envResolver.js') diff --git a/public/js/envResolver.js b/public/js/envResolver.js new file mode 100644 index 0000000..a7419b4 --- /dev/null +++ b/public/js/envResolver.js @@ -0,0 +1,83 @@ +var http = function (url, success, failure) { + var request = new XMLHttpRequest(); + request.open("GET", url, true); + request.send(null); + request.onreadystatechange = function () { + if (request.readyState == 4) { + if (request.status == 200) + success(request.responseText); + else if (failure) + failure(request.status, request.statusText); + } + }; +}; +var mockXhr = function () { + XMLHttpRequest = XhrMock; + FormData = FormDataMock; +}; +var resolveEnvironment = function () { + if (window.location.href.toString().toLowerCase().indexOf('file://') >= 0) { + mockXhr(); + return; + } + http('api/check', function (result) { + if (result !== 'API OK') + mockXhr(); + }, function () { + mockXhr(); + }); +}; +var FormDataMock = (function () { + function FormDataMock() { + this.data = {}; + } + FormDataMock.prototype.append = function (key, data, additional) { + this.data[key] = { data: data, additional: additional }; + }; + return FormDataMock; +})(); +var XhrMock = (function () { + function XhrMock() { + this.loaded = 0; + this.step = 2000000; + this.readyState = 0; + this.status = 0; + this.upload = { onprogress: function () { } }; + } + XhrMock.prototype.open = function (method, url, async) { + }; + XhrMock.prototype.setRequestHeader = function (name, value) { + }; + XhrMock.prototype.send = function (formData) { + this.file = formData.data['file'].data; + this.performStep(); + }; + XhrMock.prototype.abort = function () { + window.clearTimeout(this.timeoutId); + }; + XhrMock.prototype.performStep = function () { + var _this = this; + this.timeoutId = window.setTimeout(function () { + if (_this.addStep() === _this.file.size) { + _this.readyState = 4; + _this.status = 200; + _this.onload(new Event('loaded')); + } + else { + var e = { + lengthComputable: true, + loaded: _this.loaded, + total: _this.file.size + }; + _this.upload.onprogress(e); + _this.performStep(); + } + }, 100); + }; + XhrMock.prototype.addStep = function () { + var newValue = this.loaded + this.step; + this.loaded = newValue > this.file.size ? this.file.size : newValue; + return this.loaded; + }; + return XhrMock; +})(); -- cgit v1.2.3