Simple image host.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

script.js 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. (function() {
  2. if (!window.File || !window.FileReader || !window.FileList || !window.Blob) {
  3. notify("Your Browser Sucks.");
  4. }
  5. function draw(files) {
  6. var output = [];
  7. files.forEach(function(f, i) {
  8. output.push(
  9. '<li class="file list-group-item" data-index='+i+'>'+
  10. '<div class="progress-bar"></div>'+
  11. '<button class="btn btn-default delete" onclick="uploaderDelete(this.parentNode)">X</button>'+
  12. '<img class="thumbnail" src="'+f.thumbnail+'">'+
  13. '<span class="name">'+util.htmlEntities(f.name)+'</span>'+
  14. '</li>'
  15. );
  16. });
  17. $("#uploader-list").html(output.join(""));
  18. }
  19. var files = [];
  20. $("#uploader-input").on("change", function(evt) {
  21. //Enable upload button
  22. $("#uploader-upload").removeAttr("disabled")
  23. console.log("making uploader button not disabled");
  24. var inputFiles = evt.target.files;
  25. for (var i = 0; i < inputFiles.length; ++i) (function() {
  26. var f = inputFiles[i];
  27. f.thumbnail = "";
  28. var reader = new FileReader();
  29. reader.readAsDataURL(f);
  30. reader.onload = function(evt) {
  31. f.thumbnail = reader.result;
  32. draw(files);
  33. }
  34. files.push(inputFiles[i]);
  35. })();
  36. draw(files);
  37. });
  38. window.uploaderDelete = function(elem) {
  39. var index = elem.getAttribute("data-index");
  40. delete files[index];
  41. draw(files);
  42. }
  43. //Upload things when the upload button is clicked
  44. $("#uploader-upload").on("click", function(evt) {
  45. //First, disable all buttons
  46. $("#uploader button.btn").prop("disabled", true);
  47. console.log("making buttons disabled");
  48. var elems = [];
  49. $("#uploader-list .file").each(function() {
  50. var elem = $(this);
  51. elems[elem.data("index")] = elem;
  52. });
  53. files.forEach(function(f, i) {
  54. var progressBar = elems[i].children(".progress-bar");
  55. function getXhr(xhr) {
  56. xhr.upload.addEventListener("progress", function(evt) {
  57. if (!evt.lengthComputable)
  58. return;
  59. var percent = (evt.loaded / evt.total) * 100;
  60. progressBar.css({width: percent+"%"});
  61. }, false);
  62. }
  63. var ajax = util.api("upload", {
  64. name: f.name,
  65. data: f
  66. }, function(err, res) {
  67. console.log(res);
  68. }, getXhr);
  69. });
  70. });
  71. })();