1035 lines
38 KiB
JavaScript
1035 lines
38 KiB
JavaScript
import './css/dropdown.scss';
|
|
import { r as lang } from "../utility/lgres";
|
|
import { contains, nullOrEmpty } from "../utility/strings";
|
|
import { global, isPositive, throttle } from "../utility";
|
|
import { createElement } from "../functions";
|
|
import { createCheckbox } from "./checkbox";
|
|
import { createIcon } from "./icon"
|
|
|
|
const SymbolDropdown = Symbol.for('ui-dropdown');
|
|
const DropdownItemHeight = 30;
|
|
|
|
let r = lang;
|
|
let dropdownGlobal = global[SymbolDropdown];
|
|
|
|
if (dropdownGlobal == null) {
|
|
// init
|
|
dropdownGlobal = {};
|
|
Object.defineProperty(dropdownGlobal, 'clear', {
|
|
writable: false,
|
|
configurable: false,
|
|
enumerable: false,
|
|
value: function () {
|
|
const panels = document.querySelectorAll('.ui-drop-box.active');
|
|
for (let panel of [...panels]) {
|
|
if (panel == null) {
|
|
continue;
|
|
}
|
|
panel.classList.remove('active');
|
|
const dropId = panel.parentElement?.dataset.dropId;
|
|
if (dropId == null) {
|
|
continue;
|
|
}
|
|
/** @type {Dropdown | undefined} */
|
|
const dropdown = this[dropId];
|
|
if (dropdown?.multiSelect && typeof dropdown.onCollapsed === 'function') {
|
|
dropdown.onCollapsed();
|
|
}
|
|
}
|
|
}
|
|
})
|
|
global[SymbolDropdown] = dropdownGlobal;
|
|
|
|
document.addEventListener('mousedown', e => {
|
|
let parent = e.target;
|
|
while (parent instanceof Element) {
|
|
if (parent.classList.contains('ui-drop-box')) {
|
|
e.stopPropagation();
|
|
return;
|
|
}
|
|
parent = parent.parentElement;
|
|
}
|
|
dropdownGlobal.clear();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @private
|
|
* @this Dropdown
|
|
* @param {DropdownItem[]} itemlist
|
|
*/
|
|
function selectItems(itemlist) {
|
|
const label = this._var.label;
|
|
if (label == null) {
|
|
return;
|
|
}
|
|
const template = this._var.options.htmlTemplate;
|
|
const htmlkey = this._var.options.htmlKey;
|
|
const textkey = this._var.options.textKey;
|
|
let htmls;
|
|
if (typeof template === 'function') {
|
|
htmls = itemlist.map(it => template.call(this, it));
|
|
} else {
|
|
htmls = itemlist.map(it => it[htmlkey]);
|
|
}
|
|
if (htmls.some(it => it instanceof HTMLElement)) {
|
|
label.replaceChildren(...htmls.filter(it => it != null).map(it => it.cloneNode(true)));
|
|
} else {
|
|
let text = itemlist.map(it => String(it[textkey])).join(', ');
|
|
if (nullOrEmpty(text)) {
|
|
text = r('noneItem', '( None )');
|
|
}
|
|
label.innerText = text;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @private
|
|
* @param {string[] | undefined} searchkeys
|
|
* @param {string} textkey
|
|
* @param {string} key
|
|
* @param {DropdownItem[]} source
|
|
*/
|
|
function filterSource(searchkeys, textkey, key, source) {
|
|
if (!Array.isArray(searchkeys) || searchkeys.length === 0) {
|
|
searchkeys = [textkey];
|
|
}
|
|
if (key.length > 0) {
|
|
source = source.filter(it => {
|
|
for (let k of searchkeys) {
|
|
if (contains(it[k].toLowerCase(), key)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
});
|
|
}
|
|
return source;
|
|
}
|
|
|
|
/**
|
|
* @private
|
|
* @param {DropdownItem} it
|
|
* @param {string} valuekey
|
|
* @param {string} textkey
|
|
*/
|
|
function getValue(it, valuekey, textkey) {
|
|
if (it == null) {
|
|
return null;
|
|
}
|
|
const value = it[valuekey];
|
|
if (value == null || value === '') {
|
|
return it[textkey];
|
|
}
|
|
return value;
|
|
}
|
|
|
|
/**
|
|
* 下拉列表参数对象
|
|
* @typedef DropdownOptions
|
|
* @property {string} textKey 文本关键字
|
|
* @property {string} valueKey 值关键字
|
|
* @property {string} htmlKey 源码显示的关键字
|
|
* @property {string} childKey 子集合的关键字
|
|
* @property {number} maxLength 最大输入长度
|
|
* @property {(this: Dropdown, it: DropdownItem) => string} [htmlTemplate] 模板创建函数
|
|
* @property {boolean} [multiSelect] 是否允许多选
|
|
* @property {string} [selected] 选中值
|
|
* @property {string[]} [selectedList] 选中的数组
|
|
* @property {boolean} [disabled] 是否禁用
|
|
* @property {boolean} [input] 是否支持输入
|
|
* @property {boolean} [search] 是否支持搜索
|
|
* @property {string[]} [searchKeys] 搜索的关键字数组
|
|
* @property {string} [searchPlaceholder] 搜索提示文本,默认值取语言资源 `searchHolder` "Search..."
|
|
* @property {number} [tabIndex] 焦点索引
|
|
* @property {string} [placeholder] 输入框的提示文本
|
|
* @property {boolean} [slideFixed] 是否固定为向下展开
|
|
* @property {HTMLElement} [wrapper] 父元素,默认添加到头元素之后
|
|
* @property {boolean} [ignoreAll] 是否忽略 All 选项
|
|
* @property {import('../types').GetTextCallback} [getText] 获取文本的代理函数
|
|
*/
|
|
|
|
/**
|
|
* 数据项基础类型
|
|
* @typedef DropdownItemBase
|
|
* @property {0 | 1 | 2 | undefined} [__checked] 是否选中
|
|
* @property {string} [__p] 父节点 Id
|
|
* @property {number} [__level] 层级
|
|
* @interface
|
|
*/
|
|
|
|
/**
|
|
* 数据项类型
|
|
* @typedef {DropdownItemBase & Object.<string, any>} DropdownItem
|
|
*/
|
|
|
|
export class Dropdown {
|
|
_var = {
|
|
/** @type {DropdownOptions} */
|
|
options: {
|
|
textKey: 'text',
|
|
valueKey: 'value',
|
|
htmlKey: 'html',
|
|
childKey: 'children',
|
|
maxLength: 500
|
|
},
|
|
/** @type {HTMLDivElement | undefined} */
|
|
wrapper: undefined,
|
|
/** @type {HTMLDivElement | undefined} */
|
|
container: undefined,
|
|
/** @type {HTMLInputElement | HTMLLabelElement | undefined} */
|
|
label: undefined,
|
|
allChecked: false,
|
|
/** @type {DropdownItem[] | undefined} */
|
|
source: undefined,
|
|
/** @type {DropdownItem[] | undefined} */
|
|
currentSource: undefined,
|
|
/** @type {string | undefined} */
|
|
lastSelected: undefined,
|
|
/** @type {DropdownItem | undefined} */
|
|
selected: undefined,
|
|
/** @type {DropdownItem[] | undefined} */
|
|
selectedList: undefined,
|
|
dropTop: 0,
|
|
dropHeight: 0
|
|
};
|
|
|
|
/** @type {(() => DropdownItem[]) | undefined} */
|
|
sourceFilter;
|
|
/** @type {((list: DropdownItem[]) => void) | undefined} */
|
|
onSelectedList;
|
|
/** @type {((it: DropdownItem | undefined) => void) | undefined} */
|
|
onSelected;
|
|
/** @type {((this: Dropdown) => void) | undefined} */
|
|
onExpanded;
|
|
/** @type {((this: Dropdown) => void) | undefined} */
|
|
onCollapsed;
|
|
|
|
/**
|
|
* @param {DropdownOptions} [options]
|
|
*/
|
|
constructor(options) {
|
|
if (options != null) {
|
|
this._var.options = options;
|
|
}
|
|
const opts = this._var.options;
|
|
opts.textKey ??= 'text';
|
|
opts.valueKey ??= 'value';
|
|
opts.htmlKey ??= 'html';
|
|
opts.childKey ??= 'children';
|
|
opts.maxLength ??= 500;
|
|
const getText = opts.getText;
|
|
if (typeof getText === 'function') {
|
|
r = getText;
|
|
} else if (typeof global.GetTextByKey === 'function') {
|
|
r = global.GetTextByKey;
|
|
}
|
|
opts.searchPlaceholder ??= r('searchHolder', 'Search...');
|
|
}
|
|
|
|
create() {
|
|
const options = this._var.options;
|
|
|
|
// wrapper
|
|
const wrapper = createElement('div', 'ui-drop-wrapper');
|
|
const dropId = String(Math.random()).substring(2);
|
|
if (options.wrapper instanceof HTMLElement) {
|
|
options.wrapper.dataset.dropId = dropId;
|
|
}
|
|
wrapper.dataset.dropId = dropId;
|
|
dropdownGlobal[dropId] = this;
|
|
this._var.wrapper = wrapper;
|
|
|
|
// header
|
|
const header = createElement('div', 'ui-drop-header');
|
|
header.addEventListener('keypress', e => {
|
|
if (e.key === ' ' || e.key === 'Enter') {
|
|
header.dispatchEvent(new MouseEvent('click'));
|
|
}
|
|
});
|
|
header.addEventListener('keydown', e => {
|
|
const up = e.key === 'ArrowUp';
|
|
const down = e.key === 'ArrowDown';
|
|
if (up || down) {
|
|
const source = this.source;
|
|
const count = source.length;
|
|
const valuekey = this._var.options.valueKey;
|
|
const textkey = this._var.options.textKey;
|
|
let index = this._var.selected == null ? -1 : source?.indexOf(this._var.selected);
|
|
if (isNaN(index) || index < -1) {
|
|
index = -1;
|
|
} else if (index >= count) {
|
|
index = count - 1;
|
|
}
|
|
if (up) {
|
|
if (index > 0) {
|
|
index--;
|
|
} else {
|
|
index = 0;
|
|
}
|
|
} else if (down) {
|
|
if (index < 0) {
|
|
index = 0;
|
|
} else if (index < count) {
|
|
index++;
|
|
} else {
|
|
index = count - 1;
|
|
}
|
|
}
|
|
const target = getValue(source[index], valuekey, textkey);
|
|
if (target != null) {
|
|
this.select(target);
|
|
}
|
|
} else if (e.key === 'Tab') {
|
|
this._dropdown(false);
|
|
}
|
|
});
|
|
header.addEventListener('click', () => {
|
|
if (this.disabled) {
|
|
return;
|
|
}
|
|
const active = this._expanded;
|
|
const label = this._var.label;
|
|
if (active && label?.ownerDocument?.activeElement === label) {
|
|
return;
|
|
}
|
|
this._dropdown(!active);
|
|
if (!active && typeof this.onExpanded === 'function') {
|
|
setTimeout(this.onExpanded.bind(this), 120);
|
|
}
|
|
});
|
|
|
|
// label or input
|
|
let label;
|
|
if (options.input) {
|
|
label = createElement('input', 'ui-drop-text');
|
|
label.type = 'text';
|
|
label.autocomplete = 'off';
|
|
label.draggable = false;
|
|
options.placeholder && label.setAttribute('placeholder', options.placeholder);
|
|
isPositive(options.maxLength) && label.setAttribute('maxlength', String(options.maxLength));
|
|
isPositive(options.tabIndex) && label.setAttribute('tabindex', String(options.tabIndex));
|
|
label.addEventListener('input', e => {
|
|
if (!(e.target instanceof HTMLInputElement)) {
|
|
return;
|
|
}
|
|
const key = e.target.value.toLowerCase();
|
|
const source = filterSource(options.searchKeys, options.textKey, key, this.source);
|
|
this._filllist(source);
|
|
this._var.container?.classList?.add('active');
|
|
});
|
|
label.addEventListener('blur', e => e.target instanceof HTMLInputElement && this.select(e.target.value));
|
|
label.addEventListener('mousedown', e => this._expanded && e.stopPropagation());
|
|
} else {
|
|
isPositive(options.tabIndex) && header.setAttribute('tabindex', String(options.tabIndex));
|
|
label = createElement('label', 'ui-drop-text');
|
|
}
|
|
this._var.label = label;
|
|
if (options.multiSelect) {
|
|
if (Array.isArray(options.selectedList)) {
|
|
this.selectlist(options.selectedList, true);
|
|
} else {
|
|
this._var.allChecked = true;
|
|
label.innerText = r('allItem', '( All )');
|
|
}
|
|
} else if (options.selected != null) {
|
|
this.select(options.selected, true);
|
|
}
|
|
header.append(label, createElement('label', 'ui-drop-caret'));
|
|
wrapper.appendChild(header);
|
|
|
|
this.disabled = options.disabled || false;
|
|
return wrapper;
|
|
}
|
|
|
|
get multiSelect() { return this._var.options.multiSelect }
|
|
|
|
get ignoreAll() { return this._var.options.ignoreAll }
|
|
|
|
get disabled() { return this._var.wrapper == null || this._var.wrapper.querySelector('.ui-drop-header.disabled') != null }
|
|
|
|
set disabled(flag) {
|
|
if (this._var.wrapper == null) {
|
|
return;
|
|
}
|
|
const header = this._var.wrapper.querySelector('.ui-drop-header');
|
|
if (header != null) {
|
|
if (flag) {
|
|
header.classList.add('disabled');
|
|
} else {
|
|
header.classList.remove('disabled');
|
|
}
|
|
}
|
|
/** @type {HTMLInputElement | null} */
|
|
const input = this._var.wrapper.querySelector('.ui-drop-text');
|
|
if (input != null) {
|
|
input.disabled = flag;
|
|
}
|
|
}
|
|
|
|
get source() {
|
|
let source = this._var.source;
|
|
if (source == null || !Array.isArray(source)) {
|
|
if (typeof this.sourceFilter === 'function') {
|
|
source = this.sourceFilter();
|
|
}
|
|
if (!Array.isArray(source)) {
|
|
source = [];
|
|
}
|
|
this._var.source = source;
|
|
}
|
|
return source;
|
|
}
|
|
|
|
set source(list) {
|
|
if (!Array.isArray(list)) {
|
|
return;
|
|
}
|
|
const valuekey = this._var.options.valueKey;
|
|
const childkey = this._var.options.childKey;
|
|
/**
|
|
* @private
|
|
* @param {DropdownItem[]} list
|
|
* @param {string} [id]
|
|
* @param {number} [level]
|
|
* @returns {DropdownItem[]}
|
|
*/
|
|
function reduceItems(list, id, level = 0) {
|
|
if (!Array.isArray(list)) {
|
|
return [];
|
|
}
|
|
return list.reduce((array, item) => [
|
|
...array,
|
|
(id == null ? item : { __p: String(id), __level: level, ...item }),
|
|
...reduceItems(item[childkey], item[valuekey], level + 1)
|
|
], []);
|
|
}
|
|
this._var.source = reduceItems(list);
|
|
if (this._expanded) {
|
|
setTimeout(() => this._dropdown(), 120);
|
|
}
|
|
}
|
|
|
|
get selected() { return this._var.selected }
|
|
|
|
get selectedList() { return this._var.selectedList || [] }
|
|
|
|
/**
|
|
*
|
|
* @param {string} [selected]
|
|
* @param {boolean} [silence]
|
|
* @param {boolean} [ignoreCase]
|
|
* @returns {boolean | undefined}
|
|
*/
|
|
select(selected, silence, ignoreCase) {
|
|
if (typeof selected !== 'string') {
|
|
selected = String(selected);
|
|
}
|
|
if (ignoreCase) {
|
|
selected = selected.toLowerCase();
|
|
}
|
|
if (this._var.lastSelected === selected) {
|
|
return;
|
|
}
|
|
this._var.lastSelected = selected;
|
|
const valuekey = this._var.options.valueKey;
|
|
const textkey = this._var.options.textKey;
|
|
const template = this._var.options.htmlTemplate;
|
|
const htmlkey = this._var.options.htmlKey;
|
|
let item = this.source.find(it => (ignoreCase ? String(getValue(it, valuekey, textkey)).toLowerCase() : String(getValue(it, valuekey, textkey))) === selected);
|
|
const label = this._var.label;
|
|
if (label != null) {
|
|
if (label instanceof HTMLInputElement) {
|
|
if (item == null) {
|
|
item = { [valuekey]: selected };
|
|
}
|
|
label.value = selected;
|
|
} else {
|
|
const expanded = this._expanded;
|
|
if (expanded) {
|
|
this._var.container?.querySelectorAll('li[data-value].selected')?.forEach(li => li.classList.remove('selected'));
|
|
}
|
|
if (item == null) {
|
|
delete this._var.selected;
|
|
label.innerText = ' ';
|
|
return false;
|
|
}
|
|
let html;
|
|
if (typeof template === 'function') {
|
|
html = template.call(this, item);
|
|
} else {
|
|
html = item[htmlkey];
|
|
}
|
|
if (html instanceof HTMLElement) {
|
|
label.replaceChildren(html.cloneNode(true));
|
|
} else if (typeof html === 'string') {
|
|
label.innerHTML = html;
|
|
} else {
|
|
let text = item[textkey];
|
|
if (nullOrEmpty(text)) {
|
|
text = ' ';
|
|
}
|
|
label.innerText = text;
|
|
}
|
|
if (expanded) {
|
|
const lis = this._var.container?.querySelectorAll('li[data-value]');
|
|
if (lis != null) {
|
|
for (let li of lis) {
|
|
if (!(li instanceof HTMLLIElement)) {
|
|
continue;
|
|
}
|
|
if ((ignoreCase ? li.dataset.value?.toLowerCase() : li.dataset.value) === selected) {
|
|
li.classList.add('selected');
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
// const val = selected.replace(/"/g, '\\"');
|
|
// const li = this._var.container.querySelector(`li[data-value="${val}"]`);
|
|
// if (li != null) {
|
|
// li.classList.add('selected');
|
|
// }
|
|
}
|
|
}
|
|
}
|
|
this._var.selected = item;
|
|
if (!silence && typeof this.onSelected === 'function') {
|
|
this.onSelected(item);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {string[]} selectedlist
|
|
* @param {boolean} silence
|
|
* @returns {boolean | undefined}
|
|
*/
|
|
selectlist(selectedlist, silence) {
|
|
const source = this.source;
|
|
const valuekey = this._var.options.valueKey;
|
|
const textkey = this._var.options.textKey;
|
|
const itemlist = selectedlist.map(a => {
|
|
const v = typeof a === 'string' ? a : String(a);
|
|
let item = source.find(it => String(getValue(it, valuekey, textkey)) === v);
|
|
if (item == null) {
|
|
item = {
|
|
[valuekey]: v,
|
|
[textkey]: v
|
|
};
|
|
}
|
|
return item;
|
|
});
|
|
this._var.allChecked = false;
|
|
source.forEach(it => delete it.__checked);
|
|
if (itemlist.length === 0) {
|
|
delete this._var.selectedList;
|
|
if (this._var.label != null) {
|
|
this._var.label.innerText = r('none', '( None )');
|
|
}
|
|
return false;
|
|
}
|
|
this._selectList(itemlist, silence);
|
|
}
|
|
|
|
get _expanded() { return this._var.container?.classList?.contains('active') }
|
|
|
|
_dropdown(flag = true) {
|
|
const options = this._var.options;
|
|
let panel = this._var.container;
|
|
if (panel == null) {
|
|
panel = createElement('div', 'ui-drop-box');
|
|
// search box
|
|
if (!options.input && options.search) {
|
|
const search = createElement('div', 'ui-drop-search');
|
|
const input = createElement('input');
|
|
input.type = 'text';
|
|
input.className = 'ui-input';
|
|
isPositive(options.tabIndex) && input.setAttribute('tabindex', String(options.tabIndex));
|
|
!nullOrEmpty(options.searchPlaceholder) && input.setAttribute('placeholder', options.searchPlaceholder);
|
|
input.addEventListener('input', e => {
|
|
if (!(e.target instanceof HTMLInputElement)) {
|
|
return;
|
|
}
|
|
const key = e.target.value.toLowerCase();
|
|
const source = filterSource(options.searchKeys, options.textKey, key, this.source);
|
|
this._filllist(source);
|
|
});
|
|
input.addEventListener('blur', e => {
|
|
if (!(e.target instanceof HTMLInputElement)) {
|
|
return;
|
|
}
|
|
const key = e.target.value.toLowerCase();
|
|
if (!nullOrEmpty(key)) {
|
|
const list = this._getSelectedList(this._var.currentSource);
|
|
this._selectList(list);
|
|
}
|
|
});
|
|
search.append(input, createIcon('fa-light', 'search'));
|
|
panel.appendChild(search);
|
|
}
|
|
// list
|
|
const list = createElement('div', 'ui-drop-list');
|
|
list.addEventListener('scroll', e => throttle(this._onlistscroll, 10, this, list, e.target?.scrollTop), { passive: true });
|
|
if (!this.multiSelect) {
|
|
list.addEventListener('click', e => {
|
|
let li = e.target;
|
|
while (li instanceof Element && li.tagName !== 'LI') {
|
|
li = li.parentElement;
|
|
if (li == null) {
|
|
return;
|
|
}
|
|
}
|
|
if (li instanceof HTMLElement) {
|
|
const value = li.dataset.value;
|
|
if (this.select(value) !== false) {
|
|
dropdownGlobal.clear();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
panel.appendChild(list);
|
|
this._var.container = panel;
|
|
if (options.wrapper instanceof HTMLElement) {
|
|
options.wrapper.appendChild(panel);
|
|
} else {
|
|
this._var.wrapper?.appendChild(panel);
|
|
}
|
|
}
|
|
if (flag) {
|
|
let source = this.source;
|
|
if (!options.input && options.search) {
|
|
const search = panel.querySelector('.ui-drop-search > input');
|
|
if (search instanceof HTMLInputElement && !nullOrEmpty(search?.value)) {
|
|
source = filterSource(options.searchKeys, options.textKey, search.value, source);
|
|
}
|
|
}
|
|
this._filllist(source);
|
|
// slide direction
|
|
if (!options.slideFixed) {
|
|
const parent = options.wrapper ?? document.body;
|
|
/** @type {HTMLElement | null | undefined} */
|
|
let p = this._var.wrapper;
|
|
if (p != null) {
|
|
panel.style.minWidth = `${p.offsetWidth}px`;
|
|
const headerHeight = p.offsetHeight;
|
|
let top = p.offsetTop + headerHeight;
|
|
let left = p.offsetLeft;
|
|
if (p !== parent) {
|
|
while ((p = p.parentElement) != null && p !== parent) {
|
|
top -= p.scrollTop;
|
|
left -= p.scrollLeft;
|
|
}
|
|
}
|
|
p = this._var.wrapper;
|
|
if (p !== parent) {
|
|
while ((p = /** @type {HTMLElement} */(p?.offsetParent)) != null && p !== parent) {
|
|
top += p.offsetTop;
|
|
left += p.offsetLeft;
|
|
}
|
|
}
|
|
const slideUp = top - parent.scrollTop + panel.offsetHeight >= parent.offsetHeight;
|
|
if (options.wrapper instanceof HTMLElement) {
|
|
if (slideUp) {
|
|
panel.style.top = '';
|
|
panel.style.bottom = `${parent.offsetHeight - top + headerHeight - 4}px`;
|
|
} else {
|
|
panel.style.top = `${top}px`;
|
|
panel.style.bottom = '';
|
|
}
|
|
panel.style.left = `${left}px`;
|
|
}
|
|
if (slideUp) {
|
|
panel.classList.add('slide-up');
|
|
} else {
|
|
panel.classList.remove('slide-up');
|
|
}
|
|
}
|
|
}
|
|
panel.classList.add('active');
|
|
this._var.dropTop = 0;
|
|
panel.querySelector('.ui-drop-list')?.dispatchEvent(new Event('scroll'));
|
|
} else {
|
|
panel.classList.remove('active');
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {HTMLDivElement} list
|
|
* @param {number} top
|
|
*/
|
|
_onlistscroll(list, top) {
|
|
const offset = (this.multiSelect && !this.ignoreAll) ? DropdownItemHeight : 0;
|
|
top -= (top % (DropdownItemHeight * 2)) + offset;
|
|
if (top < 0) {
|
|
top = 0;
|
|
} else {
|
|
let bottomTop = this._var.dropHeight - (20 * DropdownItemHeight);
|
|
if (bottomTop < 0) {
|
|
bottomTop = 0;
|
|
}
|
|
if (top > bottomTop) {
|
|
top = bottomTop;
|
|
}
|
|
}
|
|
if (this._var.dropTop !== top) {
|
|
this._var.dropTop = top;
|
|
const startIndex = top / DropdownItemHeight;
|
|
let array = this._var.currentSource;
|
|
if (array != null) {
|
|
if (startIndex + 20 < array.length) {
|
|
array = array.slice(startIndex, startIndex + 20);
|
|
} else {
|
|
array = array.slice(-20);
|
|
}
|
|
/** @type {HTMLDivElement | null} */
|
|
const content = list.querySelector('.drop-content');
|
|
if (content != null) {
|
|
content.replaceChildren();
|
|
this._dofilllist(content, array);
|
|
content.style.top = `${top + offset}px`;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {DropdownItem[]} source
|
|
*/
|
|
_filllist(source) {
|
|
const list = this._var.container?.querySelector('.ui-drop-list');
|
|
if (list == null) {
|
|
return;
|
|
}
|
|
list.replaceChildren();
|
|
const height = source.length * DropdownItemHeight;
|
|
this._var.dropHeight = height;
|
|
this._var.currentSource = source;
|
|
const holder = createElement('div', 'drop-holder');
|
|
holder.style.height = `${height}px`;
|
|
const content = createElement('div', 'drop-content');
|
|
if (this.multiSelect && !this.ignoreAll) {
|
|
list.appendChild(
|
|
createElement('li', undefined,
|
|
createCheckbox({
|
|
label: r('allItem', '( All )'),
|
|
checked: this._var.allChecked,
|
|
customAttributes: { 'isall': '1' },
|
|
onchange: e => this._triggerselect(/** @type {HTMLInputElement} */(e.target))
|
|
})
|
|
)
|
|
);
|
|
content.style.top = `${DropdownItemHeight}px`;
|
|
} else {
|
|
content.style.top = '0px';
|
|
}
|
|
const multiselect = this.multiSelect;
|
|
const valuekey = this._var.options.valueKey;
|
|
const textkey = this._var.options.textKey;
|
|
const childkey = this._var.options.childKey;
|
|
const allchecked = this._var.allChecked;
|
|
const selectedlist = this.selectedList;
|
|
source.forEach((item, i) => {
|
|
const val = String(getValue(item, valuekey, textkey));
|
|
if (multiselect) {
|
|
const selected = selectedlist.some(s => String(getValue(s, valuekey, textkey)) === val);
|
|
if (allchecked || selected) {
|
|
item.__checked = 1;
|
|
source.filter(it => it.__p === val).forEach(it => it.__checked = 1);
|
|
} else {
|
|
const indeterminate = selectedlist.some(s => this._contains(String(getValue(s, valuekey, textkey)), item, valuekey, textkey, childkey));
|
|
if (indeterminate) {
|
|
item.__checked = 2;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
if (source.length > 20) {
|
|
source = source.slice(0, 20);
|
|
}
|
|
const scrolled = this._dofilllist(content, source);
|
|
list.append(holder, content);
|
|
if (scrolled != null) {
|
|
setTimeout(() => list.scrollTop = scrolled, 10);
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {string} it
|
|
* @param {DropdownItem} item
|
|
* @param {string} valuekey
|
|
* @param {string} textkey
|
|
* @param {string} [childkey]
|
|
* @returns {boolean}
|
|
*/
|
|
_contains(it, item, valuekey, textkey, childkey) {
|
|
if (childkey == null) {
|
|
return false;
|
|
}
|
|
if (Array.isArray(item[childkey])) {
|
|
for (let t of item[childkey]) {
|
|
if (it === String(getValue(t, valuekey, textkey))) {
|
|
return true;
|
|
}
|
|
if (this._contains(it, t, valuekey, textkey)) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {HTMLDivElement} content
|
|
* @param {DropdownItem[]} array
|
|
* @returns {number | undefined}
|
|
*/
|
|
_dofilllist(content, array) {
|
|
const multiselect = this.multiSelect;
|
|
const valuekey = this._var.options.valueKey;
|
|
const textkey = this._var.options.textKey;
|
|
const template = this._var.options.htmlTemplate;
|
|
const htmlkey = this._var.options.htmlKey;
|
|
// const childkey = this._var.options.childKey;
|
|
const selected = this.selected;
|
|
let scrolled;
|
|
array.forEach((item, i) => {
|
|
const val = String(getValue(item, valuekey, textkey));
|
|
const li = createElement('li');
|
|
li.dataset.value = val;
|
|
li.title = item[textkey];
|
|
if (isPositive(item.__level)) {
|
|
li.style.marginLeft = `${item.__level * 24}px`;
|
|
}
|
|
const wrapper = createElement('span', 'li-wrapper',
|
|
createElement('span', span => {
|
|
// events
|
|
span.className = 'ui-expandor';
|
|
// if (Array.isArray(item[childkey]) && item[childkey].length > 0) {
|
|
// span.classList.add('active');
|
|
// function hideChildren(children) {
|
|
// for (let c of children) {
|
|
// c.__visible = false;
|
|
// if (Array.isArray(c[childkey])) {
|
|
// hideChildren(c[childkey]);
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
},
|
|
createIcon('fa-solid', 'caret-down')
|
|
)
|
|
);
|
|
li.appendChild(wrapper);
|
|
let label;
|
|
let html;
|
|
if (typeof template === 'function') {
|
|
html = template.call(this, item);
|
|
} else {
|
|
html = item[htmlkey];
|
|
}
|
|
if (html instanceof HTMLElement) {
|
|
label = html;
|
|
} else if (typeof html === 'string') {
|
|
label = createElement('span');
|
|
label.innerHTML = html;
|
|
}
|
|
if (multiselect) {
|
|
if (label == null) {
|
|
label = createElement('span');
|
|
label.innerText = item[textkey];
|
|
}
|
|
const box = createCheckbox({
|
|
label,
|
|
checked: item.__checked === 1,
|
|
indeterminate: item.__checked === 2,
|
|
customAttributes: {
|
|
'class': 'dataitem',
|
|
'data-value': val
|
|
},
|
|
onchange: e => this._triggerselect(/** @type {HTMLInputElement} */(e.target), item)
|
|
});
|
|
wrapper.appendChild(box);
|
|
} else {
|
|
if (label == null) {
|
|
label = createElement('span');
|
|
label.innerText = item[textkey];
|
|
}
|
|
wrapper.appendChild(label);
|
|
if (selected != null && String(selected[valuekey]) === val) {
|
|
scrolled = DropdownItemHeight * i;
|
|
li.classList.add('selected');
|
|
}
|
|
}
|
|
content.appendChild(li);
|
|
});
|
|
return scrolled;
|
|
}
|
|
|
|
/**
|
|
* @param {DropdownItem[]} [source]
|
|
* @returns {DropdownItem[]}
|
|
*/
|
|
_getSelectedList(source) {
|
|
const valuekey = this._var.options.valueKey;
|
|
const textkey = this._var.options.textKey;
|
|
/** @type {DropdownItem[]} */
|
|
const list = [];
|
|
source ??= this.source;
|
|
source.filter(it => it.__p == null).forEach(it => {
|
|
const key = String(getValue(it, valuekey, textkey));
|
|
if (it.__checked === 1) {
|
|
list.push(it);
|
|
} else if (it.__checked === 2) {
|
|
const children = source.filter(c => c.__checked === 1 && c.__p === key);
|
|
if (children.length > 0) {
|
|
Array.prototype.push.apply(list, children);
|
|
}
|
|
}
|
|
});
|
|
return list;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {HTMLInputElement} checkbox
|
|
* @param {DropdownItem} [item]
|
|
*/
|
|
_triggerselect(checkbox, item) {
|
|
/** @type {DropdownItem[]} */
|
|
let list;
|
|
const valuekey = this._var.options.valueKey;
|
|
const textkey = this._var.options.textKey;
|
|
const childkey = this._var.options.childKey;
|
|
const source = this.source;
|
|
if (item == null || checkbox.getAttribute('isall') === '1') {
|
|
const allchecked = this._var.allChecked = checkbox.checked;
|
|
/** @type {NodeListOf<HTMLInputElement> | undefined} */
|
|
const boxes = this._var.container?.querySelectorAll('input.dataitem');
|
|
boxes?.forEach(box => box.checked = allchecked);
|
|
source.forEach(it => it.__checked = allchecked ? 1 : 0);
|
|
list = [];
|
|
} else {
|
|
/** @type {HTMLInputElement | null | undefined} */
|
|
const all = this._var.container?.querySelector('input[isall="1"]');
|
|
const val = checkbox.dataset.value;
|
|
if (checkbox.checked) {
|
|
item.__checked = 1;
|
|
if (item.__p != null) {
|
|
// parent
|
|
const parent = source.filter(it => String(getValue(it, valuekey, textkey)) === item.__p)[0];
|
|
if (parent != null) {
|
|
if (!source.filter(it => it.__p === item.__p).some(it => it.__checked !== 1)) {
|
|
parent.__checked = 1;
|
|
} else if (parent.__checked !== 2) {
|
|
parent.__checked = 2;
|
|
}
|
|
}
|
|
}
|
|
if (item[childkey]?.length > 0) {
|
|
source.filter(it => it.__p === val).forEach(it => it.__checked = 1);
|
|
}
|
|
if (!source.some(it => it.__checked !== 1)) {
|
|
this._var.allChecked = true;
|
|
if (all != null) {
|
|
all.checked = true;
|
|
}
|
|
list = [];
|
|
} else {
|
|
list = this._getSelectedList();
|
|
}
|
|
} else {
|
|
if (item[childkey]?.length > 0) {
|
|
if (item.__checked === 2) {
|
|
item.__checked = 0;
|
|
source.filter(it => it.__p === val).forEach(it => it.__checked = 0);
|
|
} else {
|
|
item.__checked = 2;
|
|
}
|
|
} else {
|
|
item.__checked = 0;
|
|
}
|
|
if (item.__p != null) {
|
|
// parent
|
|
const parent = source.filter(it => String(getValue(it, valuekey, textkey)) === item.__p)[0];
|
|
if (parent != null) {
|
|
const some = source.filter(it => it.__p === item.__p).some(it => it.__checked === 1 || it.__checked === 2);
|
|
parent.__checked = some ? 2 : 0;
|
|
}
|
|
}
|
|
if (this._var.allChecked) {
|
|
this._var.allChecked = false;
|
|
if (all != null) {
|
|
all.checked = false;
|
|
}
|
|
}
|
|
list = this._getSelectedList();
|
|
}
|
|
}
|
|
// refresh selection
|
|
/** @type {NodeListOf<HTMLInputElement> | undefined} */
|
|
const checkboxes = this._var.container?.querySelectorAll('input.dataitem');
|
|
checkboxes?.forEach(c => {
|
|
const val = c.dataset.value;
|
|
const it = source.filter(it => String(getValue(it, valuekey, textkey)) === val)[0];
|
|
if (it != null) {
|
|
c.checked = it.__checked === 1 || it.__checked === 2;
|
|
c.indeterminate = it.__checked === 2;
|
|
}
|
|
});
|
|
|
|
this._selectList(list);
|
|
}
|
|
|
|
/**
|
|
* @private
|
|
* @param {DropdownItem[]} list
|
|
* @param {boolean} [silence]
|
|
*/
|
|
_selectList(list, silence) {
|
|
if (this._var.allChecked) {
|
|
if (this._var.label != null) {
|
|
this._var.label.innerText = r('allItem', '( All )');
|
|
}
|
|
} else {
|
|
selectItems.call(this, list);
|
|
}
|
|
this._var.selectedList = list;
|
|
if (!silence && typeof this.onSelectedList === 'function') {
|
|
this.onSelectedList(list);
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {HTMLElement} dom
|
|
* @param {(this: Dropdown, item?: DropdownItem) => void} [trigger]
|
|
* @returns
|
|
*/
|
|
static resolve(dom = document.body, trigger) {
|
|
const selects = dom.querySelectorAll('select');
|
|
for (let sel of selects) {
|
|
const source = [...sel.children].map(
|
|
/**
|
|
* @param {HTMLOptionElement} it
|
|
* @returns {DropdownItem}
|
|
*/
|
|
it => ({ value: it.value, text: it.innerText }));
|
|
const drop = new Dropdown({
|
|
selected: sel.value,
|
|
disabled: sel.disabled,
|
|
tabIndex: sel.tabIndex
|
|
});
|
|
drop.source = source;
|
|
if (typeof trigger === 'function') {
|
|
drop.onSelected = item => trigger.call(drop, item);
|
|
}
|
|
sel.parentElement?.replaceChild(drop.create(), sel);
|
|
}
|
|
return dom;
|
|
}
|
|
} |