| 1 |
/* |
| 2 |
* cssUpdate - v1.0.0 |
| 3 |
*/ |
| 4 |
|
| 5 |
/** @namespace customify */ |
| 6 |
window.customify = window.customify || parent.customify || {} |
| 7 |
|
| 8 |
;(function ($, window, customify) { |
| 9 |
const pluginName = 'cssUpdate', |
| 10 |
defaults = { |
| 11 |
properties: ['color'], |
| 12 |
propertyValue: 'pink', |
| 13 |
classes: 'Null' |
| 14 |
} |
| 15 |
|
| 16 |
// Plugin constructor |
| 17 |
function cssLiveUpdater (element, options) { |
| 18 |
this.element = element |
| 19 |
this.settings = $.extend({}, defaults, options) |
| 20 |
this._defaults = defaults |
| 21 |
this._name = pluginName |
| 22 |
this._cssproperties = CSSOM.parse($(this.element).html()) |
| 23 |
this.init() |
| 24 |
} |
| 25 |
|
| 26 |
cssLiveUpdater.prototype = { |
| 27 |
init: function () { |
| 28 |
this.changeProperties() |
| 29 |
}, |
| 30 |
|
| 31 |
update_plugin: function (options) { |
| 32 |
this.settings = $.extend({}, defaults, options) |
| 33 |
this.changeProperties() |
| 34 |
}, |
| 35 |
|
| 36 |
changeProperties: function () { |
| 37 |
|
| 38 |
const self = this, |
| 39 |
css = this._cssproperties.cssRules |
| 40 |
|
| 41 |
if (typeof css[0] !== 'undefined' && css[0].hasOwnProperty('media')) { |
| 42 |
// in this case we run a media query object |
| 43 |
$.each(css, function (key, media_query) { |
| 44 |
// simple object with css properties |
| 45 |
// change them with new ones |
| 46 |
$.each(media_query.cssRules, function (i, property) { |
| 47 |
const property_name = property.style[0] |
| 48 |
css[key].cssRules[i].style[property_name] = self.updateCssRule(property_name, self.settings, css[key].cssRules[i].selectorText) |
| 49 |
}) |
| 50 |
}) |
| 51 |
|
| 52 |
} else { |
| 53 |
|
| 54 |
// simple object with css properties |
| 55 |
// change them with new ones |
| 56 |
$.each(css, function (i, property) { |
| 57 |
if (property.hasOwnProperty('style')) { |
| 58 |
const property_name = property.style[0] |
| 59 |
css[i].style[property_name] = self.updateCssRule(property_name, self.settings, css[i].selectorText) |
| 60 |
} |
| 61 |
}) |
| 62 |
} |
| 63 |
|
| 64 |
//Insert the new properties into <style> tag |
| 65 |
$(this.element).html(this._cssproperties.toString()) |
| 66 |
}, |
| 67 |
|
| 68 |
/** |
| 69 |
* Update one css properties by the given params |
| 70 |
* @param property_name |
| 71 |
* @param settings |
| 72 |
* @param selectorText |
| 73 |
* @returns {string} |
| 74 |
*/ |
| 75 |
updateCssRule: function (property_name, settings, selectorText) { |
| 76 |
|
| 77 |
const new_value = settings.propertyValue |
| 78 |
// if there is a negative property ... keep it negative |
| 79 |
const sign = settings['negative_value'] ? '-' : '' |
| 80 |
|
| 81 |
let unit = (typeof this.settings.unit !== 'undefined') ? this.settings.unit : '' |
| 82 |
// If the unit is empty (string, not boolean false) but the property should have a unit force 'px' as it |
| 83 |
if (unit === '' && customify.config.px_dependent_css_props.indexOf(property_name) !== -1) { |
| 84 |
unit = 'px' |
| 85 |
} |
| 86 |
|
| 87 |
if (typeof window[settings.properties.callback] === 'function') { |
| 88 |
window[settings.properties.callback](new_value, selectorText, property_name, unit) |
| 89 |
return '' |
| 90 |
} |
| 91 |
|
| 92 |
return sign + new_value + unit |
| 93 |
}, |
| 94 |
} |
| 95 |
|
| 96 |
// Plugin wrapper |
| 97 |
$.fn[pluginName] = function (options) { |
| 98 |
this.each(function () { |
| 99 |
|
| 100 |
const old_plugin = $(this).data('plugin_cssUpdate') |
| 101 |
|
| 102 |
if (typeof old_plugin !== 'undefined') { |
| 103 |
old_plugin.update_plugin(options) |
| 104 |
} else { |
| 105 |
$.data(this, 'plugin_' + pluginName, new cssLiveUpdater(this, options)) |
| 106 |
} |
| 107 |
}) |
| 108 |
|
| 109 |
// chain jQuery functions |
| 110 |
return this |
| 111 |
} |
| 112 |
|
| 113 |
})(jQuery, window, customify) |
| 114 |
|