Completed
Push — master ( 3d8e2d...b3f37b )
by Danila
02:44
created

skin/frontend/base/default/_js/yandexdelivery.js   A

Complexity

Total Complexity 22
Complexity/F 1.16

Size

Lines of Code 104
Function Count 19

Duplication

Duplicated Lines 104
Ratio 100 %

Importance

Changes 0
Metric Value
cc 0
nc 1
dl 104
loc 104
rs 10
c 0
b 0
f 0
wmc 22
mnd 2
bc 24
fnc 19
bpm 1.263
cpm 1.1578
noi 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
/* Mygento_Yandexdelivery
2
 * http://www.mygento.ru
3
 * Copyright © 2017 NKS LLC
4
 * License GPLv2 */
5
6
jQuery(function () {
7
    function autocomp(name)
8
    {
9
        jQuery('input[name="' + name + '[city]"]').autocomplete({
10
            source: function (request, response) {
11
                jQuery.ajax({
12
                    url: MYGENTO_YANDEXDELIVERY_BASE_URL + "/yandexdelivery/index/json/?callback=?",
13
                    dataType: "jsonp",
14
                    data: {
15
                        search: function () {
16
                            return jQuery('input[name="' + name + '[city]"]').val();
17
                        },
18
                        type: 'locality'
19
                    },
20
                    success: function (data) {
21
                        response(jQuery.map(data.jsonresult, function (item) {
22
                            return {
23
                                label: item.label,
24
                                value: item.value
25
                            };
26
                        }));
27
                    }
28
                });
29
            },
30
            minLength: 3,
31
            close: function () {
32
            },
33
            select: function (event, ui) {
34
                jQuery('input[name="' + name + '[city]"]').val(ui.item.value);
35
            }
36
        });
37
        jQuery('input[name="' + name + '[street][]"]').autocomplete({
38
            source: function (request, response) {
39
                jQuery.ajax({
40
                    url: MYGENTO_YANDEXDELIVERY_BASE_URL + "/yandexdelivery/index/json/?callback=?",
41
                    dataType: "jsonp",
42
                    data: {
43
                        city: function () {
44
                            return jQuery('input[name="' + name + '[city]"]').val();
45
                        },
46
                        search: function () {
47
                            return jQuery('input[name="' + name + '[street][]"]').val();
48
                        },
49
                        type: 'street'
50
                    },
51
                    success: function (data) {
52
                        response(jQuery.map(data.jsonresult, function (item) {
53
                            return {
54
                                label: item.label,
55
                                value: item.value
56
                            };
57
                        }));
58
                    }
59
                });
60
            },
61
            minLength: 3,
62
            close: function () {
63
            },
64
            select: function (event, ui) {
65
                jQuery('input[name="' + name + '[street]"]').val(ui.item.value);
66
            }
67
        });
68
    }
69
    autocomp('billing');
70
    autocomp('shipping');
71
});
72
function changeT()
73
{
74
    if (jQuery(".js-widget-delivery-type").length) {
75
        jQuery(".js-widget-delivery-type").click(function (e) {
76
            e.preventDefault();
77
            console.log(this);
78
            var deliveryType = jQuery(this).attr('data-delivery-type');
79
            console.log('setted delivery type -> ' + deliveryType);
80
            window.deliveryTypes = [deliveryType];
81
            ydwidget.cartWidget.changeDeliveryTypes();
82
        });
83
    }
84
}
85
function ydMethodAdd(name, title, buttonTitle)
86
{
87
    var bodyClass = jQuery('body').attr('class');
88
    if (bodyClass.indexOf('adminhtml' > 0)) {
89
        setTimeout(function () {
90
            console.log('probably adminhtml -> ' + title);
91
            var div = jQuery("dt:contains(" + title + ")");
92
            var ul = div.next();
93
            var last = ul.find('li:last');
94
            last.after('<li><input id="yd_delivery" type="radio" class="radio" name="shipping_method"><label class="js-widget-delivery-type" id="yd_label" data-ydwidget-open data-delivery-type="' + name + '" for="yd_delivery">' + buttonTitle + '</label></li>');
95
            changeT();
96
        }, 1000);
97
    } else {
98
        var dt = jQuery("#checkout-shipping-method-load dt:contains(" + title + ")");
99
        if (!dt.length > 0) {
100
            var div = jQuery("#onestepcheckout-shipping-methods div:contains(" + title + ")");
101
            var ul = div.next();
102
            var last = ul.find('li:last');
103
        } else {
104
            var dd = dt.next();
105
            var last = dd.find('ul li:last');
106
        }
107
        last.after('<input id="yd_delivery" type="radio" class="radio" name="shipping_method"><label class="js-widget-delivery-type" id="yd_label" data-ydwidget-open data-delivery-type="' + name + '" for="yd_delivery">' + buttonTitle + '</label>');
108
    }
109
}