Passed
Push — trunk ( 1572c3...2f9940 )
by Christian
15:38 queued 17s
created

StateStyleService.addStyle   B

Complexity

Conditions 6

Size

Total Lines 28
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 22
dl 0
loc 28
rs 8.4186
c 0
b 0
f 0
1
const { deepCopyObject } = Shopware.Utils.object;
2
3
type variantKeys = 'neutral' | 'progress' | 'done' | 'warning' | 'danger';
4
5
type style = {
6
    icon: variantKeys;
7
    color: variantKeys;
8
    variant: variantKeys;
9
}
10
11
type storedStyle = {
12
    selectBackgroundStyle: string;
13
    iconBackgroundStyle: string;
14
    icon: string;
15
    variant: string;
16
    colorCode: string;
17
    iconStyle: string;
18
}
19
20
type store = {
21
    [key: string]: storedStyle | {
22
        [key: string]: storedStyle
23
    },
24
};
25
26
// eslint-disable-next-line sw-deprecation-rules/private-feature-declarations
27
export type stateStyleService = {
28
    getPlaceholder: () => storedStyle;
29
    getStyle: (stateMachine: string, state: string) => (style);
30
    addStyle: (stateMachine: string, state: string, style: style) => void
31
};
32
33
/**
34
 * @package admin
35
 *
36
 * @memberOf module:core/service/login
37
 * @constructor
38
 * @method createStateStyleService
39
 * @returns {Object}
40
 */
41
// eslint-disable-next-line sw-deprecation-rules/private-feature-declarations
42
export default class StateStyleService {
43
    $store: store = {
44
        placeholder: {
45
            icon: 'regular-chevron-down-xxs',
46
            iconStyle: 'sw-order-state__bg-neutral-icon',
47
            iconBackgroundStyle: 'sw-order-state__bg-neutral-icon-bg',
48
            selectBackgroundStyle: 'sw-order-state__bg-neutral-select',
49
            variant: 'neutral',
50
            colorCode: '#94a6b8',
51
        },
52
    };
53
54
    $icons = {
55
        neutral: 'regular-chevron-down-xxs',
56
        progress: 'regular-circle-xxs',
57
        warning: 'regular-exclamation-s',
58
        done: 'regular-checkmark-xxs',
59
        danger: 'regular-times-xs',
60
    };
61
62
    $colors = {
63
        neutral: 'sw-order-state__neutral',
64
        progress: 'sw-order-state__progress',
65
        done: 'sw-order-state__success',
66
        warning: 'sw-order-state__warning',
67
        danger: 'sw-order-state__danger',
68
    };
69
70
    $colorCodes = {
71
        neutral: '#94a6b8',
72
        progress: '#189eff',
73
        done: '#37d046',
74
        warning: '#ffab22',
75
        danger: '#de294c',
76
    };
77
78
    $variants = {
79
        neutral: 'neutral',
80
        progress: 'info',
81
        done: 'success',
82
        warning: 'warning',
83
        danger: 'danger',
84
    };
85
86
    getPlaceholder(): storedStyle {
87
        return this.$store.placeholder as storedStyle;
88
    }
89
90
    addStyle(stateMachine: string, state: string, style: style): void {
91
        if (!(stateMachine in this.$store)) {
92
            this.$store[stateMachine] = {};
93
        }
94
95
        const entry = deepCopyObject(this.getPlaceholder());
96
97
        if (style.icon in this.$icons) {
98
            entry.icon = this.$icons[style.icon];
99
        }
100
101
        if (style.color in this.$colors) {
102
            entry.iconStyle = `${this.$colors[style.color]}-icon`;
103
            entry.iconBackgroundStyle = `${this.$colors[style.color]}-icon-bg`;
104
            entry.selectBackgroundStyle = `${this.$colors[style.color]}-select`;
105
        }
106
107
        if (style.color in this.$colorCodes) {
108
            entry.colorCode = this.$colorCodes[style.color];
109
        }
110
111
        if (style.variant in this.$variants) {
112
            entry.variant = this.$variants[style.variant];
113
        }
114
115
        // @ts-expect-error
116
        this.$store[stateMachine][state] = entry;
117
    }
118
119
    getStyle(stateMachine: string, state: string): storedStyle {
120
        if (stateMachine in this.$store && state in this.$store[stateMachine]) {
121
            // @ts-expect-error
122
            return this.$store[stateMachine][state] as storedStyle;
123
        }
124
125
        return this.getPlaceholder();
126
    }
127
}
128