Resources/views/frontend/_public/src/js/jquery.variant-switch.js   A
last analyzed

Complexity

Total Complexity 26
Complexity/F 1.63

Size

Lines of Code 251
Function Count 16

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 16
Bugs 1 Features 1
Metric Value
cc 0
nc 1
dl 0
loc 251
rs 10
c 16
b 1
f 1
wmc 26
mnd 2
bc 27
fnc 16
bpm 1.6875
cpm 1.625
noi 0

10 Functions

Rating   Name   Duplication   Size   Complexity  
A $.plugin(ꞌdnVariantSwitchꞌ).registerEvents 0 9 1
A $.plugin(ꞌdnVariantSwitchꞌ).init 0 14 1
B $.subscribe(ꞌplugin/swAjaxVariant/onRequestDataꞌ) 0 31 3
A $.plugin(ꞌdnVariantSwitchꞌ).removeDetailElements 0 4 1
A $.plugin(ꞌdnVariantSwitchꞌ).onSubmit 0 64 1
A $.plugin(ꞌdnVariantSwitchꞌ).onClose 0 8 1
A $.plugin(ꞌdnVariantSwitchꞌ).destroy 0 11 2
A $.subscribe(ꞌplugin/swAjaxVariant/onBeforeRequestDataꞌ) 0 11 2
A $.plugin(ꞌdnVariantSwitchꞌ).setBuyboxQuantity 0 12 3
B $.plugin(ꞌdnVariantSwitchꞌ).onBuyboxSubmit 0 41 1
1
;(function($, window, document) {
2
    'use strict';
3
4
    $.plugin('dnVariantSwitch', {
5
6
        defaults: {
7
8
            switchUrl: '',
9
10
            detailId: 0,
11
12
            productUrl: '',
13
14
            productQuery: '',
15
16
            quantity: 1,
17
18
            offCanvas: false,
19
20
            modalWidth: 1024,
21
22
            zIndex: 8001,
23
24
            modalContentOuterCls: 'product--details ajax-modal--custom'
25
26
        },
27
28
        init: function () {
29
            var me = this;
30
31
            me.applyDataAttributes();
32
33
            me._isOpened = false;
34
35
            me.opts.modal = $.extend({}, Object.create($.modal.defaults), me.opts);
36
            me.opts.modal.additionalClass = 'switch-variant--modal';
37
            me.opts.modal.width = me.opts.modalWidth;
38
            me.opts.modal.overlay = !me.opts.offCanvas;
39
40
            me.registerEvents();
41
        },
42
43
        registerEvents: function() {
44
            var me = this;
45
46
            me._on(me.$el, 'submit', $.proxy(me.onSubmit, me));
47
48
            $.subscribe(me.getEventName('plugin/swModal/onClose'), $.proxy(me.onClose, me));
49
50
            $.publish('plugin/dnVariantSwitch/onRegisterEvents', [ me ]);
51
        },
52
53
        onSubmit: function (event) {
54
            event.preventDefault();
55
56
            var me = this,
57
                target = me.opts.productUrl,
58
                query = me.opts.productQuery;
59
60
            $.loadingIndicator.open();
61
62
            $.ajax({
63
                url: target + query,
64
                type: "GET",
65
                dataType: "html",
66
                success: function (response) {
67
                    var $response = $($.parseHTML(response, document, true)),
68
                        $detail = $response.find('.product--detail-upper'),
69
                        index = $('*[data-variant-switch="true"]').index(me.$el);
70
71
                    $.loadingIndicator.close();
72
73
                    if (!$detail.length) {
74
                        return;
75
                    }
76
77
                    me.removeDetailElements($detail);
78
79
                    $.modal.open(
80
                        $('<div></div>')
81
                            .prop('class', me.opts.modalContentOuterCls)
82
                            .attr('data-index', index)
83
                            .attr('data-ajax-variants-container', 'true')
84
                            .attr('data-ajax-wishlist', 'true')
85
                            .append($detail[0].outerHTML)[0].outerHTML,
86
                        me.opts.modal
87
                    );
88
89
                    window.StateManager.addPlugin(
90
                        '*[data-ajax-variants-container="true"]',
91
                        'swAjaxVariant',
92
                        ['xs', 's', 'm', 'l', 'xl']
93
                    );
94
95
                    var $modal = $('.switch-variant--modal'),
96
                        $buyboxForm = $modal.find('*[data-add-article="true"]');
97
98
                    $modal.css('zIndex', me.opts.zIndex);
99
                    $modal.find('.modal--close').css('zIndex', 1001);
100
101
                    $modal.find('*[data-ajax-variants-container="true"]').data('plugin_swAjaxVariant')._getUrl = function () {
102
                       return target;
103
                    };
104
105
                    if (!$buyboxForm.length) {
106
                        return;
107
                    }
108
109
                    me._on($buyboxForm, 'submit', $.proxy(me.onBuyboxSubmit, me));
110
111
                    me.setBuyboxQuantity($buyboxForm);
112
                }
113
            });
114
115
            me._isOpened = true;
116
        },
117
118
        onBuyboxSubmit: function (event) {
119
            event.preventDefault();
120
121
            var me = this,
122
                data = $(event.target).serialize();
123
124
            $.loadingIndicator.open({
125
                closeOnClick: false,
126
                renderElement: '.switch-variant--modal .content'
127
            });
128
129
            $.ajax({
130
                'data': data + '&detailId=' + me.opts.detailId,
131
                'method': 'GET',
132
                'url': me.opts.switchUrl,
133
                'success': function () {
134
                    if (me.opts.offCanvas) {
135
                        var plugin = $('*[data-collapse-cart="true"]').data('plugin_swCollapseCart');
136
137
                        plugin.loadCart(function () {
138
                            $.modal.close();
139
                            plugin.openMenu();
140
141
                            window.StateManager.addPlugin(
142
                                '*[data-off-canvas-variant-switch="true"]',
143
                                'dnOffCanvasVariantSwitch',
144
                                ['xs', 's', 'm', 'l', 'xl']
145
                            );
146
                        });
147
148
                        return;
149
                    }
150
151
                    if (window.location.href.includes("addArticle")) {
152
                        window.location.href = window.location.href.replace("addArticle", "cart");
153
                    } else {
154
                        window.location.reload();
155
                    }
156
                }
157
            });
158
        },
159
160
        setBuyboxQuantity: function ($buyboxForm) {
161
            var me = this,
162
                $sQuantity = $buyboxForm.find('#sQuantity');
163
164
            if ($sQuantity.length) {
165
                var $option = $sQuantity.find('option[value="' + me.opts.quantity + '"]');
166
167
                if ($option) {
168
                    $option.prop('selected', true);
169
                }
170
            }
171
        },
172
173
        removeDetailElements: function ($el) {
174
            $el.find('*[data-product-compare-add="true"]').parent('form').remove();
175
            $el.find('*[data-show-tab="true"]').remove();
176
        },
177
178
        onClose: function () {
179
            var me = this;
180
181
            me._isOpened = false;
182
            $.loadingIndicator.close();
183
184
            $.publish('plugin/dnVariantSwitch/onClose', [ me ]);
185
        },
186
187
        destroy: function () {
188
            var me = this;
189
190
            if (me._isOpened) {
191
                $.modal.close();
192
            }
193
194
            $.unsubscribe(me.getEventName('plugin/swModal/onClose'));
195
196
            me._destroy();
197
        }
198
199
    });
200
201
    window.StateManager.addPlugin(
202
        '*[data-variant-switch="true"]',
203
        'dnVariantSwitch',
204
        ['xs', 's', 'm', 'l', 'xl']
205
    );
206
207
    $.subscribe("plugin/swAjaxVariant/onBeforeRequestData", function() {
208
        var $el = $('.switch-variant--modal');
209
210
        if ($el.length) {
211
            $.loadingIndicator.close();
212
            $.loadingIndicator.open({
213
                closeOnClick: false,
214
                renderElement: '.switch-variant--modal .content'
215
            });
216
        }
217
    });
218
219
    $.subscribe("plugin/swAjaxVariant/onRequestData", function(e, me) {
220
        var $modal = $('.switch-variant--modal'),
221
            index = me.$el.data('index');
222
223
        if ($modal.length) {
224
            $.loadingIndicator.close();
225
226
            var $buyboxForm = $modal.find('*[data-add-article="true"]'),
227
                plugin = $($('*[data-variant-switch="true"]').get(index)).data('plugin_dnVariantSwitch');
228
229
            if (!$buyboxForm.length) {
230
                return;
231
            }
232
233
            window.StateManager.removePlugin('.switch-variant--modal *[data-add-article="true"]', 'swAddArticle');
234
            $buyboxForm.data('plugin_swAddArticle').destroy();
235
236
            me._on($buyboxForm, 'submit', $.proxy(
237
                plugin.onBuyboxSubmit,
238
                plugin
239
            ));
240
241
            plugin.removeDetailElements($modal);
242
            plugin.setBuyboxQuantity($buyboxForm);
243
244
            me.hasHistorySupport = false;
245
            setTimeout(function(){
246
                me.hasHistorySupport = true;
247
            }, 50);
248
        }
249
    });
250
251
})(jQuery, window);