JSFiddle - React, Tailwind, and code Playground
by Emanuel Vecchio
HTML
<div ng-app="brianite" ng-controller="BodyCtrl as body">
<form>
<file-drop message="Drop files here or click to upload"></file-drop>
</form>
</div>
CSS
.dropzone {
position: relative;
border: 2px dashed #0087F7;
border-radius: 5px;
background: white;
height: 150px;
text-align: center;
line-height: 140px;
}
.drag {
background: red;
}
.filepicker {
position: absolute;
top: 0;
left: 0;
opacity: 0;
width: 100%;
height: 100%;
}
JavaScript
var app = angular.module('brianite', []);
function BodyCtrl($scope, $timeout) {
this.model = {};
this.model.app_name = "Brianite";
}
BodyCtrl.$inject = ['$scope', '$timeout'];
app.controller('BodyCtrl', BodyCtrl);
function FileDropDirective() {
return {
restrict: 'E',
scope: {
message: '@'
},
template: '<div class="dropzone" ng-class="{ drag: dragging }">' +
'<input type="file" class="filepicker"/>' +
'<span class="message">{{message}}</span>' +
'</div>',
link: linkFunction
};
function linkFunction(scope, element, attrs) {
scope.dragging = false;
function processDragLeave(event) {
scope.$apply(function () {
scope.dragging = false;
});
}
function processDragOverOrEnter(event) {
scope.$apply(function () {
scope.dragging = true;
});
if (event != null) {
event.preventDefault();
}
event.dataTransfer.effectAllowed = 'copy';
return false;
};
element.bind('dragover', processDragOverOrEnter);
element.bind('dragenter', processDragOverOrEnter);
element.bind('dragleave', processDragLeave);
element.bind('drop', function (event) {
processDragLeave(event);
var file, name, reader, size, type;
if (event != null) {
event.preventDefault();
}
reader = new FileReader();
file = event.dataTransfer.files[0];
name = file.name;
type = file.type;
size = file.size;
reader.readAsDataURL(file);
return false;
});
}
}
FileDropDirective.$inject = [];
app.directive('fileDrop', FileDropDirective);