import { Widget } from "./Widget";
/**
* @class ui.Label
* @since ui
*/
export class Label extends Widget {
/**
* @hideconstructor
* @param {string} [value] 显示文本
* @param {object} [object] 样式
* @param {string} [targetUrl] 链接
* @param {string} [onClick] 点击事件
* @return ui.Label
*/
constructor(
value?: string,
style?: object,
targetUrl?: string,
onClick?: Function
) {
let a, div;
if (typeof process === 'undefined') {
a = document.createElement('a')
a.innerText = value ?? '文本链接'
if (targetUrl) {
a.href = targetUrl
}
div = document.createElement('div')
div.appendChild(a)
div.classList.add("ui-gvee-label");
if (onClick) {
div.onclick = function () {
onClick();
};
}
}
else {
style = null;
}
// console.log('这是创建的label节点',div)
super(div, style);
// console.log(super(div, style))
// console.table(super(div, style))
if (!(this instanceof Label)) {
return new Label(value, style, targetUrl,onClick);
// console.log('这是创建的label实例',new Label(value, style, targetUrl))
}
}
/**
* 获取文本组件样式
* @return object
*/
getStyle() {
return super.style();
}
/**
* 设置文本组件样式
* @param {String} style 文本组件样式内容。
* @return ui.Label
*/
setStyle(style: object) {
for (let key in style) {
if (key === 'position') {
const pos = this.posDic[style[key]];
if (pos) {
(this.el as HTMLElement).classList.add('positioned', ...pos);
}
} else {
this.styleDic.set(key, style[key]);
(this.el as HTMLElement).style[key] = style[key];
}
}
return this;
}
/**
* 设置文本内容
* @param {String} value 文本内容
* @return ui.Label
*/
setValue(value: string) {
(this.el as HTMLElement).querySelector('a')!.innerText = value ?? '';
return this;
}
/**
* 获取文本内容
* @param {String} value 文本内容
* @return String
*/
getValue() {
// console.log('这是当前对象', (this.el as HTMLElement).querySelector('a').innerText)
return (this.el as HTMLElement).innerText
}
/**
* 点击Label回调方法
* @param {Function} callback 回调方法。
*/
onClick(callback: Function) {
let self = this;
self.el.onclick = function () {
callback();
};
}
/**
* 获取文本url
* @return String
*/
getURl() {
// return ((this.el as HTMLElement)?.firstChild as HTMLElement)?.getAttribute("href");
return (this.el as HTMLElement).querySelector('a')!.href
}
/**
* 设置指定的URL,默认为空。
* @param {String} url 文本内容
* @return ui.Label
*/
setURl(url: string) {
(this.el as HTMLElement).querySelector('a')!.href = url ?? '';
return this
}
}