From 80916da9d109cfb6109f00e502f307e78eda6f3f Mon Sep 17 00:00:00 2001 From: WubTheCaptain Date: Sat, 19 Sep 2015 10:51:34 +0000 Subject: Change JS indentation from tabs to two spaces Airbnb, Google, npm, Node.js and Idiomatic style guides suggest to use two spaces for indentation. It seems to be the widely agreed consensus. --- js/pomf.js | 292 ++++++++++++++++++++++++++++++------------------------------- 1 file changed, 146 insertions(+), 146 deletions(-) (limited to 'js') diff --git a/js/pomf.js b/js/pomf.js index c066b7f..aa7d64d 100644 --- a/js/pomf.js +++ b/js/pomf.js @@ -1,148 +1,148 @@ $(function () { - var uploadInput = $('#upload-input') - , uploadBtn = $('#upload-btn') - , uploadFiles = $('#upload-filelist') - - var btnContent = "" - - if (!$.hasFileAPI()) { - $('#no-file-api').show() - uploadBtn.hide() - } - - uploadBtn.cabinet(uploadInput) - - uploadBtn.on('dragenter', function (e) { - if (this === e.target) { - $(this).addClass('drop') - btnContent = $(this).html() - $(this).html('Drop it here~') - } - }) - - uploadBtn.on('drop', function (e) { - $(this).trigger('dragleave') - }) - - uploadBtn.on('dragleave', function (e) { - node = e.target - do { - if (node === this) { - $(this).removeClass('drop') - $(this).html(btnContent) - break - } - } while (node = node.parentNode) - }) - - var MAX_SIZE = (function (node) { - var max = node.attr('data-max-size') || '120MiB' - var num = parseInt(/([0-9,]+).*/.exec(max)[1].replace(',', '')) - var unit = /(?:([KMGTPEZY])(i)?B|([BKMGTPEZY]))/.exec(max) || ["B","",""] - - var oneUnit = Math.pow( - (unit[2] === "i" ? 1024 : 1000), - 'BKMGTPEZY'.indexOf(unit[1]) - ) - - return num * oneUnit - })(uploadInput) - - var createRow = function (filename, size, extra) { - var rowItem = $('
  • ') - , rowName = $('') - , rowProg = $('
    ') - , rowSize = $('') - , rowUrl = $('') - - rowItem.addClass(extra || '') - - $('
    ').appendTo(rowProg) - - rowItem.attr('data-filename', escape(filename)) - rowName.text(filename) - rowSize.text(size) - - rowItem.append(rowName, rowProg, rowSize, rowUrl) - - return rowItem - } - - uploadBtn.on('change', function (e) { - uploadFiles.empty().removeClass('error completed') - - var files = uploadInput[0].filelist; - - files.forEach(function (file) { - createRow(file.name, file.humanSize).appendTo(uploadFiles) - }) - - var totalRow = createRow('', files.humanSize, 'total') - totalRow.appendTo(uploadFiles) - - if (files.size > MAX_SIZE) { - uploadFiles.addClass('error') - - $('.file-name', totalRow).html('onii-chan y-your upload is t-too big…'); - return - } - - var up = files.upload("upload.php") - - var eachRow = function (files, fn) { - var hits = {} - files.forEach(function (file) { - ++hits[file.name] || (hits[file.name] = 0) - var row = $($('li[data-filename="' + escape(file.name) + '"]')[hits[file.name] || 0]) - fn.call(row, row, file, files) - }) - } - - var totalName = $('.file-name', totalRow) - - up.on('uploadprogress', function (e, files) { - eachRow(files, function (row, file, files) { - $('.progress-inner', row).width((file.percentUploaded * 100) + '%') - }) - $('.progress-inner', totalRow).width((files.percentUploaded * 100) + '%') - }) - - up.on('uploadcomplete', function (e) { - $('.progress-inner').width('100%') - totalName.text('Grabbing URLs...') - }) - - up.on('load', function (e, res) { - switch (e.target.status) { - case 200: - var res = JSON.parse(res) - if (!res.success) { - uploadFiles.addClass('error') - $('.file-name', totalRow).text('Something went wrong; try again later.') - break - } - eachRow(res.files, function (row, file, files) { - var link = $('') - - link.attr('href', file.url) - .attr('target', '_BLANK') - .text(file.url.replace("http://","").replace("https://","")) - - $('.file-url', row).append(link) - }) - uploadFiles.addClass('completed') - totalName.text('Done!') - break - case 413: - uploadFiles.addClass('error completed') - // Terrible work-around, but necessary since otherwise the '…' entity is left decoded - totalName.html($('
    ').html('onii-chan, y-your upload is t-too big…').text()); - break - default: - uploadFiles.addClass('error completed') - totalName.text('Something went wrong; try again later.') - } - }) - up.upload() - }) + var uploadInput = $('#upload-input') + , uploadBtn = $('#upload-btn') + , uploadFiles = $('#upload-filelist') + + var btnContent = "" + + if (!$.hasFileAPI()) { + $('#no-file-api').show() + uploadBtn.hide() + } + + uploadBtn.cabinet(uploadInput) + + uploadBtn.on('dragenter', function (e) { + if (this === e.target) { + $(this).addClass('drop') + btnContent = $(this).html() + $(this).html('Drop it here~') + } + }) + + uploadBtn.on('drop', function (e) { + $(this).trigger('dragleave') + }) + + uploadBtn.on('dragleave', function (e) { + node = e.target + do { + if (node === this) { + $(this).removeClass('drop') + $(this).html(btnContent) + break + } + } while (node = node.parentNode) + }) + + var MAX_SIZE = (function (node) { + var max = node.attr('data-max-size') || '120MiB' + var num = parseInt(/([0-9,]+).*/.exec(max)[1].replace(',', '')) + var unit = /(?:([KMGTPEZY])(i)?B|([BKMGTPEZY]))/.exec(max) || ["B","",""] + + var oneUnit = Math.pow( + (unit[2] === "i" ? 1024 : 1000), + 'BKMGTPEZY'.indexOf(unit[1]) + ) + + return num * oneUnit + })(uploadInput) + + var createRow = function (filename, size, extra) { + var rowItem = $('
  • ') + , rowName = $('') + , rowProg = $('
    ') + , rowSize = $('') + , rowUrl = $('') + + rowItem.addClass(extra || '') + + $('
    ').appendTo(rowProg) + + rowItem.attr('data-filename', escape(filename)) + rowName.text(filename) + rowSize.text(size) + + rowItem.append(rowName, rowProg, rowSize, rowUrl) + + return rowItem + } + + uploadBtn.on('change', function (e) { + uploadFiles.empty().removeClass('error completed') + + var files = uploadInput[0].filelist; + + files.forEach(function (file) { + createRow(file.name, file.humanSize).appendTo(uploadFiles) + }) + + var totalRow = createRow('', files.humanSize, 'total') + totalRow.appendTo(uploadFiles) + + if (files.size > MAX_SIZE) { + uploadFiles.addClass('error') + + $('.file-name', totalRow).html('onii-chan y-your upload is t-too big…'); + return + } + + var up = files.upload("upload.php") + + var eachRow = function (files, fn) { + var hits = {} + files.forEach(function (file) { + ++hits[file.name] || (hits[file.name] = 0) + var row = $($('li[data-filename="' + escape(file.name) + '"]')[hits[file.name] || 0]) + fn.call(row, row, file, files) + }) + } + + var totalName = $('.file-name', totalRow) + + up.on('uploadprogress', function (e, files) { + eachRow(files, function (row, file, files) { + $('.progress-inner', row).width((file.percentUploaded * 100) + '%') + }) + $('.progress-inner', totalRow).width((files.percentUploaded * 100) + '%') + }) + + up.on('uploadcomplete', function (e) { + $('.progress-inner').width('100%') + totalName.text('Grabbing URLs...') + }) + + up.on('load', function (e, res) { + switch (e.target.status) { + case 200: + var res = JSON.parse(res) + if (!res.success) { + uploadFiles.addClass('error') + $('.file-name', totalRow).text('Something went wrong; try again later.') + break + } + eachRow(res.files, function (row, file, files) { + var link = $('') + + link.attr('href', file.url) + .attr('target', '_BLANK') + .text(file.url.replace("http://","").replace("https://","")) + + $('.file-url', row).append(link) + }) + uploadFiles.addClass('completed') + totalName.text('Done!') + break + case 413: + uploadFiles.addClass('error completed') + // Terrible work-around, but necessary since otherwise the '…' entity is left decoded + totalName.html($('
    ').html('onii-chan, y-your upload is t-too big…').text()); + break + default: + uploadFiles.addClass('error completed') + totalName.text('Something went wrong; try again later.') + } + }) + up.upload() + }) }) -- cgit v1.2.3 From 00de9a775b2bdcb5d3285b2048261612b64c6f27 Mon Sep 17 00:00:00 2001 From: WubTheCaptain Date: Sat, 19 Sep 2015 11:16:44 +0000 Subject: Add JSHint header --- js/pomf.js | 1 + 1 file changed, 1 insertion(+) (limited to 'js') diff --git a/js/pomf.js b/js/pomf.js index aa7d64d..81c7ac8 100644 --- a/js/pomf.js +++ b/js/pomf.js @@ -1,3 +1,4 @@ +/* jshint browser:true, jquery:true */ $(function () { var uploadInput = $('#upload-input') , uploadBtn = $('#upload-btn') -- cgit v1.2.3 From f93e6e1eec495bbf6af43754355c5f7ba1bdf47e Mon Sep 17 00:00:00 2001 From: WubTheCaptain Date: Sat, 19 Sep 2015 14:35:40 +0000 Subject: JSBeautify with few hand adjustments This fixes another npm style guide idiom, which is "comma first". --- js/pomf.js | 52 ++++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) (limited to 'js') diff --git a/js/pomf.js b/js/pomf.js index 81c7ac8..34fed72 100644 --- a/js/pomf.js +++ b/js/pomf.js @@ -1,8 +1,8 @@ /* jshint browser:true, jquery:true */ -$(function () { - var uploadInput = $('#upload-input') - , uploadBtn = $('#upload-btn') - , uploadFiles = $('#upload-filelist') +$(function() { + var uploadInput = $('#upload-input'), + uploadBtn = $('#upload-btn'), + uploadFiles = $('#upload-filelist') var btnContent = "" @@ -13,7 +13,7 @@ $(function () { uploadBtn.cabinet(uploadInput) - uploadBtn.on('dragenter', function (e) { + uploadBtn.on('dragenter', function(e) { if (this === e.target) { $(this).addClass('drop') btnContent = $(this).html() @@ -21,11 +21,11 @@ $(function () { } }) - uploadBtn.on('drop', function (e) { + uploadBtn.on('drop', function(e) { $(this).trigger('dragleave') }) - uploadBtn.on('dragleave', function (e) { + uploadBtn.on('dragleave', function(e) { node = e.target do { if (node === this) { @@ -36,10 +36,10 @@ $(function () { } while (node = node.parentNode) }) - var MAX_SIZE = (function (node) { + var MAX_SIZE = (function(node) { var max = node.attr('data-max-size') || '120MiB' var num = parseInt(/([0-9,]+).*/.exec(max)[1].replace(',', '')) - var unit = /(?:([KMGTPEZY])(i)?B|([BKMGTPEZY]))/.exec(max) || ["B","",""] + var unit = /(?:([KMGTPEZY])(i)?B|([BKMGTPEZY]))/.exec(max) || ["B", "", ""] var oneUnit = Math.pow( (unit[2] === "i" ? 1024 : 1000), @@ -49,12 +49,12 @@ $(function () { return num * oneUnit })(uploadInput) - var createRow = function (filename, size, extra) { - var rowItem = $('
  • ') - , rowName = $('') - , rowProg = $('
    ') - , rowSize = $('') - , rowUrl = $('') + var createRow = function(filename, size, extra) { + var rowItem = $('
  • '), + rowName = $(''), + rowProg = $('
    '), + rowSize = $(''), + rowUrl = $('') rowItem.addClass(extra || '') @@ -69,12 +69,12 @@ $(function () { return rowItem } - uploadBtn.on('change', function (e) { + uploadBtn.on('change', function(e) { uploadFiles.empty().removeClass('error completed') var files = uploadInput[0].filelist; - files.forEach(function (file) { + files.forEach(function(file) { createRow(file.name, file.humanSize).appendTo(uploadFiles) }) @@ -90,9 +90,9 @@ $(function () { var up = files.upload("upload.php") - var eachRow = function (files, fn) { + var eachRow = function(files, fn) { var hits = {} - files.forEach(function (file) { + files.forEach(function(file) { ++hits[file.name] || (hits[file.name] = 0) var row = $($('li[data-filename="' + escape(file.name) + '"]')[hits[file.name] || 0]) fn.call(row, row, file, files) @@ -101,19 +101,19 @@ $(function () { var totalName = $('.file-name', totalRow) - up.on('uploadprogress', function (e, files) { - eachRow(files, function (row, file, files) { + up.on('uploadprogress', function(e, files) { + eachRow(files, function(row, file, files) { $('.progress-inner', row).width((file.percentUploaded * 100) + '%') }) $('.progress-inner', totalRow).width((files.percentUploaded * 100) + '%') }) - up.on('uploadcomplete', function (e) { + up.on('uploadcomplete', function(e) { $('.progress-inner').width('100%') totalName.text('Grabbing URLs...') }) - up.on('load', function (e, res) { + up.on('load', function(e, res) { switch (e.target.status) { case 200: var res = JSON.parse(res) @@ -122,12 +122,12 @@ $(function () { $('.file-name', totalRow).text('Something went wrong; try again later.') break } - eachRow(res.files, function (row, file, files) { + eachRow(res.files, function(row, file, files) { var link = $('') - link.attr('href', file.url) + link.attr('href', file.url) .attr('target', '_BLANK') - .text(file.url.replace("http://","").replace("https://","")) + .text(file.url.replace("http://", "").replace("https://", "")) $('.file-url', row).append(link) }) -- cgit v1.2.3 From 40de8d58015700a04ccd376f3a8324422f6c9132 Mon Sep 17 00:00:00 2001 From: WubTheCaptain Date: Sat, 19 Sep 2015 14:38:15 +0000 Subject: Hand align variables to same indentation --- js/pomf.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'js') diff --git a/js/pomf.js b/js/pomf.js index 34fed72..c5474fb 100644 --- a/js/pomf.js +++ b/js/pomf.js @@ -51,10 +51,10 @@ $(function() { var createRow = function(filename, size, extra) { var rowItem = $('
  • '), - rowName = $(''), - rowProg = $('
    '), - rowSize = $(''), - rowUrl = $('') + rowName = $(''), + rowProg = $('
    '), + rowSize = $(''), + rowUrl = $('') rowItem.addClass(extra || '') -- cgit v1.2.3 From 109e8c3b85bbea468607b92c3e09650093a2a7e4 Mon Sep 17 00:00:00 2001 From: WubTheCaptain Date: Sat, 19 Sep 2015 14:46:39 +0000 Subject: Add semicolons to JavaScript source The npm style guide is bad. Don't use it! The lack of semicolons can create very subtle bugs that are hard to debug. This commit fixes about a hundred JSHint warnings. --- js/pomf.js | 146 ++++++++++++++++++++++++++++++------------------------------- 1 file changed, 73 insertions(+), 73 deletions(-) (limited to 'js') diff --git a/js/pomf.js b/js/pomf.js index c5474fb..ec69360 100644 --- a/js/pomf.js +++ b/js/pomf.js @@ -2,148 +2,148 @@ $(function() { var uploadInput = $('#upload-input'), uploadBtn = $('#upload-btn'), - uploadFiles = $('#upload-filelist') + uploadFiles = $('#upload-filelist'); - var btnContent = "" + var btnContent = ""; if (!$.hasFileAPI()) { - $('#no-file-api').show() - uploadBtn.hide() + $('#no-file-api').show(); + uploadBtn.hide(); } - uploadBtn.cabinet(uploadInput) + uploadBtn.cabinet(uploadInput); uploadBtn.on('dragenter', function(e) { if (this === e.target) { - $(this).addClass('drop') - btnContent = $(this).html() - $(this).html('Drop it here~') + $(this).addClass('drop'); + btnContent = $(this).html(); + $(this).html('Drop it here~'); } - }) + }); uploadBtn.on('drop', function(e) { - $(this).trigger('dragleave') - }) + $(this).trigger('dragleave'); + }); uploadBtn.on('dragleave', function(e) { - node = e.target + node = e.target; do { if (node === this) { - $(this).removeClass('drop') - $(this).html(btnContent) - break + $(this).removeClass('drop'); + $(this).html(btnContent); + break; } - } while (node = node.parentNode) - }) + } while (node = node.parentNode); + }); var MAX_SIZE = (function(node) { - var max = node.attr('data-max-size') || '120MiB' - var num = parseInt(/([0-9,]+).*/.exec(max)[1].replace(',', '')) - var unit = /(?:([KMGTPEZY])(i)?B|([BKMGTPEZY]))/.exec(max) || ["B", "", ""] + var max = node.attr('data-max-size') || '120MiB'; + var num = parseInt(/([0-9,]+).*/.exec(max)[1].replace(',', '')); + var unit = /(?:([KMGTPEZY])(i)?B|([BKMGTPEZY]))/.exec(max) || ["B", "", ""]; var oneUnit = Math.pow( (unit[2] === "i" ? 1024 : 1000), 'BKMGTPEZY'.indexOf(unit[1]) - ) + ); - return num * oneUnit - })(uploadInput) + return num * oneUnit; + })(uploadInput); var createRow = function(filename, size, extra) { var rowItem = $('
  • '), rowName = $(''), rowProg = $('
    '), rowSize = $(''), - rowUrl = $('') + rowUrl = $(''); - rowItem.addClass(extra || '') + rowItem.addClass(extra || ''); - $('
    ').appendTo(rowProg) + $('
    ').appendTo(rowProg); - rowItem.attr('data-filename', escape(filename)) - rowName.text(filename) - rowSize.text(size) + rowItem.attr('data-filename', escape(filename)); + rowName.text(filename); + rowSize.text(size); - rowItem.append(rowName, rowProg, rowSize, rowUrl) + rowItem.append(rowName, rowProg, rowSize, rowUrl); - return rowItem - } + return rowItem; + }; uploadBtn.on('change', function(e) { - uploadFiles.empty().removeClass('error completed') + uploadFiles.empty().removeClass('error completed'); var files = uploadInput[0].filelist; files.forEach(function(file) { - createRow(file.name, file.humanSize).appendTo(uploadFiles) - }) + createRow(file.name, file.humanSize).appendTo(uploadFiles); + }); - var totalRow = createRow('', files.humanSize, 'total') - totalRow.appendTo(uploadFiles) + var totalRow = createRow('', files.humanSize, 'total'); + totalRow.appendTo(uploadFiles); if (files.size > MAX_SIZE) { - uploadFiles.addClass('error') + uploadFiles.addClass('error'); $('.file-name', totalRow).html('onii-chan y-your upload is t-too big…'); - return + return; } - var up = files.upload("upload.php") + var up = files.upload("upload.php"); var eachRow = function(files, fn) { - var hits = {} + var hits = {}; files.forEach(function(file) { - ++hits[file.name] || (hits[file.name] = 0) - var row = $($('li[data-filename="' + escape(file.name) + '"]')[hits[file.name] || 0]) - fn.call(row, row, file, files) - }) - } + ++hits[file.name] || (hits[file.name] = 0); + var row = $($('li[data-filename="' + escape(file.name) + '"]')[hits[file.name] || 0]); + fn.call(row, row, file, files); + }); + }; - var totalName = $('.file-name', totalRow) + var totalName = $('.file-name', totalRow); up.on('uploadprogress', function(e, files) { eachRow(files, function(row, file, files) { - $('.progress-inner', row).width((file.percentUploaded * 100) + '%') - }) - $('.progress-inner', totalRow).width((files.percentUploaded * 100) + '%') - }) + $('.progress-inner', row).width((file.percentUploaded * 100) + '%'); + }); + $('.progress-inner', totalRow).width((files.percentUploaded * 100) + '%'); + }); up.on('uploadcomplete', function(e) { - $('.progress-inner').width('100%') - totalName.text('Grabbing URLs...') - }) + $('.progress-inner').width('100%'); + totalName.text('Grabbing URLs...'); + }); up.on('load', function(e, res) { switch (e.target.status) { case 200: - var res = JSON.parse(res) + var res = JSON.parse(res); if (!res.success) { - uploadFiles.addClass('error') - $('.file-name', totalRow).text('Something went wrong; try again later.') - break + uploadFiles.addClass('error'); + $('.file-name', totalRow).text('Something went wrong; try again later.'); + break; } eachRow(res.files, function(row, file, files) { - var link = $('') + var link = $(''); link.attr('href', file.url) .attr('target', '_BLANK') - .text(file.url.replace("http://", "").replace("https://", "")) + .text(file.url.replace("http://", "").replace("https://", "")); - $('.file-url', row).append(link) - }) - uploadFiles.addClass('completed') - totalName.text('Done!') - break + $('.file-url', row).append(link); + }); + uploadFiles.addClass('completed'); + totalName.text('Done!'); + break; case 413: - uploadFiles.addClass('error completed') + uploadFiles.addClass('error completed'); // Terrible work-around, but necessary since otherwise the '…' entity is left decoded totalName.html($('
    ').html('onii-chan, y-your upload is t-too big…').text()); - break + break; default: - uploadFiles.addClass('error completed') - totalName.text('Something went wrong; try again later.') + uploadFiles.addClass('error completed'); + totalName.text('Something went wrong; try again later.'); } - }) - up.upload() - }) -}) + }); + up.upload(); + }); +}); -- cgit v1.2.3 From bc9ddfdf951cd03d3d00af10a082284c6c3ccb8f Mon Sep 17 00:00:00 2001 From: WubTheCaptain Date: Sat, 19 Sep 2015 14:47:33 +0000 Subject: Align link attr indentation --- js/pomf.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'js') diff --git a/js/pomf.js b/js/pomf.js index ec69360..389b590 100644 --- a/js/pomf.js +++ b/js/pomf.js @@ -126,8 +126,8 @@ $(function() { var link = $(''); link.attr('href', file.url) - .attr('target', '_BLANK') - .text(file.url.replace("http://", "").replace("https://", "")); + .attr('target', '_BLANK') + .text(file.url.replace("http://", "").replace("https://", "")); $('.file-url', row).append(link); }); -- cgit v1.2.3 From 556c60d9027d6da1dc373d50adede97ade86d61f Mon Sep 17 00:00:00 2001 From: WubTheCaptain Date: Sat, 19 Sep 2015 15:40:40 +0000 Subject: Use singlequotes Both Google and Node.js style guides prefer the use of ' over ". Much of the rest of this code also uses singlequotes, so this brings the rest of it to same level of consistency. --- js/pomf.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'js') diff --git a/js/pomf.js b/js/pomf.js index 389b590..c1d89de 100644 --- a/js/pomf.js +++ b/js/pomf.js @@ -4,7 +4,7 @@ $(function() { uploadBtn = $('#upload-btn'), uploadFiles = $('#upload-filelist'); - var btnContent = ""; + var btnContent = ''; if (!$.hasFileAPI()) { $('#no-file-api').show(); @@ -39,10 +39,10 @@ $(function() { var MAX_SIZE = (function(node) { var max = node.attr('data-max-size') || '120MiB'; var num = parseInt(/([0-9,]+).*/.exec(max)[1].replace(',', '')); - var unit = /(?:([KMGTPEZY])(i)?B|([BKMGTPEZY]))/.exec(max) || ["B", "", ""]; + var unit = /(?:([KMGTPEZY])(i)?B|([BKMGTPEZY]))/.exec(max) || ['B', '', '']; var oneUnit = Math.pow( - (unit[2] === "i" ? 1024 : 1000), + (unit[2] === 'i' ? 1024 : 1000), 'BKMGTPEZY'.indexOf(unit[1]) ); @@ -88,7 +88,7 @@ $(function() { return; } - var up = files.upload("upload.php"); + var up = files.upload('upload.php'); var eachRow = function(files, fn) { var hits = {}; @@ -127,7 +127,7 @@ $(function() { link.attr('href', file.url) .attr('target', '_BLANK') - .text(file.url.replace("http://", "").replace("https://", "")); + .text(file.url.replace('http://', '').replace('https://', '')); $('.file-url', row).append(link); }); -- cgit v1.2.3 From 287d0536213783c778a69fc9a28ba8b819384156 Mon Sep 17 00:00:00 2001 From: WubTheCaptain Date: Sat, 19 Sep 2015 16:40:48 +0000 Subject: Wrap long lines to 80 characters in length String concatenation is used because line terminating characters are not allowed in the ECMAScript spec. Certain style guides such as Airbnb also endorse using string concatenation. --- js/pomf.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'js') diff --git a/js/pomf.js b/js/pomf.js index c1d89de..7375797 100644 --- a/js/pomf.js +++ b/js/pomf.js @@ -84,7 +84,8 @@ $(function() { if (files.size > MAX_SIZE) { uploadFiles.addClass('error'); - $('.file-name', totalRow).html('onii-chan y-your upload is t-too big…'); + $('.file-name', totalRow).html('onii-chan y-your upload is t-too ' + + 'big…'); return; } @@ -94,7 +95,8 @@ $(function() { var hits = {}; files.forEach(function(file) { ++hits[file.name] || (hits[file.name] = 0); - var row = $($('li[data-filename="' + escape(file.name) + '"]')[hits[file.name] || 0]); + var row = $($('li[data-filename="' + escape(file.name) + '"]') + + [hits[file.name] || 0]); fn.call(row, row, file, files); }); }; @@ -119,7 +121,8 @@ $(function() { var res = JSON.parse(res); if (!res.success) { uploadFiles.addClass('error'); - $('.file-name', totalRow).text('Something went wrong; try again later.'); + $('.file-name', totalRow).text('Something went wrong; try again ' + + 'later.'); break; } eachRow(res.files, function(row, file, files) { @@ -136,8 +139,10 @@ $(function() { break; case 413: uploadFiles.addClass('error completed'); - // Terrible work-around, but necessary since otherwise the '…' entity is left decoded - totalName.html($('
    ').html('onii-chan, y-your upload is t-too big…').text()); + // Terrible work-around, but necessary since otherwise the '…' + // entity is left decoded. + totalName.html($('
    ').html('onii-chan, y-your upload is t-too ' + + 'big…').text()); break; default: uploadFiles.addClass('error completed'); -- cgit v1.2.3 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/cabinet.js | 1 + js/cheesesteak.js | 1 + 2 files changed, 2 insertions(+) (limited to 'js') diff --git a/js/cabinet.js b/js/cabinet.js index ebbae4f..23a1329 100644 --- a/js/cabinet.js +++ b/js/cabinet.js @@ -1,3 +1,4 @@ +/* jshint browser:true, jquery:true */ ;(function($){ $.hasFileAPI = function hasFileAPI () { return (window.FormData !== undefined) 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/cabinet.js | 70 ++++++----- js/cheesesteak.js | 367 +++++++++++++++++++++++++++++------------------------- 2 files changed, 233 insertions(+), 204 deletions(-) (limited to 'js') diff --git a/js/cabinet.js b/js/cabinet.js index 23a1329..cb8039b 100644 --- a/js/cabinet.js +++ b/js/cabinet.js @@ -1,37 +1,43 @@ /* jshint browser:true, jquery:true */ -;(function($){ - $.hasFileAPI = function hasFileAPI () { - return (window.FormData !== undefined) - } - $.fn.cabinet = function (input) { - input = $(input) +(function($) { + $.hasFileAPI = function hasFileAPI() { + return (window.FormData !== undefined); + }; - var _this = this - var passthrough = function (outer, inner, prevent, fn) { - _this.on(outer, function (e) { - if (inner === 'click') - input[0].click() - else - input.trigger(inner) - if (prevent) e.preventDefault() - if (fn) fn(e) - }, false) - } + $.fn.cabinet = function(input) { + input = $(input); - input[0].filelist = Object.create(FileList) + var _this = this; + var passthrough = function(outer, inner, prevent, fn) { + _this.on(outer, function(e) { + if (inner === 'click') { + input[0].click(); + } else { + input.trigger(inner); + } + if (prevent) { + e.preventDefault(); + } + if (fn) { + fn(e); + } + }, false); + }; - input.on('change', function (e) { - this.filelist = e.target.files - _this.change() - }) + input[0].filelist = Object.create(FileList); - passthrough('dragenter', 'dragenter', true) - passthrough('dragover', 'dragover', true) - passthrough('dragleave', 'dragleave', true) - passthrough('click', 'click', false) - passthrough('drop', 'dragleave', true, function (e) { - input[0].filelist = e.dataTransfer.files - _this.change() - }) - } -})($) + input.on('change', function(e) { + this.filelist = e.target.files; + _this.change(); + }); + + passthrough('dragenter', 'dragenter', true); + passthrough('dragover', 'dragover', true); + passthrough('dragleave', 'dragleave', true); + passthrough('click', 'click', false); + passthrough('drop', 'dragleave', true, function(e) { + input[0].filelist = e.dataTransfer.files; + _this.change(); + }); + }; +})($); 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') 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') 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