UNPKG

19.7 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 columnController_1 = require("../columnController/columnController");
21var column_1 = require("../entities/column");
22var utils_1 = require("../utils");
23var dragAndDropService_1 = require("../dragAndDrop/dragAndDropService");
24var gridOptionsWrapper_1 = require("../gridOptionsWrapper");
25var 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 // we do dummy drag, so make sure column appears in the right location when first placed
44 var columns = draggingEvent.dragItem.columns;
45 var dragCameFromToolPanel = draggingEvent.dragSource.type === dragAndDropService_1.DragSourceType.ToolPanel;
46 if (dragCameFromToolPanel) {
47 // the if statement doesn't work if drag leaves grid, then enters again
48 this.setColumnsVisible(columns, true, "uiColumnDragged");
49 }
50 else {
51 // restore previous state of visible columns upon re-entering. this means if the user drags
52 // a group out, and then drags the group back in, only columns that were originally visible
53 // will be visible again. otherwise a group with three columns (but only two visible) could
54 // be dragged out, then when it's dragged in again, all three are visible. this stops that.
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 // flip the coordinate if doing RTL
90 var flipHorizontallyForRtl = this.gridOptionsWrapper.isEnableRtl();
91 if (flipHorizontallyForRtl) {
92 var clientWidth = this.eContainer.clientWidth;
93 x = clientWidth - x;
94 }
95 // adjust for scroll only if centre container (the pinned containers dont scroll)
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 // scroll if the mouse has gone outside the grid (or just outside the scrollable part if pinning)
105 // putting in 50 buffer, so even if user gets to edge of grid, a scroll will happen
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 // if moving up or down (ie not left or right) then do nothing
129 if (utils_1.Utils.missing(draggingEvent.hDirection)) {
130 return;
131 }
132 var xNormalised = this.normaliseX(draggingEvent.x);
133 // if the user is dragging into the panel, ie coming from the side panel into the main grid,
134 // we don't want to scroll the grid this time, it would appear like the table is jumping
135 // each time a column is dragged in.
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 // if locked return true only if both col and container are same pin type.
145 // double equals (==) here on purpose so that null==undefined is true (for not pinned options)
146 return col.getPinned() == _this.pinned;
147 }
148 else {
149 // if not pin locked, then always allowed to be in this container
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 // returns the index of the first column in the list ONLY if the cols are all beside
168 // each other. if the cols are not beside each other, then returns null
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 // if cols are not adjacent, then this returns null. when moving, we constrain the direction of the move
185 // (ie left or right) to the mouse direction. however
186 var oldIndex = this.calculateOldIndex(allMovingColumns);
187 // fromEnter = false;
188 for (var i = 0; i < validMoves.length; i++) {
189 var newIndex = validMoves[i];
190 // the two check below stop an error when the user grabs a group my a middle column, then
191 // it is possible the mouse pointer is to the right of a column while been dragged left.
192 // so we need to make sure that the mouse pointer is actually left of the left most column
193 // if moving left, and right of the right most column if moving right
194 // we check 'fromEnter' below so we move the column to the new spot if the mouse is coming from
195 // outside the grid, eg if the column is moving from side panel, mouse is moving left, then we should
196 // place the column to the RHS even if the mouse is moving left and the column is already on
197 // the LHS. otherwise we stick to the rule described above.
198 var constrainDirection = oldIndex !== null && !fromEnter;
199 // don't consider 'fromEnter' when dragging header cells, otherwise group can jump to opposite direction of drag
200 if (dragSourceType == dragAndDropService_1.DragSourceType.HeaderCell) {
201 constrainDirection = oldIndex !== null;
202 }
203 if (constrainDirection) {
204 // only allow left drag if this column is moving left
205 if (draggingLeft && newIndex >= oldIndex) {
206 continue;
207 }
208 // only allow right drag if this column is moving right
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 // important to return here, so once we do the first valid move, we don't try do any more
218 return;
219 }
220 };
221 MoveColumnController.prototype.calculateValidMoves = function (movingCols, draggingRight, x) {
222 // this is the list of cols on the screen, so it's these we use when comparing the x mouse position
223 var allDisplayedCols = this.columnController.getDisplayedColumns(this.pinned);
224 // but this list is the list of all cols, when we move a col it's the index within this list that gets used,
225 // so the result we return has to be and index location for this list
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 // work out how many DISPLAYED columns fit before the 'x' position. this gives us the displayIndex.
233 // for example, if cols are a,b,c,d and we find a,b fit before 'x', then we want to place the moving
234 // col between b and c (so that it is under the mouse position).
235 var displayIndex = 0;
236 var availableWidth = x;
237 // if we are dragging right, then the columns will be to the left of the mouse, so we also want to
238 // include the width of the moving columns
239 if (draggingRight) {
240 var widthOfMovingDisplayedCols_1 = 0;
241 movingDisplayedCols.forEach(function (col) { return widthOfMovingDisplayedCols_1 += col.getActualWidth(); });
242 availableWidth -= widthOfMovingDisplayedCols_1;
243 }
244 // now count how many of the displayed columns will fit to the left
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 // trial and error, if going right, we adjust by one, i didn't manage to quantify why, but it works
254 if (draggingRight) {
255 displayIndex++;
256 }
257 // the display index is with respect to all the showing columns, however when we move, it's with
258 // respect to all grid columns, so we need to translate from display index to grid index
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 // add in all adjacent empty columns as other valid moves. this allows us to try putting the new
269 // column in any place of a hidden column, to try different combinations so that we don't break
270 // married children. in other words, maybe the new index breaks a group, but only because some
271 // columns are hidden, maybe we can reshuffle the hidden columns to find a place that works.
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 // isHidden takes into account visible=false and group=closed, ie it is not displayed
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 // the amounts we move get bigger at each interval, so the speed accelerates, starting a bit slow
306 // and getting faster. this is to give smoother user experience. we max at 100px to limit the speed.
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 // we count the failed move attempts. if we fail to move 7 times, then we pin the column.
326 // this is how we achieve pining by dragging the column to the edge of the grid.
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}());
364exports.MoveColumnController = MoveColumnController;