| 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 |
this.UploadedFiles = 0; //file count on each upload |
| 19 |
|
| 20 |
//if no element found on the page, bail out |
| 21 |
if( !$('#'+browse_button).length ) { |
| 22 |
return; |
| 23 |
} |
| 24 |
|
| 25 |
// enable drag option for ordering |
| 26 |
$( "ul.wpuf-attachment-list" ).sortable({ |
| 27 |
placeholder: "highlight" |
| 28 |
}); |
| 29 |
$( "ul.wpuf-attachment-list" ).disableSelection(); |
| 30 |
|
| 31 |
//instantiate the uploader |
| 32 |
this.uploader = new plupload.Uploader({ |
| 33 |
runtimes: 'html5,html4', |
| 34 |
browse_button: browse_button, |
| 35 |
container: container, |
| 36 |
multipart: true, |
| 37 |
multipart_params: { |
| 38 |
action: 'wpuf_upload_file', |
| 39 |
form_id: $( '#' + browse_button ).data('form_id') |
| 40 |
}, |
| 41 |
max_file_count : 2, |
| 42 |
multiple_queues: false, |
| 43 |
multi_selection: ( ( browse_button == 'wpuf-avatar-pickfiles' || browse_button == 'wpuf-featured_image-pickfiles' ) ? false : true ), |
| 44 |
urlstream_upload: true, |
| 45 |
file_data_name: 'wpuf_file', |
| 46 |
max_file_size: max_file_size + 'kb', |
| 47 |
url: wpuf_frontend_upload.plupload.url + '&type=' + type, |
| 48 |
flash_swf_url: wpuf_frontend_upload.flash_swf_url, |
| 49 |
filters: [{ |
| 50 |
title: 'Allowed Files', |
| 51 |
extensions: allowed_type |
| 52 |
}] |
| 53 |
}); |
| 54 |
|
| 55 |
//attach event handlers |
| 56 |
this.uploader.bind('Init', $.proxy(this, 'init')); |
| 57 |
this.uploader.bind('FilesAdded', $.proxy(this, 'added')); |
| 58 |
this.uploader.bind('QueueChanged', $.proxy(this, 'upload')); |
| 59 |
this.uploader.bind('UploadProgress', $.proxy(this, 'progress')); |
| 60 |
this.uploader.bind('Error', $.proxy(this, 'error')); |
| 61 |
this.uploader.bind('FileUploaded', $.proxy(this, 'uploaded')); |
| 62 |
|
| 63 |
this.uploader.init(); |
| 64 |
|
| 65 |
$('#' + container).on('click', 'a.attachment-delete', $.proxy(this.removeAttachment, this)); |
| 66 |
|
| 67 |
return this.uploader; |
| 68 |
}; |
| 69 |
|
| 70 |
WPUF_Uploader.prototype = { |
| 71 |
|
| 72 |
init: function (up, params) { |
| 73 |
this.showHide(); |
| 74 |
$('#' + this.container).prepend('<div class="wpuf-file-warning"></div>'); |
| 75 |
}, |
| 76 |
|
| 77 |
showHide: function () { |
| 78 |
|
| 79 |
if ( this.count >= this.max) { |
| 80 |
|
| 81 |
if ( this.count > this.max ) { |
| 82 |
$('#' + this.container + ' .wpuf-file-warning').html( wpuf_frontend_upload.warning ); |
| 83 |
} else { |
| 84 |
$('#' + this.container + ' .wpuf-file-warning').html( wpuf_frontend_upload.warning ); |
| 85 |
} |
| 86 |
|
| 87 |
$('#' + this.container).find('.file-selector').hide(); |
| 88 |
|
| 89 |
return; |
| 90 |
}; |
| 91 |
$('#' + this.container + ' .wpuf-file-warning').html( '' ); |
| 92 |
$('#' + this.container).find('.file-selector').show(); |
| 93 |
}, |
| 94 |
|
| 95 |
added: function (up, files) { |
| 96 |
var $container = $('#' + this.container).find('.wpuf-attachment-upload-filelist'); |
| 97 |
|
| 98 |
this.showHide(); |
| 99 |
|
| 100 |
$.each(files, function(i, file) { |
| 101 |
$(".wpuf-submit-button").attr("disabled", "disabled"); |
| 102 |
|
| 103 |
$container.append( |
| 104 |
'<div class="upload-item" id="' + file.id + '"><div class="progress progress-striped active"><div class="bar"></div></div><div class="filename original">' + |
| 105 |
file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>' + |
| 106 |
'</div></div>'); |
| 107 |
}); |
| 108 |
|
| 109 |
up.refresh(); // Reposition Flash/Silverlight |
| 110 |
up.start(); |
| 111 |
}, |
| 112 |
|
| 113 |
upload: function (uploader) { |
| 114 |
this.count = uploader.files.length - this.removed_files.length ; |
| 115 |
this.showHide(); |
| 116 |
}, |
| 117 |
|
| 118 |
progress: function (up, file) { |
| 119 |
var item = $('#' + file.id); |
| 120 |
|
| 121 |
$('.bar', item).css({ width: file.percent + '%' }); |
| 122 |
$('.percent', item).html( file.percent + '%' ); |
| 123 |
}, |
| 124 |
|
| 125 |
error: function (up, error) { |
| 126 |
$('#' + this.container).find('#' + error.file.id).remove(); |
| 127 |
|
| 128 |
var msg = ''; |
| 129 |
switch (error.code) { |
| 130 |
case -600: |
| 131 |
msg = wpuf_frontend_upload.plupload.size_error; |
| 132 |
break; |
| 133 |
|
| 134 |
case -601: |
| 135 |
msg = wpuf_frontend_upload.plupload.type_error; |
| 136 |
break; |
| 137 |
|
| 138 |
default: |
| 139 |
msg = 'Error #' + error.code + ': ' + error.message; |
| 140 |
break; |
| 141 |
} |
| 142 |
|
| 143 |
alert(msg); |
| 144 |
|
| 145 |
this.count -= 1; |
| 146 |
this.showHide(); |
| 147 |
this.uploader.refresh(); |
| 148 |
}, |
| 149 |
|
| 150 |
uploaded: function (up, file, response) { |
| 151 |
// var res = $.parseJSON(response.response); |
| 152 |
var self = this; |
| 153 |
|
| 154 |
$('#' + file.id + " b").html("100%"); |
| 155 |
$('#' + file.id).remove(); |
| 156 |
|
| 157 |
if(response.response !== 'error') { |
| 158 |
|
| 159 |
this.perFileCount++; |
| 160 |
this.UploadedFiles++; |
| 161 |
var $container = $('#' + this.container).find('.wpuf-attachment-list'); |
| 162 |
$container.append(response.response); |
| 163 |
|
| 164 |
if ( this.perFileCount > this.max ) { |
| 165 |
var attach_id = $('.wpuf-image-wrap:last a.attachment-delete',$container).data('attach_id'); |
| 166 |
self.removeExtraAttachment(attach_id); |
| 167 |
$('.wpuf-image-wrap',$container).last().remove(); |
| 168 |
this.perFileCount--; |
| 169 |
} |
| 170 |
|
| 171 |
} else { |
| 172 |
alert(response.error); |
| 173 |
|
| 174 |
this.count -= 1; |
| 175 |
this.showHide(); |
| 176 |
} |
| 177 |
|
| 178 |
var uploaded = this.UploadedFiles, |
| 179 |
FileProgress = up.files.length, |
| 180 |
imageCount = $('ul.wpuf-attachment-list > li').length; |
| 181 |
|
| 182 |
if ( imageCount >= this.max ) { |
| 183 |
$('#' + this.container).find('.file-selector').hide(); |
| 184 |
} |
| 185 |
|
| 186 |
if ( FileProgress === uploaded ) { |
| 187 |
if ( typeof grecaptcha !== 'undefined' && !grecaptcha.getResponse().length ) { |
| 188 |
return; |
| 189 |
} |
| 190 |
$(".wpuf-submit-button").removeAttr("disabled"); |
| 191 |
} |
| 192 |
}, |
| 193 |
|
| 194 |
removeAttachment: function(e) { |
| 195 |
e.preventDefault(); |
| 196 |
|
| 197 |
var self = this, |
| 198 |
el = $(e.currentTarget); |
| 199 |
|
| 200 |
swal({ |
| 201 |
text: wpuf_frontend_upload.confirmMsg, |
| 202 |
type: 'warning', |
| 203 |
showCancelButton: true, |
| 204 |
confirmButtonColor: '#d54e21', |
| 205 |
confirmButtonText: wpuf_frontend_upload.delete_it, |
| 206 |
cancelButtonText: wpuf_frontend_upload.cancel_it, |
| 207 |
confirmButtonClass: 'btn btn-success', |
| 208 |
cancelButtonClass: 'btn btn-danger', |
| 209 |
}).then(function () { |
| 210 |
var data = { |
| 211 |
'attach_id' : el.data('attach_id'), |
| 212 |
'nonce' : wpuf_frontend_upload.nonce, |
| 213 |
'action' : 'wpuf_file_del' |
| 214 |
}; |
| 215 |
self.removed_files.push(data); |
| 216 |
jQuery('#del_attach').val(el.data('attach_id')); |
| 217 |
jQuery.post(wpuf_frontend_upload.ajaxurl, data, function() { |
| 218 |
self.perFileCount--; |
| 219 |
el.parent().parent().remove(); |
| 220 |
|
| 221 |
self.count -= 1; |
| 222 |
self.showHide(); |
| 223 |
self.uploader.refresh(); |
| 224 |
}); |
| 225 |
}); |
| 226 |
}, |
| 227 |
|
| 228 |
removeExtraAttachment : function( attach_id ) { |
| 229 |
|
| 230 |
|
| 231 |
var self = this; |
| 232 |
|
| 233 |
var data = { |
| 234 |
'attach_id' : attach_id, |
| 235 |
'nonce' : wpuf_frontend_upload.nonce, |
| 236 |
'action' : 'wpuf_file_del' |
| 237 |
}; |
| 238 |
this.removed_files.push(data); |
| 239 |
jQuery.post(wpuf_frontend_upload.ajaxurl, data, function() { |
| 240 |
self.count -= 1; |
| 241 |
self.showHide(); |
| 242 |
self.uploader.refresh(); |
| 243 |
}); |
| 244 |
} |
| 245 |
|
| 246 |
}; |
| 247 |
})(jQuery); |
| 248 |
|