UNPKG

9.81 kBJavaScriptView Raw
1/**
2 * ag-grid - Advanced Data Grid / Data Table supporting Javascript / React / AngularJS / Web Components
3 * @version v18.1.2
4 * @link http://www.ag-grid.com/
5 * @license MIT
6 */
7"use strict";
8var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
9 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
10 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
11 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;
12 return c > 3 && r && Object.defineProperty(target, key, r), r;
13};
14var __metadata = (this && this.__metadata) || function (k, v) {
15 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
16};
17Object.defineProperty(exports, "__esModule", { value: true });
18var dragAndDropService_1 = require("../dragAndDrop/dragAndDropService");
19var context_1 = require("../context/context");
20var focusedCellController_1 = require("../focusedCellController");
21var gridOptionsWrapper_1 = require("../gridOptionsWrapper");
22var eventService_1 = require("../eventService");
23var eventKeys_1 = require("../eventKeys");
24var RowDragFeature = (function () {
25 function RowDragFeature(eContainer, gridPanel) {
26 this.eContainer = eContainer;
27 this.gridPanel = gridPanel;
28 }
29 RowDragFeature.prototype.postConstruct = function () {
30 if (this.gridOptionsWrapper.isRowModelDefault()) {
31 this.clientSideRowModel = this.rowModel;
32 }
33 };
34 RowDragFeature.prototype.getContainer = function () {
35 return this.eContainer;
36 };
37 RowDragFeature.prototype.isInterestedIn = function (type) {
38 return type === dragAndDropService_1.DragSourceType.RowDrag;
39 };
40 RowDragFeature.prototype.getIconName = function () {
41 return dragAndDropService_1.DragAndDropService.ICON_MOVE;
42 };
43 RowDragFeature.prototype.onDragEnter = function (draggingEvent) {
44 // when entering, we fire the enter event, then in onEnterOrDragging,
45 // we also fire the move event. so we get both events when entering.
46 this.dispatchEvent(eventKeys_1.Events.EVENT_ROW_DRAG_ENTER, draggingEvent);
47 this.dragAndDropService.setGhostIcon(dragAndDropService_1.DragAndDropService.ICON_MOVE);
48 draggingEvent.dragItem.rowNode.setDragging(true);
49 this.onEnterOrDragging(draggingEvent);
50 };
51 RowDragFeature.prototype.onDragging = function (draggingEvent) {
52 this.onEnterOrDragging(draggingEvent);
53 };
54 RowDragFeature.prototype.onEnterOrDragging = function (draggingEvent) {
55 // this event is fired for enter and move
56 this.dispatchEvent(eventKeys_1.Events.EVENT_ROW_DRAG_MOVE, draggingEvent);
57 this.lastDraggingEvent = draggingEvent;
58 var pixel = this.normaliseForScroll(draggingEvent.y);
59 var managedDrag = this.gridOptionsWrapper.isRowDragManaged();
60 if (managedDrag) {
61 this.doManagedDrag(draggingEvent, pixel);
62 }
63 this.checkCenterForScrolling(pixel);
64 };
65 RowDragFeature.prototype.doManagedDrag = function (draggingEvent, pixel) {
66 var rowNode = draggingEvent.dragItem.rowNode;
67 var rowWasMoved = this.clientSideRowModel.ensureRowAtPixel(rowNode, pixel);
68 if (rowWasMoved) {
69 this.focusedCellController.clearFocusedCell();
70 if (this.rangeController) {
71 this.rangeController.clearSelection();
72 }
73 }
74 };
75 RowDragFeature.prototype.normaliseForScroll = function (pixel) {
76 var gridPanelHasScrolls = !this.gridOptionsWrapper.isGridAutoHeight();
77 if (gridPanelHasScrolls) {
78 var pixelRange = this.gridPanel.getVScrollPosition();
79 return pixel + pixelRange.top;
80 }
81 else {
82 return pixel;
83 }
84 };
85 RowDragFeature.prototype.checkCenterForScrolling = function (pixel) {
86 // scroll if the mouse is within 50px of the grid edge
87 var pixelRange = this.gridPanel.getVScrollPosition();
88 // console.log(`pixelRange = (${pixelRange.top}, ${pixelRange.bottom})`);
89 this.needToMoveUp = pixel < (pixelRange.top + 50);
90 this.needToMoveDown = pixel > (pixelRange.bottom - 50);
91 // console.log(`needToMoveUp = ${this.needToMoveUp} = pixel < (pixelRange.top + 50) = ${pixel} < (${pixelRange.top} + 50)`);
92 // console.log(`needToMoveDown = ${this.needToMoveDown} = pixel < (pixelRange.top + 50) = ${pixel} < (${pixelRange.top} + 50)`);
93 if (this.needToMoveUp || this.needToMoveDown) {
94 this.ensureIntervalStarted();
95 }
96 else {
97 this.ensureIntervalCleared();
98 }
99 };
100 RowDragFeature.prototype.ensureIntervalStarted = function () {
101 if (!this.movingIntervalId) {
102 this.intervalCount = 0;
103 this.movingIntervalId = setInterval(this.moveInterval.bind(this), 100);
104 }
105 };
106 RowDragFeature.prototype.ensureIntervalCleared = function () {
107 if (this.moveInterval) {
108 clearInterval(this.movingIntervalId);
109 this.movingIntervalId = null;
110 }
111 };
112 RowDragFeature.prototype.moveInterval = function () {
113 // the amounts we move get bigger at each interval, so the speed accelerates, starting a bit slow
114 // and getting faster. this is to give smoother user experience. we max at 100px to limit the speed.
115 var pixelsToMove;
116 this.intervalCount++;
117 pixelsToMove = 10 + (this.intervalCount * 5);
118 if (pixelsToMove > 100) {
119 pixelsToMove = 100;
120 }
121 var pixelsMoved;
122 if (this.needToMoveDown) {
123 pixelsMoved = this.gridPanel.scrollVertically(pixelsToMove);
124 }
125 else if (this.needToMoveUp) {
126 pixelsMoved = this.gridPanel.scrollVertically(-pixelsToMove);
127 }
128 if (pixelsMoved !== 0) {
129 this.onDragging(this.lastDraggingEvent);
130 }
131 };
132 // i tried using generics here with this:
133 // public createEvent<T extends RowDragEvent>(type: string, clazz: {new(): T; }, draggingEvent: DraggingEvent) {
134 // but it didn't work - i think it's because it only works with classes, and not interfaces, (the events are interfaces)
135 RowDragFeature.prototype.dispatchEvent = function (type, draggingEvent) {
136 var yNormalised = this.normaliseForScroll(draggingEvent.y);
137 var overIndex = -1;
138 var overNode = null;
139 var mouseIsPastLastRow = yNormalised > this.rowModel.getCurrentPageHeight();
140 if (!mouseIsPastLastRow) {
141 overIndex = this.rowModel.getRowIndexAtPixel(yNormalised);
142 overNode = this.rowModel.getRow(overIndex);
143 }
144 var vDirectionString;
145 switch (draggingEvent.vDirection) {
146 case dragAndDropService_1.VDirection.Down:
147 vDirectionString = 'down';
148 break;
149 case dragAndDropService_1.VDirection.Up:
150 vDirectionString = 'up';
151 break;
152 default:
153 vDirectionString = null;
154 break;
155 }
156 var event = {
157 type: type,
158 api: this.gridOptionsWrapper.getApi(),
159 columnApi: this.gridOptionsWrapper.getColumnApi(),
160 event: draggingEvent.event,
161 node: draggingEvent.dragItem.rowNode,
162 overIndex: overIndex,
163 overNode: overNode,
164 y: yNormalised,
165 vDirection: vDirectionString
166 };
167 this.eventService.dispatchEvent(event);
168 };
169 RowDragFeature.prototype.onDragLeave = function (draggingEvent) {
170 this.dispatchEvent(eventKeys_1.Events.EVENT_ROW_DRAG_LEAVE, draggingEvent);
171 this.stopDragging(draggingEvent);
172 };
173 RowDragFeature.prototype.onDragStop = function (draggingEvent) {
174 this.dispatchEvent(eventKeys_1.Events.EVENT_ROW_DRAG_END, draggingEvent);
175 this.stopDragging(draggingEvent);
176 };
177 RowDragFeature.prototype.stopDragging = function (draggingEvent) {
178 this.ensureIntervalCleared();
179 draggingEvent.dragItem.rowNode.setDragging(false);
180 };
181 __decorate([
182 context_1.Autowired('dragAndDropService'),
183 __metadata("design:type", dragAndDropService_1.DragAndDropService)
184 ], RowDragFeature.prototype, "dragAndDropService", void 0);
185 __decorate([
186 context_1.Autowired('rowModel'),
187 __metadata("design:type", Object)
188 ], RowDragFeature.prototype, "rowModel", void 0);
189 __decorate([
190 context_1.Autowired('focusedCellController'),
191 __metadata("design:type", focusedCellController_1.FocusedCellController)
192 ], RowDragFeature.prototype, "focusedCellController", void 0);
193 __decorate([
194 context_1.Autowired('gridOptionsWrapper'),
195 __metadata("design:type", gridOptionsWrapper_1.GridOptionsWrapper)
196 ], RowDragFeature.prototype, "gridOptionsWrapper", void 0);
197 __decorate([
198 context_1.Optional('rangeController'),
199 __metadata("design:type", Object)
200 ], RowDragFeature.prototype, "rangeController", void 0);
201 __decorate([
202 context_1.Autowired('eventService'),
203 __metadata("design:type", eventService_1.EventService)
204 ], RowDragFeature.prototype, "eventService", void 0);
205 __decorate([
206 context_1.PostConstruct,
207 __metadata("design:type", Function),
208 __metadata("design:paramtypes", []),
209 __metadata("design:returntype", void 0)
210 ], RowDragFeature.prototype, "postConstruct", null);
211 return RowDragFeature;
212}());
213exports.RowDragFeature = RowDragFeature;