Completed
Push — master ( 4e5281...3423c0 )
by Simon
01:28
created

rangefield.js ➔ ???   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 1
dl 0
loc 25
rs 8.5806
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A rangefield.js ➔ ... ➔ rangefield.noUiSlider.update 0 9 2
1
import noUiSlider from 'nouislider';
2
3
const observerConfig = {
4
  attributes: false,
5
  characterData: false,
6
  childList: true,
7
  subtree: true
8
};
9
10
const mountRangeField = (rangefield) => {
11
  let fieldName = rangefield.getAttribute('data-settings');
12
  let input = document.body.querySelectorAll(`input[name=${fieldName}]`)[0];
13
  let settings = window[fieldName];
14
  if (!Array.isArray(settings['start'])) {
15
    settings['start'] = [settings['start']];
16
  }
17
  if (input.hasAttribute('value')) {
18
    let values = input.getAttribute('value').split(',');
19
    if (values.length === settings['start'].length) {
20
      settings['start'] = values;
21
    }
22
  }
23
  noUiSlider.create(rangefield, settings);
24
25
  rangefield.noUiSlider.on('update', function () {
26
    let description = document.body.getElementsByClassName(`${fieldName}-range-description`)[0];
27
    let value = rangefield.noUiSlider.get();
28
    if (!Array.isArray(value)) {
29
      value = [value];
30
    }
31
    input.setAttribute('value', value.join(','));
32
    description.innerHTML = value.join(' ');
33
  });
34
}
35
36
const mount = () => {
37
  // If things change and we haven't painted our UiSlider yet, go for it
38
  if (document.body.getElementsByClassName('noUi-target').length === 0) {
39
    const ranges = document.body.querySelectorAll('div[data-range=rangefield]');
40
    ranges.forEach(mountRangeField);
41
  }
42
}
43
44
45
const onMutation = () => mount();
46
47
const observerFactory = callback => new MutationObserver(callback);
1 ignored issue
show
Bug introduced by
The variable MutationObserver seems to be never declared. If this is a global, consider adding a /** global: MutationObserver */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Unused Code introduced by
The constant observerFactory seems to be never used. Consider removing it.
Loading history...
48
49
const loadRangeSlider = (createObserver = observerFactory) => {
50
  mount(); //mount any elements in the dom on load
51
  createObserver(onMutation).observe(document.body, observerConfig);
52
};
53
54
export default loadRangeSlider;
55