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 columnController_1 = require("../columnController/columnController");
|
21 | var column_1 = require("../entities/column");
|
22 | var utils_1 = require("../utils");
|
23 | var dragAndDropService_1 = require("../dragAndDrop/dragAndDropService");
|
24 | var gridOptionsWrapper_1 = require("../gridOptionsWrapper");
|
25 | var MoveColumnController = (function () {
|
26 | function MoveColumnController(pinned, eContainer) {
|
27 | this.needToMoveLeft = false;
|
28 | this.needToMoveRight = false;
|
29 | this.pinned = pinned;
|
30 | this.eContainer = eContainer;
|
31 | this.centerContainer = !utils_1.Utils.exists(pinned);
|
32 | }
|
33 | MoveColumnController.prototype.registerGridComp = function (gridPanel) {
|
34 | this.gridPanel = gridPanel;
|
35 | };
|
36 | MoveColumnController.prototype.init = function () {
|
37 | this.logger = this.loggerFactory.create('MoveColumnController');
|
38 | };
|
39 | MoveColumnController.prototype.getIconName = function () {
|
40 | return this.pinned ? dragAndDropService_1.DragAndDropService.ICON_PINNED : dragAndDropService_1.DragAndDropService.ICON_MOVE;
|
41 | };
|
42 | MoveColumnController.prototype.onDragEnter = function (draggingEvent) {
|
43 |
|
44 | var columns = draggingEvent.dragItem.columns;
|
45 | var dragCameFromToolPanel = draggingEvent.dragSource.type === dragAndDropService_1.DragSourceType.ToolPanel;
|
46 | if (dragCameFromToolPanel) {
|
47 |
|
48 | this.setColumnsVisible(columns, true, "uiColumnDragged");
|
49 | }
|
50 | else {
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | var visibleState_1 = draggingEvent.dragItem.visibleState;
|
56 | var visibleColumns = columns.filter(function (column) { return visibleState_1[column.getId()]; });
|
57 | this.setColumnsVisible(visibleColumns, true, "uiColumnDragged");
|
58 | }
|
59 | this.setColumnsPinned(columns, this.pinned, "uiColumnDragged");
|
60 | this.onDragging(draggingEvent, true);
|
61 | };
|
62 | MoveColumnController.prototype.onDragLeave = function (draggingEvent) {
|
63 | var hideColumnOnExit = !this.gridOptionsWrapper.isSuppressDragLeaveHidesColumns() && !draggingEvent.fromNudge;
|
64 | if (hideColumnOnExit) {
|
65 | var dragItem = draggingEvent.dragSource.dragItemCallback();
|
66 | var columns = dragItem.columns;
|
67 | this.setColumnsVisible(columns, false, "uiColumnDragged");
|
68 | }
|
69 | this.ensureIntervalCleared();
|
70 | };
|
71 | MoveColumnController.prototype.setColumnsVisible = function (columns, visible, source) {
|
72 | if (source === void 0) { source = "api"; }
|
73 | if (columns) {
|
74 | var allowedCols = columns.filter(function (c) { return !c.isLockVisible(); });
|
75 | this.columnController.setColumnsVisible(allowedCols, visible, source);
|
76 | }
|
77 | };
|
78 | MoveColumnController.prototype.setColumnsPinned = function (columns, pinned, source) {
|
79 | if (source === void 0) { source = "api"; }
|
80 | if (columns) {
|
81 | var allowedCols = columns.filter(function (c) { return !c.isLockPinned(); });
|
82 | this.columnController.setColumnsPinned(allowedCols, pinned, source);
|
83 | }
|
84 | };
|
85 | MoveColumnController.prototype.onDragStop = function () {
|
86 | this.ensureIntervalCleared();
|
87 | };
|
88 | MoveColumnController.prototype.normaliseX = function (x) {
|
89 |
|
90 | var flipHorizontallyForRtl = this.gridOptionsWrapper.isEnableRtl();
|
91 | if (flipHorizontallyForRtl) {
|
92 | var clientWidth = this.eContainer.clientWidth;
|
93 | x = clientWidth - x;
|
94 | }
|
95 |
|
96 | var adjustForScroll = this.centerContainer;
|
97 | if (adjustForScroll) {
|
98 | x += this.gridPanel.getBodyViewportScrollLeft();
|
99 | }
|
100 | return x;
|
101 | };
|
102 | MoveColumnController.prototype.checkCenterForScrolling = function (xAdjustedForScroll) {
|
103 | if (this.centerContainer) {
|
104 |
|
105 |
|
106 | var firstVisiblePixel = this.gridPanel.getBodyViewportScrollLeft();
|
107 | var lastVisiblePixel = firstVisiblePixel + this.gridPanel.getCenterWidth();
|
108 | if (this.gridOptionsWrapper.isEnableRtl()) {
|
109 | this.needToMoveRight = xAdjustedForScroll < (firstVisiblePixel + 50);
|
110 | this.needToMoveLeft = xAdjustedForScroll > (lastVisiblePixel - 50);
|
111 | }
|
112 | else {
|
113 | this.needToMoveLeft = xAdjustedForScroll < (firstVisiblePixel + 50);
|
114 | this.needToMoveRight = xAdjustedForScroll > (lastVisiblePixel - 50);
|
115 | }
|
116 | if (this.needToMoveLeft || this.needToMoveRight) {
|
117 | this.ensureIntervalStarted();
|
118 | }
|
119 | else {
|
120 | this.ensureIntervalCleared();
|
121 | }
|
122 | }
|
123 | };
|
124 | MoveColumnController.prototype.onDragging = function (draggingEvent, fromEnter) {
|
125 | var _this = this;
|
126 | if (fromEnter === void 0) { fromEnter = false; }
|
127 | this.lastDraggingEvent = draggingEvent;
|
128 |
|
129 | if (utils_1.Utils.missing(draggingEvent.hDirection)) {
|
130 | return;
|
131 | }
|
132 | var xNormalised = this.normaliseX(draggingEvent.x);
|
133 |
|
134 |
|
135 |
|
136 | if (!fromEnter) {
|
137 | this.checkCenterForScrolling(xNormalised);
|
138 | }
|
139 | var hDirectionNormalised = this.normaliseDirection(draggingEvent.hDirection);
|
140 | var dragSourceType = draggingEvent.dragSource.type;
|
141 | var columnsToMove = draggingEvent.dragSource.dragItemCallback().columns;
|
142 | columnsToMove = columnsToMove.filter(function (col) {
|
143 | if (col.isLockPinned()) {
|
144 |
|
145 |
|
146 | return col.getPinned() == _this.pinned;
|
147 | }
|
148 | else {
|
149 |
|
150 | return true;
|
151 | }
|
152 | });
|
153 | this.attemptMoveColumns(dragSourceType, columnsToMove, hDirectionNormalised, xNormalised, fromEnter);
|
154 | };
|
155 | MoveColumnController.prototype.normaliseDirection = function (hDirection) {
|
156 | if (this.gridOptionsWrapper.isEnableRtl()) {
|
157 | switch (hDirection) {
|
158 | case dragAndDropService_1.HDirection.Left: return dragAndDropService_1.HDirection.Right;
|
159 | case dragAndDropService_1.HDirection.Right: return dragAndDropService_1.HDirection.Left;
|
160 | default: console.error("ag-Grid: Unknown direction " + hDirection);
|
161 | }
|
162 | }
|
163 | else {
|
164 | return hDirection;
|
165 | }
|
166 | };
|
167 |
|
168 |
|
169 | MoveColumnController.prototype.calculateOldIndex = function (movingCols) {
|
170 | var gridCols = this.columnController.getAllGridColumns();
|
171 | var indexes = [];
|
172 | movingCols.forEach(function (col) { return indexes.push(gridCols.indexOf(col)); });
|
173 | utils_1.Utils.sortNumberArray(indexes);
|
174 | var firstIndex = indexes[0];
|
175 | var lastIndex = indexes[indexes.length - 1];
|
176 | var spread = lastIndex - firstIndex;
|
177 | var gapsExist = spread !== indexes.length - 1;
|
178 | return gapsExist ? null : firstIndex;
|
179 | };
|
180 | MoveColumnController.prototype.attemptMoveColumns = function (dragSourceType, allMovingColumns, hDirection, xAdjusted, fromEnter) {
|
181 | var draggingLeft = hDirection === dragAndDropService_1.HDirection.Left;
|
182 | var draggingRight = hDirection === dragAndDropService_1.HDirection.Right;
|
183 | var validMoves = this.calculateValidMoves(allMovingColumns, draggingRight, xAdjusted);
|
184 |
|
185 |
|
186 | var oldIndex = this.calculateOldIndex(allMovingColumns);
|
187 |
|
188 | for (var i = 0; i < validMoves.length; i++) {
|
189 | var newIndex = validMoves[i];
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | var constrainDirection = oldIndex !== null && !fromEnter;
|
199 |
|
200 | if (dragSourceType == dragAndDropService_1.DragSourceType.HeaderCell) {
|
201 | constrainDirection = oldIndex !== null;
|
202 | }
|
203 | if (constrainDirection) {
|
204 |
|
205 | if (draggingLeft && newIndex >= oldIndex) {
|
206 | continue;
|
207 | }
|
208 |
|
209 | if (draggingRight && newIndex <= oldIndex) {
|
210 | continue;
|
211 | }
|
212 | }
|
213 | if (!this.columnController.doesMovePassRules(allMovingColumns, newIndex)) {
|
214 | continue;
|
215 | }
|
216 | this.columnController.moveColumns(allMovingColumns, newIndex, "uiColumnDragged");
|
217 |
|
218 | return;
|
219 | }
|
220 | };
|
221 | MoveColumnController.prototype.calculateValidMoves = function (movingCols, draggingRight, x) {
|
222 |
|
223 | var allDisplayedCols = this.columnController.getDisplayedColumns(this.pinned);
|
224 |
|
225 |
|
226 | var allGridCols = this.columnController.getAllGridColumns();
|
227 | var colIsMovingFunc = function (col) { return movingCols.indexOf(col) >= 0; };
|
228 | var colIsNotMovingFunc = function (col) { return movingCols.indexOf(col) < 0; };
|
229 | var movingDisplayedCols = allDisplayedCols.filter(colIsMovingFunc);
|
230 | var otherDisplayedCols = allDisplayedCols.filter(colIsNotMovingFunc);
|
231 | var otherGridCols = allGridCols.filter(colIsNotMovingFunc);
|
232 |
|
233 |
|
234 |
|
235 | var displayIndex = 0;
|
236 | var availableWidth = x;
|
237 |
|
238 |
|
239 | if (draggingRight) {
|
240 | var widthOfMovingDisplayedCols_1 = 0;
|
241 | movingDisplayedCols.forEach(function (col) { return widthOfMovingDisplayedCols_1 += col.getActualWidth(); });
|
242 | availableWidth -= widthOfMovingDisplayedCols_1;
|
243 | }
|
244 |
|
245 | for (var i = 0; i < otherDisplayedCols.length; i++) {
|
246 | var col = otherDisplayedCols[i];
|
247 | availableWidth -= col.getActualWidth();
|
248 | if (availableWidth < 0) {
|
249 | break;
|
250 | }
|
251 | displayIndex++;
|
252 | }
|
253 |
|
254 | if (draggingRight) {
|
255 | displayIndex++;
|
256 | }
|
257 |
|
258 |
|
259 | var gridColIndex;
|
260 | if (displayIndex > 0) {
|
261 | var leftColumn = otherDisplayedCols[displayIndex - 1];
|
262 | gridColIndex = otherGridCols.indexOf(leftColumn) + 1;
|
263 | }
|
264 | else {
|
265 | gridColIndex = 0;
|
266 | }
|
267 | var validMoves = [gridColIndex];
|
268 |
|
269 |
|
270 |
|
271 |
|
272 | var nextCol = allGridCols[gridColIndex];
|
273 | while (utils_1.Utils.exists(nextCol) && this.isColumnHidden(allDisplayedCols, nextCol)) {
|
274 | gridColIndex++;
|
275 | validMoves.push(gridColIndex);
|
276 | nextCol = allGridCols[gridColIndex];
|
277 | }
|
278 | return validMoves;
|
279 | };
|
280 |
|
281 | MoveColumnController.prototype.isColumnHidden = function (displayedColumns, col) {
|
282 | return displayedColumns.indexOf(col) < 0;
|
283 | };
|
284 | MoveColumnController.prototype.ensureIntervalStarted = function () {
|
285 | if (!this.movingIntervalId) {
|
286 | this.intervalCount = 0;
|
287 | this.failedMoveAttempts = 0;
|
288 | this.movingIntervalId = setInterval(this.moveInterval.bind(this), 100);
|
289 | if (this.needToMoveLeft) {
|
290 | this.dragAndDropService.setGhostIcon(dragAndDropService_1.DragAndDropService.ICON_LEFT, true);
|
291 | }
|
292 | else {
|
293 | this.dragAndDropService.setGhostIcon(dragAndDropService_1.DragAndDropService.ICON_RIGHT, true);
|
294 | }
|
295 | }
|
296 | };
|
297 | MoveColumnController.prototype.ensureIntervalCleared = function () {
|
298 | if (this.moveInterval) {
|
299 | clearInterval(this.movingIntervalId);
|
300 | this.movingIntervalId = null;
|
301 | this.dragAndDropService.setGhostIcon(dragAndDropService_1.DragAndDropService.ICON_MOVE);
|
302 | }
|
303 | };
|
304 | MoveColumnController.prototype.moveInterval = function () {
|
305 |
|
306 |
|
307 | var pixelsToMove;
|
308 | this.intervalCount++;
|
309 | pixelsToMove = 10 + (this.intervalCount * 5);
|
310 | if (pixelsToMove > 100) {
|
311 | pixelsToMove = 100;
|
312 | }
|
313 | var pixelsMoved;
|
314 | if (this.needToMoveLeft) {
|
315 | pixelsMoved = this.gridPanel.scrollHorizontally(-pixelsToMove);
|
316 | }
|
317 | else if (this.needToMoveRight) {
|
318 | pixelsMoved = this.gridPanel.scrollHorizontally(pixelsToMove);
|
319 | }
|
320 | if (pixelsMoved !== 0) {
|
321 | this.onDragging(this.lastDraggingEvent);
|
322 | this.failedMoveAttempts = 0;
|
323 | }
|
324 | else {
|
325 |
|
326 |
|
327 | this.failedMoveAttempts++;
|
328 | var columns = this.lastDraggingEvent.dragItem.columns;
|
329 | var columnsThatCanPin = columns.filter(function (c) { return !c.isLockPinned(); });
|
330 | if (columnsThatCanPin.length > 0) {
|
331 | this.dragAndDropService.setGhostIcon(dragAndDropService_1.DragAndDropService.ICON_PINNED);
|
332 | if (this.failedMoveAttempts > 7) {
|
333 | var pinType = this.needToMoveLeft ? column_1.Column.PINNED_LEFT : column_1.Column.PINNED_RIGHT;
|
334 | this.setColumnsPinned(columnsThatCanPin, pinType, "uiColumnDragged");
|
335 | this.dragAndDropService.nudge();
|
336 | }
|
337 | }
|
338 | }
|
339 | };
|
340 | __decorate([
|
341 | context_1.Autowired('loggerFactory'),
|
342 | __metadata("design:type", logger_1.LoggerFactory)
|
343 | ], MoveColumnController.prototype, "loggerFactory", void 0);
|
344 | __decorate([
|
345 | context_1.Autowired('columnController'),
|
346 | __metadata("design:type", columnController_1.ColumnController)
|
347 | ], MoveColumnController.prototype, "columnController", void 0);
|
348 | __decorate([
|
349 | context_1.Autowired('dragAndDropService'),
|
350 | __metadata("design:type", dragAndDropService_1.DragAndDropService)
|
351 | ], MoveColumnController.prototype, "dragAndDropService", void 0);
|
352 | __decorate([
|
353 | context_1.Autowired('gridOptionsWrapper'),
|
354 | __metadata("design:type", gridOptionsWrapper_1.GridOptionsWrapper)
|
355 | ], MoveColumnController.prototype, "gridOptionsWrapper", void 0);
|
356 | __decorate([
|
357 | context_1.PostConstruct,
|
358 | __metadata("design:type", Function),
|
359 | __metadata("design:paramtypes", []),
|
360 | __metadata("design:returntype", void 0)
|
361 | ], MoveColumnController.prototype, "init", null);
|
362 | return MoveColumnController;
|
363 | }());
|
364 | exports.MoveColumnController = MoveColumnController;
|