Files
ArtPlayer/packages/artplayer/src/utils/component.js
T
2021-11-28 19:32:40 +08:00

138 lines
4.3 KiB
JavaScript

import { hasClass, addClass, removeClass, append, setStyles, tooltip, getStyle, inverseClass } from './dom';
import { has, def } from './property';
import { errorHandle } from './error';
export default class Component {
constructor(art) {
this.id = 0;
this.art = art;
this.add = this.add.bind(this);
}
get show() {
return hasClass(this.art.template.$player, `art-${this.name}-show`);
}
set show(value) {
const { $player } = this.art.template;
const className = `art-${this.name}-show`;
if (value) {
addClass($player, className);
} else {
removeClass($player, className);
}
this.art.emit(this.name, value);
}
set toggle(value) {
if (value) {
this.show = !this.show;
}
}
add(getOption) {
const option = typeof getOption === 'function' ? getOption(this.art) : getOption;
if (!this.$parent || !this.name || option.disable) return;
const name = option.name || `${this.name}${this.id}`;
errorHandle(!has(this, name), `Cannot add an existing name [${name}] to the [${this.name}]`);
this.id += 1;
const $ref = document.createElement('div');
addClass($ref, `art-${this.name}`);
addClass($ref, `art-${this.name}-${name}`);
const childs = Array.from(this.$parent.children);
$ref.dataset.index = option.index || this.id;
const nextChild = childs.find((item) => Number(item.dataset.index) >= Number($ref.dataset.index));
if (nextChild) {
nextChild.insertAdjacentElement('beforebegin', $ref);
} else {
append(this.$parent, $ref);
}
if (option.html) {
append($ref, option.html);
}
if (option.style) {
setStyles($ref, option.style);
}
if (option.tooltip) {
tooltip($ref, option.tooltip);
}
if (option.click) {
this.art.events.proxy($ref, 'click', (event) => {
event.preventDefault();
option.click.call(this.art, this, event);
});
}
if (option.selector && ['left', 'right'].includes(option.position)) {
this.selector(option, $ref);
}
if (option.mounted) {
option.mounted.call(this.art, $ref);
}
if ($ref.childNodes.length === 1 && $ref.childNodes[0].nodeType === 3) {
addClass($ref, 'art-control-onlyText');
}
def(this, name, {
value: $ref,
});
}
selector(option, $ref) {
const { hover, proxy } = this.art.events;
addClass($ref, 'art-control-selector');
const $value = document.createElement('div');
addClass($value, 'art-selector-value');
append($value, option.html);
$ref.innerText = '';
append($ref, $value);
const list = option.selector
.map(
(item, index) =>
`<div class="art-selector-item ${item.default ? 'art-current' : ''}" data-index="${index}">${
item.html
}</div>`,
)
.join('');
const $list = document.createElement('div');
addClass($list, 'art-selector-list');
append($list, list);
append($ref, $list);
const setLeft = () => {
const left = getStyle($ref, 'width') / 2 - getStyle($list, 'width') / 2;
$list.style.left = `${left}px`;
};
hover($ref, setLeft);
proxy($list, 'click', (event) => {
const path = event.composedPath() || [];
const $item = path.find((item) => hasClass(item, 'art-selector-item'));
if (!$item) return;
inverseClass($item, 'art-current');
const index = Number($item.dataset.index);
const find = option.selector[index] || {};
$value.innerText = $item.innerText;
if (option.onSelect) {
const result = option.onSelect.call(this.art, find, $item);
if (typeof result === 'string') {
$value.innerHTML = result;
}
}
setLeft();
this.art.emit('selector', find, $item);
});
}
}