Simple image host.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. $(document).on("ready", 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. console.log(evt);
  22. //Enable upload button
  23. $("#uploader-upload").removeAttr("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. var elems = [];
  48. $("#uploader-list .file").each(function() {
  49. var elem = $(this);
  50. elems[elem.data("index")] = elem;
  51. });
  52. //First, create a collection
  53. util.api("collection_create", {
  54. name: "New Collection"
  55. }, function(err, res) {
  56. if (err)
  57. return util.error(err);
  58. var collectionId = res.id;
  59. //Go to collection once files are uploaded
  60. var a = util.async(files.length, function() {
  61. setTimeout(function() {
  62. location.href = "/view?"+collectionId;
  63. }, 1000);
  64. });
  65. //Loop through files, uploading them
  66. files.forEach(function(f, i) {
  67. var progressBar = elems[i].children(".progress-bar");
  68. //Handle progress bars
  69. function getXhr(xhr) {
  70. xhr.upload.addEventListener("progress", function(evt) {
  71. if (!evt.lengthComputable)
  72. return;
  73. var percent = (evt.loaded / evt.total) * 100;
  74. progressBar.css({width: percent+"%"});
  75. }, false);
  76. }
  77. //Get file extension
  78. var ext = f.name.split(".");
  79. ext = ext[ext.length - 1];
  80. util.api("image_create", {
  81. name: f.name,
  82. description: "An image.",
  83. extension: ext,
  84. collectionId: collectionId,
  85. file: f
  86. }, function(err, res) {
  87. if (err)
  88. return util.error(err);
  89. a();
  90. }, getXhr);
  91. });
  92. });
  93. });
  94. });