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

303 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 var sliced_id = key.slice(0, -1);
15 sliced_id = sliced_id.replace(customify_settings.options_name + '[', '');
16
17 api(key, function (setting) {
18 setting.bind(function (to) {
19 var $values = maybeJsonParse(to);
20
21 if (typeof $values.font_family !== "undefined") {
22 maybeLoadFontFamily($values);
23 }
24
25 var vls = get_CSS_values(this.id, $values);
26 var CSS = get_CSS_code(this.id, vls);
27 var field_style = $('#customify_font_output_for_' + sliced_id);
28
29 field_style.html(CSS);
30 });
31 });
32
33 } else if (typeof wp_settings[key] !== "undefined" && typeof el.css !== "undefined" && typeof el.live !== 'undefined' && el.live === true) {
34
35 var sliced_id = key.slice(0, -1);
36 sliced_id = sliced_id.replace(customify_settings.options_name + '[', '');
37
38 api(key, function (setting) {
39
40 setting.bind(function (to) {
41 var properties = [];
42
43 $.each(el.css, function (counter, property_config) {
44
45 properties[property_config.property] = property_config.selector;
46 if (typeof property_config.callback_filter !== "undefined") {
47 properties['callback'] = property_config.callback_filter;
48 }
49
50 var css_update_args = {
51 properties: properties,
52 propertyValue: to
53 };
54
55 if (typeof this.unit !== 'undefined') {
56 css_update_args.unit = this.unit;
57 }
58
59 var req_Exp_for_multiple_replace = new RegExp('-', 'g');
60 $('#dynamic_setting_' + sliced_id + '_property_' + property_config.property.replace(req_Exp_for_multiple_replace, '_')).cssUpdate(css_update_args);
61 });
62
63 });
64 });
65 } else if (typeof el.live === "object" && el.live.length > 0) {
66 // if the live parameter is an object it means that is a list of css classes
67 // these classes should be affected by the change of the text fields
68 var field_class = el.live.join();
69
70 // if this field is allowed to modify text then we'll edit this live
71 if ($.inArray(el.type, ['text', 'textarea', 'ace_editor']) > -1) {
72 wp.customize(key, function (value) {
73 value.bind(function (text) {
74 var sanitizer = document.createElement('div');
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 var tk = document.createElement('script');
107 tk.src = '//ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
108 tk.type = 'text/javascript';
109 var s = document.getElementsByTagName('script')[0];
110 s.parentNode.insertBefore(tk, s);
111 }
112 }
113
114 var get_CSS_values = function (ID, $values) {
115
116 var 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 var 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 && 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 + get_field_unit(ID, 'font-size');
149 }
150
151 if (typeof $values.letter_spacing !== "undefined") {
152 store['letter-spacing'] = $values.letter_spacing + get_field_unit(ID, 'letter-spacing');
153 }
154
155 if (typeof $values.line_height !== "undefined") {
156 store['line-height'] = $values.line_height + get_field_unit(ID, 'line-height');
157 }
158
159 if (typeof $values.text_align !== "undefined") {
160 store['text-align'] = $values.text_align;
161 }
162
163 if (typeof $values.text_transform !== "undefined") {
164 store['text-transform'] = $values.text_transform;
165 }
166 if (typeof $values.text_decoration !== "undefined") {
167 store['text-decoration'] = $values.text_decoration;
168 }
169
170 return store;
171 };
172
173 var get_CSS_code = function (ID, $values) {
174
175 var field = customify_settings.settings[ID];
176 var output = '';
177
178 if (typeof window !== "undefined" && typeof field.callback !== "undefined" && typeof window[field.callback] === "function") {
179 output = window[field.callback]($values, field);
180 } else {
181 output = field.selector + "{\n";
182 $.each($values, function (k, v) {
183 output += k + ': ' + v + ";\n";
184 })
185 output += "}\n";
186 }
187
188 return output;
189 };
190
191 var get_field_unit = function (ID, field) {
192 var unit = 'px';
193 if (typeof customify_settings.settings[ID] === "undefined" || typeof customify_settings.settings[ID].fields[field] === "undefined") {
194 return unit;
195 }
196
197 if (typeof customify_settings.settings[ID].fields[field].unit !== "undefined") {
198 return customify_settings.settings[ID].fields[field].unit;
199 } else if (typeof customify_settings.settings[ID].fields[field][3] !== "undefined") {
200 // in case of an associative array
201 return customify_settings.settings[ID].fields[field][3];
202 }
203 }
204
205 var maybeLoadFontFamily = function (font) {
206
207 if (typeof WebFont === "undefined") {
208 var tk = document.createElement('script');
209 tk.src = '//ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
210 tk.type = 'text/javascript';
211 var s = document.getElementsByTagName('script')[0];
212 s.parentNode.insertBefore(tk, s);
213 }
214
215 if (font.type === 'theme_font') {
216 WebFont.load({
217 custom: {
218 families: [font.font_family],
219 urls: [font.src]
220 }
221 });
222 } else if (font.type === 'google') {
223 var family = font.font_family,
224 variants = null,
225 subsets = null;
226
227 if (typeof font.variants !== "undefined") {
228 variants = maybeJsonParse(font.variants);
229
230 $.each(variants, function (k, v) {
231
232 if (k === "0") {
233 family = family + ':';
234 }
235
236 family = family + v;
237
238 if ( Object.keys(variants).length > ( parseInt(k) + 1 ) ) {
239 family = family + ',';
240 } else if ( typeof font.selected_subsets !== "undefined" ) {
241 // in case there is a subset selected, we need to separate it from the font weight
242 family = family + ':';
243 }
244 });
245 }
246
247 if (typeof font.selected_subsets !== "undefined") {
248 subsets = maybeJsonParse(font.selected_subsets);
249
250 $.each(subsets, function (k, v) {
251
252 if ( k === "0" ) {
253 family = family + ':';
254 }
255
256 family = family + v;
257
258 if ( Object.keys(subsets).length > ( parseInt(k) + 1 ) ) {
259 family = family + ',';
260 }
261 });
262 }
263
264 if (fonts_cache.indexOf(family) === -1) {
265 setTimeout(function(){
266 WebFont.load({
267 google: {families: [family]},
268 classes: false,
269 events: false,
270 error: function (e) {
271 console.log(e);
272 },
273 active: function () {
274 sessionStorage.fonts = true;
275 }
276 });
277 },10);
278
279 fonts_cache.push(family);
280 }
281
282 } else {
283 // else what?
284 }
285 }
286
287 var maybeJsonParse = function (value) {
288 var parsed;
289
290 //try and parse it, with decodeURIComponent
291 try {
292 parsed = JSON.parse(decodeURIComponent(value));
293 } catch (e) {
294
295 // in case of an error, treat is as a string
296 parsed = value;
297 }
298
299 return parsed;
300 };
301 });
302 })(jQuery, window, document);
303