Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

240 linhas
6.1 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. var _linkRule = require('./utils/linkRule');
  8. var _linkRule2 = _interopRequireDefault(_linkRule);
  9. var _RuleList = require('./RuleList');
  10. var _RuleList2 = _interopRequireDefault(_RuleList);
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  12. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  13. /* eslint-disable-next-line no-use-before-define */
  14. var StyleSheet = function () {
  15. function StyleSheet(styles, options) {
  16. var _this = this;
  17. _classCallCheck(this, StyleSheet);
  18. this.update = function (name, data) {
  19. if (typeof name === 'string') {
  20. _this.rules.update(name, data);
  21. } else {
  22. _this.rules.update(name);
  23. }
  24. return _this;
  25. };
  26. this.attached = false;
  27. this.deployed = false;
  28. this.linked = false;
  29. this.classes = {};
  30. this.options = _extends({}, options, {
  31. sheet: this,
  32. parent: this,
  33. classes: this.classes
  34. });
  35. this.renderer = new options.Renderer(this);
  36. this.rules = new _RuleList2['default'](this.options);
  37. for (var _name in styles) {
  38. this.rules.add(_name, styles[_name]);
  39. }
  40. this.rules.process();
  41. }
  42. /**
  43. * Attach renderable to the render tree.
  44. */
  45. _createClass(StyleSheet, [{
  46. key: 'attach',
  47. value: function attach() {
  48. if (this.attached) return this;
  49. if (!this.deployed) this.deploy();
  50. this.renderer.attach();
  51. if (!this.linked && this.options.link) this.link();
  52. this.attached = true;
  53. return this;
  54. }
  55. /**
  56. * Remove renderable from render tree.
  57. */
  58. }, {
  59. key: 'detach',
  60. value: function detach() {
  61. if (!this.attached) return this;
  62. this.renderer.detach();
  63. this.attached = false;
  64. return this;
  65. }
  66. /**
  67. * Add a rule to the current stylesheet.
  68. * Will insert a rule also after the stylesheet has been rendered first time.
  69. */
  70. }, {
  71. key: 'addRule',
  72. value: function addRule(name, decl, options) {
  73. var queue = this.queue;
  74. // Plugins can create rules.
  75. // In order to preserve the right order, we need to queue all `.addRule` calls,
  76. // which happen after the first `rules.add()` call.
  77. if (this.attached && !queue) this.queue = [];
  78. var rule = this.rules.add(name, decl, options);
  79. this.options.jss.plugins.onProcessRule(rule);
  80. if (this.attached) {
  81. if (!this.deployed) return rule;
  82. // Don't insert rule directly if there is no stringified version yet.
  83. // It will be inserted all together when .attach is called.
  84. if (queue) queue.push(rule);else {
  85. this.insertRule(rule);
  86. if (this.queue) {
  87. this.queue.forEach(this.insertRule, this);
  88. this.queue = undefined;
  89. }
  90. }
  91. return rule;
  92. }
  93. // We can't add rules to a detached style node.
  94. // We will redeploy the sheet once user will attach it.
  95. this.deployed = false;
  96. return rule;
  97. }
  98. /**
  99. * Insert rule into the StyleSheet
  100. */
  101. }, {
  102. key: 'insertRule',
  103. value: function insertRule(rule) {
  104. var renderable = this.renderer.insertRule(rule);
  105. if (renderable && this.options.link) (0, _linkRule2['default'])(rule, renderable);
  106. }
  107. /**
  108. * Create and add rules.
  109. * Will render also after Style Sheet was rendered the first time.
  110. */
  111. }, {
  112. key: 'addRules',
  113. value: function addRules(styles, options) {
  114. var added = [];
  115. for (var _name2 in styles) {
  116. added.push(this.addRule(_name2, styles[_name2], options));
  117. }
  118. return added;
  119. }
  120. /**
  121. * Get a rule by name.
  122. */
  123. }, {
  124. key: 'getRule',
  125. value: function getRule(name) {
  126. return this.rules.get(name);
  127. }
  128. /**
  129. * Delete a rule by name.
  130. * Returns `true`: if rule has been deleted from the DOM.
  131. */
  132. }, {
  133. key: 'deleteRule',
  134. value: function deleteRule(name) {
  135. var rule = this.rules.get(name);
  136. if (!rule) return false;
  137. this.rules.remove(rule);
  138. if (this.attached && rule.renderable) {
  139. return this.renderer.deleteRule(rule.renderable);
  140. }
  141. return true;
  142. }
  143. /**
  144. * Get index of a rule.
  145. */
  146. }, {
  147. key: 'indexOf',
  148. value: function indexOf(rule) {
  149. return this.rules.indexOf(rule);
  150. }
  151. /**
  152. * Deploy pure CSS string to a renderable.
  153. */
  154. }, {
  155. key: 'deploy',
  156. value: function deploy() {
  157. this.renderer.deploy();
  158. this.deployed = true;
  159. return this;
  160. }
  161. /**
  162. * Link renderable CSS rules from sheet with their corresponding models.
  163. */
  164. }, {
  165. key: 'link',
  166. value: function link() {
  167. var cssRules = this.renderer.getRules();
  168. // Is undefined when VirtualRenderer is used.
  169. if (cssRules) this.rules.link(cssRules);
  170. this.linked = true;
  171. return this;
  172. }
  173. /**
  174. * Update the function values with a new data.
  175. */
  176. }, {
  177. key: 'toString',
  178. /**
  179. * Convert rules to a CSS string.
  180. */
  181. value: function toString(options) {
  182. return this.rules.toString(options);
  183. }
  184. }]);
  185. return StyleSheet;
  186. }();
  187. exports['default'] = StyleSheet;