401 lines
12 KiB
JavaScript
401 lines
12 KiB
JavaScript
import { global } from "../../utility";
|
|
import { createElement } from "../../functions";
|
|
import { createIcon } from "../icon";
|
|
import { createCheckbox } from "../checkbox";
|
|
// import { setTooltip } from "../tooltip";
|
|
import { Dropdown } from "../dropdown";
|
|
import { convertCssStyle } from "../extension";
|
|
import { createDateInput, formatDate, setDateValue, getDateValue } from "../date";
|
|
|
|
/**
|
|
* 列定义基类
|
|
* @class
|
|
*/
|
|
export class GridColumn {
|
|
/**
|
|
* 创建显示单元格时调用的方法
|
|
* @param {GridColumnDefinition} col - 列定义对象
|
|
* @returns {HTMLElement} 返回创建的单元格元素
|
|
* @virtual
|
|
*/
|
|
static create() {
|
|
return createElement('span');
|
|
}
|
|
|
|
/**
|
|
* 创建编辑单元格时调用的方法
|
|
*
|
|
* 元素修改后设置行包装对象的 `__editing` 后,支持在离开编辑状态时及时触发 [leaveEdit]{@linkcode GridColumn.leaveEdit} 方法<br/>
|
|
* 更多例子参考代码中 {@linkcode GridDropdownColumn} 的实现。
|
|
* @param {Function} trigger - 编辑事件回调函数,`e` 参数会传递给 [getValue]{@linkcode GridColumn.getValue} 方法
|
|
* @param {GridColumnDefinition} col - 列定义对象
|
|
* @param {HTMLElement} container - 父容器元素
|
|
* @param {GridItemWrapper} vals - 行包装对象,其 `values` 属性为行数据对象
|
|
* @returns {HTMLElement} 返回创建的编辑状态的单元格元素
|
|
* @virtual
|
|
*/
|
|
static createEdit() { }
|
|
|
|
/**
|
|
* 创建列头时调用的方法
|
|
* @param {GridColumnDefinition} col - 列定义对象
|
|
* @returns {HTMLElement} 返回创建的列头元素
|
|
* @virtual
|
|
*/
|
|
static createCaption() { }
|
|
|
|
/**
|
|
* 设置单元格值时调用的方法
|
|
* @param {HTMLElement} element - 单元格元素
|
|
* @param {(string | boolean | number)} val - 待设置的单元格值
|
|
* @param {GridItemWrapper} vals - 行包装对象
|
|
* @param {GridColumnDefinition} col - 列定义对象
|
|
* @param {Grid} grid - Grid 对象
|
|
* @virtual
|
|
*/
|
|
static setValue(element, val) {
|
|
element.innerText = val;
|
|
}
|
|
|
|
/**
|
|
* 获取编辑状态单元格值时调用的方法
|
|
* @param {any} `e` 由 [createEdit]{@linkcode GridColumn.createEdit} 方法中 `trigger` 函数传递来的对象
|
|
* @param {GridColumnDefinition} col - 列定义对象
|
|
* @returns {(string | boolean | number)} 返回单元格的值
|
|
* @virtual
|
|
*/
|
|
static getValue() { }
|
|
|
|
/**
|
|
* 设置单元格样式时调用的方法
|
|
* @param {HTMLElement} element - 单元格元素
|
|
* @param {object} style - 样式对象
|
|
* @virtual
|
|
*/
|
|
static setStyle(element, style) {
|
|
// for (let css of Object.entries(style)) {
|
|
// element.style.setProperty(css[0], css[1]);
|
|
// }
|
|
element.style.cssText = convertCssStyle(style);
|
|
}
|
|
|
|
/**
|
|
* 设置单元格可用性时调用的方法
|
|
* @param {HTMLElement} element - 单元格元素
|
|
* @param {boolean} enabled - 启用值,为 `false` 时代表禁用
|
|
* @virtual
|
|
*/
|
|
static setEnabled(element, enabled) {
|
|
const tooltip = element.querySelector('.ui-tooltip-wrapper');
|
|
if (tooltip != null) {
|
|
tooltip.style.display = enabled === false ? 'none' : '';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 单元格离开编辑元素时触发,需要由行包装对象的 `__editing` 来确定是否触发。
|
|
* @param {HTMLElement} element - 单元格元素
|
|
* @param {HTMLElement} container - 父容器元素
|
|
* @virtual
|
|
*/
|
|
static leaveEdit() { }
|
|
|
|
static toString() { return '[object Column]' }
|
|
}
|
|
|
|
/**
|
|
* 单行文本列
|
|
* @extends GridColumn
|
|
*/
|
|
export class GridInputColumn extends GridColumn {
|
|
/**
|
|
* 设置该类型是否支持触发 [onInputEnded]{@linkcode GridColumnDefinition.onInputEnded} 方法<br/>
|
|
* 该属性返回 `true` 后,在任意事件中修改行包装对象的 `__editing` 值,则会在行列元素变动时及时触发 [onInputEnded]{@linkcode GridColumnDefinition.onInputEnded} 方法,避免例如文本框还未触发 `onchange` 事件就被移除元素而导致的问题<br/>
|
|
* 更多例子参考代码中 {@linkcode GridInputColumn} 的实现
|
|
* @readonly
|
|
* @type {boolean}
|
|
*/
|
|
static get editing() { return true };
|
|
|
|
static createEdit(trigger, col, _wrapper, vals) {
|
|
const input = createElement('input');
|
|
input.setAttribute('type', 'text');
|
|
if (typeof trigger === 'function') {
|
|
input.addEventListener('change', trigger);
|
|
}
|
|
input.addEventListener('input', () => {
|
|
if (vals.__editing == null) {
|
|
vals.__editing = {
|
|
[col.key]: true
|
|
}
|
|
} else {
|
|
vals.__editing[col.key] = true;
|
|
}
|
|
});
|
|
return input;
|
|
}
|
|
|
|
static setValue(element, val) {
|
|
if (element.tagName !== 'INPUT') {
|
|
super.setValue(element, val);
|
|
} else {
|
|
element.value = val;
|
|
}
|
|
}
|
|
|
|
static getValue(e) { return e.target.value }
|
|
|
|
static setEnabled(element, enabled) {
|
|
super.setEnabled(element, enabled);
|
|
element.disabled = enabled === false;
|
|
}
|
|
}
|
|
|
|
export class GridTextColumn extends GridInputColumn {
|
|
static createEdit(trigger, col, _wrapper, vals) {
|
|
const input = createElement('textarea');
|
|
if (typeof trigger === 'function') {
|
|
input.addEventListener('change', trigger);
|
|
}
|
|
input.addEventListener('input', () => {
|
|
if (vals.__editing == null) {
|
|
vals.__editing = {
|
|
[col.key]: true
|
|
}
|
|
} else {
|
|
vals.__editing[col.key] = true;
|
|
}
|
|
});
|
|
return input;
|
|
}
|
|
|
|
static setValue(element, val, _item, _col, grid) {
|
|
if (element.tagName !== 'TEXTAREA') {
|
|
super.setValue(element, val);
|
|
} else {
|
|
element.value = val;
|
|
if (val != null) {
|
|
const lines = String(val).split('\n').length;
|
|
element.style.height = `${lines * grid.lineHeight + 12}px`;
|
|
}
|
|
// TODO: bad performance
|
|
}
|
|
}
|
|
}
|
|
|
|
const SymbolDropdown = Symbol.for('ui-dropdown');
|
|
|
|
export class GridDropdownColumn extends GridColumn {
|
|
static createEdit(trigger, col, container, it) {
|
|
const drop = new Dropdown({
|
|
...col.dropOptions,
|
|
wrapper: container.parentElement
|
|
});
|
|
drop.onSelected = trigger;
|
|
drop.onExpanded = () => {
|
|
if (it.__editing == null) {
|
|
it.__editing = {
|
|
[col.key]: true
|
|
}
|
|
} else {
|
|
it.__editing[col.key] = true;
|
|
}
|
|
if (typeof col.onDropExpanded === 'function') {
|
|
col.onDropExpanded.call(col, it.values, drop);
|
|
}
|
|
};
|
|
return drop.create();
|
|
}
|
|
|
|
static _getDrop(element) {
|
|
const dropGlobal = global[SymbolDropdown];
|
|
if (dropGlobal == null) {
|
|
return null;
|
|
}
|
|
const dropId = element.dataset.dropId;
|
|
const drop = dropGlobal[dropId];
|
|
if (drop == null) {
|
|
return null;
|
|
}
|
|
return drop;
|
|
}
|
|
|
|
static _getSource(item, col) {
|
|
let source;
|
|
if (col.sourceCache !== false) {
|
|
source = item.source?.[col.key];
|
|
if (source != null) {
|
|
return source;
|
|
}
|
|
}
|
|
source = col.source;
|
|
if (typeof source === 'function') {
|
|
source = source(item.values);
|
|
}
|
|
if (col.sourceCache !== false) {
|
|
if (item.source == null) {
|
|
item.source = { [col.key]: source };
|
|
} else {
|
|
item.source[col.key] = source;
|
|
}
|
|
}
|
|
return source;
|
|
}
|
|
|
|
static _setValue(source, element, val, opts) {
|
|
const data = source?.find(v => v[opts?.valueKey ?? 'value'] === val);
|
|
if (data != null) {
|
|
val = data[opts?.textKey ?? 'text'];
|
|
}
|
|
super.setValue(element, val);
|
|
}
|
|
|
|
static setValue(element, val, item, col) {
|
|
if (element.tagName !== 'DIV') {
|
|
let source = this._getSource(item, col);
|
|
if (source instanceof Promise) {
|
|
source.then(s => this._setValue(s, element, val, col.dropOptions));
|
|
} else {
|
|
this._setValue(source, element, val, col.dropOptions);
|
|
}
|
|
return;
|
|
}
|
|
const drop = this._getDrop(element);
|
|
if (drop == null) {
|
|
return;
|
|
}
|
|
if (drop.source == null || drop.source.length === 0) {
|
|
let source = this._getSource(item, col);
|
|
if (source instanceof Promise) {
|
|
source.then(s => {
|
|
drop.source = s;
|
|
drop.select(val, true);
|
|
})
|
|
return;
|
|
} else if (source != null) {
|
|
drop.source = source;
|
|
}
|
|
}
|
|
drop.select(val, true);
|
|
}
|
|
|
|
static getValue(e, col) {
|
|
return e[col.dropOptions?.valueKey ?? 'value'];
|
|
}
|
|
|
|
static setEnabled(element, enabled) {
|
|
super.setEnabled(element, enabled);
|
|
const drop = this._getDrop(element);
|
|
if (drop == null) {
|
|
return;
|
|
}
|
|
drop.disabled = enabled === false;
|
|
}
|
|
|
|
static leaveEdit(element, container) {
|
|
container.parentElement.querySelectorAll('.ui-drop-box.active').forEach(e => {
|
|
if (e != null) {
|
|
e.classList.remove('active');
|
|
}
|
|
});
|
|
const drop = this._getDrop(element);
|
|
if (drop == null) {
|
|
return;
|
|
}
|
|
if (drop?.multiSelect && typeof drop.onCollapsed === 'function') {
|
|
drop.onCollapsed();
|
|
}
|
|
}
|
|
}
|
|
|
|
export class GridCheckboxColumn extends GridColumn {
|
|
static createEdit(trigger) {
|
|
const check = createCheckbox({
|
|
onchange: typeof trigger === 'function' ? trigger : null
|
|
});
|
|
return check;
|
|
}
|
|
|
|
static setValue(element, val) {
|
|
element.querySelector('input').checked = val;
|
|
}
|
|
|
|
static getValue(e) { return e.target.checked }
|
|
|
|
static setEnabled(element, enabled) {
|
|
super.setEnabled(element, enabled);
|
|
element.querySelector('input').disabled = enabled === false;
|
|
}
|
|
}
|
|
|
|
export class GridIconColumn extends GridColumn {
|
|
static create() { return createElement('span', 'col-icon') }
|
|
|
|
static setValue(element, val, item, col) {
|
|
let className = col.iconClassName;
|
|
if (typeof className === 'function') {
|
|
className = className.call(col, item.values);
|
|
}
|
|
if (className == null) {
|
|
element.className = 'col-icon';
|
|
} else {
|
|
element.className = `col-icon ${className}`;
|
|
}
|
|
let type = col.iconType;
|
|
if (typeof type === 'function') {
|
|
type = type.call(col, item.values);
|
|
}
|
|
type ??= 'fa-light';
|
|
if (element.dataset.type !== type || element.dataset.icon !== val) {
|
|
const icon = createIcon(type, val);
|
|
// const layer = element.children[0];
|
|
element.replaceChildren(icon);
|
|
// !nullOrEmpty(col.tooltip) && setTooltip(element, col.tooltip, false, grid.element);
|
|
element.dataset.type = type;
|
|
element.dataset.icon = val;
|
|
}
|
|
}
|
|
|
|
static setEnabled(element, enabled) {
|
|
super.setEnabled(element, enabled);
|
|
if (enabled === false) {
|
|
element.classList.add('disabled');
|
|
} else {
|
|
element.classList.remove('disabled');
|
|
}
|
|
}
|
|
}
|
|
|
|
export class GridDateColumn extends GridColumn {
|
|
static createEdit(trigger, col, _container, vals) {
|
|
let enabled = col.enabled;
|
|
if (typeof enabled === 'string') {
|
|
enabled = vals.values[enabled];
|
|
} else if (typeof enabled === 'function') {
|
|
enabled = col.enabled(vals.values);
|
|
}
|
|
if (enabled === false) {
|
|
return super.create();
|
|
}
|
|
const date = createDateInput(col.dateMin, col.dateMax);
|
|
// date.addEventListener('change', trigger);
|
|
date.addEventListener('blur', trigger);
|
|
return date;
|
|
}
|
|
|
|
static setValue(element, val) {
|
|
setDateValue(element, val);
|
|
}
|
|
|
|
static getValue(e, col) {
|
|
return getDateValue(e.target, col.dateValueFormatter);
|
|
}
|
|
|
|
static setEnabled(element, enabled) {
|
|
element.disabled = enabled === false;
|
|
}
|
|
|
|
static formatDate(date) {
|
|
return formatDate(date);
|
|
}
|
|
} |