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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. var inputFiles = evt.target.files;
  24. for (var i = 0; i < inputFiles.length; ++i) (function() {
  25. var f = inputFiles[i];
  26. f.thumbnail = "";
  27. var reader = new FileReader();
  28. reader.readAsDataURL(f);
  29. reader.onload = function(evt) {
  30. f.thumbnail = reader.result;
  31. draw(files);
  32. }
  33. files.push(inputFiles[i]);
  34. })();
  35. draw(files);
  36. });
  37. window.uploaderDelete = function(elem) {
  38. var index = elem.getAttribute("data-index");
  39. delete files[index];
  40. draw(files);
  41. }
  42. //Upload things when the upload button is clicked
  43. $("#uploader-upload").on("click", function(evt) {
  44. //First, disable all buttons
  45. $("#uploader button.btn").prop("disabled", true);
  46. var elems = [];
  47. $("#uploader-list .file").each(function() {
  48. var elem = $(this);
  49. elems[elem.data("index")] = elem;
  50. });
  51. //First, create a collection
  52. util.api("collection_create", {
  53. name: "New Collection"
  54. }, function(err, res) {
  55. if (err)
  56. return util.error(err);
  57. var collectionId = res.id;
  58. //Go to collection once files are uploaded
  59. var a = util.async(files.length, function() {
  60. setTimeout(function() {
  61. location.href = "/view?"+collectionId;
  62. }, 1000);
  63. });
  64. //Loop through files, uploading them
  65. files.forEach(function(f, i) {
  66. var progressBar = elems[i].children(".progress-bar");
  67. //Handle progress bars
  68. function getXhr(xhr) {
  69. xhr.upload.addEventListener("progress", function(evt) {
  70. if (!evt.lengthComputable)
  71. return;
  72. var percent = (evt.loaded / evt.total) * 100;
  73. progressBar.css({width: percent+"%"});
  74. }, false);
  75. }
  76. //Get file extension
  77. var ext = f.name.split(".");
  78. ext = ext[ext.length - 1];
  79. util.api("image_create", {
  80. name: f.name,
  81. description: "An image.",
  82. extension: ext,
  83. collectionId: collectionId,
  84. file: f
  85. }, function(err, res) {
  86. if (err)
  87. return util.error(err);
  88. a();
  89. }, getXhr);
  90. });
  91. });
  92. });
  93. })();