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

358 lines 11.2 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 /** Bind Custom Events **/
84
85 // api.previewer.bind('highlight',function(e){
86 // $('.customizerHighlight').removeClass('customizerHighlight');
87 //
88 // if ( $(e).length > 0 ) {
89 // $(e).each(function(){
90 // $(this).addClass('customizerHighlight');
91 // });
92 // }
93 // });
94
95 api('live_css_edit', function (setting) {
96 setting.bind(function (new_text) {
97 $('#customify_css_editor_output').text(new_text);
98 });
99 });
100
101
102 /*** HELPERS **/
103
104 function load_webfont_once() {
105 if (typeof WebFont === "undefined") {
106 let tk = document.createElement('script');
107 tk.src = '//ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
108 tk.type = 'text/javascript';
109 let s = document.getElementsByTagName('script')[0];
110 s.parentNode.insertBefore(tk, s);
111 }
112 }
113
114 const get_CSS_values = function (ID, values) {
115
116 let store = {};
117
118 if (typeof values.font_family !== "undefined") {
119 store['font-family'] = values.font_family;
120 }
121
122 if (typeof values.selected_variants !== "undefined") {
123
124 let variants = null;
125
126 if (typeof values.selected_variants !== "undefined" && values.selected_variants !== null) {
127 variants = values.selected_variants;
128 } else if (typeof values.variants !== "undefined" && typeof values.variants[0] !== "undefined") {
129 variants = values.variants[0];
130 }
131
132 // google fonts also have the italic string inside, split that
133 if (variants !== null && _.isString( variants ) && variants.indexOf('italic') !== -1) {
134 store['font-style'] = 'italic';
135 variants = variants.replace('italic', '');
136 }
137
138 if (variants !== "") {
139 if (variants === 'regular') {
140 variants = 'normal';
141 }
142
143 store['font-weight'] = variants;
144 }
145 }
146
147 if (typeof values.font_size !== "undefined") {
148 store['font-size'] = values.font_size;
149 // If the value already contains a unit (is not numeric), go with that.
150 if ( isNaN(values.font_size) ) {
151 // If we have a standardized value field (as array), use that.
152 if ( typeof values.font_size.value !== "undefined" ) {
153 store['font-size'] = values.font_size.value;
154 if ( typeof values.font_size.unit !== "undefined" ) {
155 store['font-size'] += values.font_size.unit;
156 }
157 } else {
158 store['font-size'] += get_field_unit(ID, 'font-size');
159 }
160 } else {
161 store['font-size'] += get_field_unit(ID, 'font-size');
162 }
163 }
164
165 if (typeof values.letter_spacing !== "undefined") {
166 store['letter-spacing'] = values.letter_spacing;
167 // If the value already contains a unit (is not numeric), go with that.
168 if ( isNaN(values.letter_spacing) ) {
169 // If we have a standardized value field (as array), use that.
170 if ( typeof values.letter_spacing.value !== "undefined" ) {
171 store['letter-spacing'] = values.letter_spacing.value;
172 if ( typeof values.letter_spacing.unit !== "undefined" ) {
173 store['letter-spacing'] += values.letter_spacing.unit;
174 }
175 } else {
176 store['letter-spacing'] += get_field_unit(ID, 'letter-spacing');
177 }
178 } else {
179 store['letter-spacing'] += get_field_unit(ID, 'letter-spacing');
180 }
181 }
182
183 if (typeof values.line_height !== "undefined") {
184 store['line-height'] = values.line_height;
185 // If the value already contains a unit (is not numeric), go with that.
186 if ( isNaN(values.line_height) ) {
187 // If we have a standardized value field (as array), use that.
188 if ( typeof values.line_height.value !== "undefined" ) {
189 store['line-height'] = values.line_height.value;
190 if ( typeof values.line_height.unit !== "undefined" ) {
191 store['line-height'] += values.line_height.unit;
192 }
193 } else {
194 store['line-height'] += get_field_unit(ID, 'line-height');
195 }
196 } else {
197 store['line-height'] += get_field_unit(ID, 'line-height');
198 }
199 }
200
201 if (typeof values.text_align !== "undefined") {
202 store['text-align'] = values.text_align;
203 }
204
205 if (typeof values.text_transform !== "undefined") {
206 store['text-transform'] = values.text_transform;
207 }
208 if (typeof values.text_decoration !== "undefined") {
209 store['text-decoration'] = values.text_decoration;
210 }
211
212 return store;
213 };
214
215 const get_CSS_code = function (ID, values) {
216
217 let field = customify_settings.settings[ID];
218 let output = '';
219
220 if (typeof window !== "undefined" && typeof field.callback !== "undefined" && typeof window[field.callback] === "function") {
221 output = window[field.callback](values, field);
222 } else {
223 output = field.selector + "{\n";
224 $.each(values, function (k, v) {
225 output += k + ': ' + v + ";\n";
226 });
227 output += "}\n";
228 }
229
230 return output;
231 };
232
233 const get_field_unit = function (ID, field) {
234 let unit = '';
235 if (typeof customify_settings.settings[ID] === "undefined" || typeof customify_settings.settings[ID].fields[field] === "undefined") {
236 return unit;
237 }
238
239 if (typeof customify_settings.settings[ID].fields[field].unit !== "undefined") {
240 return customify_settings.settings[ID].fields[field].unit;
241 } else if (typeof customify_settings.settings[ID].fields[field][3] !== "undefined") {
242 // in case of an associative array
243 return customify_settings.settings[ID].fields[field][3];
244 }
245 };
246
247 const maybeLoadFontFamily = function (font) {
248
249 if (typeof WebFont === "undefined") {
250 let tk = document.createElement('script');
251 tk.src = '//ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
252 tk.type = 'text/javascript';
253 let s = document.getElementsByTagName('script')[0];
254 s.parentNode.insertBefore(tk, s);
255 }
256
257 // If the font type is not defined, we assume is a Google font.
258 if ( typeof font.type === "undefined" ) {
259 font.type = 'google';
260 }
261
262 if (font.type === 'theme_font') {
263 WebFont.load({
264 custom: {
265 families: [font.font_family],
266 urls: [font.src]
267 }
268 });
269 } else if (font.type === 'google') {
270 let family = font.font_family,
271 variants = null,
272 subsets = null;
273
274 if (typeof font.variants !== "undefined") {
275 variants = maybeJsonParse(font.variants);
276
277 if ( typeof variants === "string" || typeof variants === "number") {
278 variants = [ variants ];
279 }
280
281 $.each(variants, function (k, v) {
282
283 if (k == 0) {
284 family = family + ':';
285 }
286
287 family = family + v;
288
289 if ( Object.keys(variants).length > ( parseInt(k) + 1 ) ) {
290 family = family + ',';
291 } else if ( typeof font.selected_subsets !== "undefined" ) {
292 // in case there is a subset selected, we need to separate it from the font weight
293 family = family + ':';
294 }
295 });
296 }
297
298 if (typeof font.selected_subsets !== "undefined") {
299 subsets = maybeJsonParse(font.selected_subsets);
300
301 $.each(subsets, function (k, v) {
302
303 if ( k === "0" ) {
304 family = family + ':';
305 }
306
307 family = family + v;
308
309 if ( Object.keys(subsets).length > ( parseInt(k) + 1 ) ) {
310 family = family + ',';
311 }
312 });
313 }
314
315 if (fonts_cache.indexOf(family) === -1) {
316 setTimeout(function(){
317 WebFont.load({
318 google: {families: [family]},
319 classes: false,
320 events: false,
321 error: function (e) {
322 console.log(e);
323 },
324 active: function () {
325 sessionStorage.fonts = true;
326 }
327 });
328 },10);
329
330 fonts_cache.push(family);
331 }
332
333 } else {
334 // else what?
335 }
336 };
337
338 const maybeJsonParse = function (value) {
339 let parsed;
340
341 if ( typeof value !== "string" ) {
342 return value;
343 }
344
345 //try and parse it, with decodeURIComponent
346 try {
347 parsed = JSON.parse(decodeURIComponent(value));
348 } catch (e) {
349
350 // in case of an error, treat is as a string
351 parsed = value;
352 }
353
354 return parsed;
355 };
356 });
357 })(jQuery, window, document);
358