@true-directive/base
Version:
The set of base classes for the TrueDirective Grid
149 lines (148 loc) • 7.06 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
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;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
/**
* Copyright (c) 2018-2019 Aleksey Melnikov, True Directive Company.
* @link https://truedirective.com/
* @license MIT
*/
import { AxInject } from '../classes/ax-inject.class';
import { CheckedChangedEvent } from '../classes/events';
import { EditorShowMode, ColumnType } from '../classes/enums';
import { GridSettings } from '../classes/grid-settings.class';
import { Selection } from '../classes/selection.class';
import { DataSource } from '../classes/data-source.class';
import { ColumnCollection } from '../classes/column-collection.class';
import { LayoutsHandler } from './layouts.handler';
var GridCheckHandler = /** @class */ (function () {
function GridCheckHandler() {
}
Object.defineProperty(GridCheckHandler.prototype, "cc", {
get: function () {
return this.columnCollection;
},
enumerable: true,
configurable: true
});
Object.defineProperty(GridCheckHandler.prototype, "columns", {
get: function () {
return this.cc.columns;
},
enumerable: true,
configurable: true
});
/**
* Проверка возможности переключения чекбокса (ColumnType.CHECKBOX и
* ColumnType.BOOLEAN с возможностью изменения)
* @param cp Позиция ячейки
* @return Можно переключить или нельзя
*/
GridCheckHandler.prototype.canToggleCheck = function (cp) {
if (cp) {
// Определяем колонку
var col = this.cc.columnByFieldName(cp.fieldName);
var canEdit = this.settings.editorShowMode !== EditorShowMode.NONE && col.allowEdit;
if (col && (col.isCheckbox || (col.isBoolean && canEdit))) {
return true;
}
}
// Не. Нечего переключать
return false;
};
GridCheckHandler.prototype.setColumnCheck = function (col, value) {
col.setChecked(value);
this.dataSource.resultRows.forEach(function (r) { return r[col.fieldName] = value; });
this.events.checkedChangedEvent(new CheckedChangedEvent('column', null, col.fieldName, value));
// Всё же непонятно, почему поменялся запрос, если мы чекнули данные..
// Возможно потому, что нужно обновить всё.
// Перезагрузить данные, т.к. они обновились. В этом случае нам
// ленивая загрузка не нужна.
this.events.queryChangedEvent(this.dataSource.getQuery());
};
/**
* Обновление зависимых галок (чекбоксов) в гриде
* @param fieldName Поле чекбокса
*/
GridCheckHandler.prototype.updateCheckColumns = function (fieldName) {
var _this = this;
if (fieldName === void 0) { fieldName = null; }
this.columns.forEach(function (col) {
if (col.type === ColumnType.CHECKBOX && (fieldName === null || col.fieldName === fieldName)) {
var allChecked_1 = true;
var allNotChecked_1 = true;
_this.dataSource.resultRows.forEach(function (r) {
if (r[col.fieldName]) {
allNotChecked_1 = false;
}
else {
allChecked_1 = false;
}
});
if (_this.dataSource.resultRowCount === 0) {
// Если ни одной записи нет, то состояние "Выключено"
allChecked_1 = false;
}
col.setChecked((allChecked_1 && allNotChecked_1 || !allChecked_1 && !allNotChecked_1) ? null : allChecked_1);
}
});
};
/**
* Поиск первой колонки, в которой есть чекбокс.
* @param forEdit Поиск только редактируемых полей (dataType = ColumnType.BOOLEAN)
* @return Найденная колонка или null, если ничего не найдено
*/
GridCheckHandler.prototype.firstCheckableField = function (forEdit) {
if (forEdit === void 0) { forEdit = true; }
if (forEdit && this.selection.focusedCell) {
var col = this.cc.columnByFieldName(this.selection.focusedCell.fieldName);
if (col && col.type === ColumnType.BOOLEAN) {
return col.fieldName;
}
}
var res = null;
this.layoutsHandler.layouts.some(function (l) {
res = l.columns.find(function (col) { return col.isCheckbox; });
return res ? true : false;
});
return res ? res.fieldName : null;
};
GridCheckHandler.prototype.firstCheckboxField = function () {
return this.firstCheckableField(false);
};
GridCheckHandler.prototype.isRowChecked = function (r) {
var f = this.firstCheckboxField();
return f && r[f];
};
__decorate([
AxInject('events'),
__metadata("design:type", Object)
], GridCheckHandler.prototype, "events", void 0);
__decorate([
AxInject('dataSource'),
__metadata("design:type", DataSource)
], GridCheckHandler.prototype, "dataSource", void 0);
__decorate([
AxInject('settings'),
__metadata("design:type", GridSettings)
], GridCheckHandler.prototype, "settings", void 0);
__decorate([
AxInject('columns'),
__metadata("design:type", ColumnCollection)
], GridCheckHandler.prototype, "columnCollection", void 0);
__decorate([
AxInject('layouts'),
__metadata("design:type", LayoutsHandler)
], GridCheckHandler.prototype, "layoutsHandler", void 0);
__decorate([
AxInject('selection'),
__metadata("design:type", Selection)
], GridCheckHandler.prototype, "selection", void 0);
return GridCheckHandler;
}());
export { GridCheckHandler };