fit-rows.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * fitRows layout mode
  3. */
  4. ( function( window, factory ) {
  5. // universal module definition
  6. /* jshint strict: false */ /*globals define, module, require */
  7. if ( typeof define == 'function' && define.amd ) {
  8. // AMD
  9. define( [
  10. '../layout-mode'
  11. ],
  12. factory );
  13. } else if ( typeof exports == 'object' ) {
  14. // CommonJS
  15. module.exports = factory(
  16. require('../layout-mode')
  17. );
  18. } else {
  19. // browser global
  20. factory(
  21. window.Isotope.LayoutMode
  22. );
  23. }
  24. }( window, function factory( LayoutMode ) {
  25. 'use strict';
  26. var FitRows = LayoutMode.create('fitRows');
  27. var proto = FitRows.prototype;
  28. proto._resetLayout = function() {
  29. this.x = 0;
  30. this.y = 0;
  31. this.maxY = 0;
  32. this._getMeasurement( 'gutter', 'outerWidth' );
  33. };
  34. proto._getItemLayoutPosition = function( item ) {
  35. item.getSize();
  36. var itemWidth = item.size.outerWidth + this.gutter;
  37. // if this element cannot fit in the current row
  38. var containerWidth = this.isotope.size.innerWidth + this.gutter;
  39. if ( this.x !== 0 && itemWidth + this.x > containerWidth ) {
  40. this.x = 0;
  41. this.y = this.maxY;
  42. }
  43. var position = {
  44. x: this.x,
  45. y: this.y
  46. };
  47. this.maxY = Math.max( this.maxY, this.y + item.size.outerHeight );
  48. this.x += itemWidth;
  49. return position;
  50. };
  51. proto._getContainerSize = function() {
  52. return { height: this.maxY };
  53. };
  54. return FitRows;
  55. }));