Conditions | 2 |
Paths | 2 |
Total Lines | 63 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 | <?php |
||
93 | protected function setupScript($show, $columns) |
||
94 | { |
||
95 | if (empty($show)) { |
||
96 | $show = $columns->keys()->toArray(); |
||
97 | } |
||
98 | |||
99 | $show = json_encode($show); |
||
100 | |||
101 | $script = <<<SCRIPT |
||
102 | |||
103 | var selected_columns = {$show}; |
||
104 | |||
105 | $('.column-selector .dropdown-menu a').on('click', function(event) { |
||
106 | |||
107 | var \$target = $( event.currentTarget ), |
||
108 | val = \$target.attr('data-value'), |
||
109 | \$inp = \$target.find('input'), |
||
110 | idx; |
||
111 | |||
112 | if ((idx = selected_columns.indexOf(val)) > -1) { |
||
113 | selected_columns.splice(idx, 1); |
||
114 | setTimeout(function() {\$inp.prop('checked', false)}, 0); |
||
115 | } else { |
||
116 | selected_columns.push(val); |
||
117 | setTimeout(function() {\$inp.prop('checked', true)}, 0); |
||
118 | } |
||
119 | |||
120 | $(event.target).blur(); |
||
121 | |||
122 | return false; |
||
123 | }); |
||
124 | |||
125 | $('.column-select-submit').on('click', function () { |
||
126 | if (selected_columns.length == 0) { |
||
127 | return; |
||
128 | } |
||
129 | |||
130 | var url = new URL(location); |
||
131 | |||
132 | // select all |
||
133 | if ($('.column-selector .dropdown-menu a input').length == selected_columns.length) { |
||
134 | url.searchParams.delete('_columns_'); |
||
135 | } else { |
||
136 | url.searchParams.set('_columns_', selected_columns.join()); |
||
137 | } |
||
138 | |||
139 | $.pjax({container:'#pjax-container', url: url.toString() }); |
||
140 | }); |
||
141 | |||
142 | $('.column-select-all').on('click', function () { |
||
143 | selected_columns = []; |
||
144 | $('.column-selector .dropdown-menu a input').prop('checked', true); |
||
145 | $('.column-selector .dropdown-menu a').each(function (_, val) { |
||
146 | selected_columns.push($(val).data('value')); |
||
147 | }); |
||
148 | |||
149 | return false; |
||
150 | }); |
||
151 | |||
152 | SCRIPT; |
||
153 | |||
154 | Admin::script($script); |
||
155 | } |
||
156 | } |