Created
May 27, 2014 21:48
-
-
Save kylecoberly/34dc2c39bc0ce801df3c to your computer and use it in GitHub Desktop.
Ember.js Image Upload Component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App.ImageUploadComponent = Ember.Component.extend({ | |
layoutName: "components/image-upload", | |
label: "", | |
progress: "", | |
classNames: ["fieldLabel"], | |
file: function(key, value, previousValue){ | |
//Setter | |
if (arguments.length > 1){ | |
return value; | |
} | |
}.property(), | |
imageData: function(key, value, previousValue){ | |
if (arguments.length > 1){ | |
return value; | |
} | |
if (this.get("file")){ | |
var that = this; | |
var Reader = new FileReader(); | |
Reader.onload = function(event){ | |
that.set("imageData", Reader.result); | |
}; | |
Reader.readAsDataURL(this.get("file")); | |
} | |
}.property("file"), | |
didInsertElement: function(){ | |
var that = this; | |
this.$(".file-input").change(function(){ | |
var file = that.$(".file-input").prop("files")[0]; | |
that.set("file", file); | |
}); | |
}, | |
dragEnter: function(event) { | |
event.preventDefault(); | |
}, | |
dragOver: function(event) { | |
event.preventDefault(); | |
event.dataTransfer.dropEffect = "copy"; | |
}, | |
drop: function(event) { | |
event.preventDefault(); | |
var file = event.dataTransfer.files[0]; | |
if (!file.type.match('image.*')) { | |
return false; | |
} | |
this.set("file", file); | |
}, | |
actions: { | |
remove: function(component){ | |
this.set("file", ""); | |
this.set("imageData", ""); | |
this.$(".file-input").val(""); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment