UNPKG

ng2-dnd

Version:

Angular 2 Drag-and-Drop without dependencies

176 lines 7.71 kB
// Copyright (C) 2016 Sergey Akopkokhyants // This project is licensed under the terms of the MIT license. // https://github.com/akserg/ng2-dnd var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var core_1 = require('angular2/core'); var core_2 = require('angular2/core'); var dnd_config_1 = require('./dnd.config'); var dnd_service_1 = require('./dnd.service'); var AbstractComponent = (function () { function AbstractComponent(elemRef, _dragDropService, _config) { var _this = this; this._dragDropService = _dragDropService; this._config = _config; /** * Whether the object is draggable. Default is true. */ this._dragEnabled = false; this.dropEnabled = false; /** * Array of Strings. It permits to specify the drop zones associated with this component. * By default, if the drop-zones attribute is not specified, the droppable component accepts * drop operations by all the draggable components that do not specify the allowed-drop-zones */ this.dropZones = []; this._elem = elemRef.nativeElement; this.dragEnabled = true; //drop events this._elem.ondragenter = function (event) { _this._onDragEnter(event); }; this._elem.ondragover = function (event) { _this._onDragOver(event); // if (event.dataTransfer != null) { event.dataTransfer.dropEffect = _this._config.dropEffect.name; } }; this._elem.ondragleave = function (event) { _this._onDragLeave(event); }; // this._elem.ontouchenter = (event: Event) => { // this._onDragEnter(event); // }; // this._elem.ontouchleave = (event: Event) => { // this._onDragLeave(event); // }; this._elem.ondrop = function (event) { _this._onDrop(event); }; //drag events this._elem.ondragstart = function (event) { // console.log('ondragstart', event.target); _this._onDragStart(event); // if (event.dataTransfer != null) { event.dataTransfer.effectAllowed = _this._config.dragEffect.name; event.dataTransfer.setData('text/html', ''); if (_this._config.dragImage != null) { var dragImage = _this._config.dragImage; event.dataTransfer.setDragImage(dragImage.imageElement, dragImage.x_offset, dragImage.y_offset); } } }; this._elem.ondragend = function (event) { // console.log('ondragend', event.target); _this._onDragEnd(event); }; this._elem.ontouchstart = function (event) { // console.log('ontouchstart', event.target); _this._onDragStart(event); }; this._elem.ontouchend = function (event) { // console.log('ontouchend', event.target); _this._onDragEnd(event); }; } Object.defineProperty(AbstractComponent.prototype, "dragEnabled", { get: function () { return this._dragEnabled; }, set: function (enabled) { this._dragEnabled = !!enabled; // this._elem.draggable = this._dragEnabled; if (this._config.dragCursor != null) { this._elem.style.cursor = this._dragEnabled ? this._config.dragCursor : this._defaultCursor; } }, enumerable: true, configurable: true }); //****** Droppable *******// AbstractComponent.prototype._onDragEnter = function (event) { // console.log('ondragenter._isDropAllowed', this._isDropAllowed); if (this._isDropAllowed) { event.preventDefault(); this._onDragEnterCallback(event); } }; AbstractComponent.prototype._onDragOver = function (event) { // // console.log('ondragover._isDropAllowed', this._isDropAllowed); if (this._isDropAllowed) { event.preventDefault(); this._onDragOverCallback(event); } }; AbstractComponent.prototype._onDragLeave = function (event) { // console.log('ondragleave._isDropAllowed', this._isDropAllowed); if (this._isDropAllowed) { event.preventDefault(); this._onDragLeaveCallback(event); } }; AbstractComponent.prototype._onDrop = function (event) { // console.log('ondrop._isDropAllowed', this._isDropAllowed); if (this._isDropAllowed) { event.preventDefault(); this._onDropCallback(event); } }; Object.defineProperty(AbstractComponent.prototype, "_isDropAllowed", { get: function () { if (this.dropEnabled) { if (this.dropZones.length === 0 && this._dragDropService.allowedDropZones.length === 0) { return true; } for (var i = 0; i < this._dragDropService.allowedDropZones.length; i++) { var dragZone = this._dragDropService.allowedDropZones[i]; if (this.dropZones.indexOf(dragZone) !== -1) { return true; } } } return false; }, enumerable: true, configurable: true }); //*********** Draggable **********// AbstractComponent.prototype._onDragStart = function (event) { // console.log('ondragstart.dragEnabled', this._dragEnabled); if (this._dragEnabled) { this._dragDropService.allowedDropZones = this.dropZones; // console.log('ondragstart.allowedDropZones', this._dragDropService.allowedDropZones); this._onDragStartCallback(event); } }; AbstractComponent.prototype._onDragEnd = function (event) { this._dragDropService.allowedDropZones = []; // console.log('ondragend.allowedDropZones', this._dragDropService.allowedDropZones); this._onDragEndCallback(event); }; //**** Drop Callbacks ****// AbstractComponent.prototype._onDragEnterCallback = function (event) { }; AbstractComponent.prototype._onDragOverCallback = function (event) { }; AbstractComponent.prototype._onDragLeaveCallback = function (event) { }; AbstractComponent.prototype._onDropCallback = function (event) { }; //**** Drag Callbacks ****// AbstractComponent.prototype._onDragStartCallback = function (event) { }; AbstractComponent.prototype._onDragEndCallback = function (event) { }; AbstractComponent = __decorate([ core_1.Injectable(), __metadata('design:paramtypes', [core_2.ElementRef, dnd_service_1.DragDropService, dnd_config_1.DragDropConfig]) ], AbstractComponent); return AbstractComponent; })(); exports.AbstractComponent = AbstractComponent; //# sourceMappingURL=dnd.component.js.map