app-radio-group.vue 8.8 KB
Newer Older
1
<template>
2
    <radio-group v-if="button" class="app-radio-group" v-model="value" type="button" :size="size">
3 4 5 6
        <radio
            v-for="(_item, index) in items"
            :key="index"
            :label="_item.value"
7 8 9 10 11 12 13 14 15 16 17 18
            :border="border"
            :disabled="isDisabled || _item.disabled || readonly"
        >
            <span>{{ _item.text }}</span>
        </radio>
    </radio-group>
    <radio-group v-else class="app-radio-group" v-model="value" :size="size">
        <radio
            v-for="(_item, index) in items"
            :key="index"
            :label="_item.value"
            :border="border"
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
            :disabled="isDisabled || _item.disabled || readonly"
        >
            <span>{{ _item.text }}</span>
        </radio>
    </radio-group>
</template>
<script lang="ts">
import { Component, Vue, Prop, Model, Watch } from 'vue-property-decorator';
import { LogUtil, CodeListService } from 'ibiz-core';
import { Subject, Subscription } from 'rxjs';

@Component({})
export default class AppRadioGroup extends Vue {
    /**
     * 代码表服务对象
     *
     * @type {CodeListService}
     * @memberof AppRadioGroup
     */

    public codeListService: CodeListService = new CodeListService({ $store: this.$store });

    /**
     * 双向绑定值
     *
     * @type {*}
     * @memberof AppRadioGroup
     */
    @Model('change') item?: any;

    /**
     * 属性类型
     *
     * @type {'string' | 'number'}
     * @memberof AppRadioGroup
     */
    @Prop({ default: 'string' })
    public valueType!: 'string' | 'number';

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
    /**
     * 是否显示边框
     *
     * @type {boolean}
     * @default false
     * @memberof AppRadioGroup
     */
    @Prop({ default: false })
    public border?: boolean;

    /**
     * 是否按钮
     *
     * @type {boolean}
     * @default false
     * @memberof AppRadioGroup
     */
    @Prop({ default: false })
    public button?: boolean;

    /**
     * 尺寸
     *
     * @type {'large' | 'small' | 'default'}
     * @default 'default'
     * @memberof AppRadioGroup
     */
    @Prop({ default: 'default' })
    public size!: 'large' | 'small' | 'default';

88 89 90 91 92 93
    /**
     * 获取值
     *
     * @memberof AppRadioGroup
     */
    get value() {
94
        if (this.valueType && this.valueType === 'number') {
95 96 97 98 99 100 101 102 103 104 105 106 107
            return this.item;
        } else {
            return `${this.item}`;
        }
    }

    /**
     * 设置值
     *
     * @memberof AppRadioGroup
     */
    set value(val: any) {
        if (typeof val == 'string') {
108
            if (this.valueType && this.valueType === 'number') {
109 110 111 112 113 114 115 116 117
                if (val.indexOf('.') === -1) {
                    this.$emit('change', parseInt(val));
                } else {
                    this.$emit('change', parseFloat(val));
                }
            } else {
                this.$emit('change', val);
            }
        } else if (typeof val == 'number') {
118
            if (this.valueType && this.valueType === 'number') {
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
                this.$emit('change', val);
            } else {
                this.$emit('change', val.toString());
            }
        }
    }

    /**
     * 代码表标识
     *
     * @type {string}
     * @memberof AppRadioGroup
     */
    @Prop() public tag?: string;

    /**
     * 代码表类型
     *
     * @type {string}
     * @memberof AppRadioGroup
     */
    @Prop() public codelistType?: string;

    /**
     * 代码表
     *
     * @type {*}
     * @memberof AppRadioGroup
     */
    @Prop() public codeList!: any;

    /**
     * 传入表单数据
     *
     * @type {*}
     * @memberof AppRadioGroup
     */
    @Prop() public data?: any;

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

166 167 168 169 170 171
    /**
     * 只读模式
     *
     * @type {boolean}
     */
    @Prop({ default: false }) public readonly?: boolean;
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

    /**
     * 局部上下文导航参数
     *
     * @type {any}
     * @memberof AppRadioGroup
     */
    @Prop() public localContext!: any;

    /**
     * 局部导航参数
     *
     * @type {any}
     * @memberof AppRadioGroup
     */
    @Prop() public localParam!: any;

    /**
     * 视图上下文
     *
     * @type {*}
     * @memberof AppRadioGroup
     */
    @Prop() public context!: any;

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

    /**
     * 属性名称
     *
     * @type {string}
     * @memberof AppRadioGroup
     */
    @Prop() name?: string;

    /**
     * 编辑器状态
     *
     * @type {Subject<any>}
     * @memberof AppRadioGroup
     */
    @Prop() public contextState?: Subject<any>;

    /**
     * 编辑器状态事件
     *
     * @type {*}
     * @memberof AppRadioGroup
     */
    public contextStateEvents: Subscription | undefined;

    /**
     * 是否禁用
     *
     * @readonly
     * @memberof AppRadioGroup
     */
    get isDisabled() {
        if (this.disabled) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 代码表
     *
     * @type {any[]}
     * @memberof AppRadioGroup
     */
    public items: any[] = [];

    /**
     * 公共参数处理
     *
     * @param {*} arg
     * @returns
     * @memberof AppRadioGroup
     */
    public handlePublicParams(arg: any) {
        // 合并表单参数
        arg.param = this.viewparams ? JSON.parse(JSON.stringify(this.viewparams)) : {};
        arg.context = this.context ? JSON.parse(JSON.stringify(this.context)) : {};
        // 附加参数处理
        if (this.localContext && Object.keys(this.localContext).length > 0) {
            let _context = this.$util.computedNavData(this.data, arg.context, arg.param, this.localContext);
            Object.assign(arg.context, _context);
        }
        if (this.localParam && Object.keys(this.localParam).length > 0) {
            let _param = this.$util.computedNavData(this.data, arg.param, arg.param, this.localParam);
            Object.assign(arg.param, _param);
        }
    }

    /**
     * vue生命周期 -- created
     *
     * @memberof AppRadioGroup
     */
    public created() {
        this.handleData();
    }

    /**
     * vue生命周期 -- mounted
     *
     * @memberof AppRadioGroup
     */
    public mounted() {
        if (this.contextState) {
            this.contextStateEvents = this.contextState.subscribe(({ type, data }: any) => {
                if (type == 'load') {
                    this.handleData();
                }
293
            });
294 295 296 297 298
        }
    }

    /**
     * @description: 组件销毁
299
     *
300
     * @return {*}
301 302 303 304
     */

    public destroyed() {
        if (this.contextStateEvents) {
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
            this.contextStateEvents.unsubscribe();
        }
    }

    public handleData() {
        if (this.tag && this.codelistType) {
            // 公共参数处理
            let data: any = {};
            this.handlePublicParams(data);
            // 参数处理
            let context = data.context;
            let viewparam = data.param;
            this.codeListService
                .getDataItems({
                    tag: this.tag,
                    type: this.codelistType,
                    data: this.codeList,
                    context: context,
                    viewparam: viewparam,
                })
                .then((codelistItems: Array<any>) => {
                    this.formatCodeList(codelistItems);
                })
                .catch((error: any) => {
                    LogUtil.log(`----${this.tag}----${this.$t('app.commonwords.codenotexist')}`);
                });
        }
    }

    /**
     * 代码表类型和属性匹配
336
     *
337 338 339
     * @param {*} items
     * @memberof AppRadioGroup
     */
340
    public formatCodeList(items: Array<any>) {
341 342
        let matching: boolean = false;
        this.items = [];
343 344
        try {
            items.forEach((item: any) => {
345
                const type = this.$util.typeOf(item.value);
346
                if (type != this.valueType) {
347
                    matching = true;
348
                    if (type === 'number') {
349
                        item.value = item.value.toString();
350 351 352 353
                    } else {
                        if (type == 'null') {
                            this.valueType == 'number' ? (item.value = 0) : (item.value = '');
                        } else if (item.value.indexOf('.') == -1) {
354
                            item.value = parseInt(item.value);
355
                        } else {
356 357 358 359 360 361
                            item.value = parseFloat(item.value);
                        }
                    }
                }
                this.items.push(item);
            });
362 363
            if (matching) {
                LogUtil.warn(`${this.tag}${this.$t('app.commonwords.codelistwarn')}`);
364
            }
365
        } catch (error) {
366 367 368 369
            LogUtil.warn(this.$t('app.commonwords.codelistwarn'));
        }
    }
}
370
</script>