Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

191 řádky
4.8 KiB

  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  6. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  7. exports['default'] = jssGlobal;
  8. var _jss = require('jss');
  9. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  10. var propKey = '@global';
  11. var prefixKey = '@global ';
  12. var GlobalContainerRule = function () {
  13. function GlobalContainerRule(key, styles, options) {
  14. _classCallCheck(this, GlobalContainerRule);
  15. this.type = 'global';
  16. this.key = key;
  17. this.options = options;
  18. this.rules = new _jss.RuleList(_extends({}, options, {
  19. parent: this
  20. }));
  21. for (var selector in styles) {
  22. this.rules.add(selector, styles[selector], { selector: selector });
  23. }
  24. this.rules.process();
  25. }
  26. /**
  27. * Get a rule.
  28. */
  29. _createClass(GlobalContainerRule, [{
  30. key: 'getRule',
  31. value: function getRule(name) {
  32. return this.rules.get(name);
  33. }
  34. /**
  35. * Create and register rule, run plugins.
  36. */
  37. }, {
  38. key: 'addRule',
  39. value: function addRule(name, style, options) {
  40. var rule = this.rules.add(name, style, options);
  41. this.options.jss.plugins.onProcessRule(rule);
  42. return rule;
  43. }
  44. /**
  45. * Get index of a rule.
  46. */
  47. }, {
  48. key: 'indexOf',
  49. value: function indexOf(rule) {
  50. return this.rules.indexOf(rule);
  51. }
  52. /**
  53. * Generates a CSS string.
  54. */
  55. }, {
  56. key: 'toString',
  57. value: function toString() {
  58. return this.rules.toString();
  59. }
  60. }]);
  61. return GlobalContainerRule;
  62. }();
  63. var GlobalPrefixedRule = function () {
  64. function GlobalPrefixedRule(name, style, options) {
  65. _classCallCheck(this, GlobalPrefixedRule);
  66. this.name = name;
  67. this.options = options;
  68. var selector = name.substr(prefixKey.length);
  69. this.rule = options.jss.createRule(selector, style, _extends({}, options, {
  70. parent: this,
  71. selector: selector
  72. }));
  73. }
  74. _createClass(GlobalPrefixedRule, [{
  75. key: 'toString',
  76. value: function toString(options) {
  77. return this.rule.toString(options);
  78. }
  79. }]);
  80. return GlobalPrefixedRule;
  81. }();
  82. var separatorRegExp = /\s*,\s*/g;
  83. function addScope(selector, scope) {
  84. var parts = selector.split(separatorRegExp);
  85. var scoped = '';
  86. for (var i = 0; i < parts.length; i++) {
  87. scoped += scope + ' ' + parts[i].trim();
  88. if (parts[i + 1]) scoped += ', ';
  89. }
  90. return scoped;
  91. }
  92. function handleNestedGlobalContainerRule(rule) {
  93. var options = rule.options,
  94. style = rule.style;
  95. var rules = style[propKey];
  96. if (!rules) return;
  97. for (var name in rules) {
  98. options.sheet.addRule(name, rules[name], _extends({}, options, {
  99. selector: addScope(name, rule.selector)
  100. }));
  101. }
  102. delete style[propKey];
  103. }
  104. function handlePrefixedGlobalRule(rule) {
  105. var options = rule.options,
  106. style = rule.style;
  107. for (var prop in style) {
  108. if (prop.substr(0, propKey.length) !== propKey) continue;
  109. var selector = addScope(prop.substr(propKey.length), rule.selector);
  110. options.sheet.addRule(selector, style[prop], _extends({}, options, {
  111. selector: selector
  112. }));
  113. delete style[prop];
  114. }
  115. }
  116. /**
  117. * Convert nested rules to separate, remove them from original styles.
  118. *
  119. * @param {Rule} rule
  120. * @api public
  121. */
  122. function jssGlobal() {
  123. function onCreateRule(name, styles, options) {
  124. if (name === propKey) {
  125. return new GlobalContainerRule(name, styles, options);
  126. }
  127. if (name[0] === '@' && name.substr(0, prefixKey.length) === prefixKey) {
  128. return new GlobalPrefixedRule(name, styles, options);
  129. }
  130. var parent = options.parent;
  131. if (parent) {
  132. if (parent.type === 'global' || parent.options.parent.type === 'global') {
  133. options.global = true;
  134. }
  135. }
  136. if (options.global) options.selector = name;
  137. return null;
  138. }
  139. function onProcessRule(rule) {
  140. if (rule.type !== 'style') return;
  141. handleNestedGlobalContainerRule(rule);
  142. handlePrefixedGlobalRule(rule);
  143. }
  144. return { onCreateRule: onCreateRule, onProcessRule: onProcessRule };
  145. }