UNPKG

@true-directive/base

Version:

The set of base classes for the TrueDirective Grid

160 lines (159 loc) 6.33 kB
import { Column } from './column.class'; import { Utils } from '../common/utils.class'; /** * Column collection. */ var ColumnCollection = /** @class */ (function () { function ColumnCollection() { this._columns = []; } Object.defineProperty(ColumnCollection.prototype, "columns", { get: function () { return this._columns; }, set: function (v) { this._columns = v; }, enumerable: true, configurable: true }); Object.defineProperty(ColumnCollection.prototype, "firstField", { // Наименование поля первой видимой колонки (исключая чекбоксы) get: function () { var col = this.columns.find(function (c) { return c.visible && !c.isCheckbox; }); if (col) { return col.fieldName; } return ''; }, enumerable: true, configurable: true }); /** * Getting a column by field name. * @param fieldName Name of column field. * @return Column if found. */ ColumnCollection.prototype.columnByFieldName = function (fieldName) { for (var i = 0; i < this.columns.length; i++) { var col = this.columns[i]; if (col.fieldName === fieldName) { return col; } } return null; }; // Возвращает, привязана ли слева колонка с чекбоксом к указанной колонке // Немного нестандартная feature ColumnCollection.prototype.prevCheckbox = function (column, place, list, clone) { if (place === void 0) { place = null; } if (list === void 0) { list = null; } if (clone === void 0) { clone = true; } for (var i = 1; i < this.columns.length; i++) { if (this.columns[i].fieldName === column.fieldName) { var col = this.columns[i - 1]; if (col.isCheckbox) { var cbCol = clone ? new Column(col.fieldName, col.caption, col.width, col.type, '') : col; if (list) { list.push(cbCol); } if (place) { cbCol.fixed = place; } return col; // Обратите внимание - возвращаем исходную колонку } return null; } } return null; }; /** * Перемещение колонки * @param target Колонка, которую перемещает пользователь * @param dropInfo Описание места и обстоятельств, куда перемещает. * @return Можно ли переместить */ ColumnCollection.prototype.reorderColumn = function (target, dropInfo) { var _this = this; var update = false; target = this.columnByFieldName(target.fieldName); if (!target) { return; } var oldIndex = -1; var newIndex = -1; var fixed = dropInfo.place; if (dropInfo.item) { fixed = dropInfo.item.fixed; } this.columns.some(function (c, index) { if (c === target) { oldIndex = index; } if (c === dropInfo.item) { newIndex = index; if (dropInfo.pos !== 'left') { newIndex++; } } var col = target; col.fixed = fixed; col.visible = true; if (oldIndex >= 0 && (newIndex >= 0 || dropInfo.item === null)) { if (oldIndex > 0 && _this.columns[oldIndex - 1].isCheckbox) { // Цепляем паровозиком наш чекбокс var cbCol = _this.columns[oldIndex - 1]; cbCol.fixed = fixed; cbCol.visible = true; if (oldIndex < newIndex || dropInfo.item === null) { Utils.moveArrayItem(_this.columns, oldIndex - 1, newIndex - 1); Utils.moveArrayItem(_this.columns, oldIndex - 1, newIndex - 1); } else { Utils.moveArrayItem(_this.columns, oldIndex - 1, newIndex); Utils.moveArrayItem(_this.columns, oldIndex, newIndex + 1); } update = true; } else { if (oldIndex < newIndex) { newIndex--; } Utils.moveArrayItem(_this.columns, oldIndex, newIndex); update = oldIndex != newIndex; } return true; } return false; }); return update; }; /** * Перемещение бэнда * @param targetBand Бэнд, который перемещает пользователь * @param dropInfo Описание места и обстоятельств, куда перемещает. * @return Можно ли переместить */ ColumnCollection.prototype.reorderBand = function (targetBand, dropInfo) { var dropBand = dropInfo.item; var col = null; if (dropBand) { if (dropInfo.pos === 'right') { col = dropBand.columns[dropBand.columns.length - 1]; } else { col = dropBand.columns[0]; } } for (var j = 0; j < targetBand.columns.length; j++) { var bandCol = targetBand.columns[j]; this.reorderColumn(bandCol, { item: col, pos: dropInfo.pos, place: dropInfo.place }); if (dropInfo.pos === 'right') { col = bandCol; } } return true; }; return ColumnCollection; }()); export { ColumnCollection };