| 1 |
;(function($) { |
| 2 |
|
| 3 |
/** |
| 4 |
* Upload handler helper |
| 5 |
* |
| 6 |
* @param string {browse_button} browse_button ID of the pickfile |
| 7 |
* @param string {container} container ID of the wrapper |
| 8 |
* @param int {max} maximum number of file uplaods |
| 9 |
* @param string {type} |
| 10 |
*/ |
| 11 |
window.WPUF_Uploader = function (browse_button, container, max, type, allowed_type, max_file_size) { |
| 12 |
this.removed_files = [], |
| 13 |
this.container = container; |
| 14 |
this.browse_button = browse_button; |
| 15 |
this.max = max || 1; |
| 16 |
this.count = $('#' + container).find('.wpuf-attachment-list > li').length; //count how many items are there |
| 17 |
this.perFileCount = 0; //file count on each upload |
| 18 |
|
| 19 |
//if no element found on the page, bail out |
| 20 |
if( !$('#'+browse_button).length ) { |
| 21 |
return; |
| 22 |
} |
| 23 |
|
| 24 |
//instantiate the uploader |
| 25 |
this.uploader = new plupload.Uploader({ |
| 26 |
runtimes: 'html5,html4', |
| 27 |
browse_button: browse_button, |
| 28 |
container: container, |
| 29 |
multipart: true, |
| 30 |
multipart_params: { |
| 31 |
action: 'wpuf_file_upload', |
| 32 |
form_id: $( '#' + browse_button ).data('form_id') |
| 33 |
}, |
| 34 |
max_file_count : 2, |
| 35 |
multiple_queues: false, |
| 36 |
multi_selection: ( ( browse_button == 'wpuf-avatar-pickfiles' || browse_button == 'wpuf-featured_image-pickfiles' ) ? false : true ), |
| 37 |
urlstream_upload: true, |
| 38 |
file_data_name: 'wpuf_file', |
| 39 |
max_file_size: max_file_size + 'kb', |
| 40 |
url: wpuf_frontend_upload.plupload.url + '&type=' + type, |
| 41 |
flash_swf_url: wpuf_frontend_upload.flash_swf_url, |
| 42 |
filters: [{ |
| 43 |
title: 'Allowed Files', |
| 44 |
extensions: allowed_type |
| 45 |
}] |
| 46 |
}); |
| 47 |
|
| 48 |
//attach event handlers |
| 49 |
this.uploader.bind('Init', $.proxy(this, 'init')); |
| 50 |
this.uploader.bind('FilesAdded', $.proxy(this, 'added')); |
| 51 |
this.uploader.bind('QueueChanged', $.proxy(this, 'upload')); |
| 52 |
this.uploader.bind('UploadProgress', $.proxy(this, 'progress')); |
| 53 |
this.uploader.bind('Error', $.proxy(this, 'error')); |
| 54 |
this.uploader.bind('FileUploaded', $.proxy(this, 'uploaded')); |
| 55 |
|
| 56 |
this.uploader.init(); |
| 57 |
|
| 58 |
$('#' + container).on('click', 'a.attachment-delete', $.proxy(this.removeAttachment, this)); |
| 59 |
|
| 60 |
return this.uploader; |
| 61 |
}; |
| 62 |
|
| 63 |
WPUF_Uploader.prototype = { |
| 64 |
|
| 65 |
init: function (up, params) { |
| 66 |
this.showHide(); |
| 67 |
$('#' + this.container).prepend('<div class="wpuf-file-warning"></div>'); |
| 68 |
}, |
| 69 |
|
| 70 |
showHide: function () { |
| 71 |
|
| 72 |
if ( this.count >= this.max) { |
| 73 |
|
| 74 |
if ( this.count > this.max ) { |
| 75 |
$('#' + this.container + ' .wpuf-file-warning').html( wpuf_frontend_upload.warning ); |
| 76 |
} else { |
| 77 |
$('#' + this.container + ' .wpuf-file-warning').html( wpuf_frontend_upload.warning ); |
| 78 |
} |
| 79 |
|
| 80 |
$('#' + this.container).find('.file-selector').hide(); |
| 81 |
|
| 82 |
return; |
| 83 |
}; |
| 84 |
$('#' + this.container + ' .wpuf-file-warning').html( '' ); |
| 85 |
$('#' + this.container).find('.file-selector').show(); |
| 86 |
}, |
| 87 |
|
| 88 |
added: function (up, files) { |
| 89 |
var $container = $('#' + this.container).find('.wpuf-attachment-upload-filelist'); |
| 90 |
|
| 91 |
this.showHide(); |
| 92 |
|
| 93 |
$.each(files, function(i, file) { |
| 94 |
$container.append( |
| 95 |
'<div class="upload-item" id="' + file.id + '"><div class="progress progress-striped active"><div class="bar"></div></div><div class="filename original">' + |
| 96 |
file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>' + |
| 97 |
'</div></div>'); |
| 98 |
}); |
| 99 |
|
| 100 |
up.refresh(); // Reposition Flash/Silverlight |
| 101 |
up.start(); |
| 102 |
}, |
| 103 |
|
| 104 |
upload: function (uploader) { |
| 105 |
|
| 106 |
|
| 107 |
this.count = uploader.files.length - this.removed_files.length ; |
| 108 |
this.showHide(); |
| 109 |
|
| 110 |
|
| 111 |
}, |
| 112 |
|
| 113 |
progress: function (up, file) { |
| 114 |
var item = $('#' + file.id); |
| 115 |
|
| 116 |
$('.bar', item).css({ width: file.percent + '%' }); |
| 117 |
$('.percent', item).html( file.percent + '%' ); |
| 118 |
}, |
| 119 |
|
| 120 |
error: function (up, error) { |
| 121 |
$('#' + this.container).find('#' + error.file.id).remove(); |
| 122 |
|
| 123 |
var msg = ''; |
| 124 |
switch (error.code) { |
| 125 |
case -600: |
| 126 |
msg = wpuf_frontend_upload.plupload.size_error; |
| 127 |
break; |
| 128 |
|
| 129 |
case -601: |
| 130 |
msg = wpuf_frontend_upload.plupload.type_error; |
| 131 |
break; |
| 132 |
|
| 133 |
default: |
| 134 |
msg = 'Error #' + error.code + ': ' + error.message; |
| 135 |
break; |
| 136 |
} |
| 137 |
|
| 138 |
alert(msg); |
| 139 |
|
| 140 |
this.count -= 1; |
| 141 |
this.showHide(); |
| 142 |
this.uploader.refresh(); |
| 143 |
}, |
| 144 |
|
| 145 |
uploaded: function (up, file, response) { |
| 146 |
// var res = $.parseJSON(response.response); |
| 147 |
var self = this; |
| 148 |
|
| 149 |
$('#' + file.id + " b").html("100%"); |
| 150 |
$('#' + file.id).remove(); |
| 151 |
|
| 152 |
if(response.response !== 'error') { |
| 153 |
|
| 154 |
this.perFileCount++; |
| 155 |
var $container = $('#' + this.container).find('.wpuf-attachment-list'); |
| 156 |
$container.append(response.response); |
| 157 |
|
| 158 |
if ( this.perFileCount > this.max ) { |
| 159 |
var attach_id = $('.wpuf-image-wrap:last a.attachment-delete',$container).data('attach_id'); |
| 160 |
self.removeExtraAttachment(attach_id); |
| 161 |
$('.wpuf-image-wrap',$container).last().remove(); |
| 162 |
this.perFileCount--; |
| 163 |
} |
| 164 |
|
| 165 |
} else { |
| 166 |
alert(response.error); |
| 167 |
|
| 168 |
this.count -= 1; |
| 169 |
this.showHide(); |
| 170 |
} |
| 171 |
}, |
| 172 |
|
| 173 |
removeAttachment: function(e) { |
| 174 |
e.preventDefault(); |
| 175 |
|
| 176 |
var self = this, |
| 177 |
el = $(e.currentTarget); |
| 178 |
|
| 179 |
if ( confirm(wpuf_frontend_upload.confirmMsg) ) { |
| 180 |
var data = { |
| 181 |
'attach_id' : el.data('attach_id'), |
| 182 |
'nonce' : wpuf_frontend_upload.nonce, |
| 183 |
'action' : 'wpuf_file_del' |
| 184 |
}; |
| 185 |
this.removed_files.push(data); |
| 186 |
jQuery.post(wpuf_frontend_upload.ajaxurl, data, function() { |
| 187 |
self.perFileCount--; |
| 188 |
el.parent().parent().remove(); |
| 189 |
|
| 190 |
self.count -= 1; |
| 191 |
self.showHide(); |
| 192 |
self.uploader.refresh(); |
| 193 |
}); |
| 194 |
} |
| 195 |
}, |
| 196 |
|
| 197 |
removeExtraAttachment : function( attach_id ) { |
| 198 |
|
| 199 |
|
| 200 |
var self = this; |
| 201 |
|
| 202 |
var data = { |
| 203 |
'attach_id' : attach_id, |
| 204 |
'nonce' : wpuf_frontend_upload.nonce, |
| 205 |
'action' : 'wpuf_file_del' |
| 206 |
}; |
| 207 |
this.removed_files.push(data); |
| 208 |
jQuery.post(wpuf_frontend_upload.ajaxurl, data, function() { |
| 209 |
self.count -= 1; |
| 210 |
self.showHide(); |
| 211 |
self.uploader.refresh(); |
| 212 |
}); |
| 213 |
} |
| 214 |
|
| 215 |
}; |
| 216 |
})(jQuery); |