Handle classes better, support drop effect

This commit is contained in:
Timothy Farrell 2017-11-27 23:00:37 -06:00
parent abcd5c58c5
commit 574a2ed38b
2 changed files with 23 additions and 9 deletions

View File

@ -16,17 +16,32 @@ const CSS_DROPZONE_ACTIVE = {
}; };
export function Dropzone(vm, params) { export function Dropzone(vm, params) {
const { ondrop, ondragenter, ondragleave, className, activeClassName, content } = params; const {
ondrop,
ondragenter,
ondragleave,
className,
hoverClassName,
content,
hoverContent,
dropEffect
} = params;
const baseClassName = className || injectStyle(CSS_DROPZONE); const baseClassName = className || injectStyle(CSS_DROPZONE);
const hoverClassName = `${baseClassName} ${activeClassName || injectStyle(CSS_DROPZONE_ACTIVE)}`; const activeClassName = `${baseClassName} ${hoverClassName || injectStyle(CSS_DROPZONE_ACTIVE)}`;
const enterCounter = prop(0); const enterCounter = prop(0);
const class_ = computed(c => (c === 0 ? baseClassName : hoverClassName), [enterCounter]); const active = computed(c => c > 0, [enterCounter]);
const _class = computed(a => (a && hoverClassName ? activeClassName : baseClassName), [active]);
const _content = computed(a => (a && hoverContent ? hoverContent : content), [active]);
function onDragOver(evt) { function onDragOver(evt) {
// allows the browser to accept drops. // allows the browser to accept drops.
evt.preventDefault(); evt.preventDefault();
if (dropEffect) {
evt.dataTransfer.dropEffect = dropEffect;
}
} }
function onDragEnter() { function onDragEnter() {
@ -50,7 +65,7 @@ export function Dropzone(vm, params) {
enterCounter(0); enterCounter(0);
if (ondrop) { if (ondrop) {
ondrop(evt.dataTransfer.files); ondrop(evt, evt.dataTransfer.files);
} }
} }
@ -58,13 +73,14 @@ export function Dropzone(vm, params) {
return el( return el(
'div', 'div',
{ {
class: class_, class: _class,
'data-hover': active, // only here to subscribe to the change
ondragenter: onDragEnter, ondragenter: onDragEnter,
ondragover: onDragOver, ondragover: onDragOver,
ondragleave: onDragLeave, ondragleave: onDragLeave,
ondrop: onDrop ondrop: onDrop
}, },
content() _content()()
); );
}; };
} }

View File

@ -100,10 +100,8 @@ export function GalleryView(vm) {
{ {
className: slate, className: slate,
activeClassName: 'dropHover', activeClassName: 'dropHover',
dropEffect: 'copy',
ondrop: uploadImages, ondrop: uploadImages,
type: 'file',
multiple: true, // FIXME - these don't carry through to the input tag
accept: 'image/jpeg',
content: renderDropzone content: renderDropzone
}, },
'dz' 'dz'