import { Vue, Component, Prop, Watch, Emit } from 'vue-property-decorator';
import { throttle, ToolbarItem } from '@ibizstudio/runtime';
import { IPSDEUIAction } from '@ibizstudio/runtime';
import "./view-toolbar.less";
/**
* 视图工具栏
*
* @export
* @class ViewToolbar
* @extends {Vue}
*/
@Component({})
export class ViewToolbar extends Vue {
/**
* 工具栏模型
*
* @type {{ [key: string]: ToolbarItem }}
* @memberof ViewToolbar
*/
@Prop()
toolbarModels?: { [key: string]: ToolbarItem };
/**
* 绘制模式
*
* @type {string}
* @memberof ViewToolbar
*/
@Prop({ default: 'DEFAULT' })
mode?: string;
/**
* 计树器服务集合
*
* @type {any}
* @memberof ViewToolbar
*/
@Prop()
counterServiceArray?:any;
/**
* 视图loading服务
*
* @type {any}
* @memberof ViewToolbar
*/
@Prop({default: false})
isViewLoading?:any;
/**
* 监控工具栏模型变更
*
* @memberof ViewToolbar
*/
@Watch('toolbarModels', { immediate: true })
watchModels(): void {
if (this.toolbarModels) {
this.items = [];
this.format();
}
}
/**
* 所有工机具项
*
* @protected
* @type {ToolbarItem[]}
* @memberof ViewToolbar
*/
protected items: ToolbarItem[] = [];
/**
* 格式化工具栏模型
*
* @protected
* @param {*} [model=this.model]
* @memberof ViewToolbar
*/
protected format(model: any = this.toolbarModels, items: ToolbarItem[] = this.items): void {
for (const key in model) {
const item: any = model[key];
items.push(item);
if (item.model) {
item.items = [];
this.format(item.model, item.items);
}
}
}
/**
* 工具栏项点击
*
* @param {*} uiAction
* @param {MouseEvent} e
* @memberof ViewToolbar
*/
@Emit('item-click')
itemClick(uiAction: any, e: MouseEvent): void { }
/**
* 绘制分割线
*
* @protected
* @returns {*}
* @memberof ViewToolbar
*/
protected renderSeperator(): any {
return |;
}
/**
* 绘制菜单
*
* @protected
* @param {any[]} [items]
* @returns {*}
* @memberof ViewToolbar
*/
protected renderMenuItems(items?: any[]): any {
if (!items) {
return;
}
return items.map((item: any) => {
if (item.itemType === 'SEPERATOR') {
return
;
}
if (item.items && item.items.length > 0) {
return this.renderMenuGroup(item);
}
return {this.renderMenuItem(item,false)};
});
}
/**
* 绘制菜单项
*
* @protected
* @param {*} item
* @returns {*}
* @memberof ViewToolbar
*/
protected renderMenuItem(item: any, showButton: boolean = true): any {
const targetCounterService: any = {};
if (this.counterServiceArray?.length > 0) {
Object.assign(targetCounterService,this.counterServiceArray[0].service)
}
if(item.visabled){
if (item.itemType == 'RAWITEM') {
return
{this.renderRawItem(item)}
} else if (!showButton) {
return (
throttle(this.itemClick,[{ tag: item.name }, e],this)}>
{item.showIcon ? : null}
{item.showCaption ? {item.caption} : ''}
);
}
return (
{item.uiaction && Object.is(item.uiaction.uIActionTag, 'ExportExcel') ? (
throttle(this.itemClick,[{ tag: item.name }, $event],this)}
loading={this.isViewLoading}
>
) : (item.uiaction && item.uiaction.counterId) && Object.keys(targetCounterService).length > 0 ? (
throttle(this.itemClick,[{ tag: item.name }, e],this)}
loading={this.isViewLoading}
>
{item.showIcon ? : null}
{item.showCaption ? {item.caption} : ''}
) : (
throttle(this.itemClick,[{ tag: item.name }, e],this)}
loading={this.isViewLoading}
>
{item.showIcon ? : null}
{item.showCaption ? {item.caption} : ''}
)}
{item.tooltip}
);
}else{
return null;
}
}
/**
* 绘制菜单分组
*
* @protected
* @param {*} item
* @returns {*}
* @memberof ViewToolbar
*/
protected renderMenuGroup(item: ToolbarItem): any {
return (
);
}
/**
* 绘制模式2
*
* @protected
* @returns {*}
* @memberof ViewToolbar
*/
protected renderStyle2(): any {
return this.items.map((item: ToolbarItem) => {
if (!item.visabled) {
return;
}
let content: any;
if (item.itemType === 'SEPERATOR') {
content = this.renderSeperator();
} else if (!item.items) {
content = this.renderMenuItem(item);
} else {
content = (
{
{item.showIcon ? : null}
{item.showCaption ? item.caption : ''}
}
{this.renderMenuItems(item.items)}
);
}
return {content}
;
});
}
/**
* 绘制默认模式工具栏
*
* @protected
* @returns {*}
* @memberof ViewToolbar
*/
protected renderDefault(): any {
return this.items.map((item: ToolbarItem) => {
if (item.itemType === 'SEPERATOR') {
return this.renderSeperator();
}
if (Object.is(item.itemType, 'ITEMS') && item.items && item.items.length > 0) {
return (
{item.icon ? : null}
{item.caption ? {item.caption} : null}
{item.tooltip}
{this.renderMenuItems(item.items)}
);
}
return this.renderMenuItem(item);
});
}
/**
* 渲染直接内容
*
* @protected
* @returns {*}
* @memberof ViewToolbar
*/
renderRawItem(item: any) {
let { style, rawContent, htmlContent, rawType, getPSSysImage } = item;
let content: any;
let sysImage = getPSSysImage?.cssClass;
let sysImgurl = getPSSysImage?.imagePath;
if (Object.is(rawType,'RAW')) {
content = rawContent;
} else if (Object.is(rawType,'HTML')){
content = htmlContent;
}
if (content) {
const items = content.match(/\{{(.+?)\}}/g);
if (items) {
items.forEach((item: string) => {
content = content.replace(/\{{(.+?)\}}/, eval(item.substring(2, item.length - 2)));
});
}
content = content.replaceAll('<','<');
content = content.replaceAll('>','>');
content = content.replaceAll(' ',' ');
content = content.replaceAll(' ',' ');
}
return (
)
}
/**
* 绘制工具栏内容
*
* @returns {*}
* @memberof ViewToolbar
*/
render(): any {
if (this.items.length == 0) {
return;
}
let content: any = this.mode == "STYLE2" ? this.renderStyle2() : this.renderDefault();
return {content}
;
}
/**
* 绘制工具栏项样式
*
* @returns {*}
* @memberof ViewToolbar
*/
getToolBarItemClass(item: any) {
const uiAction: IPSDEUIAction = item.uiaction;
let tempClass = `toolbar_${item.name}` + uiAction ? '' : ` ${uiAction.uIActionTag}`;
if(item.actionLevel){
tempClass += ` srfactionlevel${item.actionLevel}`
}
if(item.class){
tempClass += ` ${item.class}`
}
return tempClass;
}
}