-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
144 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,177 +1,172 @@ | ||
/* | ||
The MIT License (MIT) | ||
Copyright (c) 2014 Bradley Griffiths | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
The MIT License (MIT) | ||
Copyright (c) 2014 Bradley Griffiths | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
(function(){ | ||
'use strict'; | ||
|
||
"use strict" | ||
(function () { | ||
var getCookie = function (name) { | ||
var value = '; ' + document.cookie | ||
var parts = value.split('; ' + name + '=') | ||
if (parts.length === 2) return parts.pop().split(';').shift() | ||
} | ||
|
||
var getCookie = function(name) { | ||
var value = '; ' + document.cookie, | ||
parts = value.split('; ' + name + '=') | ||
if (parts.length == 2) return parts.pop().split(';').shift() | ||
} | ||
|
||
var request = function(method, url, data, headers, el, showProgress, cb) { | ||
var req = new XMLHttpRequest() | ||
req.open(method, url, true) | ||
|
||
Object.keys(headers).forEach(function(key){ | ||
req.setRequestHeader(key, headers[key]) | ||
}) | ||
var request = function (method, url, data, headers, el, showProgress, cb) { | ||
var req = new XMLHttpRequest() | ||
req.open(method, url, true) | ||
|
||
req.onload = function() { | ||
cb(req.status, req.responseText) | ||
} | ||
|
||
req.onerror = req.onabort = function() { | ||
disableSubmit(false) | ||
error(el, 'Sorry, failed to upload file.') | ||
} | ||
|
||
req.upload.onprogress = function(data) { | ||
progressBar(el, data, showProgress) | ||
} | ||
Object.keys(headers).forEach(function (key) { | ||
req.setRequestHeader(key, headers[key]) | ||
}) | ||
|
||
req.send(data) | ||
req.onload = function () { | ||
cb(req.status, req.responseText) | ||
} | ||
|
||
var parseURL = function(text) { | ||
var xml = new DOMParser().parseFromString(text, 'text/xml'), | ||
tag = xml.getElementsByTagName('Key')[0], | ||
url = unescape(tag.childNodes[0].nodeValue) | ||
|
||
return url | ||
req.onerror = req.onabort = function () { | ||
disableSubmit(false) | ||
error(el, 'Sorry, failed to upload file.') | ||
} | ||
|
||
var parseJson = function(json) { | ||
var data | ||
try {data = JSON.parse(json)} | ||
catch(e){ data = null } | ||
return data | ||
req.upload.onprogress = function (data) { | ||
progressBar(el, data, showProgress) | ||
} | ||
|
||
var progressBar = function(el, data, showProgress) { | ||
if(data.lengthComputable === false || showProgress === false) return | ||
|
||
var pcnt = Math.round(data.loaded * 100 / data.total), | ||
bar = el.querySelector('.progress-bar') | ||
req.send(data) | ||
} | ||
|
||
bar.style.width = pcnt + '%' | ||
bar.innerHTML = pcnt + '%' | ||
} | ||
|
||
var error = function(el, msg) { | ||
el.className = 's3file' | ||
el.querySelector('input[type="file"]').value = '' | ||
alert(msg) | ||
} | ||
var parseURL = function (text) { | ||
var xml = new DOMParser().parseFromString(text, 'text/xml') | ||
var tag = xml.getElementsByTagName('Key')[0] | ||
return unescape(tag.childNodes[0].nodeValue) | ||
} | ||
|
||
var update = function(el, xml) { | ||
el.querySelector('input[type="hidden"]').value = parseURL(xml) | ||
el.className = 's3file' | ||
el.querySelector('.progress-bar').style.width = '0%' | ||
} | ||
var parseJson = function (json) { | ||
var data | ||
try { data = JSON.parse(json) } catch (e) { data = null } | ||
return data | ||
} | ||
|
||
var concurrentUploads = 0 | ||
var disableSubmit = function(status) { | ||
var submitRow = document.querySelector('.submit-row') | ||
if( ! submitRow) return | ||
var progressBar = function (el, data, showProgress) { | ||
if (data.lengthComputable === false || showProgress === false) return | ||
|
||
var buttons = submitRow.querySelectorAll('input[type=submit]') | ||
var pcnt = Math.round(data.loaded * 100 / data.total), | ||
bar = el.querySelector('.progress-bar') | ||
|
||
if (status === true) concurrentUploads++ | ||
else concurrentUploads-- | ||
bar.style.width = pcnt + '%' | ||
bar.innerHTML = pcnt + '%' | ||
} | ||
|
||
;[].forEach.call(buttons, function(el){ | ||
el.disabled = (concurrentUploads !== 0) | ||
}) | ||
} | ||
var error = function (el, msg) { | ||
el.className = 's3file' | ||
el.querySelector('input[type="file"]').value = '' | ||
alert(msg) | ||
} | ||
|
||
var upload = function(file, data, el) { | ||
var form = new FormData() | ||
var update = function (el, xml) { | ||
el.querySelector('input[type="hidden"]').value = parseURL(xml) | ||
el.className = 's3file' | ||
el.querySelector('.progress-bar').style.width = '0%' | ||
} | ||
|
||
disableSubmit(true) | ||
var concurrentUploads = 0 | ||
var disableSubmit = function (status) { | ||
var submitRow = document.querySelector('.submit-row') | ||
if (!submitRow) return | ||
|
||
if (data === null) return error(el, 'Sorry, could not get upload URL.') | ||
var buttons = submitRow.querySelectorAll('input[type=submit]') | ||
|
||
el.className = 's3file progress-active' | ||
var url = data['form_action'] | ||
delete data['form_action'] | ||
if (status === true) concurrentUploads++ | ||
else concurrentUploads-- | ||
|
||
Object.keys(data).forEach(function(key){ | ||
form.append(key, data[key]) | ||
}) | ||
form.append('file', file) | ||
[].forEach.call(buttons, function (el) { | ||
el.disabled = (concurrentUploads !== 0) | ||
}) | ||
} | ||
|
||
request('POST', url, form, {}, el, true, function(status, xml){ | ||
disableSubmit(false) | ||
if(status !== 201) return error(el, 'Sorry, failed to upload to S3.') | ||
update(el, xml) | ||
}) | ||
} | ||
var upload = function (file, data, el) { | ||
var form = new FormData() | ||
|
||
var getUploadURL = function(e) { | ||
var el = e.target.parentElement, | ||
file = el.querySelector('input[type="file"]').files[0], | ||
url = el.getAttribute('data-policy-url'), | ||
form = new FormData(), | ||
headers = {'X-CSRFToken': getCookie('csrftoken')} | ||
|
||
form.append('type', file.type) | ||
form.append('name', file.name) | ||
|
||
request('POST', url, form, headers, el, false, function(status, json){ | ||
var data = parseJson(json) | ||
|
||
switch(status) { | ||
case 200: | ||
upload(file, data, el) | ||
break | ||
case 400: | ||
case 403: | ||
error(el, data.error) | ||
break; | ||
default: | ||
error(el, 'Sorry, could not get upload URL.') | ||
} | ||
}) | ||
} | ||
disableSubmit(true) | ||
|
||
var addHandlers = function(el) { | ||
var input = el.querySelector('input[type="file"]') | ||
if (data === null) return error(el, 'Sorry, could not get upload URL.') | ||
|
||
input.addEventListener('change', getUploadURL, false) | ||
} | ||
el.className = 's3file progress-active' | ||
var url = data['form_action'] | ||
delete data['form_action'] | ||
|
||
document.addEventListener('DOMContentLoaded', function(e) { | ||
;[].forEach.call(document.querySelectorAll('.s3file'), addHandlers) | ||
Object.keys(data).forEach(function (key) { | ||
form.append(key, data[key]) | ||
}) | ||
form.append('file', file) | ||
|
||
document.addEventListener('DOMNodeInserted', function(e){ | ||
if(e.target.tagName) { | ||
var el = e.target.querySelector('.s3file') | ||
if(el) addHandlers(el) | ||
} | ||
request('POST', url, form, {}, el, true, function (status, xml) { | ||
disableSubmit(false) | ||
if (status !== 201) return error(el, 'Sorry, failed to upload to S3.') | ||
update(el, xml) | ||
}) | ||
} | ||
|
||
var getUploadURL = function (e) { | ||
var el = e.target.parentElement, | ||
file = el.querySelector('input[type="file"]').files[0], | ||
url = el.getAttribute('data-policy-url'), | ||
form = new FormData(), | ||
headers = {'X-CSRFToken': getCookie('csrftoken')} | ||
|
||
form.append('type', file.type) | ||
form.append('name', file.name) | ||
|
||
request('POST', url, form, headers, el, false, function (status, json) { | ||
var data = parseJson(json) | ||
|
||
switch (status) { | ||
case 200: | ||
upload(file, data, el) | ||
break | ||
case 400: | ||
case 403: | ||
error(el, data.error) | ||
break | ||
default: | ||
error(el, 'Sorry, could not get upload URL.') | ||
} | ||
}) | ||
} | ||
|
||
var addHandlers = function (el) { | ||
var input = el.querySelector('input[type="file"]') | ||
|
||
input.addEventListener('change', getUploadURL, false) | ||
} | ||
|
||
document.addEventListener('DOMContentLoaded', function (e) { | ||
[].forEach.call(document.querySelectorAll('.s3file'), addHandlers) | ||
}) | ||
|
||
document.addEventListener('DOMNodeInserted', function (e) { | ||
if (e.target.tagName) { | ||
var el = e.target.querySelector('.s3file') | ||
if (el) addHandlers(el) | ||
} | ||
}) | ||
})() |