Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add url / text / html support

  • Loading branch information...
commit e63c7c64f6efee5d0b97d05af2cebaaf8ed7e0a4 1 parent 90d3671
@tj tj authored
View
5 History.md
@@ -1,4 +1,9 @@
+n.n.n / 2013-02-27
+==================
+
+
+
0.0.4 / 2013-01-09
==================
View
15 Readme.md
@@ -9,6 +9,9 @@
## Events
- `upload` (upload) a file was dropped
+ - `text` (string) string representation
+ - `url` (string) url representation
+ - `html` (string) html representation
## Example
@@ -24,6 +27,18 @@ drop.on('upload', function(upload){
console.log('uploading %s', upload.file.name);
upload.to('/upload');
});
+
+drop.on('text', function(str){
+ console.log('text "%s"', str);
+});
+
+drop.on('url', function(str){
+ console.log('url "%s"', str);
+});
+
+drop.on('html', function(str){
+ console.log('html "%s"', str);
+});
```
## Running example
View
2  component.json
@@ -2,7 +2,7 @@
"name": "dropload",
"repo": "component/dropload",
"description": "drag and drop uploads",
- "version": "0.0.5",
+ "version": "0.1.0",
"keywords": [
"drag",
"drop",
View
41 index.js
@@ -15,6 +15,16 @@ var Emitter = require('emitter')
module.exports = Dropload;
/**
+ * Types.
+ */
+
+var typeMap = {
+ 'text/plain': 'text',
+ 'text/uri-list': 'url',
+ 'text/html': 'html'
+};
+
+/**
* Initialize a drop point
* on the given `el`.
*
@@ -87,10 +97,39 @@ Dropload.prototype.ondrop = function(e){
e.stopPropagation();
e.preventDefault();
this.classes.remove('over');
+ this.drop(e.dataTransfer.items);
this.upload(e.dataTransfer.files);
};
/**
+ * Handle the given `items`.
+ *
+ * @param {DataTransferItemList}
+ * @api private
+ */
+
+Dropload.prototype.drop = function(items){
+ for (var i = 0; i < items.length; i++) {
+ this.dropItem(items[i]);
+ }
+};
+
+/**
+ * Handle `item`.
+ *
+ * @param {Object} item
+ * @api private
+ */
+
+Dropload.prototype.dropItem = function(item){
+ var self = this;
+ var type = typeMap[item.type];
+ item.getAsString(function(str){
+ self.emit(type, str, item);
+ });
+};
+
+/**
* Upload the given `files`.
*
* Presents each `file` in the FileList
@@ -102,7 +141,7 @@ Dropload.prototype.ondrop = function(e){
*/
Dropload.prototype.upload = function(files){
- for (var i = 0, len = files.length; i < len; ++i) {
+ for (var i = 0; i < files.length; i++) {
this.emit('upload', new Upload(files[i]));
}
};
View
2  package.json
@@ -1,7 +1,7 @@
{
"name": "dropload-component",
"description": "drag and drop file uploading",
- "version": "0.0.5",
+ "version": "0.1.0",
"keywords": ["upload", "file"],
"dependencies": {
"emitter-component": "*",
View
12 test/tests.html
@@ -31,6 +31,18 @@
console.log('uploading %s', upload.file.name);
upload.to('/upload');
});
+
+ drop.on('text', function(str){
+ console.log('text "%s"', str);
+ });
+
+ drop.on('url', function(str){
+ console.log('url "%s"', str);
+ });
+
+ drop.on('html', function(str){
+ console.log('html "%s"', str);
+ });
</script>
</body>
</html>
Please sign in to comment.
Something went wrong with that request. Please try again.