UNPKG

13.8 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 context_1 = require("../context/context");
19var logger_1 = require("../logger");
20var utils_1 = require("../utils");
21var eventService_1 = require("../eventService");
22var events_1 = require("../events");
23var gridOptionsWrapper_1 = require("../gridOptionsWrapper");
24var columnApi_1 = require("../columnController/columnApi");
25var gridApi_1 = require("../gridApi");
26/** Adds drag listening onto an element. In ag-Grid this is used twice, first is resizing columns,
27 * second is moving the columns and column groups around (ie the 'drag' part of Drag and Drop. */
28var DragService = (function () {
29 function DragService() {
30 this.onMouseUpListener = this.onMouseUp.bind(this);
31 this.onMouseMoveListener = this.onMouseMove.bind(this);
32 this.onTouchEndListener = this.onTouchUp.bind(this);
33 this.onTouchMoveListener = this.onTouchMove.bind(this);
34 this.dragEndFunctions = [];
35 this.dragSources = [];
36 }
37 DragService.prototype.init = function () {
38 this.logger = this.loggerFactory.create('DragService');
39 };
40 DragService.prototype.destroy = function () {
41 this.dragSources.forEach(this.removeListener.bind(this));
42 this.dragSources.length = 0;
43 };
44 DragService.prototype.removeListener = function (dragSourceAndListener) {
45 var element = dragSourceAndListener.dragSource.eElement;
46 var mouseDownListener = dragSourceAndListener.mouseDownListener;
47 element.removeEventListener('mousedown', mouseDownListener);
48 // remove touch listener only if it exists
49 if (dragSourceAndListener.touchEnabled) {
50 var touchStartListener = dragSourceAndListener.touchStartListener;
51 element.removeEventListener('touchstart', touchStartListener, { passive: true });
52 }
53 };
54 DragService.prototype.removeDragSource = function (params) {
55 var dragSourceAndListener = utils_1.Utils.find(this.dragSources, function (item) { return item.dragSource === params; });
56 if (!dragSourceAndListener) {
57 return;
58 }
59 this.removeListener(dragSourceAndListener);
60 utils_1.Utils.removeFromArray(this.dragSources, dragSourceAndListener);
61 };
62 DragService.prototype.setNoSelectToBody = function (noSelect) {
63 var usrDocument = this.gridOptionsWrapper.getDocument();
64 var eBody = usrDocument.querySelector('body');
65 if (utils_1.Utils.exists(eBody)) {
66 utils_1.Utils.addOrRemoveCssClass(eBody, 'ag-body-no-select', noSelect);
67 }
68 };
69 DragService.prototype.addDragSource = function (params, includeTouch) {
70 if (includeTouch === void 0) { includeTouch = false; }
71 var mouseListener = this.onMouseDown.bind(this, params);
72 params.eElement.addEventListener('mousedown', mouseListener);
73 var touchListener = null;
74 var suppressTouch = this.gridOptionsWrapper.isSuppressTouch();
75 var reallyIncludeTouch = includeTouch && !suppressTouch;
76 if (reallyIncludeTouch) {
77 touchListener = this.onTouchStart.bind(this, params);
78 params.eElement.addEventListener('touchstart', touchListener, { passive: false });
79 }
80 this.dragSources.push({
81 dragSource: params,
82 mouseDownListener: mouseListener,
83 touchStartListener: touchListener,
84 touchEnabled: includeTouch
85 });
86 };
87 // gets called whenever mouse down on any drag source
88 DragService.prototype.onTouchStart = function (params, touchEvent) {
89 var _this = this;
90 this.currentDragParams = params;
91 this.dragging = false;
92 var touch = touchEvent.touches[0];
93 this.touchLastTime = touch;
94 this.touchStart = touch;
95 touchEvent.preventDefault();
96 // we temporally add these listeners, for the duration of the drag, they
97 // are removed in touch end handling.
98 params.eElement.addEventListener('touchmove', this.onTouchMoveListener, { passive: true });
99 params.eElement.addEventListener('touchend', this.onTouchEndListener, { passive: true });
100 params.eElement.addEventListener('touchcancel', this.onTouchEndListener, { passive: true });
101 this.dragEndFunctions.push(function () {
102 params.eElement.removeEventListener('touchmove', _this.onTouchMoveListener, { passive: true });
103 params.eElement.removeEventListener('touchend', _this.onTouchEndListener, { passive: true });
104 params.eElement.removeEventListener('touchcancel', _this.onTouchEndListener, { passive: true });
105 });
106 // see if we want to start dragging straight away
107 if (params.dragStartPixels === 0) {
108 this.onCommonMove(touch, this.touchStart);
109 }
110 };
111 // gets called whenever mouse down on any drag source
112 DragService.prototype.onMouseDown = function (params, mouseEvent) {
113 var _this = this;
114 // we ignore when shift key is pressed. this is for the range selection, as when
115 // user shift-clicks a cell, this should not be interpreted as the start of a drag.
116 // if (mouseEvent.shiftKey) { return; }
117 if (params.skipMouseEvent) {
118 if (params.skipMouseEvent(mouseEvent)) {
119 return;
120 }
121 }
122 // if there are two elements with parent / child relationship, and both are draggable,
123 // when we drag the child, we should NOT drag the parent. an example of this is row moving
124 // and range selection - row moving should get preference when use drags the rowDrag component.
125 if (mouseEvent._alreadyProcessedByDragService) {
126 return;
127 }
128 mouseEvent._alreadyProcessedByDragService = true;
129 // only interested in left button clicks
130 if (mouseEvent.button !== 0) {
131 return;
132 }
133 this.currentDragParams = params;
134 this.dragging = false;
135 this.mouseEventLastTime = mouseEvent;
136 this.mouseStartEvent = mouseEvent;
137 var usrDocument = this.gridOptionsWrapper.getDocument();
138 // we temporally add these listeners, for the duration of the drag, they
139 // are removed in mouseup handling.
140 usrDocument.addEventListener('mousemove', this.onMouseMoveListener);
141 usrDocument.addEventListener('mouseup', this.onMouseUpListener);
142 this.dragEndFunctions.push(function () {
143 usrDocument.removeEventListener('mousemove', _this.onMouseMoveListener);
144 usrDocument.removeEventListener('mouseup', _this.onMouseUpListener);
145 });
146 // see if we want to start dragging straight away
147 if (params.dragStartPixels === 0) {
148 this.onMouseMove(mouseEvent);
149 }
150 };
151 // returns true if the event is close to the original event by X pixels either vertically or horizontally.
152 // we only start dragging after X pixels so this allows us to know if we should start dragging yet.
153 DragService.prototype.isEventNearStartEvent = function (currentEvent, startEvent) {
154 // by default, we wait 4 pixels before starting the drag
155 var requiredPixelDiff = utils_1.Utils.exists(this.currentDragParams.dragStartPixels) ? this.currentDragParams.dragStartPixels : 4;
156 return utils_1.Utils.areEventsNear(currentEvent, startEvent, requiredPixelDiff);
157 };
158 DragService.prototype.getFirstActiveTouch = function (touchList) {
159 for (var i = 0; i < touchList.length; i++) {
160 var matches = touchList[i].identifier === this.touchStart.identifier;
161 if (matches) {
162 return touchList[i];
163 }
164 }
165 return null;
166 };
167 DragService.prototype.onCommonMove = function (currentEvent, startEvent) {
168 if (!this.dragging) {
169 // if mouse hasn't travelled from the start position enough, do nothing
170 var toEarlyToDrag = !this.dragging && this.isEventNearStartEvent(currentEvent, startEvent);
171 if (toEarlyToDrag) {
172 return;
173 }
174 else {
175 // alert(`started`);
176 this.dragging = true;
177 var event_1 = {
178 type: events_1.Events.EVENT_DRAG_STARTED,
179 api: this.gridApi,
180 columnApi: this.columnApi
181 };
182 this.eventService.dispatchEvent(event_1);
183 this.currentDragParams.onDragStart(startEvent);
184 this.setNoSelectToBody(true);
185 }
186 }
187 this.currentDragParams.onDragging(currentEvent);
188 };
189 DragService.prototype.onTouchMove = function (touchEvent) {
190 var touch = this.getFirstActiveTouch(touchEvent.touches);
191 if (!touch) {
192 return;
193 }
194 // this.___statusBar.setInfoText(Math.random() + ' onTouchMove preventDefault stopPropagation');
195 // if we don't preview default, then the browser will try and do it's own touch stuff,
196 // like do 'back button' (chrome does this) or scroll the page (eg drag column could be confused
197 // with scroll page in the app)
198 // touchEvent.preventDefault();
199 this.onCommonMove(touch, this.touchStart);
200 };
201 // only gets called after a mouse down - as this is only added after mouseDown
202 // and is removed when mouseUp happens
203 DragService.prototype.onMouseMove = function (mouseEvent) {
204 this.onCommonMove(mouseEvent, this.mouseStartEvent);
205 };
206 DragService.prototype.onTouchUp = function (touchEvent) {
207 var touch = this.getFirstActiveTouch(touchEvent.changedTouches);
208 // i haven't worked this out yet, but there is no matching touch
209 // when we get the touch up event. to get around this, we swap in
210 // the last touch. this is a hack to 'get it working' while we
211 // figure out what's going on, why we are not getting a touch in
212 // current event.
213 if (!touch) {
214 touch = this.touchLastTime;
215 }
216 // if mouse was left up before we started to move, then this is a tap.
217 // we check this before onUpCommon as onUpCommon resets the dragging
218 // let tap = !this.dragging;
219 // let tapTarget = this.currentDragParams.eElement;
220 this.onUpCommon(touch);
221 // if tap, tell user
222 // console.log(`${Math.random()} tap = ${tap}`);
223 // if (tap) {
224 // tapTarget.click();
225 // }
226 };
227 DragService.prototype.onMouseUp = function (mouseEvent) {
228 this.onUpCommon(mouseEvent);
229 };
230 DragService.prototype.onUpCommon = function (eventOrTouch) {
231 if (this.dragging) {
232 this.dragging = false;
233 this.currentDragParams.onDragStop(eventOrTouch);
234 var event_2 = {
235 type: events_1.Events.EVENT_DRAG_STOPPED,
236 api: this.gridApi,
237 columnApi: this.columnApi
238 };
239 this.eventService.dispatchEvent(event_2);
240 }
241 this.setNoSelectToBody(false);
242 this.mouseStartEvent = null;
243 this.mouseEventLastTime = null;
244 this.touchStart = null;
245 this.touchLastTime = null;
246 this.currentDragParams = null;
247 this.dragEndFunctions.forEach(function (func) { return func(); });
248 this.dragEndFunctions.length = 0;
249 };
250 __decorate([
251 context_1.Autowired('loggerFactory'),
252 __metadata("design:type", logger_1.LoggerFactory)
253 ], DragService.prototype, "loggerFactory", void 0);
254 __decorate([
255 context_1.Autowired('eventService'),
256 __metadata("design:type", eventService_1.EventService)
257 ], DragService.prototype, "eventService", void 0);
258 __decorate([
259 context_1.Autowired('gridOptionsWrapper'),
260 __metadata("design:type", gridOptionsWrapper_1.GridOptionsWrapper)
261 ], DragService.prototype, "gridOptionsWrapper", void 0);
262 __decorate([
263 context_1.Autowired('columnApi'),
264 __metadata("design:type", columnApi_1.ColumnApi)
265 ], DragService.prototype, "columnApi", void 0);
266 __decorate([
267 context_1.Autowired('gridApi'),
268 __metadata("design:type", gridApi_1.GridApi)
269 ], DragService.prototype, "gridApi", void 0);
270 __decorate([
271 context_1.PostConstruct,
272 __metadata("design:type", Function),
273 __metadata("design:paramtypes", []),
274 __metadata("design:returntype", void 0)
275 ], DragService.prototype, "init", null);
276 __decorate([
277 context_1.PreDestroy,
278 __metadata("design:type", Function),
279 __metadata("design:paramtypes", []),
280 __metadata("design:returntype", void 0)
281 ], DragService.prototype, "destroy", null);
282 DragService = __decorate([
283 context_1.Bean('dragService')
284 ], DragService);
285 return DragService;
286}());
287exports.DragService = DragService;