JeremyFagis / dropify

Override your input files with style — Demo here : http://jeremyfagis.github.io/dropify
MIT License
972 stars 398 forks source link

Slightly altered the function: getError() and sizeToByte() #38

Closed zhivulinal closed 7 years ago

zhivulinal commented 8 years ago

var pluginName = "dropify";

/**

/**

/**

/**

/**

/**

/**

/**

/**

  • Reset the preview */ Dropify.prototype.resetPreview = function() { this.wrapper.removeClass('has-preview'); var render = this.preview.children('.dropify-render'); render.find('.dropify-extension').remove(); render.find('i').remove(); render.find('img').remove(); this.preview.hide(); this.hideLoader(); };

/**

  • Clean the src and get the filename *
  • @param {String} src *
  • @return {String} filename */ Dropify.prototype.cleanFilename = function(src) { var filename = src.split('\').pop(); if (filename == src) { filename = src.split('/').pop(); }

    return src !== "" ? filename : ''; };

/**

  • Clear the element, events are available */ Dropify.prototype.clearElement = function() { if (this.errorsEvent.errors.length === 0) { var eventBefore = $.Event("dropify.beforeClear"); this.input.trigger(eventBefore, [this]);

    if (eventBefore.result !== false) {
      this.resetFile();
      this.input.val('');
      this.resetPreview();
    
      this.input.trigger($.Event("dropify.afterClear"), [this]);
    }

    } else { this.resetFile(); this.input.val(''); this.resetPreview(); } };

/**

  • Reset file informations */ Dropify.prototype.resetFile = function() { this.file.object = null; this.file.name = null; this.file.size = null; this.file.type = null; this.file.width = null; this.file.height = null; };

/**

  • Set the container height */ Dropify.prototype.setContainerSize = function() { if (this.settings.height) { this.wrapper.height(this.settings.height); } };

/**

  • Test if it's touch screen *
  • @return {Boolean} */ Dropify.prototype.isTouchDevice = function() { return (('ontouchstart' in window) || (navigator.MaxTouchPoints > 0) || (navigator.msMaxTouchPoints > 0)); };

/**

  • Get the file type. *
  • @return {String} */ Dropify.prototype.getFileType = function() { return this.file.name.split('.').pop().toLowerCase(); };

/**

  • Test if the file is an image *
  • @return {Boolean} */ Dropify.prototype.isImage = function() { if (this.settings.imgFileExtensions.indexOf(this.getFileType()) != "-1") { return true; }

    return false; };

/**

  • Test if the file extension is allowed *
  • @return {Boolean} */ Dropify.prototype.isFileExtensionAllowed = function () {

    if (this.settings.allowedFileExtensions.indexOf('*') != "-1" ||  this.settings.allowedFileExtensions.indexOf(this.getFileType()) != "-1") { return true; } this.pushError("fileExtension");

    return false; };

/**

  • Translate messages if needed. */ Dropify.prototype.translateMessages = function() { for (var name in this.settings.tpl) { for (var key in this.settings.messages) { this.settings.tpl[name] = this.settings.tpl[name].replace('{{ ' + key + ' }}', this.settings.messages[key]); } } };

/**

  • Check the limit filesize. */ Dropify.prototype.checkFileSize = function() { if (this.sizeToByte(this.settings.maxFileSize) !== 0 && this.file.size > this.sizeToByte(this.settings.maxFileSize)) { this.pushError("fileSize"); } };

/**

  • Convert filesize to byte. *
  • @return {Int} value */ Dropify.prototype.sizeToByte = function(size) { var value = 0;

    if (size !== 0) { var unit = size.slice(-1).toUpperCase();

    value = 1024;
    
    switch(unit) {
      case 'T':
          value *= value;
      case 'G':
          value *= value;
      case 'M':
          value *= value;
      case 'K':
          value;
    }

    }

    return value; };

/**

  • Validate image dimensions and format */ Dropify.prototype.validateImage = function() { if (this.settings.minWidth !== 0 && this.settings.minWidth >= this.file.width) { this.pushError("minWidth"); }

    if (this.settings.maxWidth !== 0 && this.settings.maxWidth <= this.file.width) { this.pushError("maxWidth"); }

    if (this.settings.minHeight !== 0 && this.settings.minHeight >= this.file.height) { this.pushError("minHeight"); }

    if (this.settings.maxHeight !== 0 && this.settings.maxHeight <= this.file.height) { this.pushError("maxHeight"); }

    if (this.settings.allowedFormats.indexOf(this.getImageFormat()) == "-1") { this.pushError("imageFormat"); } };

/**

  • Get image format. *
  • @return {String} */ Dropify.prototype.getImageFormat = function() { if (this.file.width == this.file.height) { return "square"; }

    if (this.file.width < this.file.height) { return "portrait"; }

    if (this.file.width > this.file.height) { return "landscape"; } };

/**

  • Push error *
  • @param {String} errorKey */ Dropify.prototype.pushError = function(errorKey) { var e = $.Event("dropify.error." + errorKey); this.errorsEvent.errors.push(e); this.input.trigger(e, [this]); };

/**

  • Clear errors */ Dropify.prototype.clearErrors = function() { if (typeof this.errorsContainer !== "undefined") { this.errorsContainer.children('ul').html(''); } };

/**

  • Show error in DOM *
  • @param {String} errorKey */ Dropify.prototype.showError = function(errorKey) { if (typeof this.errorsContainer !== "undefined") { this.errorsContainer.children('ul').append('
  • ' + this.getError(errorKey) + '
  • '); } };

/**

  • Get error message *
  • @return {String} message */ Dropify.prototype.getError = function(errorKey) { var error = this.settings.error[errorKey], value = '';

    switch (errorKey) { case 'fileSize': value = this.settings.maxFileSize; break; case 'minWidth': value = this.settings.minWidth; break; case 'maxWidth': value = this.settings.maxWidth; break; case 'minHeight': value = this.settings.minHeight; break; case 'maxHeight': value = this.settings.maxHeight; break; case 'imageFormat': value = this.settings.allowedFormats.join(', '); break; case 'fileExtension': value = this.settings.allowedFileExtensions.join(', '); break; }

    if (value !== '') { return error.replace('{{ value }}', value); }

    return error; };

/**

  • Show the loader */ Dropify.prototype.showLoader = function() { if (typeof this.loader !== "undefined") { this.loader.show(); } };

/**

  • Hide the loader */ Dropify.prototype.hideLoader = function() { if (typeof this.loader !== "undefined") { this.loader.hide(); } };

/**

  • Destroy dropify */ Dropify.prototype.destroy = function() { this.input.siblings().remove(); this.input.unwrap(); this.isInit = false; };

/**

  • Init dropify */ Dropify.prototype.init = function() { this.createElements(); };

/**

  • Test if element is init */ Dropify.prototype.isDropified = function() { return this.isInit; };

$.fn[pluginName] = function(options) { this.each(function() { if (!$.data(this, pluginName)) { $.data(this, pluginName, new Dropify(this, options)); } });

return this;

};