Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function.prototype.bind = function() {
- var _$A = function(a) { return Array.prototype.slice.call(a); }
- if (arguments.length < 2 && (typeof arguments[0] == "undefined")) return this;
- var __method = this, args = _$A(arguments), object = args.shift();
- return function() {
- return __method.apply(object, args.concat(_$A(arguments)));
- }
- }
- PhotoUploader = {
- initialize : function (limit, count, url) {
- this.loaded = false;
- this.uploading = false;
- this.limit = limit;
- this.count = count;
- this.url = url;
- this.button = $('#photo_add_image');
- this.progress = $('#photo_uploader_progress');
- this.status = $('#photo_uploader_stats');
- this.bar = $('#photo_upload_bar');
- this.maxed = $('#photo_uploader_maxed');
- this.note = $('#photo_uploader_note');
- SWFUpload.onload = function() {
- this.uploader = new SWFUpload ({
- upload_url: url,
- flash_url: '/flash/swfupload_f9.swf',
- minimum_flash_version: '9.0.28',
- debug: false,
- file_size_limit: '1024', // 1MB
- file_types: '*.jpg;*.jpeg;*gif;*.png',
- file_type_description: 'Web Images',
- file_upload_limit: limit,
- swfupload_pre_load_handler: null,
- swfupload_loaded_handler: this.showUploader.bind(this),
- swfupload_load_failed_handler: this.showBasic.bind(this),
- file_queue_error_handler: this.onUploadError.bind(this),
- file_dialog_complete_handler: this.onFileDialogComplete.bind(this),
- upload_start_handler: this.onUploadStart.bind(this),
- upload_progress_handler: this.onUploadProgress.bind(this),
- upload_error_handler: this.onUploadError.bind(this),
- upload_success_handler: this.onUploadSuccess.bind(this),
- upload_complete_handler: this.onUploadComplete.bind(this),
- queue_complete_handler: this.onQueueComplete.bind(this)
- });
- }.bind(this);
- this.updateImages();
- },
- showBasic : function() {
- $('#photo_image_frame').show();
- },
- showUploader : function() {
- this.loaded = true;
- this.setCount(this.count);
- $('#photo_image_uploader').show();
- },
- setCount : function(n) {
- this.count = n;
- this.remaining = this.limit - this.count;
- var stats = this.uploader.getStats();
- stats.successful_uploads = n;
- this.uploader.setStats(stats);
- this.button.html((this.remaining == 1) ? 'Add Image' : 'Add Images');
- if (!this.uploading) this.update();
- },
- update : function() {
- if (this.remaining > 0) {
- this.button.show();
- } else {
- this.button.hide();
- }
- },
- browse : function() {
- this.uploader.selectFiles();
- },
- onUploadError : function(file, errorCode, message) {
- var error = "An image upload error has occured."
- if (errorCode === SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED || errorCode === SWFUpload.UPLOAD_ERROR_LIMIT_EXCEEDED) {
- error = "You've selected too many images."
- } else if (errorCode === SWFUpload.QUEUE_ERROR.FILE_EXCEEDES_SIZE_LIMIT) {
- error = file.name + " is too large. The max is 1 MB.";
- }
- this.onImageError(error);
- },
- onFileDialogComplete : function(numFilesSelected, numFilesQueued) {
- if (numFilesQueued == 0) return;
- this.uploading = true;
- this.button.hide();
- // this.progress.show();
- this.uploader.startUpload();
- },
- onUploadStart : function(file) {
- this.bar.hide();
- this.bar.css('width', '0%');
- this.bar.show();
- },
- onUploadProgress : function(file, bytesLoaded) {
- var percent = Math.ceil((bytesLoaded / file.size) * 100);
- percent = percent == 100 && bytesLoaded == 0 ? 0 : percent;
- percent = percent == 0 && file.size < 153600 ? 100 : percent;
- if (percent > 0) {
- }
- if (percent < 100) {
- this.status.html('Uploading ' + file.name + '... ' + percent + '%');
- } else {
- this.status.html('Saving ' + file.name + '...');
- }
- },
- onUploadSuccess : function(file, html) {
- var id = html.match('id=\'photo_image_(.*)\'')[1];
- this.addImage(html, id);
- },
- onUploadComplete : function(file) {
- if (this.uploader.getStats().files_queued > 0) this.uploader.startUpload();
- },
- addImage : function(image, id) {
- var images = $('#photo_image_list');
- images.append(image);
- var imageDiv = $('#photo_image_' + id);
- imageDiv.hide();
- imageDiv.fadeIn(0.5);
- this.updateImages();
- },
- deleteImage : function(id) {
- if (confirm('Are you sure you want to delete this image?')) {
- $('#photo_image_' + id).remove();
- this.updateImages().bind(this);
- }
- },
- updateImages : function() {
- var images = $('#photo_image_list li');
- var count = images.length;
- var under = count - this.limit;
- var frame = $('#photo_image_frame').contents();
- var form = frame.find('#image_form');
- var maxed = frame.find('#image_maxed');
- $('#photo_image_count').html(count.toString());
- this.setCount(count);
- form.css('display', ((under) ? '' : 'none'));
- maxed.css('display', ((under) ? 'none' : ''));
- this.update();
- },
- onQueueComplete : function() {
- this.uploading = false;
- this.update();
- },
- onImageError : function(message) {
- this.updateImages();
- alert(message);
- }
- }
Add Comment
Please, Sign In to add comment