[d565449] | 1 | /* eslint-disable no-invalid-this */
|
---|
| 2 | 'use strict';
|
---|
| 3 |
|
---|
| 4 | var atoms = require('./atoms').atoms;
|
---|
| 5 |
|
---|
| 6 | exports.addon = function (renderer, rules) {
|
---|
| 7 | rules = rules || {};
|
---|
| 8 |
|
---|
| 9 | var defaultRules = renderer.assign({}, atoms, {
|
---|
| 10 | s: function (prop, value) {
|
---|
| 11 | if (prop instanceof Object) {
|
---|
| 12 | for (var name in prop) {
|
---|
| 13 | defaultRules.s.call(this, name, prop[name]);
|
---|
| 14 | }
|
---|
| 15 | } else {
|
---|
| 16 | this[prop] = (value instanceof Object) ? (value.obj || value) : value;
|
---|
| 17 | }
|
---|
| 18 | },
|
---|
| 19 |
|
---|
| 20 | hover: function (value) {
|
---|
| 21 | defaultRules.s.call(this, ':hover', value);
|
---|
| 22 | },
|
---|
| 23 |
|
---|
| 24 | focus: function (value) {
|
---|
| 25 | defaultRules.s.call(this, ':focus', value);
|
---|
| 26 | },
|
---|
| 27 |
|
---|
| 28 | bgWhite: function () {
|
---|
| 29 | this.backgroundColor = '#fff';
|
---|
| 30 | },
|
---|
| 31 |
|
---|
| 32 | bgBlack: function () {
|
---|
| 33 | this.backgroundColor = '#000';
|
---|
| 34 | },
|
---|
| 35 |
|
---|
| 36 | rel: function () {
|
---|
| 37 | this.position = 'relative';
|
---|
| 38 | },
|
---|
| 39 |
|
---|
| 40 | abs: function () {
|
---|
| 41 | this.position = 'absolute';
|
---|
| 42 | },
|
---|
| 43 |
|
---|
| 44 | pointer: function () {
|
---|
| 45 | this.cursor = 'pointer';
|
---|
| 46 | },
|
---|
| 47 |
|
---|
| 48 | inlineBlock: function () {
|
---|
| 49 | this.display = 'inline-block';
|
---|
| 50 | },
|
---|
| 51 |
|
---|
| 52 | bold: function () {
|
---|
| 53 | this.fontWeight = 'bold';
|
---|
| 54 | },
|
---|
| 55 |
|
---|
| 56 | b: function () {
|
---|
| 57 | this.fontWeight = 'bold';
|
---|
| 58 | },
|
---|
| 59 |
|
---|
| 60 | italic: function () {
|
---|
| 61 | this.fontStyle = 'italic';
|
---|
| 62 | },
|
---|
| 63 |
|
---|
| 64 | i: function () {
|
---|
| 65 | this.fontStyle = 'italic';
|
---|
| 66 | },
|
---|
| 67 |
|
---|
| 68 | underline: function () {
|
---|
| 69 | this.textDecoration = 'underline';
|
---|
| 70 | },
|
---|
| 71 |
|
---|
| 72 | u: function () {
|
---|
| 73 | this.textDecoration = 'underline';
|
---|
| 74 | },
|
---|
| 75 | });
|
---|
| 76 |
|
---|
| 77 | rules = renderer.assign(defaultRules, rules);
|
---|
| 78 |
|
---|
| 79 | var snake = {};
|
---|
| 80 |
|
---|
| 81 | var start = function () {
|
---|
| 82 | var instance = Object.create(snake);
|
---|
| 83 |
|
---|
| 84 | instance.obj = {};
|
---|
| 85 | instance.toString = function () {
|
---|
| 86 | if (process.env.NODE_ENV !== 'production') {
|
---|
| 87 | require('./__dev__/warnOnMissingDependencies')('snake', renderer, ['cache']);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | return renderer.cache(instance.obj);
|
---|
| 91 | };
|
---|
| 92 | instance.valueOf = instance.toString;
|
---|
| 93 |
|
---|
| 94 | return instance;
|
---|
| 95 | };
|
---|
| 96 |
|
---|
| 97 | var checkStart = function (name, fn) {
|
---|
| 98 | return function () {
|
---|
| 99 | if (!this.obj) {
|
---|
| 100 | var instance = start();
|
---|
| 101 |
|
---|
| 102 | if (typeof instance[name] === 'function') {
|
---|
| 103 | return instance[name].apply(instance, arguments);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | return instance[name];
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | return fn.apply(this, arguments);
|
---|
| 110 | };
|
---|
| 111 | };
|
---|
| 112 |
|
---|
| 113 | var onRule = function (name) {
|
---|
| 114 | var rule = rules[name];
|
---|
| 115 |
|
---|
| 116 | if (typeof rule === 'function') {
|
---|
| 117 | if (!rule.length) {
|
---|
| 118 | Object.defineProperty(snake, name, {
|
---|
| 119 | get: checkStart(name, function () {
|
---|
| 120 | rule.call(this.obj);
|
---|
| 121 | return this;
|
---|
| 122 | })
|
---|
| 123 | });
|
---|
| 124 | } else {
|
---|
| 125 | snake[name] = checkStart(name, function () {
|
---|
| 126 | rule.apply(this.obj, arguments);
|
---|
| 127 | return this;
|
---|
| 128 | });
|
---|
| 129 | }
|
---|
| 130 | } else {
|
---|
| 131 | snake[name] = checkStart(name, function (value) {
|
---|
| 132 | this.obj['' + rule] = value;
|
---|
| 133 | return this;
|
---|
| 134 | });
|
---|
| 135 | }
|
---|
| 136 | };
|
---|
| 137 |
|
---|
| 138 | for (var name in rules) onRule(name);
|
---|
| 139 |
|
---|
| 140 | renderer.s = snake;
|
---|
| 141 | };
|
---|