(function($) { var Customizer = function($element, $options) { var $select = $($options.select, $element), $sidebar = $($options.sidebar, $element), $advanced = $($options.advanced, $element), $style, memory = window.sessionStorage || {}; this.$options = $options; this.$select = $select; $element.on({ "update": function(e, value, force) { if ($("option", $select).length != $options.styles.length) { $select.html($.mustache($options.template.select, $options)); if(memory["uikit-customizer-last-theme"]) { $select.val(memory["uikit-customizer-last-theme"]); } } if (value) { $select.val(value); } var name = $select.val(), current = $style; $.each($options.styles, function(i, style) { if (name == style.name) { $style = style; } }); if ($style !== current || force) { $element.trigger("updating", $style); loadStyle($style).done(function() { renderSidebar($style); $element.trigger("updated", $style); }); } }, "updating": $options.updating, "updated": $options.updated }); $select.on("change", function(e) { memory["uikit-customizer-last-theme"] = $select.val(); setTimeout(function() { $element.trigger("update"); }, 1); }); $advanced.on("change", function(e) { $sidebar[$(this).prop("checked") ? "addClass" : "removeClass"]("cm-show-advanced"); }).trigger("change"); $element.on("click", "a.cm-more-link", function(e) { e.preventDefault(); $(this).parents("fieldset:first").toggleClass("cm-show-more"); }); $element.on("change", "input[name=vars], select[name=vars]", function(e) { e.preventDefault(); var name = $(this).attr('data-name'), value = $(this).val(); if (value === "") { delete $style.variables[name]; } else { $style.variables[name] = value; } $element.trigger("updated", $style); }); function loadStyle(style) { var deferred = $.Deferred(), imports = ""; if (style.less) { return deferred.resolve(); } if($.isArray(style.url)) { for (var i = 0; i < style.url.length; i++) { imports += "@import url(" + style.url[i] + ");"; } } else { imports = "@import url(" + style.url + ");"; } $.less.resolveImports(imports).done(function(less) { $.ajax({"url": style.config, "cache": false, "dataType": "json"}).done(function(config) { var vars = $.less.getVars(less); style.less = less; style.config = config; style.groups = []; style.variables = style.variables || {}; style.matchName = matchName; $.each(style.config.groups, function(i, grp) { var variable, group = {label: grp.label, variables: [], advanced: grp.advanced || false, more: false}; $.each(grp.vars, function(i, opt) { $.each(vars, function(name, value) { if (matchName(opt, name)) { delete vars[name]; variable = { "name": name, "default": value, "placeholder": value, "label": name.replace(/^@/, "").replace(/^\w+\-/, "").replace(/\-/g, " "), "more": value.indexOf("@") !== -1, "value": function() { return style.variables[name] ? style.variables[name] : ""; } }; if (variable.more) { group.more = true; variable.placeholder = "@"; } group.variables.push(variable); } }); }); if (group.variables.length) { style.groups.push(group); } }); deferred.resolve(); }); }); return deferred.promise(); } function renderSidebar(style) { $sidebar.html($.mustache($options.template.sidebar, style)); $sidebar.find("input[data-name]").each(function() { var input = $(this), value = input.val() || input.data("default"), select, groups; if (input.attr("data-default").indexOf('@') !== -1) { return; } $.each(style.config.controls, function(i, control) { $.each(control.vars, function(i, pattern) { if (matchName(pattern, input.attr("data-name"))) { switch (control.type) { case "color": var placeholder = $('