Passed
Push — master ( 6e6c55...72b2aa )
by Christian
11:52 queued 10s
created

sw-cms/elements/image-gallery/component/index.js (1 issue)

Labels
Severity
1
import template from './sw-cms-el-image-gallery.html.twig';
2
import './sw-cms-el-image-gallery.scss';
3
4
const { Component, Mixin, Utils, Filter } = Shopware;
0 ignored issues
show
The variable Shopware seems to be never declared. If this is a global, consider adding a /** global: Shopware */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
5
6
Component.register('sw-cms-el-image-gallery', {
7
    template,
8
9
    mixins: [
10
        Mixin.getByName('cms-element')
11
    ],
12
13
    data() {
14
        return {
15
            galleryLimit: 3,
16
            activeMedia: null
17
        };
18
    },
19
20
    computed: {
21
        currentDeviceView() {
22
            return this.cmsPageState.currentCmsDeviceView;
23
        },
24
25
        galleryPositionClass() {
26
            return `is--preview-${this.element.config.galleryPosition.value}`;
27
        },
28
29
        currentDeviceViewClass() {
30
            if (this.currentDeviceView) {
31
                return `is--${this.currentDeviceView}`;
32
            }
33
34
            return null;
35
        },
36
37
        verticalAlignStyle() {
38
            if (!this.element.config.verticalAlign.value) {
39
                return null;
40
            }
41
42
            return `align-content: ${this.element.config.verticalAlign.value};`;
43
        },
44
45
        mediaUrls() {
46
            if (Utils.get(this.element, 'config.sliderItems.source') === 'mapped') {
47
                return this.getDemoValue(this.element.config.sliderItems.value) || [];
48
            }
49
50
            return Utils.get(this.element, 'data.sliderItems') || [];
51
        },
52
53
        isProductPage() {
54
            return Utils.get(this.cmsPageState, 'currentPage.type', '') === 'product_detail';
55
        },
56
57
        assetFilter() {
58
            return Filter.getByName('asset');
59
        }
60
    },
61
62
    watch: {
63
        currentDeviceView() {
64
            if (this.currentDeviceView === 'mobile') {
65
                this.galleryLimit = 0;
66
            }
67
68
            // timeout due to css transition 0.4s
69
            setTimeout(() => {
70
                this.setGalleryLimit();
71
            }, 400);
72
        },
73
74
        'element.config.galleryPosition.value': {
75
            deep: true,
76
            handler() {
77
                this.$nextTick(() => {
78
                    this.setGalleryLimit();
79
                });
80
            }
81
        },
82
83
        'element.config.sliderItems.value': {
84
            handler(value) {
85
                if (!value) {
86
                    this.element.config.sliderItems.value = [];
87
                    return;
88
                }
89
90
                this.$nextTick(() => {
91
                    this.setGalleryLimit();
92
                });
93
            }
94
        }
95
    },
96
97
    created() {
98
        this.createdComponent();
99
    },
100
101
    mounted() {
102
        this.mountedComponent();
103
    },
104
105
    methods: {
106
        createdComponent() {
107
            this.initElementConfig('image-gallery');
108
            this.initElementData('image-gallery');
109
110
            if (!this.isProductPage || Utils.get(this.element, 'translated.config')) {
111
                return;
112
            }
113
114
            this.element.config.sliderItems.source = 'mapped';
115
            this.element.config.sliderItems.value = 'product.media';
116
            this.element.config.navigationDots.value = 'inside';
117
            this.element.config.zoom.value = true;
118
            this.element.config.fullScreen.value = true;
119
            this.element.config.displayMode.value = 'contain';
120
            this.element.config.minHeight.value = '430px';
121
        },
122
123
        mountedComponent() {
124
            this.setGalleryLimit();
125
        },
126
127
        getPlaceholderItems() {
128
            return [
129
                { url: this.assetFilter('administration/static/img/cms/preview_mountain_large.jpg') },
130
                { url: this.assetFilter('administration/static/img/cms/preview_glasses_large.jpg') },
131
                { url: this.assetFilter('administration/static/img/cms/preview_plant_large.jpg') }
132
            ];
133
        },
134
135
        onChangeGalleryImage(mediaItem, index = 0) {
136
            mediaItem.sliderIndex = index;
137
            this.activeMedia = mediaItem;
138
        },
139
140
        activeMediaClass(mediaItem) {
141
            if (!this.activeMedia) {
142
                return null;
143
            }
144
145
            return {
146
                'is--active': mediaItem.id === this.activeMedia.id
147
            };
148
        },
149
150
        setGalleryLimit() {
151
            if (this.element.config.sliderItems.value.length === 0) {
152
                return;
153
            }
154
155
            let boxSpace = 0;
156
            let elSpace = 0;
157
            const elGap = 8;
158
            const arrowAndGapWidth = 36;
159
160
            if (this.element.config.galleryPosition.value === 'underneath') {
161
                boxSpace = this.$refs.galleryItemHolder.offsetWidth - arrowAndGapWidth;
162
                elSpace = 92;
163
            } else {
164
                boxSpace = this.$refs.galleryItemHolder.offsetHeight;
165
                elSpace = 64;
166
            }
167
168
            this.galleryLimit = Math.floor(boxSpace / (elSpace + elGap));
169
        }
170
    }
171
});
172