slide.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // slide
  2. ( function( window, factory ) {
  3. // universal module definition
  4. /* jshint strict: false */
  5. if ( typeof define == 'function' && define.amd ) {
  6. // AMD
  7. define( factory );
  8. } else if ( typeof module == 'object' && module.exports ) {
  9. // CommonJS
  10. module.exports = factory();
  11. } else {
  12. // browser global
  13. window.Flickity = window.Flickity || {};
  14. window.Flickity.Slide = factory();
  15. }
  16. }( window, function factory() {
  17. 'use strict';
  18. function Slide( parent ) {
  19. this.parent = parent;
  20. this.isOriginLeft = parent.originSide == 'left';
  21. this.cells = [];
  22. this.outerWidth = 0;
  23. this.height = 0;
  24. }
  25. var proto = Slide.prototype;
  26. proto.addCell = function( cell ) {
  27. this.cells.push( cell );
  28. this.outerWidth += cell.size.outerWidth;
  29. this.height = Math.max( cell.size.outerHeight, this.height );
  30. // first cell stuff
  31. if ( this.cells.length == 1 ) {
  32. this.x = cell.x; // x comes from first cell
  33. var beginMargin = this.isOriginLeft ? 'marginLeft' : 'marginRight';
  34. this.firstMargin = cell.size[ beginMargin ];
  35. }
  36. };
  37. proto.updateTarget = function() {
  38. var endMargin = this.isOriginLeft ? 'marginRight' : 'marginLeft';
  39. var lastCell = this.getLastCell();
  40. var lastMargin = lastCell ? lastCell.size[ endMargin ] : 0;
  41. var slideWidth = this.outerWidth - ( this.firstMargin + lastMargin );
  42. this.target = this.x + this.firstMargin + slideWidth * this.parent.cellAlign;
  43. };
  44. proto.getLastCell = function() {
  45. return this.cells[ this.cells.length - 1 ];
  46. };
  47. proto.select = function() {
  48. this.cells.forEach( function( cell ) {
  49. cell.select();
  50. });
  51. };
  52. proto.unselect = function() {
  53. this.cells.forEach( function( cell ) {
  54. cell.unselect();
  55. });
  56. };
  57. proto.getCellElements = function() {
  58. return this.cells.map( function( cell ) {
  59. return cell.element;
  60. });
  61. };
  62. return Slide;
  63. }));