Skip to content

Commit

Permalink
merging
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulKinlan committed Jan 10, 2019
2 parents 1b679ae + 89baa52 commit 558f6a1
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 73 deletions.
38 changes: 7 additions & 31 deletions lib/filedrop.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,3 @@
/**
* A decorator that binds values to their class instance.
* @example
* class C {
* @bind
* foo () {
* return this;
* }
* }
* let f = new C().foo;
* f() instanceof C; // true
*/
function bind(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
return {
// the first time the prototype property is accessed for an instance,
// define an instance property pointing to the bound function.
// This effectively "caches" the bound prototype method as an instance property.
get() {
const bound = descriptor.value.bind(this);
Object.defineProperty(this, propertyKey, {
value: bound,
});
return bound;
},
};
}

// tslint:disable-next-line:max-line-length
function firstMatchingItem(list: DataTransferItemList, acceptVal: string): DataTransferItem | undefined {
// Return the first item (or undefined) if our filter is for all files
Expand Down Expand Up @@ -110,6 +83,13 @@ export class FileDropElement extends HTMLElement {

constructor() {
super();

// Bind
this._onDragEnter = this._onDragEnter.bind(this);
this._onDragLeave = this._onDragLeave.bind(this);
this._onDrop = this._onDrop.bind(this);
this._onPaste = this._onPaste.bind(this);

this.addEventListener('dragover', event => event.preventDefault());
this.addEventListener('drop', this._onDrop);
this.addEventListener('dragenter', this._onDragEnter);
Expand All @@ -126,7 +106,6 @@ export class FileDropElement extends HTMLElement {
this.setAttribute('accept', val);
}

@bind
private _onDragEnter(event: DragEvent) {
this._dragEnterCount += 1;
if (this._dragEnterCount > 1) return;
Expand All @@ -145,15 +124,13 @@ export class FileDropElement extends HTMLElement {
}
}

@bind
private _onDragLeave() {
this._dragEnterCount -= 1;
if (this._dragEnterCount === 0) {
this._reset();
}
}

@bind
private _onDrop(event: DragEvent) {
event.preventDefault();
this._reset();
Expand All @@ -164,7 +141,6 @@ export class FileDropElement extends HTMLElement {
this.dispatchEvent(new FileDropEvent('filedrop', { action, file }));
}

@bind
private _onPaste(event: ClipboardEvent) {
const action = 'paste';
const file = getFileData(event.clipboardData, this.accept);
Expand Down
Loading

0 comments on commit 558f6a1

Please sign in to comment.