Skip to content

Commit

Permalink
Added SingleInput and allowed limit to be specified. Relates to #42
Browse files Browse the repository at this point in the history
  • Loading branch information
Wildhoney committed Sep 17, 2014
1 parent 33adced commit 9fcfb26
Show file tree
Hide file tree
Showing 11 changed files with 639 additions and 336 deletions.
2 changes: 1 addition & 1 deletion bower.json
@@ -1,6 +1,6 @@
{
"name": "ember-droplet",
"version": "0.5.9",
"version": "0.6.0",
"homepage": "https://github.com/Wildhoney/EmberDroplet",
"authors": [
"Adam Timberlake <adam.timberlake@gmail.com>"
Expand Down
145 changes: 88 additions & 57 deletions dist/ember-droplet.js
Expand Up @@ -417,6 +417,66 @@

"use strict";

/**
* @property MultipleInput
* @type {Object}
*/
var MultipleInput = {

/**
* @property tagName
* @type {String}
* @default "input"
*/
tagName: 'input',

/**
* @property classNames
* @type {String}
* @default "files"
*/
classNames: 'files',

/**
* @property attributeBindings
* @type {Array}
*/
attributeBindings: ['name', 'type', 'multiple'],

/**
* @property file
* @type {String}
* @default "file"
*/
type: 'file',

/**
* @property multiple
* @type {String}
* @default "multiple"
*/
multiple: 'multiple',

/**
* Invoked when the content of the INPUT changes.
*
* @method change
* @return {Boolean}
*/
change: function() {
var files = this.get('element').files;
return this.get('parentView').traverseFiles(files);
}

};

/**
* @property SingleInput
* @type {Object}
*/
var SingleInput = $ember.copy(MultipleInput);
SingleInput.multiple = false;

/**
* @module App
* @class DropletView
Expand Down Expand Up @@ -467,8 +527,9 @@
image: null,

/**
* @method didInsertElement
* Invoked when the view is inserted into the DOM.
*
* @method didInsertElement
* @return {void}
*/
didInsertElement: function() {
Expand Down Expand Up @@ -512,59 +573,20 @@
* @property MultipleInput
* @type {Ember.View}
*/
MultipleInput: $ember.View.extend({
MultipleInput: $ember.View.extend(MultipleInput),

/**
* @property tagName
* @type {String}
* @default "input"
*/
tagName: 'input',

/**
* @property classNames
* @type {String}
* @default "files"
*/
classNames: 'files',

/**
* @property attributeBindings
* @type {Array}
*/
attributeBindings: ['name', 'type', 'multiple'],

/**
* @property file
* @type {String}
* @default "file"
*/
type: 'file',

/**
* @property multiple
* @type {String}
* @default "multiple"
*/
multiple: 'multiple',

/**
* @method change
* Invoked when the content of the INPUT changes.
* @return {Boolean}
*/
change: function() {
var files = this.get('element').files;
return this.get('parentView').traverseFiles(files);
}

}),
/**
* @property SingleInput
* @type {Ember.View}
*/
SingleInput: $ember.View.extend(SingleInput),

/**
* Invoked when the user drops a file onto the droppable area.
*
* @method drop
* @param event {jQuery.Event}
* @param [files = []] {Array}
* Invoked when the user drops a file onto the droppable area.
* @return {Boolean}
*/
drop: function(event, files) {
Expand All @@ -573,37 +595,45 @@
},

/**
* @method traverseFiles
* @param files {FileList}
* Accepts a FileList object, and traverses them to determine if they're valid, adding them
* as either valid or invalid.
*
* @method traverseFiles
* @param files {FileList}
* @return {boolean}
*/
traverseFiles: function(files) {

// Find the controller, and the `mimeTypes` and `extensions` property.
var controller = $ember.get(this, 'controller'),
mimeTypes = $ember.get(controller, 'mimeTypes'),
extensions = $ember.get(controller, 'extensions');
extensions = $ember.get(controller, 'extensions'),
options = $ember.get(controller, 'dropletOptions') || { limit: Infinity };

// Assert that we have the `mineTypes` property, and that it's an array.
// Assert that we have the `mimeTypes` property, and that it's an array.
$ember.assert('`mimeTypes` is undefined. Does your controller implement the `$emberDropletController` mixin?', !!mimeTypes);
$ember.assert('`mimeTypes` is not an array. It should be an array of valid MIME types.', !!$ember.isArray(mimeTypes));

for (var index = 0, numFiles = files.length; index < numFiles; index++) {

if (!files.hasOwnProperty(index)&&(!(index in files))) {
if (!files.hasOwnProperty(index) && (!(index in files))) {
continue;
}

var file = files[index],
var file = files[index],
fileExt = file.name.split('.').pop();

// Determine if the file is valid based on its MIME type or extension.
if (($.inArray(file.type, mimeTypes) === -1)&&($.inArray(fileExt, extensions) === -1)) {
// Determine if the file is valid based on its MIME type or extension, and we haven't exceeded
// the user defined limit for the amount of files to upload in one go.
var invalidMime = ($.inArray(file.type, mimeTypes) === -1) && ($.inArray(fileExt, extensions) === -1),
currentLength = $ember.get(controller, 'validFiles').length;

if (invalidMime || currentLength === options.limit) {

// If it isn't valid, then we'll add it as an invalid file.
controller.send('addInvalidFile', file);
continue;

}

// Otherwise the file has a valid MIME type or extension, and therefore be added as a good file.
Expand All @@ -616,9 +646,10 @@
},

/**
* Prevents default behaviour and propagation on nodes where it's undesirable.
*
* @method _preventDefaultBehaviour
* @param event {jQuery.Event}
* Prevents default behaviour and propagation on nodes where it's undesirable.
* @return {void}
* @private
*/
Expand Down
2 changes: 1 addition & 1 deletion dist/ember-droplet.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9fcfb26

Please sign in to comment.