app-alert.vue 11.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 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
<template>
    <div 
        ref="appAlert" 
        :class="[
            'app-alert',
            enableScroll ? showStyle == 'MARQUEE2' ? 'enable-scroll vertical-scroll' : 'enable-scroll horizontal-scroll' : '',
        ]">
        <template v-if="items && items.length > 0">
            <template v-for="(item, index) in items">
                <template v-if="item.hasContent && !Object.is('POPUP', item.position)">
                    <el-alert
                        v-show="item.showState"
                        :key="index"
                        :title="item.title"
                        :type="item.type"
                        :closable="item.closeable"
                        :class="['alert-item', `item-index-${index}`, curIndex === index ? 'active' : '']"
                        @close="alertClose(item)">
                        <template slot>
                            <span v-if="item.messageType == 'HTML'" v-html="item.content"></span>
                            <span v-else>{{ item.content }}</span>
                        </template>
                    </el-alert>
                </template>
            </template>
        </template>
    </div>
</template>

<script lang="ts">
import {Vue, Component, Prop} from 'vue-property-decorator';

@Component({})
export default class AppAlert extends Vue {

    /**
     * 视图消息标识
     * 
     * @type {any}
     * @memberof AppAlert
     */
    @Prop() tag: any;

    /**
     * 显示位置
     * 
     * @type {any}
     * @memberof AppAlert
     */
    @Prop() position: any;

    /**
     * 应用上下文
     * 
     * @type {any}
     * @memberof AppAlert
     */
    @Prop() context: any;

    /**
     * 视图参数
     * 
     * @type {any}
     * @memberof AppAlert
     */
    @Prop() viewparam: any;


    /**
     * 视图消息组tag
     * 
     * @type {any}
     * @memberof AppAlert
     */
    @Prop() infoGroup!: any;

    /**
     * 视图名称
     * 
     * @type {any}
     * @memberof AppAlert
     */
    @Prop() viewname!: any;

    /**
     * 视图消息模型集合
     * 
     * @type {any}
     * @memberof AppAlert
     */
    @Prop() messageDetails!: any[];

    /**
     * 显示模式
     * 
     * @description 视图消息显示模式 {LIST:列表显示、 MARQUEE:横向滚动显示、 MARQUEE2:纵向滚动显示、 USER:用户自定义、 USER2:用户自定义2 }
     * @type {string}
     * @memberof AppAlert
     */
    @Prop({ default: 'MARQUEE2' }) public showStyle?: 'LIST' | 'MARQUEE' | 'MARQUEE2' | 'USER' | 'USER2';

    /**
     * 开启滚动
     * 
     * @type {boolean}
     * @memberof AppAlert
     */
    public enableScroll: boolean = false;
    
    /**
     * 视图消息对象
     * 
     * @type {any}
     * @memberof AppAlert
     */
    public items: any[]= [];

    /**
     * 当前展示消息下标(开启滚动时启用)
     * 
     * @type {number}
     * @memberof AppAlert
     */
    public curIndex: number = 0;

    /**
     * 定时器对象(开启滚动时启用)
     * 
     * @type {any}
     * @memberof AppAlert
     */
    public timer: any = null;

    /**
     * 定时器执行间隔(开启滚动时启用)
     * 
     * @type {number}
     * @memberof AppAlert
     */
    public delayTime: number = 3000;

    /**
     * 消息类型对应背景颜色集合
     * 
     * @type {number}
     * @memberof AppAlert
     */
    public bgColorMap: any = { 
        'success': '#f0f9eb',
        'info': '#f4f4f5', 
        'warning': '#fdf6ec',
        'error': '#fef0f0'
    }

    /**
     * Vue生命周期 --- Created
     * 
     * @memberof AppAlert
     */
    public created() {
        this.initOptions();
        this.handleItems();
    }

    /**
     * Vue生命周期 --- Mounted
     * 
     * @memberof AppAlert
     */
    public mounted() {
        if (this.enableScroll) {
            this.initScroll();
        }
    }

    /**
     * 开启滚动时初始化配置
     * 
     * @memberof AppAlert
     */
    public initOptions() {
        if (this.showStyle == "MARQUEE" || this.showStyle == 'MARQUEE2') {
            this.enableScroll = true;
        }
    }

    /**
     * 获取视图消息对象
     * 
     * @memberof AppAlert
     */
    public handleItems() {
        if (this.messageDetails.length > 0) {
            this.messageDetails.forEach((detail: any) => {
                this.handleItemOption(detail);
                let flag = this.handleItemCloseMode(detail);
                this.handleItemPosition(detail, flag);
                this.items.push(detail);
            })
            //  开启滚动时获取激活视图消息项
            if (this.enableScroll) {
                this.getActiveItem();
            }
        }
    }

    public handleItemOption(detail: any) {
        //  是否存在内容
        detail.hasContent = true;
        if(!detail.title && !detail.content) {
            detail.hasContent = false;
        }
        //  关闭模式
        detail.closeable = detail.enableRemove;
        //  类型
        switch (detail.type) {
            case 'WARN':
                detail.type = 'warning';
                break;
            case 'SUCCESS':
                detail.type = 'success';
                break;
            case 'ERROR':
                detail.type = 'error';
                break;
            default:
                detail.type = 'info';
                break;
        }
    }

    /**
     * 处理数据关闭模式
     * 
     * @memberof AppAlert
     */
    public handleItemCloseMode(data: any) {
        let flag = true;
        data.showState = true; 
        if(data.removeMode || data.removeMode == 0) {
            if(data.removeMode == 1) {
                const tag = this.viewname + '_' + this.infoGroup + '_' + data.codeName;
                const codeName = localStorage.getItem(tag);
                if(codeName) {
                    data.showState = false;
                    flag = false;
                }
            }
            if(data.removeMode == 0) {
                data.closeable = false;
            }
        }
        return flag;
    }

    /**
     * 处理数据显示位置
     * 
     * @memberof AppAlert
     */
    public handleItemPosition(data: any, flag: boolean) {
        if(data.position) {
            if(flag && Object.is('POPUP', data.position)) {
                const h = this.$createElement;
                data.showState = false;
                if(Object.is('HTML',data.messageType)) {
                    setTimeout(() => {
                        this.$message({
                            customClass: data.codeName+","+data.removeMode,
                            message: h('div',{}, [
                                h('p',data.title),
                                h('p',{domProps:{innerHTML: data.content}})
                            ]), 
                            type: data.type,
                            showClose: data.closeable,
                            onClose: (eveny: any) => this.alertClose(data),
                        })
                    }, 0)
                } else {
                    setTimeout(() => {
                        this.$message({
                            customClass: data.codeName+","+data.removeMode,
                            message: h('div',{}, [
                                h('p',data.title),
                                h('p',data.content)
                            ]), 
                            type: data.type,
                            showClose: data.closeable,
                            onClose: (eveny: any) => this.alertClose(data),
                        })
                    }, 0)
                }
            }
        }
    }

    /**
     * 视图消息关闭
     * 
     * @memberof AppAlert
     */
    public alertClose(data: any) {
        data.showState = false;
        if (this.enableScroll && data.position !== 'POPUP') {
            this.getActiveItem();
            this.initScroll();
        }
        if(data.customClass) {
            let tempArr: any[] = data.customClass.toString().split(',');
            if(tempArr && tempArr.length > 0) {
                if(Object.is("1", tempArr[1])) {
                    const tag = this.viewname + '_' + this.infoGroup + '_' + tempArr[0];
                    localStorage.setItem(tag, data.customClass);
                }
            } 
        }
        if(data.removeMode && data.removeMode == 1) {
            const tag = this.viewname + '_' + this.infoGroup + '_' + data.codeName;
            localStorage.setItem(tag, data.codeName);
        }
    }

    /**
     * 初始化滚动逻辑,设置定时器
     * 
     * @memberof AppAlert
     */
    public initScroll() {
        this.removeInterval();
        this.timer = setInterval(() => {
            this.getActiveItem();
        }, this.delayTime)
    }

    /**
     * 获取当前激活视图消息项(开启滚动时有效)
     * 
     * @memberof AppAlert
     */
    public getActiveItem() {
        const showItems = this.items.filter((item: any) => item.showState);
        let items: any = [];
        if (this.curIndex < this.items.length - 1) {
            items = this.items.slice(this.curIndex + 1, this.items.length);
        } else {
            items = [...showItems];
        }
        const index = this.getActiveItemIndex(items);
        //  无显示项
        if (index == -1 && showItems.length == 0) {
            this.noItemsShow();
        } else if(index == -1) {
            //  获取第一个激活项
            this.curIndex = this.items.findIndex((_item: any) => Object.is(_item, showItems[0]) );
        } else {
            this.curIndex = index;
        }
        if (index != -1) {
            this.handleContainerBgColorChange();
        }
    }

    /**
     * 处理视图消息容器背景颜色变化(开启滚动时有效)
     * 
     * @memberof AppAlert
     */
    public handleContainerBgColorChange() {
        const alert: any = this.$refs.appAlert;
        const item = this.items[this.curIndex];
        if (alert && item) {
            const oldBgColor = getComputedStyle(alert).getPropertyValue('--new-message-bg-color');
            const newBgColor = this.bgColorMap[item.type];
            alert.style.setProperty('--old-message-bg-color', oldBgColor);
            alert.style.setProperty('--new-message-bg-color', newBgColor);
        }
    }

    /**
     * 获取当前激活视图消息项下标(开启滚动时有效)
     * 
     * @description 获取当前激活视图消息项对应items中的下标,没有时返回-1
     * @memberof AppAlert
     */
    public getActiveItemIndex(items: any[]) {
        if (items.length == 0) {
            return -1;
        }
        let item: any = items.find((item: any) => { return item.showState });
        if (!item) {
            return -1;
        }
        return this.items.findIndex((_item: any) => { return Object.is(item, _item) });
    }

    /**
     * 无视图消息展示(开启滚动时有效)
     * 
     * @description 无视图消息展示时,隐藏消息容器,删除定时器
     * @memberof AppAlert
     */
    public noItemsShow() {
        const alert: any = this.$refs.appAlert;
        if (alert) {
            alert.style.display = 'none';
        }
        this.removeInterval();
    }

    /**
     * 删除定时器
     * 
     * @memberof AppAlert
     */
    public removeInterval() {
        if (this.timer) {
            clearInterval(this.timer);
            this.timer = null;
        }
    }

    /**
     * Vue生命周期 --- Destroyed
     * 
     * @description 无视图消息展示时,隐藏消息容器,删除定时器
     * @memberof AppAlert
     */
    public destroyed() {
        this.removeInterval();
    }

}
</script>