Sha256: 76be771d88519f246acb791626a2d5a9f53c4c79d9e70fa485f8511354c7ff0b

Contents?: true

Size: 1.81 KB

Versions: 42

Compression:

Stored size: 1.81 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;
      }
    }).then(function() {
      that.set({progress: 100})
    }).fail(function() {
      that.set({serverError: true});
    });
  }
});

Version data entries

42 entries across 42 versions & 1 rubygems

Version Path
solidus_backend-4.4.2 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-4.4.1 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-4.4.0 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-4.3.4 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-4.2.4 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-4.1.5 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-4.3.3 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-4.3.2 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-4.1.4 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-4.3.1 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-4.3.0 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-4.2.3 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-4.1.3 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-4.0.4 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-3.4.6 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-4.0.3 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-4.1.2 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-4.2.2 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-3.4.5 app/assets/javascripts/spree/backend/models/image_upload.js
solidus_backend-4.2.1 app/assets/javascripts/spree/backend/models/image_upload.js