app-model-button.vue 8.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
<template>
    <app-button
        :styleContent="styleContent"
        :classContent="classContent"
        :name="name"
        :caption="caption"
        :tooltip="tooltip"
        :showCaption="showCaption"
        :showIcon="showIcon"
        :iconcls="iconItem.iconcls"
        :icon="iconItem.icon"
        :iconAlign="iconAlign"
        :disabled="disabled"
        :loading="loading"
        :shape="shape"
        :type="buttonType"
        @onClick="onClick"
    >
        <slot></slot>
    </app-button>
</template>

<script lang="ts">
import { IPSAppDEUIAction } from '@ibiz/dynamic-model-api';
import { Vue, Component, Prop, Watch } from 'vue-property-decorator';

@Component({})
export default class AppModelButton extends Vue {
    /**
     * 按钮ID
     *
     * @type {any}
     * @memberof AppModelButton
     */
    @Prop() public id: any;

    /**
     * 按钮模型数据
     *
     * @type {any}
     * @memberof AppModelButton
     */
    @Prop() public modelJson!: any;

    /**
     * 禁用状态
     *
     * @memberof AppModelButton
     */
    @Prop({ default: false }) public disabled?: boolean;

    /**
     * 加载状态
     *
     * @memberof AppModelButton
     */
    @Prop({ default: false }) public loading?: boolean;

    /**
     * 动态样式
     *
     * @memberof AppModelButton
     */
    @Prop({ default: '' }) public dynaStyle?: any;

    /**
     * 动态类名
     *
     * @memberof AppModelButton
     */
    @Prop({ default: '' }) public dynaClass?: any;

    /**
     * 动态类型
     *
     * @memberof AppModelButton
     */
    @Prop({ default: '' }) public dynaType?: string;

    /**
     * 圆角
     *
     * @memberof AppModelButton
     */
    @Prop({ default: undefined }) public shape?: string | undefined;

    /**
     * 传入数据
     *
     * @memberof AppModelButton
     */
    @Prop() data: any;

    /**
     * 按钮名称
     *
     * @memberof AppModelButton
     */
    public name: string = '';

    /**
     * 图标对象
     *
     * @memberof AppModelButton
     */
    public iconItem: any = {};

    /**
     * 显示提示
     *
     * @memberof AppModelButton
     */
    public tooltip: any = '';

    /**
     * 标题
     *
     * @memberof AppModelButton
     */
    public caption: any = '';

    /**
     * 显示标题
     *
     * @memberof AppModelButton
     */
    public showCaption: boolean = true;

    /**
     * 显示图标
     *
     * @memberof AppModelButton
     */
    public showIcon?: boolean = true;

    /**
     * 按钮绘制模式
     *
     * @memberof AppModelButton
     */
    public renderMode: 'BUTTON' | 'LINK' = 'BUTTON';

    /**
     * 按钮样式
     *
     * @memberof AppModelButton
     */
    get styleContent(){
        let tempStyle = '';
        let { width, height} = this.modelJson;
            // 初始化样式 统一转成字符串传给下面原子组件
            if (width > 0) {
                tempStyle += `width: ${width}px;`;
            }
            if (height > 0) {
                tempStyle += `height: ${width}px;`;
            }
            if (this.modelJson && this.modelJson.M && this.modelJson.M.buttonCssStyle) {
                tempStyle += this.getContentStyle(this.modelJson.M.buttonCssStyle) + ';';
            }
            if (this.dynaStyle) {
                if (typeof this.dynaStyle == 'string') {
                    tempStyle += this.dynaStyle + ';';
                } else if (typeof this.dynaStyle == 'object') {
                    for (const [key, value] of Object.entries(this.dynaStyle)) {
                        tempStyle += `${key}: ${value};`;
                    }
                }
            }
        return tempStyle;
    }

    /**
     * 按钮类名
     *
     * @memberof AppModelButton
     */
    get classContent(): string{
        let _classContent = ''
        if (this.dynaClass) {
            if (typeof this.dynaClass == 'string') {
               _classContent += this.dynaClass + ' ';
            } if(this.dynaClass instanceof Array){
               for (const key in this.dynaClass) {
                    const element = this.dynaClass[key];
                    for (const [key, value] of Object.entries(element)) {
                    if (value == true) {
                       _classContent += `${key} `;
                    }
                }
               }
            }else if (typeof this.dynaClass == 'object') {
                for (const [key, value] of Object.entries(this.dynaClass)) {
                    if (value == true) {
                       _classContent += `${key} `;
                    }
                }
            }
        }
        const sysCss = this.modelJson.getPSSysCss();
        if (sysCss?.cssName) {
           _classContent += sysCss.cssName + ' ';
        }
        return _classContent;
    };

    /**
     * 按钮样式
     *
     * @memberof AppModelButton
     */
    public buttonStyle:
        | 'DEFAULT'
        | 'INVERSE'
        | 'PRIMARY'
        | 'INFO'
        | 'SUCCESS'
        | 'WARNING'
        | 'DANGER'
        | 'STYLE2'
        | 'STYLE3'
        | 'STYLE4' = 'DEFAULT';

    /**
     * 按钮类型
     *
     * @memberof AppModelButton
     */
    get buttonType() {
        if (Object.is(this.renderMode, 'LINK')) {
            return 'text';
        } else {
            if (
                Object.is(this.buttonStyle, 'DEFAULT') ||
                Object.is(this.buttonStyle, 'STYLE2') ||
                Object.is(this.buttonStyle, 'STYLE3') ||
                Object.is(this.buttonStyle, 'STYLE4')
            ) {
                return 'default';
            } else if (Object.is(this.buttonStyle, 'DANGER')) {
                return 'error';
            } else if (Object.is(this.buttonStyle, 'INVERSE')) {
                return 'primary';
            } else if (this.dynaType) {
                return this.dynaType.toLowerCase();
            } else {
                return this.buttonStyle.toLowerCase();
            }
        }
    }

    /**
     * 按钮幽灵属性,使按钮背景透明
     *
     * @memberof AppModelButton
     */
    get buttonGhost() {
        return Object.is(this.buttonStyle, 'INVERSE');
    }

    /**
     * 按钮图标方向
     *
     * @memberof AppModelButton
     */
    public iconAlign: 'LEFT' | 'TOP' | 'RIGHT' | 'BOTTOM' = 'LEFT';

    @Watch('data', { deep: true })
    dataChange(newVal: any, oldVal: any) {
        let { captionItemName } = this.modelJson;
        if (captionItemName && newVal[captionItemName.toLowerCase()]) {
            this.caption = newVal[captionItemName.toLowerCase()];
            this.tooltip = newVal[captionItemName.toLowerCase()];
        }
    }

    /**
     * 初始化完成
     *
     * @memberof AppModelButton
     */
    public created() {
        const _this = this;
        if (this.modelJson) {
            let { caption, showCaption, name, tooltip } = this.modelJson;
            const uiAction = this.modelJson.getPSUIAction() as IPSAppDEUIAction;
            this.name = name;
            this.caption = this.$tl(uiAction?.getCapPSLanguageRes()?.lanResTag, caption);
            this.tooltip = this.$tl(uiAction?.getTooltipPSLanguageRes()?.lanResTag, tooltip);
            this.showCaption = showCaption;
            // 初始化图标对象
            const sysImage = this.modelJson.getPSSysImage();
            const actionImage = uiAction?.getPSSysImage();
            this.iconItem = {
                iconcls: sysImage?.cssClass || actionImage?.cssClass,
                icon: sysImage?.imagePath || actionImage?.imagePath,
            };
            if (this.modelJson.renderMode) {
                this.renderMode = this.modelJson.renderMode;
            }
            if (this.modelJson.buttonStyle) {
                this.buttonStyle = this.modelJson.buttonStyle;
            }
            if (this.modelJson.iconAlign) {
                this.iconAlign = this.modelJson.iconAlign;
            }
        }
    }

    /**
     * 获取内容样式
     * @param {*} data
     * @memberof AppModelButton
     */
    getContentStyle(data: any) {
        if (data) {
            const res = data.split('\n');
            const target: string[] = [];
            res.forEach((item: any) => {
                target.push(...item.split(';').filter((value: any) => value));
            });
            return target
                .filter((value: string) => {
                    return value.split(':').length === 2;
                })
                .join(';');
        }
    }

    /**
     * 点击按钮
     *
     * @param {*} event
     * @memberof AppModelButton
     */
    public onClick(event: any) {
        Object.assign(event, { srfid: this.id });
        this.$emit('onClick', event);
    }
}
</script>