grid-rows-columns.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. "use strict";
  2. function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }
  3. function _createSuper(Derived) { return function () { var Super = _getPrototypeOf(Derived), result; if (_isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
  4. function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  5. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  6. function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
  7. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  8. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _defaults(subClass, superClass); }
  9. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  10. var Declaration = require('../declaration');
  11. var _require = require('./grid-utils'),
  12. prefixTrackProp = _require.prefixTrackProp,
  13. prefixTrackValue = _require.prefixTrackValue,
  14. autoplaceGridItems = _require.autoplaceGridItems,
  15. getGridGap = _require.getGridGap,
  16. inheritGridGap = _require.inheritGridGap;
  17. var Processor = require('../processor');
  18. var GridRowsColumns = /*#__PURE__*/function (_Declaration) {
  19. _inheritsLoose(GridRowsColumns, _Declaration);
  20. var _super = _createSuper(GridRowsColumns);
  21. function GridRowsColumns() {
  22. return _Declaration.apply(this, arguments) || this;
  23. }
  24. var _proto = GridRowsColumns.prototype;
  25. /**
  26. * Change property name for IE
  27. */
  28. _proto.prefixed = function prefixed(prop, prefix) {
  29. if (prefix === '-ms-') {
  30. return prefixTrackProp({
  31. prop: prop,
  32. prefix: prefix
  33. });
  34. }
  35. return _Declaration.prototype.prefixed.call(this, prop, prefix);
  36. }
  37. /**
  38. * Change IE property back
  39. */
  40. ;
  41. _proto.normalize = function normalize(prop) {
  42. return prop.replace(/^grid-(rows|columns)/, 'grid-template-$1');
  43. };
  44. _proto.insert = function insert(decl, prefix, prefixes, result) {
  45. if (prefix !== '-ms-') return _Declaration.prototype.insert.call(this, decl, prefix, prefixes);
  46. var parent = decl.parent,
  47. prop = decl.prop,
  48. value = decl.value;
  49. var isRowProp = prop.includes('rows');
  50. var isColumnProp = prop.includes('columns');
  51. var hasGridTemplate = parent.some(function (i) {
  52. return i.prop === 'grid-template' || i.prop === 'grid-template-areas';
  53. });
  54. /**
  55. * Not to prefix rows declaration if grid-template(-areas) is present
  56. */
  57. if (hasGridTemplate && isRowProp) {
  58. return false;
  59. }
  60. var processor = new Processor({
  61. options: {}
  62. });
  63. var status = processor.gridStatus(parent, result);
  64. var gap = getGridGap(decl);
  65. gap = inheritGridGap(decl, gap) || gap;
  66. var gapValue = isRowProp ? gap.row : gap.column;
  67. if ((status === 'no-autoplace' || status === true) && !hasGridTemplate) {
  68. gapValue = null;
  69. }
  70. var prefixValue = prefixTrackValue({
  71. value: value,
  72. gap: gapValue
  73. });
  74. /**
  75. * Insert prefixes
  76. */
  77. decl.cloneBefore({
  78. prop: prefixTrackProp({
  79. prop: prop,
  80. prefix: prefix
  81. }),
  82. value: prefixValue
  83. });
  84. var autoflow = parent.nodes.find(function (i) {
  85. return i.prop === 'grid-auto-flow';
  86. });
  87. var autoflowValue = 'row';
  88. if (autoflow && !processor.disabled(autoflow, result)) {
  89. autoflowValue = autoflow.value.trim();
  90. }
  91. if (status === 'autoplace') {
  92. /**
  93. * Show warning if grid-template-rows decl is not found
  94. */
  95. var rowDecl = parent.nodes.find(function (i) {
  96. return i.prop === 'grid-template-rows';
  97. });
  98. if (!rowDecl && hasGridTemplate) {
  99. return undefined;
  100. } else if (!rowDecl && !hasGridTemplate) {
  101. decl.warn(result, 'Autoplacement does not work without grid-template-rows property');
  102. return undefined;
  103. }
  104. /**
  105. * Show warning if grid-template-columns decl is not found
  106. */
  107. var columnDecl = parent.nodes.find(function (i) {
  108. return i.prop === 'grid-template-columns';
  109. });
  110. if (!columnDecl && !hasGridTemplate) {
  111. decl.warn(result, 'Autoplacement does not work without grid-template-columns property');
  112. }
  113. /**
  114. * Autoplace grid items
  115. */
  116. if (isColumnProp && !hasGridTemplate) {
  117. autoplaceGridItems(decl, result, gap, autoflowValue);
  118. }
  119. }
  120. return undefined;
  121. };
  122. return GridRowsColumns;
  123. }(Declaration);
  124. _defineProperty(GridRowsColumns, "names", ['grid-template-rows', 'grid-template-columns', 'grid-rows', 'grid-columns']);
  125. module.exports = GridRowsColumns;