PluginProbe
Customify / 2.6.0
Customify v2.6.0
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.6.0, at js/customizer_preview.js

353 lines 10.3 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 var properties_prefix = typeof el.properties_prefix === "undefined" ? '' : el.properties_prefix;
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, properties_prefix),
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, prefix) {
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 += prefix + 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.6.26/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
245 if (typeof font.src === 'undefined') {
246 var themeFontsArray = Object.keys(customify_settings.theme_fonts).map(key => customify_settings.theme_fonts[key]);
247 var index = themeFontsArray.findIndex(fontObj => fontObj.family === font.font_family );
248
249 if ( index > -1 ) {
250 font.src = themeFontsArray[index].src;
251 }
252 }
253
254 if ( typeof font.src === "undefined" ) {
255 return;
256 }
257
258 WebFont.load({
259 custom: {
260 families: [font.font_family],
261 urls: [font.src]
262 }
263 })
264 } else if (font.type === 'google') {
265 let family = font.font_family,
266 variants = null,
267 subsets = null
268
269 if (typeof font.variants !== 'undefined') {
270 variants = maybeJsonParse(font.variants)
271
272 if (typeof variants === 'string' || typeof variants === 'number') {
273 variants = [variants]
274 }
275
276 $.each(variants, function (k, v) {
277
278 if (k == 0) {
279 family = family + ':'
280 }
281
282 family = family + v
283
284 if (Object.keys(variants).length > (parseInt(k) + 1)) {
285 family = family + ','
286 } else if (typeof font.selected_subsets !== 'undefined') {
287 // in case there is a subset selected, we need to separate it from the font weight
288 family = family + ':'
289 }
290 })
291 }
292
293 if (typeof font.selected_subsets !== 'undefined') {
294 subsets = maybeJsonParse(font.selected_subsets)
295
296 $.each(subsets, function (k, v) {
297
298 if (k === '0') {
299 family = family + ':'
300 }
301
302 family = family + v
303
304 if (Object.keys(subsets).length > (parseInt(k) + 1)) {
305 family = family + ','
306 }
307 })
308 }
309
310 if (fonts_cache.indexOf(family) === -1) {
311 setTimeout(function () {
312 WebFont.load({
313 google: {families: [family]},
314 classes: false,
315 events: false,
316 error: function (e) {
317 console.log(e)
318 },
319 active: function () {
320 sessionStorage.fonts = true
321 }
322 })
323 }, 10)
324
325 fonts_cache.push(family)
326 }
327
328 } else {
329 // else what?
330 }
331 }
332
333 const maybeJsonParse = function (value) {
334 let parsed
335
336 if (typeof value !== 'string') {
337 return value
338 }
339
340 //try and parse it, with decodeURIComponent
341 try {
342 parsed = JSON.parse(decodeURIComponent(value))
343 } catch (e) {
344
345 // in case of an error, treat is as a string
346 parsed = value
347 }
348
349 return parsed
350 }
351 })
352 })(jQuery, window, document)
353