PluginProbe
Customify / 1.6.5
Customify v1.6.5
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 1.6.5, at js/customizer_preview.js

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