dragdrop.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. "use strict";
  2. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  3. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  4. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  5. 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;
  6. return c > 3 && r && Object.defineProperty(target, key, r), r;
  7. };
  8. var __metadata = (this && this.__metadata) || function (k, v) {
  9. if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. var core_1 = require("@angular/core");
  13. var common_1 = require("@angular/common");
  14. var domhandler_1 = require("../dom/domhandler");
  15. var Draggable = /** @class */ (function () {
  16. function Draggable(el, zone) {
  17. this.el = el;
  18. this.zone = zone;
  19. this.onDragStart = new core_1.EventEmitter();
  20. this.onDragEnd = new core_1.EventEmitter();
  21. this.onDrag = new core_1.EventEmitter();
  22. }
  23. Object.defineProperty(Draggable.prototype, "pDraggableDisabled", {
  24. get: function () {
  25. return this._pDraggableDisabled;
  26. },
  27. set: function (_pDraggableDisabled) {
  28. this._pDraggableDisabled = _pDraggableDisabled;
  29. if (this._pDraggableDisabled) {
  30. this.unbindMouseListeners();
  31. }
  32. else {
  33. this.el.nativeElement.draggable = true;
  34. this.bindMouseListeners();
  35. }
  36. },
  37. enumerable: true,
  38. configurable: true
  39. });
  40. Draggable.prototype.ngAfterViewInit = function () {
  41. if (!this.pDraggableDisabled) {
  42. this.el.nativeElement.draggable = true;
  43. this.bindMouseListeners();
  44. }
  45. };
  46. Draggable.prototype.bindDragListener = function () {
  47. var _this = this;
  48. if (!this.dragListener) {
  49. this.zone.runOutsideAngular(function () {
  50. _this.dragListener = _this.drag.bind(_this);
  51. _this.el.nativeElement.addEventListener('drag', _this.dragListener);
  52. });
  53. }
  54. };
  55. Draggable.prototype.unbindDragListener = function () {
  56. var _this = this;
  57. if (this.dragListener) {
  58. this.zone.runOutsideAngular(function () {
  59. _this.el.nativeElement.removeEventListener('drag', _this.dragListener);
  60. _this.dragListener = null;
  61. });
  62. }
  63. };
  64. Draggable.prototype.bindMouseListeners = function () {
  65. var _this = this;
  66. if (!this.mouseDownListener && !this.mouseUpListener) {
  67. this.zone.runOutsideAngular(function () {
  68. _this.mouseDownListener = _this.mousedown.bind(_this);
  69. _this.mouseUpListener = _this.mouseup.bind(_this);
  70. _this.el.nativeElement.addEventListener('mousedown', _this.mouseDownListener);
  71. _this.el.nativeElement.addEventListener('mouseup', _this.mouseUpListener);
  72. });
  73. }
  74. };
  75. Draggable.prototype.unbindMouseListeners = function () {
  76. var _this = this;
  77. if (this.mouseDownListener && this.mouseUpListener) {
  78. this.zone.runOutsideAngular(function () {
  79. _this.el.nativeElement.removeEventListener('mousedown', _this.mouseDownListener);
  80. _this.el.nativeElement.removeEventListener('mouseup', _this.mouseUpListener);
  81. _this.mouseDownListener = null;
  82. _this.mouseUpListener = null;
  83. });
  84. }
  85. };
  86. Draggable.prototype.drag = function (event) {
  87. this.onDrag.emit(event);
  88. };
  89. Draggable.prototype.dragStart = function (event) {
  90. if (this.allowDrag() && !this.pDraggableDisabled) {
  91. if (this.dragEffect) {
  92. event.dataTransfer.effectAllowed = this.dragEffect;
  93. }
  94. event.dataTransfer.setData('text', this.scope);
  95. this.onDragStart.emit(event);
  96. this.bindDragListener();
  97. }
  98. else {
  99. event.preventDefault();
  100. }
  101. };
  102. Draggable.prototype.dragEnd = function (event) {
  103. this.onDragEnd.emit(event);
  104. this.unbindDragListener();
  105. };
  106. Draggable.prototype.mousedown = function (event) {
  107. this.handle = event.target;
  108. };
  109. Draggable.prototype.mouseup = function (event) {
  110. this.handle = null;
  111. };
  112. Draggable.prototype.allowDrag = function () {
  113. if (this.dragHandle && this.handle)
  114. return domhandler_1.DomHandler.matches(this.handle, this.dragHandle);
  115. else
  116. return true;
  117. };
  118. Draggable.prototype.ngOnDestroy = function () {
  119. this.unbindDragListener();
  120. this.unbindMouseListeners();
  121. };
  122. __decorate([
  123. core_1.Input('pDraggable'),
  124. __metadata("design:type", String)
  125. ], Draggable.prototype, "scope", void 0);
  126. __decorate([
  127. core_1.Input(),
  128. __metadata("design:type", String)
  129. ], Draggable.prototype, "dragEffect", void 0);
  130. __decorate([
  131. core_1.Input(),
  132. __metadata("design:type", String)
  133. ], Draggable.prototype, "dragHandle", void 0);
  134. __decorate([
  135. core_1.Output(),
  136. __metadata("design:type", core_1.EventEmitter)
  137. ], Draggable.prototype, "onDragStart", void 0);
  138. __decorate([
  139. core_1.Output(),
  140. __metadata("design:type", core_1.EventEmitter)
  141. ], Draggable.prototype, "onDragEnd", void 0);
  142. __decorate([
  143. core_1.Output(),
  144. __metadata("design:type", core_1.EventEmitter)
  145. ], Draggable.prototype, "onDrag", void 0);
  146. __decorate([
  147. core_1.Input(),
  148. __metadata("design:type", Boolean),
  149. __metadata("design:paramtypes", [Boolean])
  150. ], Draggable.prototype, "pDraggableDisabled", null);
  151. __decorate([
  152. core_1.HostListener('dragstart', ['$event']),
  153. __metadata("design:type", Function),
  154. __metadata("design:paramtypes", [Object]),
  155. __metadata("design:returntype", void 0)
  156. ], Draggable.prototype, "dragStart", null);
  157. __decorate([
  158. core_1.HostListener('dragend', ['$event']),
  159. __metadata("design:type", Function),
  160. __metadata("design:paramtypes", [Object]),
  161. __metadata("design:returntype", void 0)
  162. ], Draggable.prototype, "dragEnd", null);
  163. Draggable = __decorate([
  164. core_1.Directive({
  165. selector: '[pDraggable]'
  166. }),
  167. __metadata("design:paramtypes", [core_1.ElementRef, core_1.NgZone])
  168. ], Draggable);
  169. return Draggable;
  170. }());
  171. exports.Draggable = Draggable;
  172. var Droppable = /** @class */ (function () {
  173. function Droppable(el, zone) {
  174. this.el = el;
  175. this.zone = zone;
  176. this.onDragEnter = new core_1.EventEmitter();
  177. this.onDragLeave = new core_1.EventEmitter();
  178. this.onDrop = new core_1.EventEmitter();
  179. }
  180. Droppable.prototype.ngAfterViewInit = function () {
  181. if (!this.pDroppableDisabled) {
  182. this.bindDragOverListener();
  183. }
  184. };
  185. Droppable.prototype.bindDragOverListener = function () {
  186. var _this = this;
  187. if (!this.dragOverListener) {
  188. this.zone.runOutsideAngular(function () {
  189. _this.dragOverListener = _this.dragOver.bind(_this);
  190. _this.el.nativeElement.addEventListener('dragover', _this.dragOverListener);
  191. });
  192. }
  193. };
  194. Droppable.prototype.unbindDragOverListener = function () {
  195. var _this = this;
  196. if (this.dragOverListener) {
  197. this.zone.runOutsideAngular(function () {
  198. _this.el.nativeElement.removeEventListener('dragover', _this.dragOverListener);
  199. _this.dragOverListener = null;
  200. });
  201. }
  202. };
  203. Droppable.prototype.dragOver = function (event) {
  204. event.preventDefault();
  205. };
  206. Droppable.prototype.drop = function (event) {
  207. if (this.allowDrop(event)) {
  208. event.preventDefault();
  209. this.onDrop.emit(event);
  210. }
  211. };
  212. Droppable.prototype.dragEnter = function (event) {
  213. event.preventDefault();
  214. if (this.dropEffect) {
  215. event.dataTransfer.dropEffect = this.dropEffect;
  216. }
  217. this.onDragEnter.emit(event);
  218. };
  219. Droppable.prototype.dragLeave = function (event) {
  220. event.preventDefault();
  221. this.onDragLeave.emit(event);
  222. };
  223. Droppable.prototype.allowDrop = function (event) {
  224. var dragScope = event.dataTransfer.getData('text');
  225. if (typeof (this.scope) == "string" && dragScope == this.scope) {
  226. return true;
  227. }
  228. else if (this.scope instanceof Array) {
  229. for (var j = 0; j < this.scope.length; j++) {
  230. if (dragScope == this.scope[j]) {
  231. return true;
  232. }
  233. }
  234. }
  235. return false;
  236. };
  237. Droppable.prototype.ngOnDestroy = function () {
  238. this.unbindDragOverListener();
  239. };
  240. __decorate([
  241. core_1.Input('pDroppable'),
  242. __metadata("design:type", Object)
  243. ], Droppable.prototype, "scope", void 0);
  244. __decorate([
  245. core_1.Input(),
  246. __metadata("design:type", Boolean)
  247. ], Droppable.prototype, "pDroppableDisabled", void 0);
  248. __decorate([
  249. core_1.Input(),
  250. __metadata("design:type", String)
  251. ], Droppable.prototype, "dropEffect", void 0);
  252. __decorate([
  253. core_1.Output(),
  254. __metadata("design:type", core_1.EventEmitter)
  255. ], Droppable.prototype, "onDragEnter", void 0);
  256. __decorate([
  257. core_1.Output(),
  258. __metadata("design:type", core_1.EventEmitter)
  259. ], Droppable.prototype, "onDragLeave", void 0);
  260. __decorate([
  261. core_1.Output(),
  262. __metadata("design:type", core_1.EventEmitter)
  263. ], Droppable.prototype, "onDrop", void 0);
  264. __decorate([
  265. core_1.HostListener('drop', ['$event']),
  266. __metadata("design:type", Function),
  267. __metadata("design:paramtypes", [Object]),
  268. __metadata("design:returntype", void 0)
  269. ], Droppable.prototype, "drop", null);
  270. __decorate([
  271. core_1.HostListener('dragenter', ['$event']),
  272. __metadata("design:type", Function),
  273. __metadata("design:paramtypes", [Object]),
  274. __metadata("design:returntype", void 0)
  275. ], Droppable.prototype, "dragEnter", null);
  276. __decorate([
  277. core_1.HostListener('dragleave', ['$event']),
  278. __metadata("design:type", Function),
  279. __metadata("design:paramtypes", [Object]),
  280. __metadata("design:returntype", void 0)
  281. ], Droppable.prototype, "dragLeave", null);
  282. Droppable = __decorate([
  283. core_1.Directive({
  284. selector: '[pDroppable]'
  285. }),
  286. __metadata("design:paramtypes", [core_1.ElementRef, core_1.NgZone])
  287. ], Droppable);
  288. return Droppable;
  289. }());
  290. exports.Droppable = Droppable;
  291. var DragDropModule = /** @class */ (function () {
  292. function DragDropModule() {
  293. }
  294. DragDropModule = __decorate([
  295. core_1.NgModule({
  296. imports: [common_1.CommonModule],
  297. exports: [Draggable, Droppable],
  298. declarations: [Draggable, Droppable]
  299. })
  300. ], DragDropModule);
  301. return DragDropModule;
  302. }());
  303. exports.DragDropModule = DragDropModule;
  304. //# sourceMappingURL=dragdrop.js.map