Total Complexity | 92 |
Complexity/F | 1.64 |
Lines of Code | 572 |
Function Count | 56 |
Duplicated Lines | 572 |
Ratio | 100 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like public/js/dashboard.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | View Code Duplication | $(function () { |
|
|
|||
2 | |||
3 | 'use strict'; |
||
4 | |||
5 | const LANGUAGE = $('html').attr('lang') || 'ru'; |
||
6 | |||
7 | $(document).ajaxStart(function () { |
||
8 | Pace.restart() |
||
9 | }); |
||
10 | |||
11 | $.ajaxSetup({ |
||
12 | headers: { |
||
13 | 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') |
||
14 | } |
||
15 | }); |
||
16 | |||
17 | $('.field-checkbox-ajax').each(function(){ |
||
18 | var $base = $(this), |
||
19 | url = $base.data('url'), |
||
20 | method = $base.data('method') || 'POST', |
||
21 | $input = $base.find('input.checkbox'), |
||
22 | fieldName = $input.attr('name'), |
||
23 | rawFieldName = $input.data('raw-name'), |
||
24 | format = $base.data('format'); |
||
25 | |||
26 | $input.on('change', function () { |
||
27 | var value = this.checked ? 1 : 0, |
||
28 | data = format === 'name,value' ? {name: rawFieldName, value: value} : {[rawFieldName] : value}; |
||
29 | $.ajax({ |
||
30 | method: method, |
||
31 | url: url, |
||
32 | dataType: 'json', |
||
33 | data: data, |
||
34 | success: function (data) { |
||
35 | if (data.message) { |
||
36 | $base.append('<div class="text-success">' + data.message + '</div>') |
||
37 | $base.find('.text-success').delay(2000).fadeOut(500, 'linear', function () { |
||
38 | $(this).remove() |
||
39 | }) |
||
40 | } |
||
41 | }, |
||
42 | error: function () { |
||
43 | console.log('Error Ajax!') |
||
44 | $base.append('<div class="text-danger">Error Ajax</div>') |
||
45 | $base.find('.text-danger').delay(2000).fadeOut(500, 'linear', function () { |
||
46 | $(this).remove() |
||
47 | }) |
||
48 | }, |
||
49 | complete: function () { |
||
50 | $base.find('.overlay').addClass('hidden') |
||
51 | } |
||
52 | }) |
||
53 | }); |
||
54 | }); |
||
55 | |||
56 | $('.field-select2-change-status-ajax').each(function () { |
||
57 | var $base = $(this), |
||
58 | $select = $base.find('.select2-change-status-ajax'), |
||
59 | url = $base.data('url'), |
||
60 | fieldName = $base.find('select').attr('name'), |
||
61 | method = $base.data('method') || 'GET', |
||
62 | $select2 = $select.select2({ |
||
63 | language: LANGUAGE, |
||
64 | tags: false |
||
65 | }); |
||
66 | |||
67 | $select2.on("select2:select", function (e) { |
||
68 | $base.find('.overlay').removeClass('hidden'); |
||
69 | |||
70 | $.ajax({ |
||
71 | method: method, |
||
72 | url: url, |
||
73 | dataType: 'json', |
||
74 | data: {name: fieldName, value: e.params.data.id}, |
||
75 | success: function (data) { |
||
76 | if (data.message) { |
||
77 | $base.append('<div class="text-success">' + data.message + '</div>') |
||
78 | $base.find('.text-success').delay(2000).fadeOut(500, 'linear', function () { |
||
79 | $(this).remove() |
||
80 | }) |
||
81 | } |
||
82 | }, |
||
83 | error: function () { |
||
84 | console.log('Error Ajax!') |
||
85 | $base.append('<div class="text-danger">Error Ajax</div>') |
||
86 | $base.find('.text-danger').delay(2000).fadeOut(500, 'linear', function () { |
||
87 | $(this).remove() |
||
88 | }) |
||
89 | }, |
||
90 | complete: function () { |
||
91 | $base.find('.overlay').addClass('hidden') |
||
92 | } |
||
93 | }) |
||
94 | }) |
||
95 | }) |
||
96 | |||
97 | $('.field-select2-tree-ajax').each(function () { |
||
98 | var $base = $(this), |
||
99 | $select = $base.find('.select2-tree'), |
||
100 | url = $base.data('url'), |
||
101 | valFld = $base.data('valFld') ? $base.data('valFld') : 'id', |
||
102 | labelFld = $base.data('labelFld') ? $base.data('labelFld') : 'name', |
||
103 | incFld = $base.data('incFld') ? $base.data('incFld') : 'children' |
||
104 | |||
105 | $.ajax({ |
||
106 | method: 'GET', |
||
107 | url: url, |
||
108 | dataType: 'json', |
||
109 | data: {data: ''}, |
||
110 | success: function (data) { |
||
111 | $select.select2ToTree({ |
||
112 | treeData: { |
||
113 | dataArr: data.data, |
||
114 | dftVal: data.default, |
||
115 | valFld: valFld, |
||
116 | labelFld: labelFld, |
||
117 | 'incFld': incFld |
||
118 | } |
||
119 | }) |
||
120 | }, |
||
121 | error: function () { |
||
122 | console.log('Error Ajax!') |
||
123 | }, |
||
124 | complete: function () { |
||
125 | $base.find('.overlay').fadeOut(200) |
||
126 | } |
||
127 | }) |
||
128 | }) |
||
129 | |||
130 | $('.field-tree').each(function () { |
||
131 | var $base = $(this), |
||
132 | $tree = $base.find('.field-tree-data'), |
||
133 | url = $base.data('url'), |
||
134 | showCheckbox = $base.data('showCheckbox') === undefined ? true : $base.data('showCheckbox'), |
||
135 | showIcon = $base.data('showIcon') === undefined ? false : $base.data('showIcon'), |
||
136 | fieldName = $base.data('field-name'), |
||
137 | $inputs = $base.find('.field-tree-inputs'), |
||
138 | getCheckedIds = function (obj) { |
||
139 | $inputs.html('') |
||
140 | var array = [] |
||
141 | obj.forEach(element => { |
||
142 | $inputs.append('<input type="hidden" name="' + fieldName + '[]" value="' + element.id + '" />') |
||
143 | }) |
||
144 | return array |
||
145 | } |
||
146 | |||
147 | $.ajax({ |
||
148 | method: 'GET', |
||
149 | url: url, |
||
150 | dataType: 'json', |
||
151 | data: {data: ''}, |
||
152 | success: function (data) { |
||
153 | $tree.treeview({ |
||
154 | data: data, |
||
155 | showIcon: showIcon, |
||
156 | showCheckbox: showCheckbox, |
||
157 | }) |
||
158 | |||
159 | getCheckedIds($tree.treeview('getChecked')) |
||
160 | |||
161 | $tree.on('nodeChecked', function (event, data) { |
||
162 | // console.log($(this).treeview('getChecked')) |
||
163 | getCheckedIds($(this).treeview('getChecked')) |
||
164 | }) |
||
165 | $tree.on('nodeUnchecked', function (event, data) { |
||
166 | // console.log($(this).treeview('getChecked')) |
||
167 | getCheckedIds($(this).treeview('getChecked')) |
||
168 | }) |
||
169 | }, |
||
170 | error: function () { |
||
171 | console.log('Error Ajax!') |
||
172 | }, |
||
173 | complete: function () { |
||
174 | $base.find('.overlay').fadeOut(200) |
||
175 | } |
||
176 | }) |
||
177 | |||
178 | }) |
||
179 | |||
180 | if ($('textarea.ck-editor.ck-mini').length) { |
||
181 | $('textarea.ck-editor.ck-mini').ckeditor(ckMini || {}) |
||
182 | } |
||
183 | |||
184 | if ($('textarea.ck-editor.ck-small').length) { |
||
185 | $('textarea.ck-editor.ck-small').ckeditor(ckSmall || {}) |
||
186 | } |
||
187 | |||
188 | if ($('textarea.ck-editor.ck-full').length) { |
||
189 | $('textarea.ck-editor.ck-full').ckeditor(ckFull || {}) |
||
190 | } |
||
191 | |||
192 | if ($('.field-x-editable').length) { |
||
193 | $('.field-x-editable').editable(xEditable || {}); |
||
194 | } |
||
195 | |||
196 | if ($('.field-colorpicker').length) { |
||
197 | $('.field-colorpicker').colorpicker(colorpickerOptions || {}); |
||
198 | } |
||
199 | |||
200 | if ($('.field-datetimepicker').length) { |
||
201 | $('.field-datetimepicker').datetimepicker(datetimepickerOptions || { |
||
202 | format: 'Y/m/d H:i:s' |
||
203 | }); |
||
204 | } |
||
205 | |||
206 | if ($('.field-datepicker').length) { |
||
207 | $('.field-datepicker').datetimepicker(datepickerOptions || { |
||
208 | timepicker:false, |
||
209 | format:'d/m/Y' |
||
210 | }); |
||
211 | } |
||
212 | |||
213 | if ($('.field-timepicker').length) { |
||
214 | $('.field-timepicker').datetimepicker(timepickerOptions || { |
||
215 | datepicker:false, |
||
216 | format: 'H:i' |
||
217 | }); |
||
218 | } |
||
219 | |||
220 | if ($('.js-verification-slug-field').length) { |
||
221 | if ($('input.js-slug-field-change').is(':checked')) { |
||
222 | $('.js-verification-slug-field input.js-slug-field-input') |
||
223 | .prop('readonly', false) |
||
224 | .prop('disabled', false) |
||
225 | } |
||
226 | $(document).on('change', '.js-verification-slug-field [type="checkbox"]', function () { |
||
227 | var $wrap = $(this).closest('.js-verification-slug-field'); |
||
228 | if(this.checked) { |
||
229 | $wrap.find('input.js-slug-field-input') |
||
230 | .prop('readonly', false) |
||
231 | .prop('disabled', false) |
||
232 | } else { |
||
233 | $wrap.find('input.js-slug-field-input') |
||
234 | .prop('readonly', true) |
||
235 | .prop('disabled', true) |
||
236 | } |
||
237 | }) |
||
238 | } |
||
239 | |||
240 | $(document).on('click', '.js-action-form', function (e) { |
||
241 | e.preventDefault() |
||
242 | var $form = $('#js-action-form'), |
||
243 | $this = $(this), |
||
244 | method = $this.data('method') || 'POST', |
||
245 | strConfirm = $this.data('confirm') ? confirm($this.data('confirm')) : true, |
||
246 | destination = $(this).data('destination'), |
||
247 | url = $(this).data('url'); |
||
248 | |||
249 | if (url && $form && strConfirm) { |
||
250 | $form.find('input[name="_method"]').val(method) |
||
251 | if (destination) { |
||
252 | $form.find('input.js-destination-val').val(destination) |
||
253 | } |
||
254 | $form.attr('action', url).submit() |
||
255 | } |
||
256 | return false |
||
257 | }); |
||
258 | |||
259 | // 10000 -> 10 000 |
||
260 | $('.js-num-format').each(function (index, value) { |
||
261 | var number = parseFloat(value.textContent); |
||
262 | value.textContent = numberWithSpaces(number); |
||
263 | }); |
||
264 | function numberWithSpaces(x) { |
||
265 | return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " "); |
||
266 | } |
||
267 | |||
268 | if ($('.select2.select2-static').length) { |
||
269 | $('.select2.select2-static').select2({ |
||
270 | language: LANGUAGE, |
||
271 | tags: false |
||
272 | }) |
||
273 | } |
||
274 | |||
275 | if ($('.select2.select2-tags').length) { |
||
276 | $('.select2.select2-tags').each(function (index) { |
||
277 | var url = $(this).data('url') || $(this).data('route'), |
||
278 | maximumSelection = $(this).data('max') || -1, |
||
279 | tokenSeparators = $(this).data('separators') || [',', ';'], |
||
280 | newTagLabel = $(this).data('new-tag-label') || ' (new tag)'; |
||
281 | |||
282 | $(this).select2({ |
||
283 | language: LANGUAGE, |
||
284 | tags: true, |
||
285 | tokenSeparators: tokenSeparators, |
||
286 | |||
287 | ajax: url ? { |
||
288 | delay: 250, |
||
289 | url: url, |
||
290 | dataType: 'json', |
||
291 | processResults: function(data) { |
||
292 | return { |
||
293 | results: data.results |
||
294 | } |
||
295 | } |
||
296 | } : undefined, |
||
297 | |||
298 | // Some nice improvements: |
||
299 | |||
300 | // max tags is 3 |
||
301 | maximumSelectionLength: maximumSelection, |
||
302 | |||
303 | // add "(new tag)" for new tags |
||
304 | createTag: function (params) { |
||
305 | var term = $.trim(params.term); |
||
306 | |||
307 | if (term === '') { |
||
308 | return null; |
||
309 | } |
||
310 | |||
311 | return { |
||
312 | id: term, |
||
313 | text: term + newTagLabel |
||
314 | }; |
||
315 | }, |
||
316 | }); |
||
317 | }); |
||
318 | } |
||
319 | |||
320 | if ($('.select2.sortable').length) { |
||
321 | $('.select2.sortable').select2({ |
||
322 | language: LANGUAGE, |
||
323 | tags: true |
||
324 | }) |
||
325 | |||
326 | $('.select2.sortable').on("select2:select", function (evt) { |
||
327 | var element = evt.params.data.element |
||
328 | var $element = $(element) |
||
329 | |||
330 | $element.detach() |
||
331 | $(this).append($element) |
||
332 | $(this).trigger("change") |
||
333 | }) |
||
334 | } |
||
335 | |||
336 | if ($('.select2.field-select-ajax').length) { |
||
337 | $('.select2.field-select-ajax').each(function (index) { |
||
338 | var url = $(this).data('url') || $(this).data('route') |
||
339 | |||
340 | $(this).select2({ |
||
341 | language: LANGUAGE, |
||
342 | tags: false, |
||
343 | ajax: { |
||
344 | delay: 250, |
||
345 | url: url, |
||
346 | dataType: 'json' |
||
347 | } |
||
348 | }); |
||
349 | }); |
||
350 | } |
||
351 | |||
352 | $('.lte-daterangepicker').each(function () { |
||
353 | $(this).on('apply.daterangepicker', function (ev, picker) { |
||
354 | var $inputNameStart = $(this).data('input-name-start'), |
||
355 | $inputNameEnd = $(this).data('input-name-end'), |
||
356 | $format = $(this).data('format') || 'MM/DD/YYYY' |
||
357 | $(this).val(picker.startDate.format($format) + ' - ' + picker.endDate.format($format)) |
||
358 | $(this).siblings('input[name="' + $inputNameStart + '"]').val(picker.startDate.format($format)) |
||
359 | $(this).siblings('input[name="' + $inputNameEnd + '"]').val(picker.endDate.format($format)) |
||
360 | }) |
||
361 | |||
362 | $(this).on('cancel.daterangepicker', function (ev, picker) { |
||
363 | $(this).val('') |
||
364 | }) |
||
365 | |||
366 | $(this).daterangepicker({ |
||
367 | autoUpdateInput: false, |
||
368 | "locale": translates.localeDateRangePicker || { |
||
369 | "format": "MM/DD/YYYY", |
||
370 | "separator": " - ", |
||
371 | "applyLabel": "Apply", |
||
372 | "cancelLabel": "Cancel", |
||
373 | "fromLabel": "From", |
||
374 | "toLabel": "To", |
||
375 | "customRangeLabel": "Custom", |
||
376 | "weekLabel": "W", |
||
377 | "daysOfWeek": [ |
||
378 | "Su", |
||
379 | "Mo", |
||
380 | "Tu", |
||
381 | "We", |
||
382 | "Th", |
||
383 | "Fr", |
||
384 | "Sa" |
||
385 | ], |
||
386 | "monthNames": [ |
||
387 | "January", |
||
388 | "February", |
||
389 | "March", |
||
390 | "April", |
||
391 | "May", |
||
392 | "June", |
||
393 | "July", |
||
394 | "August", |
||
395 | "September", |
||
396 | "October", |
||
397 | "November", |
||
398 | "December" |
||
399 | ], |
||
400 | "firstDay": 1 |
||
401 | } |
||
402 | }) |
||
403 | }) |
||
404 | |||
405 | $('[data-toggle="tooltip"]').tooltip() |
||
406 | |||
407 | $(document).on('click', '.js-fill-modal', function (e) { |
||
408 | e.preventDefault() |
||
409 | var $this = $(this), |
||
410 | url = $this.data('url'), |
||
411 | dataFields = $this.data('fields'), |
||
412 | modal = $($this.data('target')); |
||
413 | |||
414 | console.log(modal); |
||
415 | |||
416 | if (url) { |
||
417 | modal.find('form').attr('action', url) |
||
418 | } |
||
419 | for (var field in dataFields) { |
||
420 | modal.find('[name="' + field + '"]').val(dataFields[field]); |
||
421 | } |
||
422 | |||
423 | modal.modal() // Bootstrap! |
||
424 | }) |
||
425 | |||
426 | var treeSortable = {} |
||
427 | |||
428 | if ($('.tree-sortable').length) { |
||
429 | $('.tree-sortable').each(function () { |
||
430 | treeSortable[$(this).data('entity-name')] = $(this).sortable({ |
||
431 | group: 'serialization', |
||
432 | delay: 500, |
||
433 | handle: '.handle', |
||
434 | onDrop: function ($item, container, _super) { |
||
435 | _super($item, container); |
||
436 | } |
||
437 | }) |
||
438 | }) |
||
439 | } |
||
440 | |||
441 | $(document).on('click', '.post-tree-sortaple', function (e) { |
||
442 | e.preventDefault() |
||
443 | e.stopPropagation() |
||
444 | var $base = $(this), |
||
445 | data = treeSortable[$(this).data('entity-name')].sortable("serialize").get(), |
||
446 | url = $base.data('url'); |
||
447 | |||
448 | $.ajax({ |
||
449 | method: 'POST', |
||
450 | url: url, |
||
451 | dataType: 'json', |
||
452 | data: {'data': data}, |
||
453 | beforeSend: function () { |
||
454 | $base.attr('disabled', true) |
||
455 | .find('.fa') |
||
456 | .toggleClass('fa-spin') |
||
457 | .toggleClass('fa-refresh') |
||
458 | }, |
||
459 | success: function (data) { |
||
460 | console.log(data) |
||
461 | }, |
||
462 | error: function () { |
||
463 | console.log('Error Ajax!') |
||
464 | }, |
||
465 | complete: function () { |
||
466 | $base.attr('disabled', false) |
||
467 | .find('.fa') |
||
468 | .toggleClass('fa-spin') |
||
469 | .toggleClass('fa-refresh') |
||
470 | } |
||
471 | }) |
||
472 | }) |
||
473 | |||
474 | if ($('.field-links').length) { |
||
475 | $('.field-links').on('click', '.btn-info', function (e) { |
||
476 | e.preventDefault() |
||
477 | var n = $(this).parents('.field-links').find('.btn-info').index(this), |
||
478 | length = $(this).parents('.field-links').find('.btn-info').length, |
||
479 | fieldName = $(this).parents('.field-links').data('field-name'), |
||
480 | keyKey = $(this).parents('.field-links').data('key'), |
||
481 | keyValue = $(this).parents('.field-links').data('value'), |
||
482 | placeholderKey = $(this).parents('.field-links').data('placeholder-key'), |
||
483 | placeholderValue = $(this).parents('.field-links').data('placeholder-value'), |
||
484 | item = '<tr class="item">' |
||
485 | + '<td>' |
||
486 | + '<div class="input-group input-group-md">' |
||
487 | + '<input type="text" name="' + fieldName + '[' + (length) + '][' + keyKey + ']" class="form-control" placeholder="' + placeholderKey + '">' |
||
488 | + '<span class="input-group-btn" style="width: 40%">' |
||
489 | + '<input type="text" name="' + fieldName + '[' + (length) + '][' + keyValue + ']" class="form-control" placeholder="' + placeholderValue + '">' |
||
490 | + '</span>' |
||
491 | + '<span class="input-group-btn">' |
||
492 | + '<button type="button" class="btn btn-info btn-flat">' |
||
493 | + '<i class="fa fa-plus"></i>' |
||
494 | + '</button>' |
||
495 | + '<button type="button" class="btn btn-danger btn-flat">' |
||
496 | + '<i class="fa fa-remove"></i>' |
||
497 | + '</button>' |
||
498 | + '</span>' |
||
499 | + '</div>' |
||
500 | + '</td>' |
||
501 | + '</tr>"' |
||
502 | |||
503 | $(this).parents('.field-links').find('.item').eq(n).after(item) |
||
504 | }) |
||
505 | |||
506 | $('.field-links').on('click', '.btn-danger', function (e) { |
||
507 | e.preventDefault() |
||
508 | var n = $(this).parents('.field-links').find('.btn-danger:not(.first)').index(this) |
||
509 | |||
510 | $(this).parents('.field-links').find('.item').eq(n).remove() |
||
511 | }) |
||
512 | } |
||
513 | |||
514 | if ($('.field-linear-list').length) { |
||
515 | $('.field-linear-list').on('click', '.btn-info', function (e) { |
||
516 | e.preventDefault() |
||
517 | var n = $(this).parents('.field-linear-list').find('.btn-info').index(this), |
||
518 | length = $(this).parents('.field-linear-list').find('.btn-info').length, |
||
519 | fieldName = $(this).parents('.field-linear-list').data('field-name'), |
||
520 | placeholderValue = $(this).parents('.field-linear-list').data('placeholder-value'), |
||
521 | item = '<tr class="item">' |
||
522 | + '<td>' |
||
523 | + '<div class="input-group input-group-md">' |
||
524 | + '<span class="input-group-btn" style="width: 100%">' |
||
525 | + '<input type="text" name="' + fieldName + '[' + (length) + ']" class="form-control" placeholder="' + placeholderValue + ' ' + (parseInt(length) + 1) + '">' |
||
526 | + '</span>' |
||
527 | + '<span class="input-group-btn">' |
||
528 | + '<button type="button" class="btn btn-info btn-flat">' |
||
529 | + '<i class="fa fa-plus"></i>' |
||
530 | + '</button>' |
||
531 | + '<button type="button" class="btn btn-danger btn-flat">' |
||
532 | + '<i class="fa fa-remove"></i>' |
||
533 | + '</button>' |
||
534 | + '</span>' |
||
535 | + '</div>' |
||
536 | + '</td>' |
||
537 | + '</tr>"' |
||
538 | $(this).parents('.field-linear-list').find('.item').eq(n).after(item) |
||
539 | }) |
||
540 | |||
541 | $('.field-linear-list').on('click', '.btn-danger', function (e) { |
||
542 | e.preventDefault() |
||
543 | var n = $(this).parents('.field-linear-list').find('.btn-danger:not(.first)').index(this) |
||
544 | |||
545 | $(this).parents('.field-linear-list').find('.item').eq(n).remove() |
||
546 | }) |
||
547 | } |
||
548 | |||
549 | if ($('.field-more-items-sortable').length) { |
||
550 | $('.field-more-items-sortable .todo-list').sortable({ |
||
551 | handle: '.handle', |
||
552 | onDrop: function ($item, container, _super) { |
||
553 | _super($item, container); |
||
554 | $(container.target[0]).find('li').each(function (index) { |
||
555 | $(this).find('.field-weight-item').val(index) |
||
556 | }) |
||
557 | } |
||
558 | }); |
||
559 | |||
560 | $('.field-more-items-sortable').on('click', '.filed-remove', function (e) { |
||
561 | e.preventDefault() |
||
562 | $(this).parents('li').hide().find('.field-delete-item').val($(this).data('id')) |
||
563 | }) |
||
564 | } |
||
565 | |||
566 | if ($('.field-more-items').length) { |
||
567 | $('.field-more-items').on('click', '.filed-remove', function (e) { |
||
568 | e.preventDefault() |
||
569 | $(this).parents('tr').hide().find('.field-delete-item').val($(this).data('id')) |
||
570 | }) |
||
571 | } |
||
572 | }); |
||
573 |