Completed
Push — master ( a31174...af3b49 )
by Antonio
10s
created

src/Resources/public/js/Json/Renderer/Number.js   A

Complexity

Total Complexity 13
Complexity/F 1.86

Size

Lines of Code 118
Function Count 7

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
eloc 50
dl 0
loc 118
rs 10
c 2
b 0
f 0
cc 0
nc 1
mnd 2
bc 13
fnc 7
bpm 1.8571
cpm 1.8571
noi 2

1 Function

Rating   Name   Duplication   Size   Complexity  
B Number.js ➔ define 0 112 1
1
define(
2
    [
3
        'flagbit/JsonGenerator/Observer',
4
        'oro/translator',
5
    ],
6
    function(JsonGeneratorObserver, __) {
7
8
        /**
9
         * @class
10
         */
11
        var JsonGeneratorRendererNumber = 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
                if(!$data['is_decimal']) {
26
                    $data['is_decimal'] = 'false';
27
                }
28
29
                var $value = $data['is_decimal'];
30
31
                var $label = document.createElement('label');
32
                $label.innerText = __('flagbit_attribute_table_number_is_decimal_label');
33
                $container.appendChild($label);
34
35
                var $dropdown = createDropdown('is_decimal');
36
37
                var $options = {
38
                    'true': __('Yes'),
39
                    'false': __('No')
40
                };
41
42
                for(var $i in $options) {
43
                    if($options.hasOwnProperty($i)) {
44
                        var $option = document.createElement('option');
45
                        $option.value = $i;
46
                        $option.innerText = $options[$i];
47
                        $dropdown.appendChild($option);
48
                    }
49
                }
50
51
                $dropdown.value = $value;
52
53
            };
54
55
56
            /**
57
             * @public
58
             * @returns {Object}
59
             */
60
            this.read = function() {
61
62
                var $data = {};
63
64
                var $collection = $container.querySelectorAll('select');
65
                for(var $i in $collection) {
66
                    if($collection.hasOwnProperty($i)) {
67
                        var $dropdown = $collection[$i];
68
                        $data[$dropdown.name] = $dropdown.value === 'true';
69
                    }
70
                }
71
72
                return $data;
73
            };
74
75
76
            /**
77
             * @protected
78
             * @param {String} $name
79
             * @return {HTMLSelectElement}
80
             */
81
            var createDropdown = function($name) {
82
83
                var $dropdown = document.createElement('select');
84
                $dropdown.name = $name;
85
                $dropdown.style.display = 'block';
86
                $container.appendChild($dropdown);
87
88
                observeChanges($dropdown);
89
90
                if(!$editable) {
91
                    $dropdown.disabled = true;
92
                }
93
94
                return $dropdown;
95
            }.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...
96
97
98
            /**
99
             * @protected
100
             * @param {HTMLSelectElement} $dropdown
101
             */
102
            var observeChanges = function($dropdown) {
103
                $dropdown.addEventListener('change', notify);
104
            }.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...
105
106
107
            /**
108
             * @protected
109
             */
110
            var notify = function() {
111
112
                this.observer.notify('update');
113
            }.bind(this);
114
        };
115
116
        return JsonGeneratorRendererNumber;
117
    }
118
);
119