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

config-hide-fields-component.js ➔ define   B

Complexity

Conditions 1
Paths 2

Size

Total Lines 81

Duplication

Lines 81
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 2
nop 1
dl 81
loc 81
rs 8.8076
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A config-hide-fields-component.js ➔ ... ➔ BaseComponent.extend.dispose 9 9 2
A config-hide-fields-component.js ➔ ... ➔ BaseComponent.extend.initialize 9 9 1
A config-hide-fields-component.js ➔ ... ➔ BaseComponent.extend.updateDependentFields 16 16 1
A config-hide-fields-component.js ➔ ... ➔ ConfigHideFieldsComponent 3 3 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 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 ConfigHideFieldsComponent;
5
    var _ = require('underscore');
6
    var $ = require('jquery');
7
    var BaseComponent = require('oroui/js/app/components/base/component');
8
9
    ConfigHideFieldsComponent = BaseComponent.extend({
10
        /**
11
         * @property {Object}
12
         */
13
        options: {
14
            selectors: {
15
                row_container: '.control-group'
16
            }
17
        },
18
19
        /**
20
         * @property {jQuery}
21
         */
22
        $form: null,
23
24
        /**
25
         * @property {jQuery}
26
         */
27
        $el: null,
28
29
        /**
30
         * @inheritDoc
31
         */
32
        constructor: function ConfigHideFieldsComponent() {
33
            ConfigHideFieldsComponent.__super__.constructor.apply(this, arguments);
34
        },
35
36
        /**
37
         * @inheritDoc
38
         */
39
        initialize: function(options) {
40
            this.options = _.defaults(options || {}, this.options);
41
42
            this.$el = this.options._sourceElement;
43
            this.$form = this.$el.closest('form');
44
45
            this.updateDependentFields();
46
            this.$el.on('change', $.proxy(this.updateDependentFields, this));
47
        },
48
49
        /**
50
         * @inheritDoc
51
         */
52
        dispose: function() {
53
            if (this.disposed) {
54
                return;
55
            }
56
57
            this.$el.off('change', $.proxy(this.updateDependentFields, this));
58
59
            ConfigHideFieldsComponent.__super__.dispose.call(this);
60
        },
61
62
        updateDependentFields: function() {
63
            var id = this.$el.data('dependency-id');
64
            var value = this.$el.val();
65
            _.each(
66
                this.$form.find('[data-depends-on-field="' + id + '"]'),
67
                function(item) {
68
                    var $item = $(item);
69
                    if ($item.data('depends-on-field-value') === value) {
70
                        $item.closest(this.options.selectors.row_container).show();
71
                    } else {
72
                        $item.closest(this.options.selectors.row_container).hide();
73
                    }
74
                },
75
                this
76
            );
77
        }
78
    });
79
80
    return ConfigHideFieldsComponent;
81
});
82