ng2-dnd
Version:
Angular 2 Drag-and-Drop without dependencies
51 lines (41 loc) • 1.81 kB
text/typescript
// Copyright (C) 2016 Sergey Akopkokhyants
// This project is licensed under the terms of the MIT license.
// https://github.com/akserg/ng2-dnd
import {Injectable} from 'angular2/core';
import {Directive, Input, Output, EventEmitter, ElementRef} from 'angular2/core';
import {AbstractComponent} from './dnd.component';
import {DragDropConfig} from './dnd.config';
import {DragDropService} from './dnd.service';
export class DraggableComponent extends AbstractComponent {
set draggable(value:boolean) {
this.dragEnabled = !!value;
}
/**
* The data that has to be dragged. It can be any JS object
*/
dragData: any;
/**
* Callback function called when the drag action ends with a valid drop action.
* It is activated after the on-drop-success callback
*/
onDragSuccessCallback: EventEmitter<any> = new EventEmitter<any>();
set dropzones(value:Array<string>) {
this.dropZones = value;
}
constructor(elemRef: ElementRef, _dragDropService: DragDropService, _config:DragDropConfig) {
super(elemRef, _dragDropService, _config);
this._defaultCursor = this._elem.style.cursor;
this.dragEnabled = true;
}
_onDragStartCallback(event: Event) {
this._dragDropService.dragData = this.dragData;
this._dragDropService.onDragSuccessCallback = this.onDragSuccessCallback;
this._elem.classList.add(this._config.onDragStartClass);
}
_onDragEndCallback(event: Event) {
this._dragDropService.dragData = null;
this._dragDropService.onDragSuccessCallback = null;
this._elem.classList.remove(this._config.onDragStartClass);
}
}