|
1 | 1 | /* global wp, bigboxCustomizeControls, _, Event */
|
2 | 2 |
|
3 |
| -const { fontList } = bigboxCustomizeControls.typography; |
| 3 | +( () => { |
| 4 | + if ( ! bigboxCustomizeControls.typography ) { |
| 5 | + return; |
| 6 | + } |
4 | 7 |
|
5 |
| -/** |
6 |
| - * Build HTML <option>s for font families. |
7 |
| - * |
8 |
| - * @return {string} String of HTML <option>s. |
9 |
| - */ |
10 |
| -const getFamilyOptions = () => { |
11 |
| - const options = []; |
| 8 | + const { fontList } = bigboxCustomizeControls.typography; |
12 | 9 |
|
13 |
| - _.each( fontList, ( data ) => { |
14 |
| - const familyOption = document.createElement( 'option' ); |
| 10 | + /** |
| 11 | + * Build HTML <option>s for font families. |
| 12 | + * |
| 13 | + * @return {string} String of HTML <option>s. |
| 14 | + */ |
| 15 | + const getFamilyOptions = () => { |
| 16 | + const options = []; |
15 | 17 |
|
16 |
| - familyOption.value = data.family; |
17 |
| - familyOption.text = data.family; |
18 |
| - familyOption.dataset.variants = data.variants.join( ',' ); |
19 |
| - familyOption.dataset.category = data.category; |
| 18 | + _.each( fontList, ( data ) => { |
| 19 | + const familyOption = document.createElement( 'option' ); |
20 | 20 |
|
21 |
| - options.push( familyOption ); |
22 |
| - } ); |
| 21 | + familyOption.value = data.family; |
| 22 | + familyOption.text = data.family; |
| 23 | + familyOption.dataset.variants = data.variants.join( ',' ); |
| 24 | + familyOption.dataset.category = data.category; |
23 | 25 |
|
24 |
| - return options; |
25 |
| -}; |
| 26 | + options.push( familyOption ); |
| 27 | + } ); |
26 | 28 |
|
27 |
| -/** |
28 |
| - * Build HTML <option>s for weight variants. |
29 |
| - * |
30 |
| - * @param {Array} variants List of variants. |
31 |
| - * @return {string} String of HTML <option>s. |
32 |
| - */ |
33 |
| -const getWeightOptions = ( variants ) => { |
34 |
| - const options = []; |
| 29 | + return options; |
| 30 | + }; |
35 | 31 |
|
36 |
| - _.each( variants, ( variant ) => { |
37 |
| - if ( ! isNaN( variant ) || 'regular' === variant ) { |
38 |
| - const weightOption = document.createElement( 'option' ); |
| 32 | + /** |
| 33 | + * Build HTML <option>s for weight variants. |
| 34 | + * |
| 35 | + * @param {Array} variants List of variants. |
| 36 | + * @return {string} String of HTML <option>s. |
| 37 | + */ |
| 38 | + const getWeightOptions = ( variants ) => { |
| 39 | + const options = []; |
39 | 40 |
|
40 |
| - weightOption.value = variant; |
41 |
| - weightOption.text = variant; |
| 41 | + _.each( variants, ( variant ) => { |
| 42 | + if ( ! isNaN( variant ) || 'regular' === variant ) { |
| 43 | + const weightOption = document.createElement( 'option' ); |
42 | 44 |
|
43 |
| - options.push( weightOption ); |
44 |
| - } |
45 |
| - } ); |
| 45 | + weightOption.value = variant; |
| 46 | + weightOption.text = variant; |
46 | 47 |
|
47 |
| - return options; |
48 |
| -}; |
49 |
| - |
50 |
| -/** |
51 |
| - * Update weight fields with available options. |
52 |
| - * |
53 |
| - * @param {Array} variants List of variants. |
54 |
| - */ |
55 |
| -const updateWeightFields = ( variants ) => { |
56 |
| - _.each( [ 'base', 'bold' ], ( weight ) => { |
57 |
| - const control = wp.customize.control( `type-font-weight-${ weight }` ); |
58 |
| - const value = control.setting(); |
59 |
| - |
60 |
| - // Bring back to standard DOM element. |
61 |
| - const selectEl = control.container.find( 'select' ).get( 0 ); |
62 |
| - |
63 |
| - selectEl.innerHTML = ''; |
64 |
| - |
65 |
| - // Add HTML and select chosen item. |
66 |
| - getWeightOptions( variants ).forEach( ( familyWeight ) => selectEl.options.add( familyWeight ) ); |
67 |
| - |
68 |
| - // Determine if the previous value can be carried over. |
69 |
| - |
70 |
| - // Value still exists. |
71 |
| - if ( variants[ value ] ) { |
72 |
| - selectEl.value = value; |
73 |
| - } else { |
74 |
| - const first = selectEl.options[ 0 ]; |
75 |
| - const last = selectEl.options[ selectEl.options.length - 1 ]; |
76 |
| - |
77 |
| - // Select first if no previous item remains. |
78 |
| - if ( 'base' === weight ) { |
79 |
| - selectEl.value = first; |
80 |
| - selectEl.selectedIndex = 0; |
81 |
| - // Select last if no previous item remains for bold. |
82 |
| - } else { |
83 |
| - selectEl.value = last; |
84 |
| - selectEl.selectedIndex = selectEl.options.length - 1; |
| 48 | + options.push( weightOption ); |
85 | 49 | }
|
| 50 | + } ); |
| 51 | + |
| 52 | + return options; |
| 53 | + }; |
| 54 | + |
| 55 | + /** |
| 56 | + * Update weight fields with available options. |
| 57 | + * |
| 58 | + * @param {Array} variants List of variants. |
| 59 | + */ |
| 60 | + const updateWeightFields = ( variants ) => { |
| 61 | + _.each( [ 'base', 'bold' ], ( weight ) => { |
| 62 | + const control = wp.customize.control( `type-font-weight-${ weight }` ); |
| 63 | + const value = control.setting(); |
| 64 | + |
| 65 | + // Bring back to standard DOM element. |
| 66 | + const selectEl = control.container.find( 'select' ).get( 0 ); |
| 67 | + |
| 68 | + selectEl.innerHTML = ''; |
86 | 69 |
|
87 |
| - // Manually updated setting value. |
88 |
| - control.setting.bind( selectEl.value ); |
| 70 | + // Add HTML and select chosen item. |
| 71 | + getWeightOptions( variants ).forEach( ( familyWeight ) => selectEl.options.add( familyWeight ) ); |
| 72 | + |
| 73 | + // Determine if the previous value can be carried over. |
| 74 | + |
| 75 | + // Value still exists. |
| 76 | + if ( variants[ value ] ) { |
| 77 | + selectEl.value = value; |
| 78 | + } else { |
| 79 | + const first = selectEl.options[ 0 ]; |
| 80 | + const last = selectEl.options[ selectEl.options.length - 1 ]; |
| 81 | + |
| 82 | + // Select first if no previous item remains. |
| 83 | + if ( 'base' === weight ) { |
| 84 | + selectEl.value = first; |
| 85 | + selectEl.selectedIndex = 0; |
| 86 | + // Select last if no previous item remains for bold. |
| 87 | + } else { |
| 88 | + selectEl.value = last; |
| 89 | + selectEl.selectedIndex = selectEl.options.length - 1; |
| 90 | + } |
| 91 | + |
| 92 | + // Manually updated setting value. |
| 93 | + control.setting.bind( selectEl.value ); |
| 94 | + } |
| 95 | + } ); |
| 96 | + }; |
| 97 | + |
| 98 | + /** |
| 99 | + * Update category (fallback) fields with available options. |
| 100 | + * |
| 101 | + * @param {string} category Font category. |
| 102 | + */ |
| 103 | + const updateCategoryField = ( category ) => { |
| 104 | + if ( 'handwriting' === category ) { |
| 105 | + category = 'cursive'; |
89 | 106 | }
|
90 |
| - } ); |
91 |
| -}; |
92 |
| - |
93 |
| -/** |
94 |
| - * Update category (fallback) fields with available options. |
95 |
| - * |
96 |
| - * @param {string} category Font category. |
97 |
| - */ |
98 |
| -const updateCategoryField = ( category ) => { |
99 |
| - if ( 'handwriting' === category ) { |
100 |
| - category = 'cursive'; |
101 |
| - } |
102 | 107 |
|
103 |
| - wp.customize.control( 'type-font-family-fallback', ( control ) => control.setting.set( category ) ); |
104 |
| -}; |
| 108 | + wp.customize.control( 'type-font-family-fallback', ( control ) => control.setting.set( category ) ); |
| 109 | + }; |
105 | 110 |
|
106 |
| -// Wait for Customize ready. |
107 |
| -wp.customize.bind( 'ready', () => { |
108 |
| - const familyControl = wp.customize.control( 'type-font-family' ); |
109 |
| - const familyInput = familyControl.container.find( 'select' ).get( 0 ); |
| 111 | + // Wait for Customize ready. |
| 112 | + wp.customize.bind( 'ready', () => { |
| 113 | + const familyControl = wp.customize.control( 'type-font-family' ); |
| 114 | + const familyInput = familyControl.container.find( 'select' ).get( 0 ); |
110 | 115 |
|
111 |
| - // Update available weights when changing family. |
112 |
| - familyInput.addEventListener( 'change', ( e ) => { |
113 |
| - const selected = e.target.options[ e.target.options.selectedIndex ]; |
114 |
| - const variants = selected.dataset.variants ? selected.dataset.variants.split( ',' ) : [ 400, 500 ]; |
115 |
| - const category = selected.dataset.category || 'sans-serif'; |
| 116 | + // Update available weights when changing family. |
| 117 | + familyInput.addEventListener( 'change', ( e ) => { |
| 118 | + const selected = e.target.options[ e.target.options.selectedIndex ]; |
| 119 | + const variants = selected.dataset.variants ? selected.dataset.variants.split( ',' ) : [ 400, 500 ]; |
| 120 | + const category = selected.dataset.category || 'sans-serif'; |
116 | 121 |
|
117 |
| - updateWeightFields( variants ); |
118 |
| - updateCategoryField( category ); |
119 |
| - } ); |
| 122 | + updateWeightFields( variants ); |
| 123 | + updateCategoryField( category ); |
| 124 | + } ); |
120 | 125 |
|
121 |
| - getFamilyOptions().forEach( ( family ) => familyInput.add( family ) ); |
| 126 | + getFamilyOptions().forEach( ( family ) => familyInput.add( family ) ); |
122 | 127 |
|
123 |
| - familyInput.value = familyControl.setting(); |
| 128 | + familyInput.value = familyControl.setting(); |
124 | 129 |
|
125 |
| - // Trigger change on load to populate other fields. |
126 |
| - const event = new Event( 'change' ); |
127 |
| - familyInput.dispatchEvent( event ); |
128 |
| -} ); |
| 130 | + // Trigger change on load to populate other fields. |
| 131 | + const event = new Event( 'change' ); |
| 132 | + familyInput.dispatchEvent( event ); |
| 133 | + } ); |
| 134 | +} ) (); |
0 commit comments