Guest User

Untitled

a guest
May 25th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. Function.prototype.bind = function() {
  2. var _$A = function(a) { return Array.prototype.slice.call(a); }
  3. if (arguments.length < 2 && (typeof arguments[0] == "undefined")) return this;
  4. var __method = this, args = _$A(arguments), object = args.shift();
  5. return function() {
  6. return __method.apply(object, args.concat(_$A(arguments)));
  7. }
  8. }
  9.  
  10. PhotoUploader = {
  11. initialize : function (limit, count, url) {
  12. this.loaded = false;
  13. this.uploading = false;
  14. this.limit = limit;
  15. this.count = count;
  16. this.url = url;
  17.  
  18. this.button = $('#photo_add_image');
  19. this.progress = $('#photo_uploader_progress');
  20. this.status = $('#photo_uploader_stats');
  21. this.bar = $('#photo_upload_bar');
  22. this.maxed = $('#photo_uploader_maxed');
  23. this.note = $('#photo_uploader_note');
  24.  
  25. SWFUpload.onload = function() {
  26. this.uploader = new SWFUpload ({
  27. upload_url: url,
  28. flash_url: '/flash/swfupload_f9.swf',
  29. minimum_flash_version: '9.0.28',
  30. debug: false,
  31.  
  32. file_size_limit: '1024', // 1MB
  33. file_types: '*.jpg;*.jpeg;*gif;*.png',
  34. file_type_description: 'Web Images',
  35. file_upload_limit: limit,
  36.  
  37. swfupload_pre_load_handler: null,
  38. swfupload_loaded_handler: this.showUploader.bind(this),
  39. swfupload_load_failed_handler: this.showBasic.bind(this),
  40. file_queue_error_handler: this.onUploadError.bind(this),
  41. file_dialog_complete_handler: this.onFileDialogComplete.bind(this),
  42. upload_start_handler: this.onUploadStart.bind(this),
  43. upload_progress_handler: this.onUploadProgress.bind(this),
  44. upload_error_handler: this.onUploadError.bind(this),
  45. upload_success_handler: this.onUploadSuccess.bind(this),
  46. upload_complete_handler: this.onUploadComplete.bind(this),
  47. queue_complete_handler: this.onQueueComplete.bind(this)
  48. });
  49. }.bind(this);
  50.  
  51. this.updateImages();
  52. },
  53.  
  54. showBasic : function() {
  55. $('#photo_image_frame').show();
  56. },
  57.  
  58. showUploader : function() {
  59. this.loaded = true;
  60. this.setCount(this.count);
  61.  
  62. $('#photo_image_uploader').show();
  63. },
  64.  
  65. setCount : function(n) {
  66. this.count = n;
  67. this.remaining = this.limit - this.count;
  68.  
  69. var stats = this.uploader.getStats();
  70. stats.successful_uploads = n;
  71. this.uploader.setStats(stats);
  72.  
  73. this.button.html((this.remaining == 1) ? 'Add Image' : 'Add Images');
  74.  
  75. if (!this.uploading) this.update();
  76. },
  77.  
  78. update : function() {
  79. if (this.remaining > 0) {
  80. this.button.show();
  81. } else {
  82. this.button.hide();
  83. }
  84. },
  85.  
  86. browse : function() {
  87. this.uploader.selectFiles();
  88. },
  89.  
  90. onUploadError : function(file, errorCode, message) {
  91. var error = "An image upload error has occured."
  92.  
  93. if (errorCode === SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED || errorCode === SWFUpload.UPLOAD_ERROR_LIMIT_EXCEEDED) {
  94. error = "You've selected too many images."
  95. } else if (errorCode === SWFUpload.QUEUE_ERROR.FILE_EXCEEDES_SIZE_LIMIT) {
  96. error = file.name + " is too large. The max is 1 MB.";
  97. }
  98.  
  99. this.onImageError(error);
  100. },
  101.  
  102. onFileDialogComplete : function(numFilesSelected, numFilesQueued) {
  103. if (numFilesQueued == 0) return;
  104. this.uploading = true;
  105. this.button.hide();
  106. // this.progress.show();
  107. this.uploader.startUpload();
  108. },
  109.  
  110. onUploadStart : function(file) {
  111. this.bar.hide();
  112. this.bar.css('width', '0%');
  113. this.bar.show();
  114. },
  115.  
  116. onUploadProgress : function(file, bytesLoaded) {
  117. var percent = Math.ceil((bytesLoaded / file.size) * 100);
  118. percent = percent == 100 && bytesLoaded == 0 ? 0 : percent;
  119. percent = percent == 0 && file.size < 153600 ? 100 : percent;
  120.  
  121. if (percent > 0) {
  122. }
  123.  
  124. if (percent < 100) {
  125. this.status.html('Uploading ' + file.name + '... ' + percent + '%');
  126. } else {
  127. this.status.html('Saving ' + file.name + '...');
  128. }
  129. },
  130.  
  131. onUploadSuccess : function(file, html) {
  132. var id = html.match('id=\'photo_image_(.*)\'')[1];
  133. this.addImage(html, id);
  134. },
  135.  
  136. onUploadComplete : function(file) {
  137. if (this.uploader.getStats().files_queued > 0) this.uploader.startUpload();
  138. },
  139.  
  140. addImage : function(image, id) {
  141. var images = $('#photo_image_list');
  142. images.append(image);
  143.  
  144. var imageDiv = $('#photo_image_' + id);
  145. imageDiv.hide();
  146. imageDiv.fadeIn(0.5);
  147.  
  148. this.updateImages();
  149. },
  150.  
  151. deleteImage : function(id) {
  152. if (confirm('Are you sure you want to delete this image?')) {
  153. $('#photo_image_' + id).remove();
  154. this.updateImages().bind(this);
  155. }
  156. },
  157.  
  158. updateImages : function() {
  159. var images = $('#photo_image_list li');
  160. var count = images.length;
  161. var under = count - this.limit;
  162. var frame = $('#photo_image_frame').contents();
  163. var form = frame.find('#image_form');
  164. var maxed = frame.find('#image_maxed');
  165.  
  166. $('#photo_image_count').html(count.toString());
  167. this.setCount(count);
  168.  
  169. form.css('display', ((under) ? '' : 'none'));
  170. maxed.css('display', ((under) ? 'none' : ''));
  171.  
  172. this.update();
  173. },
  174.  
  175. onQueueComplete : function() {
  176. this.uploading = false;
  177. this.update();
  178. },
  179.  
  180. onImageError : function(message) {
  181. this.updateImages();
  182. alert(message);
  183. }
  184. }
Add Comment
Please, Sign In to add comment