All files / src/mixins support.datasource.js

69.44% Statements 25/36
64.28% Branches 18/28
90.9% Functions 10/11
69.44% Lines 25/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                  24x             4x       24x 24x 7x         28x       28x 4x         24x             7x 7x 7x 7x                     28x 17x 11x           11x   11x 7x   28x     7x 7x 7x   7x     7x                    
export default {
    props: {
        dataSource: [Array, Function, Object],
        dataSchema: { type: String, default: 'entity' },
        textField: { type: String, default: 'text' },
        valueField: { type: String, default: 'value' },
        treeSelectTip: { type: String, default: '请绑定数据源或插入子节点' },
    },
    data() {
        return {
            currentDataSource: undefined,
            loading: false,
        };
    },
    watch: {
        dataSource() {
            this.handleData();
        },
    },
    created() {
        this.handleData();
        if (this.currentDataSource && this.currentDataSource.load) {
            this.load();
        }
    },
    methods: {
        handleData() {
            this.currentDataSource = this.normalizeDataSource(this.dataSource, this.multiple);
        },
        normalizeDataSource(dataSource, multiple) {
            let final;
            if (dataSource === undefined) {
                final = {
                    data: [],
                    load: undefined,
                };
            } else {
                final = this.currentDataSource || {
                    data: [],
                    load: undefined,
                };
            }
 
            function createLoad(rawLoad) {
                return async function (params = {}) {
                    const res = await rawLoad(params);
                    if (Array.isArray(res)) {
                        final.data = res;
                    } else Eif (Array.isArray(res.list)) {
                        final.data = res.list;
                    } else if (res.content) {
                        final.data = res.content;
                    } else {
                        final.data = res;
                    }
                };
            }
 
            if (Array.isArray(dataSource))
                final.data = dataSource;
            else Iif (typeof dataSource === 'string') {
                try {
                    return this.normalizeDataSource(JSON.parse(dataSource));
                } catch (err) {
                    console.error(err);
                }
            } else Iif (dataSource instanceof Object && dataSource.hasOwnProperty('list') && Array.isArray(dataSource.list)) {
                final.data = dataSource.list;
            } else if (typeof dataSource === 'function')
                final.load = createLoad(dataSource);
 
            return final;
        },
        load(params) {
            this.$emit('before-load', undefined, this);
            this.loading = true;
            this.currentDataSource.load(params)
                .then(() => {
                    this.$emit('load', undefined, this);
                })
                .finally(() => {
                    this.loading = false;
                });
        },
        reload() {
            // 数据源不是function的时候,调用reload会报错,进行容错处理
            if (this.currentDataSource.load)
                this.load();
        },
    },
};