| Conditions | 1 |
| Paths | 1 |
| Total Lines | 97 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | define( |
||
| 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); |
||
|
|
|||
| 89 | |||
| 90 | |||
| 91 | /** |
||
| 92 | * @protected |
||
| 93 | * @param {HTMLSelectElement} $dropdown |
||
| 94 | */ |
||
| 95 | var observeChanges = function($select) { |
||
| 96 | $select.on('change', notify); |
||
| 97 | }.bind(this); |
||
| 98 | |||
| 99 | |||
| 100 | /** |
||
| 101 | * @protected |
||
| 102 | */ |
||
| 103 | var notify = function() { |
||
| 104 | |||
| 105 | this.observer.notify('update'); |
||
| 106 | }.bind(this); |
||
| 107 | }; |
||
| 108 | |||
| 111 | ); |