app-wf-opinion.vue 12.3 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 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
<template>
    <div v-if="mode === 'POPOVER'" :class="['app-wf-opinion', isShow ? 'is-show' : '']" ref='wf-opinion'>
        <el-button v-popover:popover type="primary" size="small" @click="click">{{ $t('components.appwfopinion.title') }}</el-button>
        <el-popover ref="popover" popper-class="wf-opinion-popover" v-model="isShow" :width="width" :visible-arrow="false" trigger="manual" placement="left">
            <div v-show="isShow" ref="wf-opinion-container" class="wf-opinion-container" :style="{ 'height': containerHeight + 'px' }">
                <div class="opinion-container__header">
                    <span class="opinion-container__header__title">{{ $t('components.appwfopinion.title') }}</span>
                    <div class="opinion-container__header__icon-container">
                        <i v-if="false" :class="['icon', enableClick ? 'el-icon-lock' : 'el-icon-unlock' ]" @click="enableClick = !enableClick;"/>
                        <i :class="['icon', 'el-icon-minus', enableClick ? 'enable' : '']" @click="minusClick"/>
                    </div>
                </div>
                <div class="opinion-container__content">
                    <div :class="['opinion-container__content__textarea', error ? 'error' : '']">
                        <form-item :rules="rules" :error="error" :ref="name" :prop="name">
                            <input-box v-model="currentVal" @change="valueChange" :placeholder="placeholder || $t('components.appwfopinion.placeholder')" type="textarea"/>
                        </form-item>
                    </div>
                    <div class="opinion-container__content__navbar">
                        <div v-if="false" class="navbar__header">
                            <i-input search :placeholder="$t('components.appwfopinion.search')" @on-search="onSearch" />
                        </div>
                        <div class="navbar__items">
                            <template v-for="(filter, index) in filterItems">
                                <span :class="getClass(filter)" :key="index" @click="filterItemDBClick(filter, $event)">{{ filter }}</span>
                            </template>
                        </div>
                    </div>
                </div>
            </div>
        </el-popover>
    </div>
    <div v-else ref="wf-opinion-container" class="wf-opinion-container wf-opinion-nobutton">
        <div class="opinion-container__content">
            <div :class="['opinion-container__content__textarea', error ? 'error' : '']">
                <form-item :rules="rules" :error="error" :ref="name" :prop="name">
                    <input-box v-model="currentVal" @change="valueChange" :placeholder="placeholder || $t('components.appwfopinion.placeholder')" type="textarea"/>
                </form-item>
            </div>
            <div class="opinion-container__content__navbar">
                <div class="navbar__items">
                    <template v-for="(filter, index) in filterItems">
                        <span :class="getClass(filter)" :key="index" @click="filterItemDBClick(filter, $event)">{{ filter }}</span>
                    </template>
                </div>
            </div>
        </div>
    </div>
</template>


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

@Component({})
export default class AppWFOpinion extends Vue {
    
    /**
     * 项名称
     * 
     * @type {string}
     * @memberof AppWFOpinion
     */
    @Prop() public name!: string;

    /**
     * 值
     * 
     * @type {*}
     * @memberof AppWFOpinion
     */
    @Prop() itemValue!: any;

    /**
     * 是否禁用
     * 
     * @type {boolean}
     * @memberof AppWFOpinion
     */
    @Prop({ default: false }) public disabled?: boolean;

    /**
     * 空白提示内容
     * 
     * @type {string}
     * @memberof AppWFOpinion
     */
    @Prop() public placeholder: any;

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

    /**
     * 视图参数
     * 
     * @type {*}
     * @memberof AppWFOpinion
     */
    @Prop() public viewparams: any;

    /**
     * 输入框id
     * 
     * @type {*}
     * @memberof AppWFOpinion
     */
    @Prop() public textareaId: any;

    /**
     * 父容器
     * 
     * @type {*}
     * @memberof AppWFOpinion
     */
    @Prop() public parentContainer: any;

    /**
     * 容器高度
     * 
     * @type {number}
     * @memberof AppWFOpinion
     */
    @Prop({ default: 220 }) public containerHeight?: number;

    /**
     * 模式 
     * 
     * @type {"POPOVER" | "DEFAULT"}
     * @memberof AppWFOpinion
     */
    @Prop({ default: 'DEFAULT' }) public mode!: "POPOVER" | "DEFAULT";

    /**
     * 自定义过滤项
     * 
     * @type {string[]}
     * @memberof AppWFOpinion
     */
    @Prop() public customFilter?: string[];

    /**
     * 是否显示详情框
     * 
     * @type {boolean}
     * @memberof AppWFOpinion
     */
    public isShow: boolean = false;

    /**
     * 过滤选中项
     * 
     * @type {*}
     * @memberof AppWFOpinion
     */
    public selectItem: any = null;

    /**
     * 过滤项
     * 
     * @type {Array<any>}
     * @memberof AppWFOpinion
     */
    public filterItems: Array<any> = [];

    /**
     * 是否禁用缩小点击
     * 
     * @type {boolean}
     * @memberof AppWFOpinion
     */
    public enableClick: boolean = false;

    /**
     * 容器宽度
     * 
     * @type {number}
     * @memberof AppWFOpinion
     */
    public width: number = 700;

    /**
     * 配置
     * 
     * @type {number}
     * @memberof AppWFOpinion
     */
    public options = {
        'append-to-body': false
    }

    /**
     * 获取输入框绑定值
     * 
     * @type {*}
     * @memberof AppWFOpinion
     */
    get currentVal() {
        return this.itemValue;
    }

    /**
     * 设置输入框绑定值
     * 
     * @type {*}
     * @memberof AppWFOpinion
     */
    set currentVal(value: any) {
        this.initData(value);
        this.$emit('change', value);
    }

    /**
     * 值规则
     * 
     * @type {any[]}
     * @memberof AppWFOpinion
     */
    get rules() {
        if (this.parentContainer) {
            return [...this.parentContainer.rules?.[this.name]] || [];
        } else {
            return [];
        }
    }

    /**
     * 错误消息提示
     * 
     * @type {*}
     * @memberof AppWFOpinion
     */
    get error() {
        if (this.parentContainer) {
            return this.parentContainer.detailsModel?.[this.name]?.error || "";
        } else {
            return "";
        }
    }

    /**
     * 监听值变化
     * 
     * @type {*}
     * @memberof AppWFOpinion
     */
    @Watch('itemValue')
    public onValueChange(newVal: any, oldVal: any) {
        if (newVal !== oldVal) {
            this.currentVal = this.itemValue;
        }
    }

    /**
     * Vue生命周期 -- created
     * 
     * @memberof AppWFOpinion
     */
    public created() {
        this.handleFilterItems();
        this.initData();
    }

    /**
     * Vue生命周期 -- mounted
     * 
     * @memberof AppWFOpinion
     */
    public mounted() {
        if (this.mode === 'POPOVER') {
            this.$nextTick(() => {
                this.isShow = true;
                this.computePosition();
            })
            //窗口变化重新计算容器宽度
            window.addEventListener('resize', () => {
                this.computePosition();
            })
            this.watchDisplay();
        }
    }

    /**
     * 监听父容器显示变化,实现悬浮框显示状态切换
     * 
     * @memberof AppWFOpinion
     */
    public watchDisplay() {
        if (!this.parentContainer || !this.parentContainer.$el) {
            return;
        }
        let MutationObserver: any = window.MutationObserver;
        let element: any = this.parentContainer.$el.parentNode;
        let observer = new MutationObserver((mutations: any) => {
            mutations.forEach((mutation: any) => {
                if (mutation.type == "attributes") {
                    if (mutation.target.style.display == 'none' && mutation.target.style.visibility == 'hidden') {
                        this.isShow = false;
                    } else {
                        this.isShow = true;
                    }
                }
            });
        });
        observer.observe(element, {
            attributes: true,
            attributeFilter: ['style']
        });
    }

    /**
     * 数据初始化
     * 
     * @memberof AppWFOpinion
     */
    public initData(value: any = this.itemValue) {
        if (value && this.filterItems.find((item: any) => { return Object.is(item, value); })) {
            this.selectItem = value;
        } else {
            this.selectItem = null;
        }
    }

    /**
     * 位置计算
     * 
     * @memberof AppWFOpinion
     */
    public computePosition() {
        if (this.mode === 'POPOVER') {
            const dom: any = this.$refs['wf-opinion'];
            if (this.parentContainer && dom) {
                this.width = this.parentContainer.$el.offsetWidth;
                dom.style.height = this.isShow ? `${this.containerHeight}px` : '0px';
            }
        }
    }

    /**
     * 处理过滤项
     * 
     * @memberof AppWFOpinion
     */
    public handleFilterItems() {
        if (this.customFilter && this.customFilter.length) {
            this.customFilter = [...this.customFilter];
        } else {
            this.filterItems = ['同意', '不同意', '已阅'];
        }
    }

    /**
     * 过滤项点击
     * 
     * @param item 过滤项
     * @param event 点击事件
     * @memberof AppWFOpinion
     */
    public filterItemClick(item: any, event: any) {
        if (!item || Object.is(this.selectItem, item)) {
            return;
        }
        this.selectItem = item;
    }

    /**
     * 过滤项双击
     * 
     * @memberof AppWFOpinion
     */
    public filterItemDBClick(item: any, event: any) {
        if (!item) {
            return;
        }
        this.valueChange(item);
    }

    /**
     * 获取过滤项样式
     * 
     * @param item 过滤项
     * @memberof AppWFOpinion
     */
    public getClass(item: any) {
        if (this.selectItem && Object.is(this.selectItem, item)) {
            return ['filter-item', 'select-item'];
        }
        return ["filter-item"];
    }

    /**
     * 搜索
     * 
     * @param item 搜索值
     * @memberof AppWFOpinion
     */
    public onSearch(value: any) {
        //TODO
    }

    /**
     * 缩小按钮点击
     * 
     * @memberof AppWFOpinion
     */
    public minusClick() {
        if (!this.enableClick) {
            this.isShow = false;
            this.computePosition();
        }
    }

    /**
     * 按钮点击事件
     * 
     * @memberof AppWFOpinion
     */
    public click() {
        this.isShow = true;
        this.initData();
        this.computePosition();
        // this.scrollToContainer();
    }

    /**
     * 滚动到容器位置
     * 
     * @memberof AppWFOpinion
     */
    public scrollToContainer() {
        const cubic = (value: any) => Math.pow(value, 3);
        const easeInOutCubic = (value: any) => value > 0.5
            ? 1 - cubic((1 - value) * 2) / 2
            : cubic(value * 2) / 2;
        const el = this.parentContainer?.$el;
        if (!el) {
            return;
        }
        const beginTime = Date.now();
        const beginValue = el.scrollTop;
        const endValue = el.scrollHeight + this.containerHeight;
        const rAF = window.requestAnimationFrame || (func => setTimeout(func, 16));
        const frameFunc = () => {
            const progress = (Date.now() - beginTime) / 500;
            if (progress < 1) {
                el.scrollTop = (endValue - beginValue) * (easeInOutCubic(progress)) + beginValue;
                rAF(frameFunc);
            } else {
                el.scrollTop = endValue;
            }
        };
        rAF(frameFunc);
    }

    /**
     * 值变更
     * 
     * @memberof AppWFOpinion
     */
    public valueChange(value: any) {
        this.currentVal = value;
    }

    /**
     * 销毁
     * 
     * @memberof AppWFOpinion
     */
    public destroyed() {
        if (this.mode === 'POPOVER') {
            window.removeEventListener('resize', () => {});
        }
    }

}    
</script>