Sha256: bb680877b47abf8df2ad2235592483acf39a2439efa72c092e8edc362ac67c50

Contents?: true

Size: 1.84 KB

Versions: 106

Compression:

Stored size: 1.84 KB

Contents

Spree.Models.ImageUpload = Backbone.Model.extend({
  initialize: function() {
    var file = this.get("file");
    this.set({
      filename: file.name,
      size: file.size ? (file.size/1024|0) + 'K' : ''
    });
  },

  defaults: function() {
    return {
      file: null,
      imgSrc: '',
      progress: 0,
      serverError: false,
      filename: '',
      size: ''
    }
  },

  acceptedTypes: {
    'image/png': true,
    'image/jpeg': true,
    'image/gif': true
  },


  previewFile: function () {
    var file = this.get('file'),
      that = this;

    if (FileReader && this.acceptedTypes[file.type] === true) {
      var reader = new FileReader();
      reader.onload = function (event) {
        that.set({imgSrc: event.target.result});
      };

      reader.readAsDataURL(file);
    }
  },

  uploadFile: function () {
    var formData = new FormData(),
      that = this;

    formData.append('image[attachment]', this.get('file'));
    formData.append('image[viewable_id]', this.get('variant_id'));
    formData.append('upload_id', this.cid);

    // send the image to the server
    Spree.ajax({
      url: window.location.pathname,
      type: "POST",
      dataType: 'script',
      data: formData,
      processData: false,  // tell jQuery not to process the data
      contentType: false,  // tell jQuery not to set contentType
      xhr: function () {
        var xhr = $.ajaxSettings.xhr();
        if (xhr.upload) {
          xhr.upload.onprogress = function (event) {
            if (event.lengthComputable) {
              var complete = (event.loaded / event.total * 100 | 0);
              that.set({progress: complete})
            }
          };
        }
        return xhr;
      }
    }).done(function() {
      that.set({progress: 100})
    }).error(function(jqXHR, textStatus, errorThrown) {
      that.set({serverError: true});
    });
  }
});

Version data entries

106 entries across 106 versions & 2 rubygems

Version Path
solidus_backend-3.1.9 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-3.1.8 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-3.0.8 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-3.2.2 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-3.2.1 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-3.2.0 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-3.2.0.alpha app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-3.0.7 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-2.11.17 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-3.1.7 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-2.11.16 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-3.0.6 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-3.1.6 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-2.11.15 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-2.11.14 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-3.0.5 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-3.1.5 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-2.11.13 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-3.0.4 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-3.1.4 app/assets/javascripts/spree/backend/models/image_upload.js