grid-template-areas.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  5. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : _defaults(subClass, superClass); }
  6. var Declaration = require('../declaration');
  7. var _require = require('./grid-utils'),
  8. parseGridAreas = _require.parseGridAreas,
  9. insertAreas = _require.insertAreas,
  10. prefixTrackProp = _require.prefixTrackProp,
  11. prefixTrackValue = _require.prefixTrackValue,
  12. getGridGap = _require.getGridGap,
  13. warnGridGap = _require.warnGridGap;
  14. function getGridRows(tpl) {
  15. return tpl.trim().slice(1, -1).split(/['"]\s*['"]?/g);
  16. }
  17. var GridTemplateAreas = function (_Declaration) {
  18. _inherits(GridTemplateAreas, _Declaration);
  19. function GridTemplateAreas() {
  20. _classCallCheck(this, GridTemplateAreas);
  21. return _possibleConstructorReturn(this, _Declaration.apply(this, arguments));
  22. }
  23. /**
  24. * Translate grid-template-areas to separate -ms- prefixed properties
  25. */
  26. GridTemplateAreas.prototype.insert = function insert(decl, prefix, prefixes, result) {
  27. if (prefix !== '-ms-') return _Declaration.prototype.insert.call(this, decl, prefix, prefixes);
  28. var hasColumns = false;
  29. var hasRows = false;
  30. var parent = decl.parent;
  31. var gap = getGridGap(decl);
  32. // remove already prefixed rows and columns
  33. // without gutter to prevent doubling prefixes
  34. parent.walkDecls(/-ms-grid-(rows|columns)/, function (i) {
  35. return i.remove();
  36. });
  37. // add empty tracks to rows and columns
  38. parent.walkDecls(/grid-template-(rows|columns)/, function (trackDecl) {
  39. if (trackDecl.prop === 'grid-template-rows') {
  40. hasRows = true;
  41. var prop = trackDecl.prop,
  42. value = trackDecl.value;
  43. trackDecl.cloneBefore({
  44. prop: prefixTrackProp({ prop: prop, prefix: prefix }),
  45. value: prefixTrackValue({ value: value, gap: gap.row })
  46. });
  47. } else {
  48. hasColumns = true;
  49. var _prop = trackDecl.prop,
  50. _value = trackDecl.value;
  51. trackDecl.cloneBefore({
  52. prop: prefixTrackProp({ prop: _prop, prefix: prefix }),
  53. value: prefixTrackValue({ value: _value, gap: gap.column })
  54. });
  55. }
  56. });
  57. var gridRows = getGridRows(decl.value);
  58. if (hasColumns && !hasRows && gap.row && gridRows.length > 1) {
  59. decl.cloneBefore({
  60. prop: '-ms-grid-rows',
  61. value: prefixTrackValue({
  62. value: 'repeat(' + gridRows.length + ', auto)',
  63. gap: gap.row
  64. }),
  65. raws: {}
  66. });
  67. }
  68. // warnings
  69. warnGridGap({
  70. gap: gap,
  71. hasColumns: hasColumns,
  72. decl: decl,
  73. result: result
  74. });
  75. var areas = parseGridAreas({
  76. rows: gridRows,
  77. gap: gap
  78. });
  79. insertAreas(areas, decl, result);
  80. return decl;
  81. };
  82. return GridTemplateAreas;
  83. }(Declaration);
  84. Object.defineProperty(GridTemplateAreas, 'names', {
  85. enumerable: true,
  86. writable: true,
  87. value: ['grid-template-areas']
  88. });
  89. module.exports = GridTemplateAreas;