ExamplesPlaygroundReference Source

coral-spectrum/coral-component-masonry/src/scripts/MasonryFixedSpreadLayout.js

  1. /**
  2. * Copyright 2019 Adobe. All rights reserved.
  3. * This file is licensed to you under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License. You may obtain a copy
  5. * of the License at http://www.apache.org/licenses/LICENSE-2.0
  6. *
  7. * Unless required by applicable law or agreed to in writing, software distributed under
  8. * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
  9. * OF ANY KIND, either express or implied. See the License for the specific language
  10. * governing permissions and limitations under the License.
  11. */
  12.  
  13. import MasonryColumnLayout from './MasonryColumnLayout';
  14. import {getFirstRowFilledColumns} from './MasonryLayoutUtil';
  15.  
  16. /**
  17. Layout with fixed width and evenly spread items. The width of the items is defined with the <code>columnwidth</code>
  18. attribute.
  19.  
  20. @example
  21. <coral-masonry layout="fixed-spread" columnwidth="300">
  22.  
  23. @class Coral.Masonry.FixedSpreadLayout
  24. @extends {MasonryColumnLayout}
  25. */
  26. class MasonryFixedSpreadLayout extends MasonryColumnLayout {
  27. /** @inheritdoc */
  28. _writeStyles(items) {
  29. const columns = this._columns;
  30.  
  31. // If the first row is not filled, then the items should be aligned left
  32. this._alignLeft = getFirstRowFilledColumns(columns, items) < columns.length;
  33. if (!this._alignLeft) {
  34. const remainingWidth = this._masonryInnerWidth - this._columnWidth * columns.length;
  35. this._horSpacing = remainingWidth / (columns.length + 1);
  36. }
  37.  
  38. super._writeStyles(items);
  39. }
  40.  
  41. /** @inheritdoc */
  42. _getItemWidth(colspan) {
  43. return this._columnWidth * colspan + this._horSpacing * (colspan - 1);
  44. }
  45.  
  46. /** @inheritdoc */
  47. _getItemLeft(columnIndex) {
  48. return this._zeroOffsetLeft + this._columnWidth * columnIndex + this._horSpacing * (columnIndex + 1);
  49. }
  50. }
  51.  
  52. export default MasonryFixedSpreadLayout;