PluginProbe
Customify / 2.5.2
Customify v2.5.2
2.10.9 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.7.1 1.3.0 1.3.1 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.6.0 1.6.0.1 1.6.5 1.7.0 1.7.1 All 77 releases
customify / js / customizer_preview.js

customizer_preview.js in Customify 2.5.2, at js/customizer_preview.js

339 lines 9.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 ;(function ($, window, document, undefined) {
2
3 const fonts_cache = []
4
5 $(document).ready(function () {
6 var api = parent.wp.customize,
7 wp_settings = api.settings.settings
8
9 load_webfont_once()
10
11 $.each(customify_settings.settings, function (key, el) {
12
13 if (el.type === 'font') {
14 api(key, function (setting) {
15 setting.bind(function (to) {
16 let $values = maybeJsonParse(to)
17
18 if (typeof $values !== 'undefined') {
19 if (typeof $values.font_family !== 'undefined') {
20 maybeLoadFontFamily($values)
21 }
22
23 let vls = get_CSS_values(this.id, $values),
24 CSS = get_CSS_code(this.id, vls),
25 field_style = $('#customify_font_output_for_' + el.html_safe_option_id)
26
27 field_style.html(CSS)
28 }
29 })
30 })
31
32 } else if (typeof wp_settings !== 'undefined' && typeof wp_settings[key] !== 'undefined' && typeof el.css !== 'undefined' && typeof el.live !== 'undefined' && el.live === true) {
33 api(key, function (setting) {
34
35 setting.bind(function (to) {
36
37 $.each(el.css, function (counter, property_config) {
38 let properties = []
39
40 properties[property_config.property] = property_config.selector
41 if (typeof property_config.callback_filter !== 'undefined') {
42 properties['callback'] = property_config.callback_filter
43 }
44
45 let css_update_args = {
46 properties: properties,
47 propertyValue: to,
48 negative_value: property_config.hasOwnProperty('negative_value') ? property_config['negative_value'] : false
49 }
50
51 if (typeof this.unit !== 'undefined') {
52 css_update_args.unit = this.unit
53 }
54
55 // Replace all dashes with underscores thus making the CSS property safe to us in a HTML ID.
56 let regex_for_multiple_replace = new RegExp('-', 'g'),
57 cssStyleSelector = '.dynamic_setting_' + el.html_safe_option_id + '_property_' + property_config.property.replace(regex_for_multiple_replace, '_') + '_' + counter
58
59 $(cssStyleSelector).cssUpdate(css_update_args)
60 })
61
62 })
63 })
64 } else if (typeof el.live === 'object' && el.live.length > 0) {
65 // if the live parameter is an object it means that is a list of css classes
66 // these classes should be affected by the change of the text fields
67 var field_class = el.live.join()
68
69 // if this field is allowed to modify text then we'll edit this live
70 if ($.inArray(el.type, ['text', 'textarea', 'ace_editor']) > -1) {
71 wp.customize(key, function (value) {
72 value.bind(function (text) {
73 let sanitizer = document.createElement('div')
74
75 sanitizer.innerHTML = text
76 $(field_class).html(text)
77 })
78 })
79 }
80 }
81 })
82
83 /*** HELPERS **/
84
85 function load_webfont_once () {
86 if (typeof WebFont === 'undefined') {
87 let tk = document.createElement('script')
88 tk.src = '//ajax.googleapis.com/ajax/libs/webfont/1/webfont.js'
89 tk.type = 'text/javascript'
90 let s = document.getElementsByTagName('script')[0]
91 s.parentNode.insertBefore(tk, s)
92 }
93 }
94
95 const get_CSS_values = function (ID, values) {
96
97 let store = {}
98
99 if (typeof values.font_family !== 'undefined') {
100 store['font-family'] = values.font_family
101 }
102
103 if (typeof values.selected_variants !== 'undefined') {
104
105 let variants = null
106
107 if (typeof values.selected_variants !== 'undefined' && values.selected_variants !== null) {
108 variants = values.selected_variants
109 } else if (typeof values.variants !== 'undefined' && typeof values.variants[0] !== 'undefined') {
110 variants = values.variants[0]
111 }
112
113 // google fonts also have the italic string inside, split that
114 if (variants !== null && _.isString(variants) && variants.indexOf('italic') !== -1) {
115 store['font-style'] = 'italic'
116 variants = variants.replace('italic', '')
117 }
118
119 if (variants !== '') {
120 if (variants === 'regular') {
121 variants = 'normal'
122 }
123
124 store['font-weight'] = variants
125 }
126 }
127
128 if (typeof values.font_size !== 'undefined') {
129 store['font-size'] = values.font_size
130 // If the value already contains a unit (is not numeric), go with that.
131 if (isNaN(values.font_size)) {
132 // If we have a standardized value field (as array), use that.
133 if (typeof values.font_size.value !== 'undefined') {
134 store['font-size'] = values.font_size.value
135 if (typeof values.font_size.unit !== 'undefined') {
136 store['font-size'] += values.font_size.unit
137 }
138 } else {
139 store['font-size'] += get_field_unit(ID, 'font-size')
140 }
141 } else {
142 store['font-size'] += get_field_unit(ID, 'font-size')
143 }
144 }
145
146 if (typeof values.letter_spacing !== 'undefined') {
147 store['letter-spacing'] = values.letter_spacing
148 // If the value already contains a unit (is not numeric), go with that.
149 if (isNaN(values.letter_spacing)) {
150 // If we have a standardized value field (as array), use that.
151 if (typeof values.letter_spacing.value !== 'undefined') {
152 store['letter-spacing'] = values.letter_spacing.value
153 if (typeof values.letter_spacing.unit !== 'undefined') {
154 store['letter-spacing'] += values.letter_spacing.unit
155 }
156 } else {
157 store['letter-spacing'] += get_field_unit(ID, 'letter-spacing')
158 }
159 } else {
160 store['letter-spacing'] += get_field_unit(ID, 'letter-spacing')
161 }
162 }
163
164 if (typeof values.line_height !== 'undefined') {
165 store['line-height'] = values.line_height
166 // If the value already contains a unit (is not numeric), go with that.
167 if (isNaN(values.line_height)) {
168 // If we have a standardized value field (as array), use that.
169 if (typeof values.line_height.value !== 'undefined') {
170 store['line-height'] = values.line_height.value
171 if (typeof values.line_height.unit !== 'undefined') {
172 store['line-height'] += values.line_height.unit
173 }
174 } else {
175 store['line-height'] += get_field_unit(ID, 'line-height')
176 }
177 } else {
178 store['line-height'] += get_field_unit(ID, 'line-height')
179 }
180 }
181
182 if (typeof values.text_align !== 'undefined') {
183 store['text-align'] = values.text_align
184 }
185
186 if (typeof values.text_transform !== 'undefined') {
187 store['text-transform'] = values.text_transform
188 }
189 if (typeof values.text_decoration !== 'undefined') {
190 store['text-decoration'] = values.text_decoration
191 }
192
193 return store
194 }
195
196 const get_CSS_code = function (ID, values) {
197
198 let field = customify_settings.settings[ID]
199 let output = ''
200
201 if (typeof window !== 'undefined' && typeof field.callback !== 'undefined' && typeof window[field.callback] === 'function') {
202 output = window[field.callback](values, field)
203 } else {
204 output = field.selector + '{\n'
205 $.each(values, function (k, v) {
206 output += k + ': ' + v + ';\n'
207 })
208 output += '}\n'
209 }
210
211 return output
212 }
213
214 const get_field_unit = function (ID, field) {
215 let unit = ''
216 if (typeof customify_settings.settings[ID] === 'undefined' || typeof customify_settings.settings[ID].fields[field] === 'undefined') {
217 return unit
218 }
219
220 if (typeof customify_settings.settings[ID].fields[field].unit !== 'undefined') {
221 return customify_settings.settings[ID].fields[field].unit
222 } else if (typeof customify_settings.settings[ID].fields[field][3] !== 'undefined') {
223 // in case of an associative array
224 return customify_settings.settings[ID].fields[field][3]
225 }
226 }
227
228 const maybeLoadFontFamily = function (font) {
229
230 if (typeof WebFont === 'undefined') {
231 let tk = document.createElement('script')
232 tk.src = '//ajax.googleapis.com/ajax/libs/webfont/1/webfont.js'
233 tk.type = 'text/javascript'
234 let s = document.getElementsByTagName('script')[0]
235 s.parentNode.insertBefore(tk, s)
236 }
237
238 // If the font type is not defined, we assume is a Google font.
239 if (typeof font.type === 'undefined') {
240 font.type = 'google'
241 }
242
243 if (font.type === 'theme_font') {
244 WebFont.load({
245 custom: {
246 families: [font.font_family],
247 urls: [font.src]
248 }
249 })
250 } else if (font.type === 'google') {
251 let family = font.font_family,
252 variants = null,
253 subsets = null
254
255 if (typeof font.variants !== 'undefined') {
256 variants = maybeJsonParse(font.variants)
257
258 if (typeof variants === 'string' || typeof variants === 'number') {
259 variants = [variants]
260 }
261
262 $.each(variants, function (k, v) {
263
264 if (k == 0) {
265 family = family + ':'
266 }
267
268 family = family + v
269
270 if (Object.keys(variants).length > (parseInt(k) + 1)) {
271 family = family + ','
272 } else if (typeof font.selected_subsets !== 'undefined') {
273 // in case there is a subset selected, we need to separate it from the font weight
274 family = family + ':'
275 }
276 })
277 }
278
279 if (typeof font.selected_subsets !== 'undefined') {
280 subsets = maybeJsonParse(font.selected_subsets)
281
282 $.each(subsets, function (k, v) {
283
284 if (k === '0') {
285 family = family + ':'
286 }
287
288 family = family + v
289
290 if (Object.keys(subsets).length > (parseInt(k) + 1)) {
291 family = family + ','
292 }
293 })
294 }
295
296 if (fonts_cache.indexOf(family) === -1) {
297 setTimeout(function () {
298 WebFont.load({
299 google: {families: [family]},
300 classes: false,
301 events: false,
302 error: function (e) {
303 console.log(e)
304 },
305 active: function () {
306 sessionStorage.fonts = true
307 }
308 })
309 }, 10)
310
311 fonts_cache.push(family)
312 }
313
314 } else {
315 // else what?
316 }
317 }
318
319 const maybeJsonParse = function (value) {
320 let parsed
321
322 if (typeof value !== 'string') {
323 return value
324 }
325
326 //try and parse it, with decodeURIComponent
327 try {
328 parsed = JSON.parse(decodeURIComponent(value))
329 } catch (e) {
330
331 // in case of an error, treat is as a string
332 parsed = value
333 }
334
335 return parsed
336 }
337 })
338 })(jQuery, window, document)
339