style.js - solid/unpolished

 /**

 * 
 * ==== DO NOT EDIT THIS FILE ====
 * 
 * ====  API Major Details ==== 
 * 
 * 1.  Each 'named' ('rule' function input parameter)  will exposes a composite function to
 * add (or modify) properties of a particular instance of a stylesheet rule within the <style> element.
 * 
 * 2.  attached to each composite rule, are the members *.as.*=[class(),id(),native()]; 
 * where class() prepends '.', id() prepends '#', and native prepends '' to the rule name.
 * 
 * 3.  Toggle rules on elements by calling '.toggle(element,t|f)'; where t|f forces removal of a rule;
 * implicitly rules use 'as.class()' and so elements are required to be toggled to view the effect.
 * 
 * 4.  The final REQUIRED STEP for a rule to be implemented is a call to the composite rule function 'promote()'
 * so that the TextNodes representing the rule components are added as children appropriately into the <style> parent.
 * 
 */

define(function() {
    var stylesheet = function() {
        var style = document.createElement('style');
        var reset = function() {
            document.head.appendChild(style);
        };
        reset();
        var proxy = function(title, element, clean) {
            if (clean) {
                if (element.className.indexOf(title) > 0) {
                    element.className = element.className.replace(' ' + title, '');
                    return;
                }
                if (element.className.indexOf(' ')>0) {
                    element.className = element.className.replace(title + ' ', '');
                    return;
                }
                element.className = element.className.replace(title + '', '');
                return;
            }
            if (element.className.indexOf(title) > -1) {
                return;
            }
            if (element.className.length) {
                element.className += ' ';
            }
            element.className += title;
        };
        var selectors = [[], []];
        var create = function(local) {
            var key = selectors[0].indexOf(local);
            if (key < 0) {
                var name = local,
                        type = '.',
                        properties = [[], []];
                var product = null;
                var promote = function() {
                    var backup = promote;
                    promote = function() {
                        promote = backup;
                        style.removeChild(product);
                        promote();
                    };
                    product = document.createTextNode(type + name + '{' + properties[1].join('') + '}');
                    style.appendChild(product);
                };
                var pattern = function(name, value) {
                    var key = properties[0].indexOf(name);
                    if (key < 0) {
                        properties[0].unshift(name),
                                properties[1].unshift(name + ':' + value + ';');
                        key = 0;
                    }
                    if (value) {
                        properties[1][key] = name + ':' + value + ';';
                    }
                    return function(name, value) {
                        return pattern(name, value);
                    };
                };
                pattern.promote = function() {
                    promote();
                };
                pattern.as = {
                    class: function() {
                        type = '.';
                    },
                    id: function() {
                        type = '#';
                    },
                    native: function() {
                        type = '';
                    }
                };
                pattern.toggle = function(element, clean) {
                    proxy(name, element, clean);
                };
                selectors[0].unshift(name), selectors[1].unshift(pattern), key = 0;
            }
            return selectors[1][key];
        };
        return function(rule) {
            return create(rule);
        };
    };
    var create = stylesheet();
    return create;
});

Comments