diff options
Diffstat (limited to 'js/cheesesteak.js')
| -rw-r--r-- | js/cheesesteak.js | 79 |
1 files changed, 48 insertions, 31 deletions
diff --git a/js/cheesesteak.js b/js/cheesesteak.js index a3cf991..ea3dc19 100644 --- a/js/cheesesteak.js +++ b/js/cheesesteak.js @@ -22,21 +22,25 @@ /* jshint browser:true, jquery:true */ (function() { - /* Lightweight EventEmitter implementation */ + // Lightweight EventEmitter implementation EventEmitter = function() {}; + EventEmitter.prototype.on = function(evt, fn) { this._events = this._events || {}; this._events[evt] = this._events[evt] || []; this._events[evt].push(fn); }; + EventEmitter.prototype.off = function(evt, fn) { - if (!this.hasOwnProperty('_events') || evt in this._events === false) { + if (!this.hasOwnProperty('_events') || evt in this._events === false) { return; } + this._events[evt].splice(this._events[evt].indexOf(fn), 1); }; + EventEmitter.prototype.emit = function(evt) { - if (!this.hasOwnProperty('_events') || evt in this._events === false) { + if (!this.hasOwnProperty('_events') || evt in this._events === false) { return; } @@ -46,7 +50,7 @@ } }; - /* Copy the stuff from Array to FileList */ + // Copy the stuff from Array to FileList FileList.prototype.forEach = Array.prototype.forEach; FileList.prototype.every = Array.prototype.every; FileList.prototype.some = Array.prototype.some; @@ -55,7 +59,7 @@ FileList.prototype.reduce = Array.prototype.reduce; FileList.prototype.reduceRight = Array.prototype.reduceRight; - Object.defineProperty(FileList.prototype, "size", { + Object.defineProperty(FileList.prototype, 'size', { get: function getSize() { return this.reduce(function(prev, curr) { return prev + curr.size; @@ -63,29 +67,29 @@ } }); - /* Utility to convert bytes into human units */ + // Utility to convert bytes into human units var humanSize = { get: function humanSize() { var units = [ - "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB" + 'B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB' ]; var e = Math.floor(Math.log(this.size) / Math.log(1024)); - return (this.size / Math.pow(1024, e)).toFixed(2) + " " + units[e]; + return (this.size / Math.pow(1024, e)).toFixed(2) + ' ' + units[e]; } }; - Object.defineProperty(FileList.prototype, "humanSize", humanSize); - Object.defineProperty(File.prototype, "humanSize", humanSize); + Object.defineProperty(FileList.prototype, 'humanSize', humanSize); + Object.defineProperty(File.prototype, 'humanSize', humanSize); var percentUploaded = { get: function percentUploaded() { return this.uploadedSize / this.size; } }; - Object.defineProperty(FileList.prototype, "percentUploaded", + Object.defineProperty(FileList.prototype, 'percentUploaded', percentUploaded); - Object.defineProperty(File.prototype, "percentUploaded", percentUploaded); + Object.defineProperty(File.prototype, 'percentUploaded', percentUploaded); - Object.defineProperty(FileList.prototype, "uploadedSize", { + Object.defineProperty(FileList.prototype, 'uploadedSize', { get: function getUploadedSize() { return this.reduce(function(prev, curr) { return prev + (curr.uploadedSize || 0); @@ -93,8 +97,8 @@ } }); - /* Object URL stuff */ - Object.defineProperty(File.prototype, "url", { + // Object URL stuff + Object.defineProperty(File.prototype, 'url', { get: function getURL() { return window.URL.createObjectURL(this); } @@ -109,10 +113,11 @@ for (var key in overlay) { if (typeof overlay[key] === 'object') { out[key] = merge(base[key], overlay[key]); - } else { + } else { out[key] = overlay[key]; } } + return out; }; @@ -123,11 +128,12 @@ opts = opts || {}; this.opts = merge({ - 'field': 'files[]', - 'method': 'POST', - 'data': {} + field: 'files[]', + method: 'POST', + data: {} }, opts); }; + FileListUploader.prototype = Object.create(EventEmitter.prototype); FileListUploader.prototype.upload = function(cb) { if (cb) { @@ -136,7 +142,7 @@ var opts = this.opts; var files = this.files; - var events = this; + var _this = this; var data = new FormData(); files.forEach(function(file) { @@ -151,14 +157,19 @@ files[i].uploadedSize = 0; } }); + xhr.upload.addEventListener('progress', function(e) { if (e.lengthComputable) { size = e.loaded; - // We know the size of the files, the order they're in, and how - // much we've uploaded. Based on this data, we can do some magic - // to figure out which file we're on, and how much we've uploaded - // of that file. - // TODO: This math has trouble on later uploads. Leak somewhere + + /** + * We know the size of the files, the order they're in, and how + * much we've uploaded. Based on this data, we can do some magic + * to figure out which file we're on, and how much we've uploaded + * of that file. + */ + + // TODO: This math has trouble on later uploads. Leak somewhere. for (var i = 0, l = files.length; i < l; i++) { files[i].uploadedSize = Math.min(size, files[i].size); size -= files[i].uploadedSize; @@ -168,23 +179,29 @@ } } } - events.emit('uploadprogress', e, files); + + _this.emit('uploadprogress', e, files); }, false); + xhr.upload.addEventListener('loadstart', function(e) { - events.emit('uploadstart', e); + _this.emit('uploadstart', e); }); + xhr.upload.addEventListener('load', function(e) { - events.emit('uploadcomplete', e); + _this.emit('uploadcomplete', e); }); + xhr.addEventListener('progress', function(e) { - events.emit('progress', e); + _this.emit('progress', e); }); + xhr.addEventListener('load', function(e) { - events.emit('load', e, xhr.responseText); + _this.emit('load', e, xhr.responseText); }); xhr.send(data); - /* TODO: Add more event passthroughs, maybe abstract it into a helper? */ + + // TODO: Add more event passthroughs, maybe abstract it into a helper? }; FileList.prototype.upload = function uploadFileList(url, opts) { |
