Test Setup Failed
Push — master ( cd1dab...2a689d )
by
unknown
03:51
created

default-variant-collection-view.js ➔ define   B

Complexity

Conditions 1
Paths 4

Size

Total Lines 77

Duplication

Lines 77
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 4
nop 1
dl 77
loc 77
rs 8.9342
c 0
b 0
f 0

6 Functions

Rating   Name   Duplication   Size   Complexity  
A default-variant-collection-view.js ➔ ... ➔ DefaultVariantCollectionView 3 3 1
A default-variant-collection-view.js ➔ ... ➔ BaseView.extend.onDefaultChange 4 4 1
A default-variant-collection-view.js ➔ ... ➔ BaseView.extend.handleAdd 7 7 3
A default-variant-collection-view.js ➔ ... ➔ BaseView.extend.checkDefaultVariant 6 6 1
A default-variant-collection-view.js ➔ ... ➔ BaseView.extend.initialize 18 18 1
A default-variant-collection-view.js ➔ ... ➔ BaseView.extend.handleRemove 6 6 2

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 View Code Duplication
define(function(require) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2
    'use strict';
3
4
    var DefaultVariantCollectionView;
5
    var BaseView = require('oroui/js/app/views/base/view');
6
    var mediator = require('oroui/js/mediator');
7
    var $ = require('jquery');
8
    var _ = require('underscore');
9
10
    DefaultVariantCollectionView = BaseView.extend({
11
        $collection: null,
12
13
        options: {
14
            defaultSelector: '[name$="[default]"]',
15
            itemSelector: '[data-role="content-variant-item"]',
16
            defaultItemClass: 'content-variant-item-default'
17
        },
18
19
        /**
20
         * @inheritDoc
21
         */
22
        constructor: function DefaultVariantCollectionView() {
23
            DefaultVariantCollectionView.__super__.constructor.apply(this, arguments);
24
        },
25
26
        /**
27
         * @inheritDoc
28
         */
29
        initialize: function(options) {
30
            this.options = $.extend(true, {}, this.options, options || {});
31
32
            this.$el.on(
33
                'click',
34
                this.options.defaultSelector,
35
                _.bind(
36
                    function(e) {
37
                        this.onDefaultChange($(e.target));
38
                    },
39
                    this
40
                )
41
            );
42
            mediator.on('webcatalog:content-variant-collection:add', this.handleAdd, this);
43
            mediator.on('webcatalog:content-variant-collection:remove', this.handleRemove, this);
44
45
            this.handleAdd();
46
        },
47
48
        handleRemove: function($container) {
49
            // Check is default variant removed
50
            if ($container.find(this.options.defaultSelector + ':checked').length === 0) {
51
                this.checkDefaultVariant();
52
            }
53
        },
54
55
        handleAdd: function() {
56
            if (this.$el.find(this.options.itemSelector).length &&
57
                this.$el.find(this.options.defaultSelector + ':checked').length === 0
58
            ) {
59
                this.checkDefaultVariant();
60
            }
61
        },
62
63
        checkDefaultVariant: function() {
64
            var $default = this.$el.find(this.options.defaultSelector + ':not(:checked)').first();
65
            $default.prop('checked', true).trigger('change');
66
67
            this.onDefaultChange($default);
68
        },
69
70
        onDefaultChange: function($default) {
71
            this.$el.find('.' + this.options.defaultItemClass).removeClass(this.options.defaultItemClass);
72
            $default.closest(this.options.itemSelector).addClass(this.options.defaultItemClass);
73
        }
74
    });
75
76
    return DefaultVariantCollectionView;
77
});
78