vendor/assets/javascripts/jquery.ezdz.js in ezdz-rails-0.2.0 vs vendor/assets/javascripts/jquery.ezdz.js in ezdz-rails-0.2.1

- old
+ new

@@ -1,16 +1,24 @@ /* ---------------------------------------------------------------------------- - // Ezdz [izy-dizy] jQuery plugin - // v0.2.0 - released 2013-10-16 00:14 + // Ezdz [izy-dizy] + // v0.2.1 - released 2013-10-16 00:41 // Licensed under the MIT license. // https://github.com/jaysalvat/ezdz // ---------------------------------------------------------------------------- // Copyright (C) 2013 Jay Salvat // http://jaysalvat.com/ // ---------------------------------------------------------------------------*/ -(function($) { +(function (factory) { + if (typeof define === 'function' && define.amd) { + define(['jquery'], factory); + } else if (typeof exports === 'object') { + factory(require('jquery')); + } else { + factory(jQuery); + } +}(function ($) { // Default settings var defaults = { className: '', text: 'Drop a file', previewImage: true, @@ -41,66 +49,27 @@ } }; // Main plugin $.ezdz = function(element, options) { - var self = this, - settings = $.extend(true, {}, defaults, $.ezdz.defaults, options), - $input = $(element); + this.settings = $.extend(true, {}, defaults, $.ezdz.defaults, options); + this.$input = $(element); + var self = this, + settings = self.settings, + $input = self.$input; + if (!$input.is('input[type="file"]')) { $.error('Ezdz error - Must be apply to inputs type file.'); return; } // Stop if not compatible with HTML5 file API if (!(window.File && window.FileList && window.FileReader)) { return; } - // Public: Inject a file or image in the preview - self.preview = function(path, callback) { - var basename = path.replace(/\\/g,'/').replace( /.*\//, ''), - formatted = settings.format(basename), - $ezdz = $input.parent('.' + settings.classes.main); - - var img = new Image(); - img.src = path; - - // Is an image - img.onload = function() { - $ezdz.find('div').html($(img).fadeIn()); - - if ($.isFunction(callback)) { - callback.apply(this); - } - }; - - // Is not an image - img.onerror = function() { - $ezdz.find('div').html('<span>' + formatted + '</span>'); - - if ($.isFunction(callback)) { - callback.apply(this); - } - }; - - $ezdz.addClass(settings.classes.accept); - }; - - // Public: Destroy ezdz - self.destroy = function() { - $input.parent('.' + settings.classes.main).replaceWith($input); - $input.off('*.ezdz'); - $input.data('ezdz', ''); - }; - - // Public: Extend settings - self.options = function(values) { - $.extend(true, settings, values); - }; - // private: Init the plugin var init = function() { var $ezdz, $container, value; // Build the container @@ -121,11 +90,11 @@ settings.leaved.apply(this); } }) .addClass(settings.className); - + // Build the whole dropzone $input .wrap($container) .before('<div>' + settings.text + '</div>'); @@ -138,11 +107,11 @@ self.preview(value); } // Trigger the init callback if ($.isFunction(settings.init)) { - settings.init.apply($ezdz, [ value ]); + settings.init.apply($input, [ value ]); } // Events on the input $input @@ -224,116 +193,179 @@ $ezdz.addClass(settings.classes.reject); // Trigger the reject callback if ($.isFunction(settings.reject)) { - settings.reject.apply($ezdz, [ file, errors ]); + settings.reject.apply($input, [ file, errors ]); } return false; } // Read the added file - var reader = new FileReader(file), - img = new Image(); + var reader = new FileReader(file); reader.readAsDataURL(file); reader.onload = function(e) { + var img = new Image(), + isImage; + file.data = e.target.result; img.src = file.data; - var isImage = (img.width && img.height); + setTimeout(function() { + isImage = (img.width && img.height); - // Validator - if (settings.validators.maxSize && file.size > settings.validators.maxSize) { - valid = false; - errors.maxSize = true; - } - - if (isImage) { - file.width = img.width; - file.height = img.height; - - if (settings.validators.width && img.width != settings.validators.width) { + // Validator + if (settings.validators.maxSize && file.size > settings.validators.maxSize) { valid = false; - errors.width = true; + errors.maxSize = true; } - if (settings.validators.maxWidth && img.width > settings.validators.maxWidth) { - valid = false; - errors.maxWidth = true; - } + if (isImage) { + file.width = img.width; + file.height = img.height; - if (settings.validators.minWidth && img.width < settings.validators.minWidth) { - valid = false; - errors.minWidth = true; - } + if (settings.validators.width && img.width != settings.validators.width) { + valid = false; + errors.width = true; + } - if (settings.validators.height && img.height != settings.validators.height) { - valid = false; - errors.height = true; - } + if (settings.validators.maxWidth && img.width > settings.validators.maxWidth) { + valid = false; + errors.maxWidth = true; + } - if (settings.validators.maxHeight && img.height > settings.validators.maxHeight) { - valid = false; - errors.maxHeight = true; - } + if (settings.validators.minWidth && img.width < settings.validators.minWidth) { + valid = false; + errors.minWidth = true; + } - if (settings.validators.minHeight && img.height < settings.validators.minHeight) { - valid = false; - errors.minHeight = true; - } - } + if (settings.validators.height && img.height != settings.validators.height) { + valid = false; + errors.height = true; + } - // The file is validated, so added to input - if (valid === true) { - $ezdz.find('img').remove(); + if (settings.validators.maxHeight && img.height > settings.validators.maxHeight) { + valid = false; + errors.maxHeight = true; + } - if (isImage && settings.previewImage === true) { - $ezdz.find('div').html($(img).fadeIn()); - } else { - $ezdz.find('div').html('<span>' + formatted + '</span>'); + if (settings.validators.minHeight && img.height < settings.validators.minHeight) { + valid = false; + errors.minHeight = true; + } } - $ezdz.addClass(settings.classes.accept); + // The file is validated, so added to input + if (valid === true) { + $ezdz.find('img').remove(); - // Trigger the accept callback - if ($.isFunction(settings.accept)) { - settings.accept.apply($ezdz, [ file ]); - } - // The file is invalidated, so rejected - } else { - $input.val(''); + if (isImage && settings.previewImage === true) { + $ezdz.find('div').html($(img).fadeIn()); + } else { + $ezdz.find('div').html('<span>' + formatted + '</span>'); + } - $ezdz.addClass(settings.classes.reject); + $ezdz.addClass(settings.classes.accept); - // Trigger the reject callback - if ($.isFunction(settings.reject)) { - settings.reject.apply($ezdz, [ file, errors ]); + // Trigger the accept callback + if ($.isFunction(settings.accept)) { + settings.accept.apply($input, [ file ]); + } + // The file is invalidated, so rejected + } else { + $input.val(''); + + $ezdz.addClass(settings.classes.reject); + + // Trigger the reject callback + if ($.isFunction(settings.reject)) { + settings.reject.apply($input, [ file, errors ]); + } } - } + }, 1); }; }); }; init(); }; - $.fn.ezdz = function(options) { - var args = arguments; + // Inject a file or image in the preview + $.ezdz.prototype.preview = function(path, callback) { + var settings = this.settings, + $input = this.$input, + $ezdz = $input.parent('.' + settings.classes.main); + basename = path.replace(/\\/g,'/').replace( /.*\//, ''), + formatted = settings.format(basename); - return this.each(function() { - var plugin = $(this).data('ezdz'); + var img = new Image(); + img.src = path; - if (!plugin) { - return $(this).data('ezdz', new $.ezdz(this, options)); - } if (plugin[options]) { - return plugin[options].apply(this, Array.prototype.slice.call(args, 1)); - } else { - $.error('Ezdz error - Method ' + options + ' does not exist.'); + // Is an image + img.onload = function() { + $ezdz.find('div').html($(img).fadeIn()); + + if ($.isFunction(callback)) { + callback.apply(this); } - }); + }; + + // Is not an image + img.onerror = function() { + $ezdz.find('div').html('<span>' + formatted + '</span>'); + + if ($.isFunction(callback)) { + callback.apply(this); + } + }; + + $ezdz.addClass(settings.classes.accept); }; - $.ezdz.defaults = defaults; + // Destroy ezdz + $.ezdz.prototype.destroy = function() { + var settings = this.settings, + $input = this.$input; -})(jQuery); + $input.parent('.' + settings.classes.main).replaceWith($input); + $input.off('*.ezdz'); + $input.data('ezdz', ''); + }; + + // Extend settings + $.ezdz.prototype.options = function(options) { + var settings = this.settings; + + if (!options) { + return settings; + } + + $.extend(true, this.settings, options); + }; + + // Get input container + $.ezdz.prototype.container = function() { + var settings = this.settings, + $input = this.$input; + + return $input.parent('.' + settings.classes.main); + }; + + // Default options + $.ezdz.prototype.defaults = defaults; + + // jQuery plugin + $.fn.ezdz = function(options) { + var args = arguments, + plugin = $(this).data('ezdz'); + + if (!plugin) { + return $(this).data('ezdz', new $.ezdz(this, options)); + } if (plugin[options]) { + return plugin[options].apply(plugin, Array.prototype.slice.call(args, 1)); + } else { + $.error('Ezdz error - Method ' + options + ' does not exist.'); + } + }; +}));