/*
* jQuery Image Upload
*
* A jQuery plugin that adds controls to the selected jQuery
* elements with a simple call.
*
* Example:
*
* $("img").imageUpload();
*
* Released with <3 and JavaScript by the @jillix developers.
*
* */
(function ($) {
// Defaults
var defaults = {
wrapContent: "
",
inputFileName: "inputFile",
inputFileClass: "inputFile",
uploadButtonValue: "Upload",
uploadButtonClass: "uploadButton",
browseButtonValue: "Browse",
browseButtonClass: "browseButton",
deleteButtonValue: "Delete image",
deleteButtonClass: "deleteButton",
automaticUpload: false,
formClass: "controlForm",
hideFileInput: true,
hideDeleteButton: false,
hover: true,
addClass: "jQuery-image-upload"
};
/*
* $("[jQuery selector]").imageUpload({...});
* */
$.fn.imageUpload = function (options) {
// Selected jQuery objects
var $self = this;
// Return if no elements
if (!$self.length) { return $self; }
// Defaults
var settings = $.extend(defaults, options);
// Call image upload for each element
if ($self.length > 1) {
$self.each(function () {
$(this).imageUpload(settings);
});
return $self;
}
// Reload imageUpload if it was already created
if ($self.data("imageUpload")) {
$self.trigger("imageUpload.reload");
return $self;
}
// Add class
$self.addClass(settings.addClass);
// Set imageUpload data
$self.data("imageUpload", options);
// Form action not provided
if (!settings.formAction) {
throw new Error ("Form action was not provided. Please provide it: $(...).imageUpload({formAction: '...'})");
}
// Wrap
if (!settings.hover) {
$self.wrap(settings.wrapContent);
}
// Create the control div
var $controls = $("
").addClass("controls");
// Create the file input element
var $fileInput = $("")
.attr({
"type": "file",
"name": settings.inputFileName
})
.addClass(settings.inputFileClass);
// Create the upload button
var $uploadButton = $("