Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Several enhancements #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "Louis Sivillo",
"name": "angular-file-dnd",
"description": "AngularJS directive leveraging HTML5 Drag and Drop and the FileReader API",
"version": "1.0.4",
"version": "1.1.0",
"main": "dist/angular-file-dnd.min.js",
"dependencies": {
"angular": "~1.2.0"
Expand Down
57 changes: 43 additions & 14 deletions dist/angular-file-dnd.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@
return {
require: '^?form',
restrict: 'A',
scope: {
file: '=',
fileName: '=',
dropzoneHoverClass: '@'
},
scope: false,
link: function(scope, element, attrs, form) {
var checkSize, getDataTransfer, isTypeValid, processDragOverOrEnter, validMimeTypes;
var checkSize, getDataTransfer, isTypeValid, processDragOverOrEnter, setScopeVar, validMimeTypes;
getDataTransfer = function(event) {
var dataTransfer;
return dataTransfer = event.dataTransfer || event.originalEvent.dataTransfer;
};
processDragOverOrEnter = function(event) {
if (!scope.$eval(attrs.dropzoneEnabled)) {
return true;
}
if (event) {
element.addClass(scope.dropzoneHoverClass);
element.addClass(attrs.dropzoneHoverClass);
if (event.preventDefault) {
event.preventDefault();
}
Expand All @@ -28,6 +27,7 @@
getDataTransfer(event).effectAllowed = 'copy';
return false;
};
attrs.dropzoneHoverClass = attrs.dropzoneHoverClass || 'file-droping';
validMimeTypes = attrs.fileDropzone;
checkSize = function(size) {
var _ref;
Expand All @@ -46,31 +46,45 @@
return false;
}
};
setScopeVar = function(name, value) {
var objName, tmp, varName;
if (name.indexOf('.') === -1) {
return scope[name] = value;
} else {
tmp = name.split('.');
objName = tmp.slice(0, -1).join('.');
varName = tmp[tmp.length - 1];
return scope.$eval(objName)[varName] = value;
}
};
element.bind('dragover', processDragOverOrEnter);
element.bind('dragenter', processDragOverOrEnter);
element.bind('dragleave', function() {
return element.removeClass(scope.dropzoneHoverClass);
return element.removeClass(attrs.dropzoneHoverClass);
});
return element.bind('drop', function(event) {
var file, name, reader, size, type;
if (!scope.$eval(attrs.dropzoneEnabled)) {
return true;
}
if (event != null) {
event.preventDefault();
}
element.removeClass(scope.dropzoneHoverClass);
element.removeClass(attrs.dropzoneHoverClass);
reader = new FileReader();
reader.onload = function(evt) {
if (checkSize(size) && isTypeValid(type)) {
scope.$apply(function() {
scope.file = evt.target.result;
if (angular.isString(scope.fileName)) {
return scope.fileName = name;
setScopeVar(attrs.file, evt.target.result);
if (angular.isString(attrs.fileName)) {
return setScopeVar(attrs.fileName, name);
}
});
if (form) {
form.$setDirty();
}
return scope.$emit('file-dropzone-drop-event', {
file: scope.file,
file: scope[attrs.file],
type: type,
name: name,
size: size
Expand All @@ -81,7 +95,22 @@
name = file.name;
type = file.type;
size = file.size;
reader.readAsDataURL(file);
if (!angular.isDefined(attrs.asFile)) {
reader.readAsDataURL(file);
} else {
scope.$apply(function() {
setScopeVar(attrs.file, file);
if (angular.isString(attrs.fileName)) {
return setScopeVar(attrs.fileName, name);
}
});
if (form) {
form.$setDirty();
}
scope.$emit('file-dropzone-drop-event', {
file: file
});
}
return false;
});
}
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-file-dnd.min.js

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-file-dnd",
"version": "1.0.4",
"version": "1.1.0",
"dependencies": {},
"devDependencies": {
"grunt": "~0.4.1",
Expand Down
45 changes: 33 additions & 12 deletions src/directive/file_dropzone.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ angular.module('omr.angularFileDnD', [])
.directive('fileDropzone', () ->
require: '^?form'
restrict: 'A'
scope: {
file: '='
fileName: '='
dropzoneHoverClass: '@'
}
scope: false,
link: (scope, element, attrs, form) ->

getDataTransfer = (event) ->
dataTransfer = event.dataTransfer || event.originalEvent.dataTransfer

# function to prevent default behavior (browser loading image)
processDragOverOrEnter = (event) ->
if !scope.$eval(attrs.dropzoneEnabled)
return true

if event
element.addClass scope.dropzoneHoverClass
element.addClass attrs.dropzoneHoverClass
event.preventDefault() if event.preventDefault
return false if event.stopPropagation
getDataTransfer(event).effectAllowed = 'copy'
false

attrs.dropzoneHoverClass = attrs.dropzoneHoverClass || 'file-droping';
validMimeTypes = attrs.fileDropzone

# if the max file size is provided and the size of dropped file is greater than it,
Expand All @@ -42,33 +42,54 @@ angular.module('omr.angularFileDnD', [])
alert "Invalid file type. File must be one of following types #{validMimeTypes}"
false

setScopeVar = (name, value) ->
if name.indexOf('.') == -1
scope[name] = value
else
tmp = name.split('.')
objName = tmp.slice(0, -1).join('.')
varName = tmp[tmp.length - 1]
scope.$eval(objName)[varName] = value

# for dragover and dragenter (IE) we stop the browser from handling the
# event and specify copy as the allowable effect
element.bind 'dragover', processDragOverOrEnter
element.bind 'dragenter', processDragOverOrEnter
element.bind 'dragleave', ->
element.removeClass scope.dropzoneHoverClass
element.removeClass attrs.dropzoneHoverClass

# on drop events we stop browser and read the dropped file via the FileReader
# the resulting droped file is bound to the image property of the scope of this directive
element.bind 'drop', (event) ->
if !scope.$eval(attrs.dropzoneEnabled)
return true

event?.preventDefault()
element.removeClass scope.dropzoneHoverClass
element.removeClass attrs.dropzoneHoverClass
reader = new FileReader()
reader.onload = (evt) ->

if checkSize(size) and isTypeValid(type)
scope.$apply ->
scope.file = evt.target.result
scope.fileName = name if angular.isString scope.fileName
setScopeVar(attrs.file, evt.target.result)
setScopeVar(attrs.fileName, name) if angular.isString attrs.fileName
if form
form.$setDirty() #notify the form of this change
scope.$emit 'file-dropzone-drop-event', {file: scope.file, type: type, name: name, size: size}
scope.$emit 'file-dropzone-drop-event', {file: scope[attrs.file], type: type, name: name, size: size}

file = getDataTransfer(event).files[0]
name = file.name
type = file.type
size = file.size
reader.readAsDataURL file
if !angular.isDefined(attrs.asFile)
reader.readAsDataURL file
else
scope.$apply ->
setScopeVar(attrs.file, file)
setScopeVar(attrs.fileName, name) if angular.isString attrs.fileName
if form
form.$setDirty() #notify the form of this change
scope.$emit 'file-dropzone-drop-event', {file: file}

false
)