Advertisement
will005

Untitled

Jun 11th, 2014
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. App.ImageUploadComponent = Ember.Component.extend({
  2.   asset: null,
  3.   layoutName: 'components/image-upload',
  4.   myDropzone: null,
  5.  
  6.   widthRequirement: function() {
  7.     if (this.get('imageWidth')) {
  8.       return this.get('imageWidth');
  9.     } else {
  10.       return null;
  11.     }
  12.   }.property('imageWidth'),
  13.  
  14.   heightRequirement: function() {
  15.     if (this.get('imageHeight')) {
  16.       return this.get('imageHeight');
  17.     } else {
  18.       return null;
  19.     }
  20.   }.property('imageHeight'),
  21.  
  22.   dimensionsRequirements: function() {
  23.     if (this.get('imageWidth') && this.get('imageHeight')) {
  24.       return "Required Dimensions (" + this.get('imageWidth') + "x" + this.get('imageHeight') + "px)";
  25.     } else if (this.get('imageWidth')) {
  26.       return "Required Width (" + this.get('imageWidth') + "px)";
  27.     } else if (this.get('imageHeight')) {
  28.       return "Required Height (" + this.get('imageHeight') + "px)";
  29.     } else {
  30.       return null;
  31.     }
  32.   }.property('imageWidth', 'imageHeight'),
  33.  
  34.   updateAssetModel: function(response) {
  35.     if (response && response.url) {
  36.       this.set('asset.url', response.url);
  37.       this.set('asset.fileName', response.fileName);
  38.       this.set('asset.s3Bucket', response.s3Bucket);
  39.       this.set('asset.s3FileKey', response.s3FileKey);
  40.     }
  41.   },
  42.  
  43.   blankAssetModel: function() {
  44.     this.set('asset', App.Asset.load(App.Asset.getBlankGraphicAsset()));
  45.   },
  46.  
  47.   initDropzone: function() {
  48.     var emberComponent = this;
  49.  
  50.     var dropZonesSelector = ".my-graphic-dropzone";
  51.     var myDropzones = this.$(dropZonesSelector);
  52.  
  53.     var imagePreviewTemplate = this.$(".my-image-dropzone-template")[0].innerHTML;
  54.  
  55.     var imageWidth = emberComponent.get('widthRequirement');
  56.     var imageHeight = emberComponent.get('heightRequirement');
  57.  
  58.     myDropzones.each(function() {
  59.       var dz = $(this);
  60.  
  61.       if (!dz.attr('data-dropzone')) {
  62.         dz.dropzone({
  63.           url: App.SETTINGS.apiHost + "/assets/uploadgraphic",
  64.           headers: {'Auth-Token': App.SETTINGS.getAuthToken()},
  65.           maxFiles: 1,
  66.           acceptedFiles: '.png,.jpg,.jpeg',
  67.           dictInvalidFileType: 'Only .png and .jpg files are accepted.',
  68.           imageWidth: imageWidth,
  69.           imageHeight: imageHeight,
  70.           addRemoveLinks: true,
  71.           previewTemplate: imagePreviewTemplate,
  72.           dictDefaultMessage: "Upload Image",
  73.           dictCancelUpload: "Remove Image",
  74.           emberComponent: emberComponent,
  75.           complete: function(file, done) {
  76.             // update our asset in Ember
  77.             emberComponent.updateAssetModel(done);
  78.  
  79.             // the normal dropzone code - DO NOT EDIT
  80.             if (file._removeLink) {
  81.               return file._removeLink.textContent = this.options.dictRemoveFile;
  82.             }
  83.           },
  84.           removedfile: function(file) {
  85.             // remove from Ember
  86.             emberComponent.blankAssetModel();
  87.  
  88.             // the normal dropzone code - DO NOT EDIT
  89.             var _ref;
  90.             return (_ref = file.previewElement) != null ? _ref.parentNode.removeChild(file.previewElement) : void 0;
  91.           }
  92.         });
  93.  
  94.         dz.attr('data-dropzone', '1');
  95.         emberComponent.set('myDropzone', dz);
  96.       }
  97.     });
  98.   },
  99.  
  100.   didInsertElement: function() {
  101.     console.log('ImageUploadComponent.didInsertElement');
  102.  
  103.     Ember.run.scheduleOnce('afterRender', this, function() {
  104.       // perform jQuery function here;
  105.       console.log('ImageUploadComponent.didInsertElement.afterRender');
  106.  
  107.       this.initDropzone();
  108.     });
  109.   }
  110. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement