From 21c769545f4d9a25530e535494d883b814b1de85 Mon Sep 17 00:00:00 2001 From: WubTheCaptain Date: Sun, 20 Sep 2015 06:07:29 +0000 Subject: Add JSHint headers to cabinet and cheesesteak --- js/cheesesteak.js | 1 + 1 file changed, 1 insertion(+) (limited to 'js/cheesesteak.js') diff --git a/js/cheesesteak.js b/js/cheesesteak.js index d4a1acf..09bd7eb 100644 --- a/js/cheesesteak.js +++ b/js/cheesesteak.js @@ -1,3 +1,4 @@ +/* jshint browser:true, jquery:true */ ;(function () { /* Lightweight EventEmitter implementation */ EventEmitter = function () {} -- cgit v1.2.3 From 5cb355b078f1b74b3b85aa23a4476dffe58b6b8e Mon Sep 17 00:00:00 2001 From: WubTheCaptain Date: Sun, 20 Sep 2015 13:12:28 +0000 Subject: Clean up cheesesteak.js and cabinet.js for JSHint Adds curly braces and semicolons. Also change indentation to two spaces. --- js/cheesesteak.js | 367 +++++++++++++++++++++++++++++------------------------- 1 file changed, 195 insertions(+), 172 deletions(-) (limited to 'js/cheesesteak.js') diff --git a/js/cheesesteak.js b/js/cheesesteak.js index 09bd7eb..1957aad 100644 --- a/js/cheesesteak.js +++ b/js/cheesesteak.js @@ -1,173 +1,196 @@ /* jshint browser:true, jquery:true */ -;(function () { - /* 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')) return - if (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')) return - if (evt in this._events === false) return - - for (var i = 0, l = this._events[evt].length; i < l; i++) { - this._events[evt][i].apply(this, Array.prototype.slice.call(arguments, 1)) - } - } - - /* 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 - FileList.prototype.filter = Array.prototype.filter - FileList.prototype.map = Array.prototype.map - FileList.prototype.reduce = Array.prototype.reduce - FileList.prototype.reduceRight = Array.prototype.reduceRight - - Object.defineProperty(FileList.prototype, "size", { - get: function getSize () { - return this.reduce(function (prev, curr) { - return prev + curr.size - }, 0) - } - }) - - /* Utility to convert bytes into human units */ - var humanSize = { - get: function humanSize () { - var units = ["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] - } - } - 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", percentUploaded) - Object.defineProperty(File.prototype, "percentUploaded", percentUploaded) - - Object.defineProperty(FileList.prototype, "uploadedSize", { - get: function getUploadedSize () { - return this.reduce(function (prev, curr) { - return prev + (curr.uploadedSize || 0) - }, 0) - } - }) - - /* Object URL stuff */ - Object.defineProperty(File.prototype, "url", { - get: function getURL () { - return window.URL.createObjectURL(this); - } - }) - File.revokeURL = function revokeURL (url) { - window.URL.revokeObjectURL(url) - } - - // We manipulate this instead of files, because the fucking geniuses at - // Mozilla interpreted the spec to mean that HTMLInputElement.prototype.files - // is read-only. It isn't, read for yourself: - // http://www.w3.org/html/wg/drafts/html/master/forms.html#dom-input-files - //HTMLInputElement.prototype.filelist = Object.create(FileList); - /* - Object.defineProperty(HTMLInputElement.prototype, "filelist", { - get: function getFileList () { - return this.filelist = this._filelist || this.files - }, - set: function (files) { - r - } - })*/ - -/* HTMLInputElement.prototype.addEventListener("change", function (e) { - this.filelist = e.target.files - })*/ - - // b gets merged as defaults for a - var merge = function merge (base, overlay) { - var out = base - for (var key in overlay) { - if (typeof overlay[key] === 'object') - out[key] = merge(base[key], overlay[key]); - else - out[key] = overlay[key] - } - return out - } - - var FileListUploader = function (url, files, opts) { - this.url = url - this.files = files - - opts = opts || {} - - this.opts = merge({ - 'field': 'files[]', - 'method': 'POST', - 'data': {} - }, opts) - } - FileListUploader.prototype = Object.create(EventEmitter.prototype) - FileListUploader.prototype.upload = function (cb) { - if (cb) this.on('uploadcomplete', cb) - - var opts = this.opts - var files = this.files - var events = this - - var data = new FormData() - files.forEach(function (file) { - data.append(opts.field, file) - }) - - var xhr = new XMLHttpRequest() - - xhr.open(opts.method, this.url, true) - xhr.upload.addEventListener('loadstart', function (e) { - for (var i = 0, l = files.length; i < l; i++) { - 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 - for (var i = 0, l = files.length; i < l; i++) { - files[i].uploadedSize = Math.min(size, files[i].size) - size -= files[i].uploadedSize - if (size <= 0) { - files.current = files[i] - size = 0 - } - } - } - events.emit('uploadprogress', e, files) - }, false) - xhr.upload.addEventListener('loadstart', function (e) { events.emit('uploadstart', e) }) - xhr.upload.addEventListener('load', function (e) { events.emit('uploadcomplete', e) }) - xhr.addEventListener('progress', function (e) { events.emit('progress', e) }) - xhr.addEventListener('load', function (e) { events.emit('load', e, xhr.responseText) }) - - xhr.send(data) - /* TODO: Add more event passthroughs, maybe abstract it into a helper? */ - } - - FileList.prototype.upload = function uploadFileList (url, opts) { - return new FileListUploader(url, this, opts) - } -})() +(function() { + /* 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')) { + return; + } + if (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')) { + return; + } + if (evt in this._events === false) { + return; + } + + for (var i = 0, l = this._events[evt].length; i < l; i++) { + this._events[evt][i].apply(this, Array.prototype.slice.call(arguments, + 1)); + } + }; + + /* 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; + FileList.prototype.filter = Array.prototype.filter; + FileList.prototype.map = Array.prototype.map; + FileList.prototype.reduce = Array.prototype.reduce; + FileList.prototype.reduceRight = Array.prototype.reduceRight; + + Object.defineProperty(FileList.prototype, "size", { + get: function getSize() { + return this.reduce(function(prev, curr) { + return prev + curr.size; + }, 0); + } + }); + + /* Utility to convert bytes into human units */ + var humanSize = { + get: function humanSize() { + var units = ["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]; + } + }; + 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", + percentUploaded); + Object.defineProperty(File.prototype, "percentUploaded", percentUploaded); + + Object.defineProperty(FileList.prototype, "uploadedSize", { + get: function getUploadedSize() { + return this.reduce(function(prev, curr) { + return prev + (curr.uploadedSize || 0); + }, 0); + } + }); + + /* Object URL stuff */ + Object.defineProperty(File.prototype, "url", { + get: function getURL() { + return window.URL.createObjectURL(this); + } + }); + File.revokeURL = function revokeURL(url) { + window.URL.revokeObjectURL(url); + }; + + // We manipulate this instead of files, because the fucking geniuses at + // Mozilla interpreted the spec to mean that HTMLInputElement.prototype.files + // is read-only. It isn't, read for yourself: + // http://www.w3.org/html/wg/drafts/html/master/forms.html#dom-input-files + //HTMLInputElement.prototype.filelist = Object.create(FileList); + /* + Object.defineProperty(HTMLInputElement.prototype, "filelist", { + get: function getFileList () { + return this.filelist = this._filelist || this.files + }, + set: function (files) { + r + } + })*/ + + /* HTMLInputElement.prototype.addEventListener("change", function (e) { + this.filelist = e.target.files + })*/ + + // b gets merged as defaults for a + var merge = function merge(base, overlay) { + var out = base; + for (var key in overlay) { + if (typeof overlay[key] === 'object') { + out[key] = merge(base[key], overlay[key]); + } else { + out[key] = overlay[key]; + } + } + return out; + }; + + var FileListUploader = function(url, files, opts) { + this.url = url; + this.files = files; + + opts = opts || {}; + + this.opts = merge({ + 'field': 'files[]', + 'method': 'POST', + 'data': {} + }, opts); + }; + FileListUploader.prototype = Object.create(EventEmitter.prototype); + FileListUploader.prototype.upload = function(cb) { + if (cb) { + this.on('uploadcomplete', cb); + } + + var opts = this.opts; + var files = this.files; + var events = this; + + var data = new FormData(); + files.forEach(function(file) { + data.append(opts.field, file); + }); + + var xhr = new XMLHttpRequest(); + + xhr.open(opts.method, this.url, true); + xhr.upload.addEventListener('loadstart', function(e) { + for (var i = 0, l = files.length; i < l; i++) { + 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 + for (var i = 0, l = files.length; i < l; i++) { + files[i].uploadedSize = Math.min(size, files[i].size); + size -= files[i].uploadedSize; + if (size <= 0) { + files.current = files[i]; + size = 0; + } + } + } + events.emit('uploadprogress', e, files); + }, false); + xhr.upload.addEventListener('loadstart', function(e) { + events.emit('uploadstart', e); + }); + xhr.upload.addEventListener('load', function(e) { + events.emit('uploadcomplete', e); + }); + xhr.addEventListener('progress', function(e) { + events.emit('progress', e); + }); + xhr.addEventListener('load', function(e) { + events.emit('load', e, xhr.responseText); + }); + + xhr.send(data); + /* TODO: Add more event passthroughs, maybe abstract it into a helper? */ + }; + + FileList.prototype.upload = function uploadFileList(url, opts) { + return new FileListUploader(url, this, opts); + }; +})(); -- cgit v1.2.3 From 77d36062d062d5f8e8c56e957045ea66ef354c52 Mon Sep 17 00:00:00 2001 From: WubTheCaptain Date: Sun, 20 Sep 2015 13:16:51 +0000 Subject: Simplify multiple ifs to one if with OR condition --- js/cheesesteak.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'js/cheesesteak.js') diff --git a/js/cheesesteak.js b/js/cheesesteak.js index 1957aad..709d446 100644 --- a/js/cheesesteak.js +++ b/js/cheesesteak.js @@ -8,19 +8,13 @@ this._events[evt].push(fn); }; EventEmitter.prototype.off = function(evt, fn) { - if (!this.hasOwnProperty('_events')) { - return; - } - if (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')) { - return; - } - if (evt in this._events === false) { + if (!this.hasOwnProperty('_events') || evt in this._events === false) { return; } -- cgit v1.2.3 From 50fdeded0b2ae6cf38d0719bfe3da30a73235020 Mon Sep 17 00:00:00 2001 From: WubTheCaptain Date: Sun, 20 Sep 2015 13:20:11 +0000 Subject: Remove commented code about Mozilla's file input This code doesn't seem to be in use nor important. There is no good reason why this was left in here. Because it's not been in use for so long and nobody has an issue with it, let's just remove it. --- js/cheesesteak.js | 19 ------------------- 1 file changed, 19 deletions(-) (limited to 'js/cheesesteak.js') diff --git a/js/cheesesteak.js b/js/cheesesteak.js index 709d446..47aee93 100644 --- a/js/cheesesteak.js +++ b/js/cheesesteak.js @@ -81,25 +81,6 @@ window.URL.revokeObjectURL(url); }; - // We manipulate this instead of files, because the fucking geniuses at - // Mozilla interpreted the spec to mean that HTMLInputElement.prototype.files - // is read-only. It isn't, read for yourself: - // http://www.w3.org/html/wg/drafts/html/master/forms.html#dom-input-files - //HTMLInputElement.prototype.filelist = Object.create(FileList); - /* - Object.defineProperty(HTMLInputElement.prototype, "filelist", { - get: function getFileList () { - return this.filelist = this._filelist || this.files - }, - set: function (files) { - r - } - })*/ - - /* HTMLInputElement.prototype.addEventListener("change", function (e) { - this.filelist = e.target.files - })*/ - // b gets merged as defaults for a var merge = function merge(base, overlay) { var out = base; -- cgit v1.2.3