From ad40c62bb6ab3a83fabd4f8cb8cf86b20083e257 Mon Sep 17 00:00:00 2001 From: WubTheCaptain Date: Sat, 19 Sep 2015 13:34:43 +0000 Subject: Change Git URL to self-hosted pantsu/pomf.git --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 599149e..efedfaf 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ following shell code: ``` npm install -g grunt-cli -git clone https://git.pantsu.cat/pantsucat/pomf.git +git clone https://git.pantsu.cat/pantsu/pomf.git cd pomf/ npm install grunt -- cgit v1.2.3 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(-) 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(+) 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(-) 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(-) 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(-) 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(-) 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(-) 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(-) 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 1b6ee5284928319da3b1834fc7837fc7ed4220ad Mon Sep 17 00:00:00 2001 From: pantsucat Date: Sun, 20 Sep 2015 15:20:21 +1000 Subject: Add jshint grunt task and dependency --- Gruntfile.js | 5 +++++ package.json | 11 ++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 2cbd42f..64c2f7a 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -97,7 +97,11 @@ module.exports = function (grunt) { dest: 'dist/' }] } + }, + jshint: { + all: ['Gruntfile.js', 'pomf.js'] } + }); grunt.loadNpmTasks('grunt-swig'); @@ -106,6 +110,7 @@ module.exports = function (grunt) { grunt.loadNpmTasks('grunt-contrib-htmlmin'); grunt.loadNpmTasks('grunt-contrib-cssmin'); grunt.loadNpmTasks('grunt-contrib-imagemin'); + grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.loadNpmTasks('grunt-contrib-copy'); grunt.registerTask('default', ['mkdir', 'swig', 'htmlmin', 'cssmin', 'uglify', 'imagemin', 'copy']); diff --git a/package.json b/package.json index 053988c..638a771 100644 --- a/package.json +++ b/package.json @@ -14,12 +14,13 @@ }, "devDependencies": { "grunt": "~0.4.1", - "grunt-swig": "~0.1.2", - "grunt-mkdir": "~0.1.1", - "grunt-contrib-uglify": "~0.2.5", + "grunt-contrib-copy": "~0.4.1", + "grunt-contrib-cssmin": "~0.6.2", "grunt-contrib-htmlmin": "~0.1.3", "grunt-contrib-imagemin": "~0.3.0", - "grunt-contrib-cssmin": "~0.6.2", - "grunt-contrib-copy": "~0.4.1" + "grunt-contrib-jshint": "^0.11.3", + "grunt-contrib-uglify": "~0.2.5", + "grunt-mkdir": "~0.1.1", + "grunt-swig": "~0.1.2", } } -- cgit v1.2.3 From dcac6c5b396b3e64ab9663de86123fd1e09d8a47 Mon Sep 17 00:00:00 2001 From: pantsucat Date: Sun, 20 Sep 2015 15:28:42 +1000 Subject: Remove extra comma in package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 638a771..d779035 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,6 @@ "grunt-contrib-jshint": "^0.11.3", "grunt-contrib-uglify": "~0.2.5", "grunt-mkdir": "~0.1.1", - "grunt-swig": "~0.1.2", + "grunt-swig": "~0.1.2" } } -- cgit v1.2.3 From 2c7aab1dde0e1814434cd79bb05b0f0f40f500e9 Mon Sep 17 00:00:00 2001 From: pantsucat Date: Sun, 20 Sep 2015 15:45:44 +1000 Subject: Add jshint to grunt.registerTask --- Gruntfile.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gruntfile.js b/Gruntfile.js index 64c2f7a..d61a120 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -113,5 +113,5 @@ module.exports = function (grunt) { grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.loadNpmTasks('grunt-contrib-copy'); - grunt.registerTask('default', ['mkdir', 'swig', 'htmlmin', 'cssmin', 'uglify', 'imagemin', 'copy']); + grunt.registerTask('default', ['jshint', 'mkdir', 'swig', 'htmlmin', 'cssmin', 'uglify', 'imagemin', 'copy']); }; -- 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(+) 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 fc638db5fa58614f2839def3d1beb4bce6baf60e Mon Sep 17 00:00:00 2001 From: pantsucat Date: Sun, 20 Sep 2015 22:11:57 +1000 Subject: Add all Javascript files to JSHint task --- Gruntfile.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Gruntfile.js b/Gruntfile.js index d61a120..28e73dd 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -99,7 +99,15 @@ module.exports = function (grunt) { } }, jshint: { - all: ['Gruntfile.js', 'pomf.js'] + options: { + curly: true, + }, + all: [ + 'Gruntfile.js', + 'js/pomf.js', + 'js/cabinet.js', + 'js/cheesesteak.js' + ] } }); -- cgit v1.2.3 From 579ef95912401c4cca67c18b9f9805a19b48f29f Mon Sep 17 00:00:00 2001 From: WubTheCaptain Date: Sun, 20 Sep 2015 12:20:30 +0000 Subject: Add undef: true check to Gruntfile's JSHint task This should also fix the syntax, which fixes a build failure. --- Gruntfile.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gruntfile.js b/Gruntfile.js index 28e73dd..7013c07 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -101,6 +101,7 @@ module.exports = function (grunt) { jshint: { options: { curly: true, + undef: true }, all: [ 'Gruntfile.js', @@ -109,7 +110,6 @@ module.exports = function (grunt) { 'js/cheesesteak.js' ] } - }); grunt.loadNpmTasks('grunt-swig'); -- cgit v1.2.3 From 8d369769c099c706a12b4b1e1393f718593b20be Mon Sep 17 00:00:00 2001 From: WubTheCaptain Date: Sun, 20 Sep 2015 12:22:32 +0000 Subject: Indent Gruntfile from tabs to two spaces This creates consistency with the project's style. --- Gruntfile.js | 242 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 121 insertions(+), 121 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 7013c07..41feacc 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -1,125 +1,125 @@ module.exports = function (grunt) { - grunt.initConfig({ - pkg: grunt.file.readJSON('package.json'), - swig: { - dist: { - init: { - root: "pages/", - allowErrors: false, - autoescape: true - }, - banners: grunt.file.readJSON('pages/banners.json'), - max_upload_size: 120, - dest: "dist/", - cwd: "pages/", - generateSitemap: false, - generateRobotstxt: false, - src: [ - 'index.swig', - 'faq.swig', - 'tools.swig', - 'other.swig', - 'nojs.swig', - 'ca.swig' - ], - siteUrl: 'https://pantsu.cat/', - production: false, - } - }, - htmlmin: { - dist: { - options: { - removeComments: true, - collapseWhitespace: true, - conservativeCollapse: true, - removeEmptyAttributes: true - }, - files: [{ - expand: true, - cwd: 'dist/', - src: '**/*.html', - dest: 'dist/' - }] - } - }, - uglify: { - options: { - banner: '/*! <%= pkg.name %> (<%= pkg.repository.url %>) @ <%= grunt.template.today("yyyy-mm-dd") %> */\n' + - '// @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT\n', - footer: '\n// @license-end', - screwIE8: true - }, - dist: { - files: { - 'dist/pomf.min.js': [ - 'js/zepto.js', - 'js/cheesesteak.js', - 'js/cabinet.js', - 'js/pomf.js' - ] - } - } - }, - cssmin: { - options: { - banner: '/*! <%= pkg.name %> (<%= pkg.repository.url %>) @ <%= grunt.template.today("yyyy-mm-dd") %> */\n\n' - }, - dist: { - files: { - 'dist/pomf.min.css': [ - 'css/pomf.css' - ] - } - } - }, - mkdir: { - options: { - mode: 0700, - create: ['dist/img'] - } - }, - imagemin: { - dist: { - files: [{ - expand: true, - cwd: 'img/', - src: '**/*.{png,jpg,gif}', - dest: 'dist/img/' - }] - } - }, - copy: { - dist: { - files: [{ - expand: true, - cwd: 'static/', - src: '**', - dest: 'dist/' - }] - } - }, - jshint: { - options: { - curly: true, - undef: true - }, - all: [ - 'Gruntfile.js', - 'js/pomf.js', - 'js/cabinet.js', - 'js/cheesesteak.js' - ] - } - }); + grunt.initConfig({ + pkg: grunt.file.readJSON('package.json'), + swig: { + dist: { + init: { + root: "pages/", + allowErrors: false, + autoescape: true + }, + banners: grunt.file.readJSON('pages/banners.json'), + max_upload_size: 120, + dest: "dist/", + cwd: "pages/", + generateSitemap: false, + generateRobotstxt: false, + src: [ + 'index.swig', + 'faq.swig', + 'tools.swig', + 'other.swig', + 'nojs.swig', + 'ca.swig' + ], + siteUrl: 'https://pantsu.cat/', + production: false, + } + }, + htmlmin: { + dist: { + options: { + removeComments: true, + collapseWhitespace: true, + conservativeCollapse: true, + removeEmptyAttributes: true + }, + files: [{ + expand: true, + cwd: 'dist/', + src: '**/*.html', + dest: 'dist/' + }] + } + }, + uglify: { + options: { + banner: '/*! <%= pkg.name %> (<%= pkg.repository.url %>) @ <%= grunt.template.today("yyyy-mm-dd") %> */\n' + + '// @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT\n', + footer: '\n// @license-end', + screwIE8: true + }, + dist: { + files: { + 'dist/pomf.min.js': [ + 'js/zepto.js', + 'js/cheesesteak.js', + 'js/cabinet.js', + 'js/pomf.js' + ] + } + } + }, + cssmin: { + options: { + banner: '/*! <%= pkg.name %> (<%= pkg.repository.url %>) @ <%= grunt.template.today("yyyy-mm-dd") %> */\n\n' + }, + dist: { + files: { + 'dist/pomf.min.css': [ + 'css/pomf.css' + ] + } + } + }, + mkdir: { + options: { + mode: 0700, + create: ['dist/img'] + } + }, + imagemin: { + dist: { + files: [{ + expand: true, + cwd: 'img/', + src: '**/*.{png,jpg,gif}', + dest: 'dist/img/' + }] + } + }, + copy: { + dist: { + files: [{ + expand: true, + cwd: 'static/', + src: '**', + dest: 'dist/' + }] + } + }, + jshint: { + options: { + curly: true, + undef: true + }, + all: [ + 'Gruntfile.js', + 'js/pomf.js', + 'js/cabinet.js', + 'js/cheesesteak.js' + ] + } + }); - grunt.loadNpmTasks('grunt-swig'); - grunt.loadNpmTasks('grunt-mkdir'); - grunt.loadNpmTasks('grunt-contrib-uglify'); - grunt.loadNpmTasks('grunt-contrib-htmlmin'); - grunt.loadNpmTasks('grunt-contrib-cssmin'); - grunt.loadNpmTasks('grunt-contrib-imagemin'); - grunt.loadNpmTasks('grunt-contrib-jshint'); - grunt.loadNpmTasks('grunt-contrib-copy'); + grunt.loadNpmTasks('grunt-swig'); + grunt.loadNpmTasks('grunt-mkdir'); + grunt.loadNpmTasks('grunt-contrib-uglify'); + grunt.loadNpmTasks('grunt-contrib-htmlmin'); + grunt.loadNpmTasks('grunt-contrib-cssmin'); + grunt.loadNpmTasks('grunt-contrib-imagemin'); + grunt.loadNpmTasks('grunt-contrib-jshint'); + grunt.loadNpmTasks('grunt-contrib-copy'); - grunt.registerTask('default', ['jshint', 'mkdir', 'swig', 'htmlmin', 'cssmin', 'uglify', 'imagemin', 'copy']); + grunt.registerTask('default', ['jshint', 'mkdir', 'swig', 'htmlmin', 'cssmin', 'uglify', 'imagemin', 'copy']); }; -- cgit v1.2.3 From f24e5ee6ad88c40f813102d385ccdc4ee7b44c6d Mon Sep 17 00:00:00 2001 From: WubTheCaptain Date: Sun, 20 Sep 2015 12:31:12 +0000 Subject: Remove Gruntfile from JSHint testing There's no sense to test the Gruntfile at this time, because it will always fail to module.exports which works as intended. --- Gruntfile.js | 1 - 1 file changed, 1 deletion(-) diff --git a/Gruntfile.js b/Gruntfile.js index 41feacc..5a1bd94 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -104,7 +104,6 @@ module.exports = function (grunt) { undef: true }, all: [ - 'Gruntfile.js', 'js/pomf.js', 'js/cabinet.js', 'js/cheesesteak.js' -- cgit v1.2.3 From e88b0a008de53cd344bf67d02392f241edbe16db Mon Sep 17 00:00:00 2001 From: WubTheCaptain Date: Sun, 20 Sep 2015 12:33:03 +0000 Subject: Move JSHint task away from default/dist to test It should not be fatal to fail the build process if there's a syntax error somewhere. Users don't need to care about that too much, only developers should. --- Gruntfile.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Gruntfile.js b/Gruntfile.js index 5a1bd94..c0249cb 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -120,5 +120,6 @@ module.exports = function (grunt) { grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.loadNpmTasks('grunt-contrib-copy'); - grunt.registerTask('default', ['jshint', 'mkdir', 'swig', 'htmlmin', 'cssmin', 'uglify', 'imagemin', 'copy']); + grunt.registerTask('default', ['mkdir', 'swig', 'htmlmin', 'cssmin', 'uglify', 'imagemin', 'copy']); + grunt.registerTask('test', ['jshint']); }; -- 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(-) 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(-) 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(-) 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