|
1
|
|
|
/* global kirkiPostMessageFields, WebFont */ |
|
2
|
|
|
var kirkiPostMessage = { |
|
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* The fields. |
|
6
|
|
|
* |
|
7
|
|
|
* @since 3.0.20 |
|
8
|
|
|
*/ |
|
9
|
|
|
fields: {}, |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* A collection of methods for the <style> tags. |
|
13
|
|
|
* |
|
14
|
|
|
* @since 3.0.20 |
|
15
|
|
|
*/ |
|
16
|
|
|
styleTag: { |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Add a <style> tag in <head> if it doesn't already exist. |
|
20
|
|
|
* |
|
21
|
|
|
* @since 3.0.20 |
|
22
|
|
|
* @param {string} id - The field-ID. |
|
23
|
|
|
* @returns {void} |
|
24
|
|
|
*/ |
|
25
|
|
|
add: function( id ) { |
|
26
|
|
|
var styleID = 'kirki-postmessage-' + id; |
|
|
|
|
|
|
27
|
|
|
if ( null === document.getElementById( styleID ) || 'undefined' === typeof document.getElementById( styleID ) ) { |
|
28
|
|
|
jQuery( 'head' ).append( '<style id="' + styleID + '"></style>' ); |
|
29
|
|
|
} |
|
30
|
|
|
}, |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Add a <style> tag in <head> if it doesn't already exist, |
|
34
|
|
|
* by calling the this.add method, and then add styles inside it. |
|
35
|
|
|
* |
|
36
|
|
|
* @since 3.0.20 |
|
37
|
|
|
* @param {string} id - The field-ID. |
|
38
|
|
|
* @param {string} styles - The styles to add. |
|
39
|
|
|
* @returns {void} |
|
40
|
|
|
*/ |
|
41
|
|
|
addData: function( id, styles ) { |
|
42
|
|
|
var self = this, |
|
|
|
|
|
|
43
|
|
|
styleID = 'kirki-postmessage-' + id; |
|
44
|
|
|
|
|
45
|
|
|
self.add( id ); |
|
46
|
|
|
jQuery( '#' + styleID ).text( styles ); |
|
47
|
|
|
} |
|
48
|
|
|
}, |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* A collection of utilities for CSS generation. |
|
52
|
|
|
* |
|
53
|
|
|
* @since 3.0.21 |
|
54
|
|
|
*/ |
|
55
|
|
|
css: { |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Generates the CSS from the output (js_vars) parameter. |
|
59
|
|
|
* |
|
60
|
|
|
* @since 3.0.21 |
|
61
|
|
|
* @param {Object} output - The output (js_vars) argument. |
|
62
|
|
|
* @param {mixed} value - The value. |
|
63
|
|
|
* @param {string} controlType - The control-type. |
|
64
|
|
|
* @returns {string} |
|
65
|
|
|
*/ |
|
66
|
|
|
fromOutput: function( output, value, controlType ) { |
|
67
|
|
|
var self = this, |
|
|
|
|
|
|
68
|
|
|
styles = '', |
|
69
|
|
|
kirkiParent = window.parent.kirki, |
|
70
|
|
|
googleFont = ''; |
|
71
|
|
|
|
|
72
|
|
|
if ( output.js_callback && 'function' === typeof window[ output.js_callback ] ) { |
|
73
|
|
|
value = window[ output.js_callback[0] ]( value, output.js_callback[1] ); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
switch ( controlType ) { |
|
76
|
|
|
case 'kirki-typography': |
|
77
|
|
|
styles += output.element + '{'; |
|
78
|
|
|
_.each( value, function( val, key ) { |
|
79
|
|
|
if ( output.choice && key !== output.choice ) { |
|
80
|
|
|
return; |
|
81
|
|
|
} |
|
82
|
|
|
styles += key + ':' + self.processValue( output, val ); +';'; |
|
83
|
|
|
} ); |
|
84
|
|
|
styles += '}'; |
|
85
|
|
|
|
|
86
|
|
|
// Check if this is a googlefont so that we may load it. |
|
87
|
|
|
if ( ! _.isUndefined( WebFont ) && value['font-family'] && 'google' === kirkiParent.util.webfonts.getFontType( value['font-family'] ) ) { |
|
88
|
|
|
|
|
89
|
|
|
// Calculate the googlefont params. |
|
90
|
|
|
googleFont = value['font-family'].replace( /\"/g, '"' ); |
|
91
|
|
|
if ( value.variant ) { |
|
92
|
|
|
if ( 'regular' === value.variant ) { |
|
93
|
|
|
googleFont += ':400'; |
|
94
|
|
|
} else if ( 'italic' === value.variant ) { |
|
95
|
|
|
googleFont += ':400i'; |
|
96
|
|
|
} else { |
|
97
|
|
|
googleFont += ':' + value.variant; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
if ( value.subsets && ! _.isEmpty( value.subsets ) ) { |
|
101
|
|
|
googleFont += ':' + value.subsets.join( ',' ); |
|
102
|
|
|
} |
|
103
|
|
|
WebFont.load( { |
|
104
|
|
|
google: { |
|
105
|
|
|
families: [ googleFont ] |
|
106
|
|
|
} |
|
107
|
|
|
} ); |
|
108
|
|
|
} |
|
109
|
|
|
break; |
|
110
|
|
|
case 'kirki-background': |
|
111
|
|
|
case 'kirki-dimensions': |
|
112
|
|
|
case 'kirki-multicolor': |
|
113
|
|
|
case 'kirki-sortable': |
|
114
|
|
|
break; |
|
115
|
|
|
default: |
|
116
|
|
|
if ( 'kirki-image' === controlType ) { |
|
117
|
|
|
value = ( ! _.isUndefined( value.url ) ) ? value.url : value; |
|
|
|
|
|
|
118
|
|
|
value = ( -1 === value.indexOf( 'url(' ) ) ? 'url(' + value + ')' : value; |
|
119
|
|
|
} |
|
120
|
|
|
styles += output.element + '{' + output.property + ':' + self.processValue( output, value ); +';}'; |
|
121
|
|
|
break; |
|
122
|
|
|
} |
|
123
|
|
|
return styles; |
|
124
|
|
|
}, |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Processes the value and applies any replacements and/or additions. |
|
128
|
|
|
* |
|
129
|
|
|
* @since 3.0.21 |
|
130
|
|
|
* @param {Object} output - The output (js_vars) argument. |
|
131
|
|
|
* @param {mixed} value - The value. |
|
132
|
|
|
* @param {string} controlType - The control-type. |
|
|
|
|
|
|
133
|
|
|
* @returns {string} |
|
134
|
|
|
*/ |
|
135
|
|
|
processValue: function( output, value ) { |
|
136
|
|
|
var settings = window.parent.wp.customize.get(); |
|
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
if ( 'string' !== typeof value ) { |
|
139
|
|
|
return value; |
|
140
|
|
|
} |
|
141
|
|
|
output = _.defaults( output, { |
|
|
|
|
|
|
142
|
|
|
prefix: '', |
|
143
|
|
|
units: '', |
|
144
|
|
|
suffix: '', |
|
145
|
|
|
value_pattern: '$', |
|
146
|
|
|
pattern_replace: {} |
|
147
|
|
|
} ); |
|
148
|
|
|
|
|
149
|
|
|
value = output.value_pattern.replace( /$/g, value ); |
|
|
|
|
|
|
150
|
|
|
_.each( output.pattern_replace, function( placeholder, id ) { |
|
151
|
|
|
if ( ! _.isUndefined( settings[ id ] ) ) { |
|
152
|
|
|
value = value.replace( placeholder, settings[ id ] ); |
|
|
|
|
|
|
153
|
|
|
} |
|
154
|
|
|
} ); |
|
155
|
|
|
return output.prefix + output.units + value + output.suffix; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
} |
|
159
|
|
|
}; |
|
160
|
|
|
|
|
161
|
|
|
jQuery( document ).ready( function() { |
|
162
|
|
|
|
|
163
|
|
|
_.each( kirkiPostMessageFields, function( field ) { |
|
164
|
|
|
wp.customize( field.settings, function( value ) { |
|
165
|
|
|
value.bind( function( newVal ) { |
|
166
|
|
|
var styles = ''; |
|
|
|
|
|
|
167
|
|
|
_.each( field.js_vars, function( output ) { |
|
168
|
|
|
styles += kirkiPostMessage.css.fromOutput( output, newVal, field.type ); |
|
169
|
|
|
} ); |
|
170
|
|
|
kirkiPostMessage.styleTag.addData( field.settings, styles ); |
|
171
|
|
|
} ); |
|
172
|
|
|
} ); |
|
173
|
|
|
} ); |
|
174
|
|
|
} ); |
|
175
|
|
|
|
Since ECMAScript 6, you can create block-scoped vars or constants with the keywords
letorconst. These variables/constants are only valid in the code block where they have been declared.Consider the following two pieces of code:
and
The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.
To know more about this ECMA6 feature, look at the MDN pages on let and const.