Completed
Pull Request — master (#5)
by Ronan
07:31
created

Constraint.js ➔ define   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 105
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
dl 0
loc 105
rs 8.92
c 1
b 0
f 0
cc 1
nc 1
nop 2

1 Function

Rating   Name   Duplication   Size   Complexity  
B Constraint.js ➔ ... ➔ JsonGeneratorRendererConstraint 0 97 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
define(
2
    [
3
        'flagbit/JsonGenerator/Observer',
4
        'jquery'
5
    ],
6
    function(JsonGeneratorObserver, jQuery) {
7
8
        /**
9
         * @class
10
         */
11
        var JsonGeneratorRendererConstraint = function($editable, $container) {
12
13
            /**
14
             * @public
15
             * @type {JsonGeneratorObserver}
16
             */
17
            this.observer = new JsonGeneratorObserver();
18
19
            /**
20
             * @public
21
             * @param {Object} $data
22
             */
23
            this.render = function($data) {
24
25
                var $options = ['NotBlank', 'Blank', 'NotNull', 'IsNull', 'IsTrue', 'IsFalse', 'Email', 'Url', 'Ip', 'Uuid', 'Date', 'DateTime', 'Time', 'Language', 'Locale', 'Country', 'Currency', 'Luhn', 'Iban', 'Isbn', 'Issn'];
26
27
                var $dropdown = createDropdown();
28
29
                $options.forEach(function($value) {
30
31
                    var $option = document.createElement('option');
32
                    $option.value = $value;
33
                    $option.innerText = $value;
34
35
                    if($value in $data) {
36
                        $option.selected = true;
37
                    }
38
39
                    $dropdown.appendChild($option);
40
                });
41
42
                var $select2 = jQuery($dropdown).select2();
43
44
                observeChanges($select2);
45
            };
46
47
48
            /**
49
             * @public
50
             * @returns {Object}
51
             */
52
            this.read = function() {
53
54
                var $data = {};
55
56
                var $collection = $container.querySelector('select').querySelectorAll('option');
57
58
                for(var $i in $collection) {
59
                    if($collection.hasOwnProperty($i)) {
60
                        var $option = $collection[$i];
61
                        if($option.selected) {
62
                            $data[$option.value] = {};
63
                        }
64
                    }
65
                }
66
67
                return $data;
68
            };
69
70
71
            /**
72
             * @protected
73
             * @param {String} $name
74
             * @return {HTMLSelectElement}
75
             */
76
            var createDropdown = function() {
77
78
                var $dropdown = document.createElement('select');
79
                $dropdown.style.display = 'block';
80
                $dropdown.multiple = true;
81
                $container.appendChild($dropdown);
82
83
                if(!$editable) {
84
                    $dropdown.disabled = true;
85
                }
86
87
                return $dropdown;
88
            }.bind(this);
0 ignored issues
show
unused-code introduced by
The call to bind does not seem necessary since the function does not use this. Consider calling it directly.
Loading history...
89
90
91
            /**
92
             * @protected
93
             * @param {HTMLSelectElement} $dropdown
94
             */
95
            var observeChanges = function($select) {
96
                $select.on('change', notify);
97
            }.bind(this);
0 ignored issues
show
unused-code introduced by
The call to bind does not seem necessary since the function does not use this. Consider calling it directly.
Loading history...
98
99
100
            /**
101
             * @protected
102
             */
103
            var notify = function() {
104
105
                this.observer.notify('update');
106
            }.bind(this);
107
        };
108
109
        return JsonGeneratorRendererConstraint;
110
    }
111
);