Passed
Push — trunk ( 024f41...cfd10b )
by Christian
15:08 queued 13s
created

form-field.mixin.ts ➔ handler   B

Complexity

Conditions 6

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 15
dl 0
loc 21
rs 8.6666
c 0
b 0
f 0
1
/**
2
 * @package admin
3
 */
4
5
import type { PropType } from 'vue';
6
7
/**
8
 * @deprecated tag:v6.6.0 - Will be private
9
 */
10
Shopware.Mixin.register('sw-form-field', {
11
    props: {
12
        mapInheritance: {
13
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
            type: Object as PropType<any>,
15
            required: false,
16
            default: null,
17
        },
18
    },
19
20
    computed: {
21
        boundExpression() {
22
            // @ts-expect-error - we check if model exists on vnode
23
            // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
24
            if (this.$vnode?.data?.model && this.$vnode?.data?.model?.expression) {
25
                // @ts-expect-error - we check if model exists on vnode
26
                // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-return
27
                return this.$vnode.data.model.expression;
28
            }
29
            return null;
30
        },
31
32
        formFieldName() {
33
            if (this.$attrs.name) {
34
                return this.$attrs.name;
35
            }
36
37
            // @ts-expect-error - name exists on main component
38
            if (this.name) {
39
                // @ts-expect-error - name exists on main component
40
                // eslint-disable-next-line @typescript-eslint/no-unsafe-return
41
                return this.name;
42
            }
43
44
            if (this.boundExpression) {
45
                // @ts-expect-error - we check if the value exists in boundExpression
46
                // eslint-disable-next-line max-len
47
                // eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/restrict-template-expressions
48
                return `sw-field--${this.$vnode?.data?.model?.expression.replace(/\./g, '-')}`;
49
            }
50
51
            return null;
52
        },
53
    },
54
55
    watch: {
56
        mapInheritance: {
57
            handler(mapInheritance) {
58
                if (!mapInheritance) {
59
                    return;
60
                }
61
62
                // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
63
                if (!mapInheritance?.isInheritField) {
64
                    return;
65
                }
66
67
                // set event listener and attributes for inheritance
68
                // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
69
                Object.keys(mapInheritance).forEach((prop) => {
70
                    // eslint-disable-next-line max-len
71
                    // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment
72
                    const propValue = mapInheritance[prop];
73
74
                    if (typeof propValue === 'function') {
75
                        this.setFunctionsForEvents(prop, propValue as () => void);
76
                    } else if (typeof propValue === 'boolean') {
77
                        this.setAttributesForProps(prop, propValue);
78
                    }
79
                });
80
            },
81
            deep: true,
82
            immediate: true,
83
        },
84
    },
85
86
    beforeDestroy() {
87
        this.beforeDestroyComponent();
88
    },
89
90
    methods: {
91
        beforeDestroyComponent() {
92
            // remove event listener
93
            this.$off('inheritance-restore');
94
            this.$off('inheritance-remove');
95
        },
96
97
        setFunctionsForEvents(prop: string, propValue: () => void) {
98
            switch (prop) {
99
                case 'restoreInheritance': {
100
                    this.$off('inheritance-restore');
101
                    this.$on('inheritance-restore', propValue);
102
                    break;
103
                }
104
105
                case 'removeInheritance': {
106
                    this.$off('inheritance-remove');
107
                    this.$on('inheritance-remove', propValue);
108
                    break;
109
                }
110
111
                default: {
112
                    break;
113
                }
114
            }
115
        },
116
117
        setAttributesForProps(prop: string, propValue: boolean) {
118
            switch (prop) {
119
                case 'isInherited': {
120
                    this.$set(this.$attrs, prop, propValue);
121
                    break;
122
                }
123
124
                case 'isInheritField': {
125
                    this.$set(this.$attrs, 'isInheritanceField', propValue);
126
                    break;
127
                }
128
129
                default: {
130
                    break;
131
                }
132
            }
133
        },
134
    },
135
});
136
137
// eslint-disable-next-line sw-deprecation-rules/private-feature-declarations
138
export default {};
139