Test Setup Failed
Push — master ( 6d5f74...30c942 )
by
unknown
03:41
created

$.widget(ꞌoroproduct.zoomWidgetꞌ)._ensureLoadedZoomInit   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 9.4285
1
define(function(require) {
2
    'use strict';
3
4
    var $ = require('jquery');
5
    var _ = require('underscore');
6
    require('jquery-elevatezoom');
7
8
    $.widget('oroproduct.zoomWidget', {
9
        options: {
10
            scrollZoom: true,
11
            zoomWindowPosition: 'zoom-container',
12
            minZoomWindowWidth: 376,
13
            maxZoomWindowWidth: 700,
14
            zoomWindowWidth: 376,
15
            zoomWindowHeight: 376,
16
            zoomWindowFadeIn: 400,
17
            zoomLevel: 0.4,
18
            maxZoomLevel: 0.8,
19
            borderSize: 1,
20
            borderColour: '#ebebeb',
21
            lensBorderColour: '#7d7d7d',
22
            lensColour: '#000',
23
            lensOpacity: 0.22
24
        },
25
26
        _init: function() {
27
            this.options.onZoomedImageLoaded = _.bind(this._resetImageProperty, this);
28
29
            // Bind activeImage event of slick gallery
30
            this.element.on('slider:activeImage', _.bind(function(e, activeImage) {
31
                if (!this.element.is(activeImage)) {
32
                    this._removeZoomContainer();
33
                    this._ensureLoadedZoomInit(activeImage);
34
                }
35
            }, this));
36
37
            var initImage = this.element.data('slider:activeImage') || this.element.get(0);
38
            this._ensureLoadedZoomInit(initImage);
39
        },
40
41
        /**
42
         * Inits of elevatezoom on image that completely loaded
43
         *
44
         * @param {HTMLElement} activeImage
45
         * @private
46
         */
47
        _ensureLoadedZoomInit: function(activeImage) {
48
            if (activeImage.complete) {
49
                this._zoomInit(activeImage);
50
            } else {
51
                $(activeImage).one('load', this._zoomInit.bind(this, activeImage));
52
            }
53
        },
54
55
        /**
56
         * Init of elevatezoom and set needed options
57
         *
58
         * @param {HTMLElement} activeImage
59
         * @private
60
         */
61
        _zoomInit: function(activeImage) {
62
            this.element = $(activeImage);
63
            this._setZoomWindowSize(activeImage);
64
            this.element.elevateZoom(this.options);
65
        },
66
67
        /**
68
         * Reset image property for correct works with small images
69
         *
70
         * @private
71
         */
72
        _resetImageProperty: function() {
73
            var imageZoomData = this.element.data('elevateZoom');
74
            var imageLargeWidth = imageZoomData.largeWidth;
75
            var imageLargeHeight = imageZoomData.largeHeight;
76
77
            var zoomWindowWidth = this.options.zoomWindowWidth;
78
            var zoomWindowHeight = this.options.zoomWindowHeight;
79
80
            // Check if image has small size
81
            if (imageLargeWidth <= zoomWindowWidth || imageLargeHeight <= zoomWindowHeight) {
82
                // Increase large size of small image
83
                var newImageZoomWidth = imageLargeWidth * 3;
84
                var newImageZoomHeight = imageLargeHeight * 3;
85
86
                imageZoomData.largeWidth = newImageZoomWidth;
87
                imageZoomData.largeHeight = newImageZoomHeight;
88
89
                // Should remove old zoom containers from DOM
90
                $('.zoomWindowContainer').remove();
91
                $('.zoomContainer').remove();
92
93
                // Call to internal method of elevatezoom for reinit zoom containers
94
                imageZoomData.startZoom();
95
            }
96
        },
97
98
        /**
99
         * Set size of zoom window
100
         *
101
         * @param {HTMLElement} activeImage
102
         * @private
103
         */
104
        _setZoomWindowSize: function(activeImage) {
105
            var imageWidth = $(activeImage).width();
106
            var imageHeight = $(activeImage).height();
107
108
            var zoomWindowHeight = this.options.zoomWindowHeight;
109
110
            var maxZoomWindowWidth = this.options.maxZoomWindowWidth;
111
            var minZoomWindowWidth = this.options.minZoomWindowWidth;
112
113
            // Calculate proportional size of zoom window
114
            var proportionalWidth = zoomWindowHeight * imageWidth / imageHeight;
115
116
            // Check max zoom window width
117
            proportionalWidth = proportionalWidth > maxZoomWindowWidth ? maxZoomWindowWidth : proportionalWidth;
118
119
            // Check min zoom window width
120
            proportionalWidth = proportionalWidth < minZoomWindowWidth ? minZoomWindowWidth : proportionalWidth;
121
122
            // Set proportionalWidth for zoom window
123
            this.options.zoomWindowWidth = proportionalWidth;
124
        },
125
126
        /**
127
         * Remove all zoom data and DOM elements of zoom widget
128
         *
129
         * @private
130
         */
131
        _removeZoomContainer: function() {
132
            var elevateZoom = this.element.data('elevateZoom');
133
134
            if (elevateZoom && elevateZoom.zoomContainer) {
135
                elevateZoom.zoomContainer.remove();
136
                elevateZoom.zoomWindow.parent().remove();
137
            }
138
139
            this.element.removeData('elevateZoom');
140
            this.element.removeData('zoomImage');
141
        },
142
143
        /**
144
         * Remove all zoom data from instans and unbind all zoom events
145
         *
146
         * @private
147
         */
148
        _destroy: function() {
149
            this._removeZoomContainer();
150
            $('.' + this.element[0].className).off('slider:activeImage');
151
            this.element.unbind('mousemove touchmove touchend mousewheel DOMMouseScroll MozMousePixelScroll');
152
        }
153
    });
154
155
    return 'zoomWidget';
156
});
157