grid-template.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. parseTemplate = _require.parseTemplate,
  9. insertAreas = _require.insertAreas,
  10. getGridGap = _require.getGridGap,
  11. warnGridGap = _require.warnGridGap;
  12. var GridTemplate = function (_Declaration) {
  13. _inherits(GridTemplate, _Declaration);
  14. function GridTemplate() {
  15. _classCallCheck(this, GridTemplate);
  16. return _possibleConstructorReturn(this, _Declaration.apply(this, arguments));
  17. }
  18. /**
  19. * Translate grid-template to separate -ms- prefixed properties
  20. */
  21. GridTemplate.prototype.insert = function insert(decl, prefix, prefixes, result) {
  22. if (prefix !== '-ms-') return _Declaration.prototype.insert.call(this, decl, prefix, prefixes);
  23. if (decl.parent.some(function (i) {
  24. return i.prop === '-ms-grid-rows';
  25. })) {
  26. return undefined;
  27. }
  28. var gap = getGridGap(decl);
  29. var _parseTemplate = parseTemplate({
  30. decl: decl,
  31. gap: gap
  32. }),
  33. rows = _parseTemplate.rows,
  34. columns = _parseTemplate.columns,
  35. areas = _parseTemplate.areas;
  36. var hasAreas = Object.keys(areas).length > 0;
  37. var hasRows = Boolean(rows);
  38. var hasColumns = Boolean(columns);
  39. warnGridGap({
  40. gap: gap,
  41. hasColumns: hasColumns,
  42. decl: decl,
  43. result: result
  44. });
  45. if (hasRows && hasColumns || hasAreas) {
  46. decl.cloneBefore({
  47. prop: '-ms-grid-rows',
  48. value: rows,
  49. raws: {}
  50. });
  51. }
  52. if (hasColumns) {
  53. decl.cloneBefore({
  54. prop: '-ms-grid-columns',
  55. value: columns,
  56. raws: {}
  57. });
  58. }
  59. if (hasAreas) {
  60. insertAreas(areas, decl, result);
  61. }
  62. return decl;
  63. };
  64. return GridTemplate;
  65. }(Declaration);
  66. Object.defineProperty(GridTemplate, 'names', {
  67. enumerable: true,
  68. writable: true,
  69. value: ['grid-template']
  70. });
  71. module.exports = GridTemplate;