Passed
Pull Request — master (#258)
by Dominik
05:19
created

lib/Customizer/Typography/control.js   A

Complexity

Total Complexity 6
Complexity/F 1.2

Size

Lines of Code 62
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 41
c 0
b 0
f 0
dl 0
loc 62
rs 10
mnd 1
bc 1
fnc 5
bpm 0.2
cpm 1.2
noi 0
1
/* globals wp, Option */
2
import $ from 'jquery'
3
4
wp.customize.controlConstructor['flynt-typography'] = wp.customize.Control.extend({
5
  ready: function () {
6
    const $family = this.container.find('.flynt-typography-family')
7
    const $variant = this.container.find('.flynt-typography-variant')
8
    let family = this.params.value.family
9
    let category = this.params.value.category
10
11
    $family.select2({
12
      data: Object.values(this.params.fonts)
13
    })
14
15
    $variant.select2()
16
17
    $family.val(this.params.font).trigger('change')
18
19
    const addOptions = function (options, selected = '') {
20
      $variant.empty()
21
22
      for (const option of options) {
23
        const isSelected = (selected === option)
24
        const newOption = new Option(option, option, isSelected, isSelected)
25
        $variant.append(newOption).trigger('change')
26
      }
27
    }
28
29
    $family.on('select2:select', (e) => {
30
      const data = e.params.data
31
      const variants = this.params.fonts[data.id].variants
32
      family = data.family
33
      category = data.category
34
35
      addOptions(variants)
36
      this.setting.set({
37
        family: family,
38
        category: category,
39
        variant: variants[0]
40
      })
41
    })
42
43
    $variant.on('select2:select', (e) => {
44
      const data = e.params.data
45
      this.setting.set({
46
        family: family,
47
        category: category,
48
        variant: data.id
49
      })
50
    })
51
52
    this.container.on('click', '.flynt-typography-reset', (e) => {
53
      const $el = $(e.currentTarget)
54
      const id = $el.data('key')
55
      const variants = this.params.fonts[id].variants
56
      const selected = this.params.default.variant
57
58
      addOptions(variants, selected)
59
      this.setting.set(this.params.default)
60
      $family.val(id).trigger('change')
61
    })
62
  }
63
})
64