1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | "use strict";
|
8 | var __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 | };
|
14 | var __metadata = (this && this.__metadata) || function (k, v) {
|
15 | if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
16 | };
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | var context_1 = require("../context/context");
|
19 | var logger_1 = require("../logger");
|
20 | var utils_1 = require("../utils");
|
21 | var eventService_1 = require("../eventService");
|
22 | var events_1 = require("../events");
|
23 | var gridOptionsWrapper_1 = require("../gridOptionsWrapper");
|
24 | var columnApi_1 = require("../columnController/columnApi");
|
25 | var gridApi_1 = require("../gridApi");
|
26 |
|
27 |
|
28 | var 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 |
|
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 |
|
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 |
|
97 |
|
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 |
|
107 | if (params.dragStartPixels === 0) {
|
108 | this.onCommonMove(touch, this.touchStart);
|
109 | }
|
110 | };
|
111 |
|
112 | DragService.prototype.onMouseDown = function (params, mouseEvent) {
|
113 | var _this = this;
|
114 |
|
115 |
|
116 |
|
117 | if (params.skipMouseEvent) {
|
118 | if (params.skipMouseEvent(mouseEvent)) {
|
119 | return;
|
120 | }
|
121 | }
|
122 |
|
123 |
|
124 |
|
125 | if (mouseEvent._alreadyProcessedByDragService) {
|
126 | return;
|
127 | }
|
128 | mouseEvent._alreadyProcessedByDragService = true;
|
129 |
|
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 |
|
139 |
|
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 |
|
147 | if (params.dragStartPixels === 0) {
|
148 | this.onMouseMove(mouseEvent);
|
149 | }
|
150 | };
|
151 |
|
152 |
|
153 | DragService.prototype.isEventNearStartEvent = function (currentEvent, startEvent) {
|
154 |
|
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 |
|
170 | var toEarlyToDrag = !this.dragging && this.isEventNearStartEvent(currentEvent, startEvent);
|
171 | if (toEarlyToDrag) {
|
172 | return;
|
173 | }
|
174 | else {
|
175 |
|
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 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 | this.onCommonMove(touch, this.touchStart);
|
200 | };
|
201 |
|
202 |
|
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 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 | if (!touch) {
|
214 | touch = this.touchLastTime;
|
215 | }
|
216 |
|
217 |
|
218 |
|
219 |
|
220 | this.onUpCommon(touch);
|
221 |
|
222 |
|
223 |
|
224 |
|
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 | }());
|
287 | exports.DragService = DragService;
|