Completed
Push — master ( 48107f...eccdf7 )
by Mario
04:35
created

Views/responsive/frontend/_public/src/js/jquery.klarna_checkout.js   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 118

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 118
rs 8.2857

10 Functions

Rating   Name   Duplication   Size   Complexity  
A $.plugin(ꞌswagKlarnaCheckoutꞌ).onChangeTabs 0 15 1
A $(document).ready 0 3 1
A $.plugin(ꞌswagKlarnaCheckoutꞌ).reloadScript 0 8 1
A $.plugin(ꞌswagKlarnaCheckoutꞌ).onSelectB2b 0 8 2
A $.plugin(ꞌswagKlarnaCheckoutꞌ).changePayment 0 5 1
A $.plugin(ꞌswagKlarnaCheckoutꞌ).onChangePayment 0 5 1
A $.plugin(ꞌswagKlarnaCheckoutꞌ).init 0 12 1
A $.plugin(ꞌswagKlarnaCheckoutꞌ).onChangeRadio 0 3 1
A $.plugin(ꞌswagKlarnaCheckoutꞌ).registerEvents 0 10 1
A $.plugin(ꞌswagKlarnaCheckoutꞌ).destroy 0 5 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
;(function ($, undefined) {
0 ignored issues
show
Unused Code introduced by
The parameter undefined is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
2
3
    /** Plugin constructor */
4
    $.plugin('swagKlarnaCheckout', {
5
        init: function () {
6
            var me = this;
7
8
            me.$klarnaTab = $('.label-klarna-register');
9
            me.$b2bSelect = $('#b2bselect');
10
            me.klarnaLink = me.$klarnaTab.find('a');
11
            me.$otherPayments = $('.select--other-payments');
12
            me.$paymentForm = $('.klarna--select-form');
13
            me.$autoSubmit = $('.klarna--auto-submit');
14
15
            me.registerEvents();
16
        },
17
18
        /**
19
         * Helper method to register the needed events
20
         */
21
        registerEvents: function () {
22
            var me = this;
23
24
            me._on(me.$klarnaTab, me.getEventName('click'), $.proxy(me.onChangeTabs, me));
25
            me._on(me.$otherPayments, me.getEventName('click'), $.proxy(me.onChangePayment, me));
26
            me._on(me.$b2bSelect, me.getEventName('change'), $.proxy(me.onSelectB2b, me));
27
            me._on(me.$autoSubmit, me.getEventName('change'), $.proxy(me.onChangeRadio, me));
28
29
            $.subscribe('plugin/swAjaxVariant/onRequestData', $.proxy(me.reloadScript, me));
30
        },
31
32
        /**
33
         * Method to change the dispatch.
34
         * It simply submits the wrapping form to reload the site.
35
         * @param event
36
         */
37
        onChangeRadio: function (event) {
38
            $(event.currentTarget).parents('form').submit();
39
        },
40
41
        /**
42
         * This method is called when the user clicks on 'other payments' in the klarna checkout.
43
         */
44
        onChangePayment: function () {
45
            var me = this;
46
47
            me.changePayment();
48
        },
49
50
        /**
51
         * This method is called when the user clicks on 'Company' in the b2b-select in the klarna checkout.
52
         *
53
         * @param event
54
         */
55
        onSelectB2b: function (event) {
56
            var me = this,
57
                $el = $(event.currentTarget);
58
59
            if ($el.find(":selected").hasClass('is--b2b')) {
60
                me.changePayment();
61
            }
62
        },
63
64
        /**
65
         * This method is called when the user clicks on klarna checkout tab in registration page.
66
         *
67
         * @param event
68
         */
69
        onChangeTabs: function (event) {
70
            var me = this,
71
                form = $('.register--form').serialize(),
72
                link = me.klarnaLink.attr('href');
73
74
            event.preventDefault();
75
76
            $.ajax({
77
                type: "POST",
78
                url: $(event.currentTarget).attr('data-form-url'),
79
                data: form
80
            }).done(function () {
81
                window.location = link;
82
            });
83
        },
84
85
        /**
86
         * Helper method to actually submit the "other payments"-form upon either clicking on "other payments" or
87
         * clicking on "company" in the b2b-select.
88
         */
89
        changePayment: function () {
90
            var me = this;
91
92
            me.$paymentForm.submit();
93
        },
94
95
        /**
96
         * Needed to reload the klarna-widgets on the detail page after changing the ajax-variant.
97
         */
98
        reloadScript: function () {
99
            var me = this,
0 ignored issues
show
Unused Code introduced by
The variable me seems to be never used. Consider removing it.
Loading history...
100
                script = $('.klarna--widget-script'),
101
                src = script.attr('src');
102
103
            script.after($('<script>').addClass('klarna--widget-script').attr('src', src));
104
            script.remove();
105
        },
106
107
        /** Destroys the plugin */
108
        destroy: function () {
109
            $.unsubscribe('plugin/swAjaxVariant/onRequestData');
110
111
            this._destroy();
112
        }
113
    });
114
115
    $(document).ready(function () {
116
        $('body').swagKlarnaCheckout();
117
    });
118
})(jQuery);