postmessage.js (10018B)
1 (function () { 2 var api = wp.customize; 3 4 _.each(jsvars, function (jsVars, setting) { 5 6 var css = '', 7 cssArray = {}; 8 9 api(setting, function (value) { 10 11 value.bind(_.debounce( 12 function (newval) { 13 14 if (undefined !== jsVars && 0 < jsVars.length) { 15 16 _.each(jsVars, function (jsVar) { 17 18 var val = newval; 19 20 // Make sure element is defined. 21 if (undefined === jsVar.element) { 22 jsVar.element = ''; 23 } 24 25 // Make sure property is defined. 26 if (undefined === jsVar.property) { 27 jsVar.property = ''; 28 } 29 30 // Use empty prefix if undefined 31 if (undefined === jsVar.prefix) { 32 jsVar.prefix = ''; 33 } 34 35 // Use empty suffix if undefined 36 if (undefined === jsVar.suffix) { 37 jsVar.suffix = ''; 38 } 39 40 // Use empty units if undefined 41 if (undefined === jsVar.units) { 42 jsVar.units = ''; 43 } 44 45 // Use css if method is undefined 46 if (undefined === jsVar['function']) { 47 jsVar['function'] = 'css'; 48 } 49 50 // Use $ (just the value) if value_pattern is undefined 51 if (undefined === jsVar.value_pattern) { 52 jsVar.value_pattern = '$'; 53 } 54 55 _.each(jsVars, function (args, i) { 56 57 // Value is a string 58 if ('string' === typeof newval) { 59 60 // Process the value pattern 61 if (undefined !== args.value_pattern) { 62 val = args.value_pattern.replace(/\$/g, args.prefix + newval + args.units + args.suffix); 63 } else { 64 val = args.prefix + newval + args.units + args.suffix; 65 } 66 67 // Simple tweak for background-image properties. 68 if ('background-image' === args.property) { 69 if (0 > val.indexOf('url(')) { 70 val = 'url("' + val + '")'; 71 } 72 } 73 74 // Inject HTML 75 if ('html' === args['function']) { 76 if ('undefined' !== typeof args.attr && undefined !== args.attr) { 77 jQuery(args.element).attr(args.attr, val); 78 } else { 79 jQuery(args.element).html(val); 80 } 81 82 // Add CSS 83 } else { 84 85 // If we have new value, replace style contents with custom css 86 if ('' !== val) { 87 var cssNewContent = ''; 88 var styleProps = args.property.split(','); 89 90 for (var propIndex = 0; propIndex < styleProps.length; propIndex++) { 91 cssNewContent += styleProps[propIndex] + ':' + val + ';' 92 } 93 94 95 cssArray[i] = args.element + '{' + cssNewContent + '}'; 96 } 97 98 // Else let's clear it out 99 else { 100 cssArray[i] = ''; 101 } 102 103 } 104 105 // Value is an object 106 } else if ('object' === typeof newval) { 107 108 cssArray[i] = ''; 109 _.each(newval, function (subValueValue, subValueKey) { 110 111 if (undefined !== args.choice) { 112 if (args.choice === subValueKey) { 113 cssArray[i] += args.element + '{' + args.property + ':' + args.prefix + subValueValue + args.units + args.suffix + ';}'; 114 } 115 } else { 116 if (_.contains(['top', 'bottom', 'left', 'right'], subValueKey)) { 117 cssArray[i] += args.element + '{' + args.property + '-' + subValueKey + ':' + args.prefix + subValueValue + args.units + args.suffix + ';}'; 118 } else { 119 if (subValueKey === "variant") { 120 121 var fontStyle = (subValueValue.indexOf('italic') !== -1) ? "italic" : "normal"; 122 cssArray[i] += args.element + '{font-style:' + fontStyle + ';}'; 123 124 var fontWeight = (subValueValue.indexOf('regular') !== -1) ? "400" : subValueValue.replace(/([a-zA-Z]+)/ig, ""); 125 cssArray[i] += args.element + '{font-weight:' + fontWeight + ';}'; 126 127 } else { 128 if (subValueKey === 'addwebfont' || subValueKey == 'subsets') { 129 return; 130 } else { 131 if (subValueKey === 'mobile-font-size') { 132 cssArray[i] += '@media screen and (max-width: 767px) {' + args.element + '{font-size:' + args.prefix + subValueValue + args.units + args.suffix + ';}}'; 133 } else { 134 cssArray[i] += args.element + '{' + subValueKey + ':' + args.prefix + subValueValue + args.units + args.suffix + ';}'; 135 } 136 } 137 138 } 139 } 140 } 141 }); 142 143 } 144 145 if (args.media_query) { 146 cssArray[i] = args.media_query + " {\n\n" + cssArray[i] + "\n\n}"; 147 } 148 149 150 }); 151 152 }); 153 154 css = _.toArray(cssArray).reduce(function (a, s) { 155 return a + s 156 }, ''); 157 158 setTimeout(function () { 159 160 if ('' !== css) { 161 162 // Make sure we have a stylesheet with the defined ID. 163 // If we don't then add it. 164 if (!jQuery('#kirki-customizer-postmessage' + setting.replace(/\[/g, '-').replace(/\]/g, '')).size()) { 165 jQuery('head').append('<style id="kirki-customizer-postmessage' + setting.replace(/\[/g, '-').replace(/\]/g, '') + '"></style>'); 166 } 167 jQuery('#kirki-customizer-postmessage' + setting.replace(/\[/g, '-').replace(/\]/g, '')).text(css); 168 } 169 170 }, 100); 171 172 // _.each(cssArray, function (singleCSS) { 173 // 174 // css = ''; 175 // 176 // setTimeout(function () { 177 // 178 // if ('' !== singleCSS) { 179 // css += singleCSS; 180 // } 181 // 182 // // Attach to <head> 183 // if ('' !== css) { 184 // 185 // // Make sure we have a stylesheet with the defined ID. 186 // // If we don't then add it. 187 // if (!jQuery('#kirki-customizer-postmessage' + setting.replace(/\[/g, '-').replace(/\]/g, '')).size()) { 188 // jQuery('head').append('<style id="kirki-customizer-postmessage' + setting.replace(/\[/g, '-').replace(/\]/g, '') + '"></style>'); 189 // } 190 // jQuery('#kirki-customizer-postmessage' + setting.replace(/\[/g, '-').replace(/\]/g, '')).text(css); 191 // } 192 // 193 // }, 100); 194 // 195 // }); 196 197 } 198 199 } 200 ), 100); 201 202 }); 203 204 }); 205 206 })(jQuery);