cell.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Flickity.Cell
  2. ( function( window, factory ) {
  3. // universal module definition
  4. /* jshint strict: false */
  5. if ( typeof define == 'function' && define.amd ) {
  6. // AMD
  7. define( [
  8. 'get-size/get-size'
  9. ], function( getSize ) {
  10. return factory( window, getSize );
  11. });
  12. } else if ( typeof module == 'object' && module.exports ) {
  13. // CommonJS
  14. module.exports = factory(
  15. window,
  16. require('get-size')
  17. );
  18. } else {
  19. // browser global
  20. window.Flickity = window.Flickity || {};
  21. window.Flickity.Cell = factory(
  22. window,
  23. window.getSize
  24. );
  25. }
  26. }( window, function factory( window, getSize ) {
  27. 'use strict';
  28. function Cell( elem, parent ) {
  29. this.element = elem;
  30. this.parent = parent;
  31. this.create();
  32. }
  33. var proto = Cell.prototype;
  34. proto.create = function() {
  35. this.element.style.position = 'absolute';
  36. this.element.setAttribute( 'aria-hidden', 'true' );
  37. this.x = 0;
  38. this.shift = 0;
  39. };
  40. proto.destroy = function() {
  41. // reset style
  42. this.unselect();
  43. this.element.style.position = '';
  44. var side = this.parent.originSide;
  45. this.element.style[ side ] = '';
  46. };
  47. proto.getSize = function() {
  48. this.size = getSize( this.element );
  49. };
  50. proto.setPosition = function( x ) {
  51. this.x = x;
  52. this.updateTarget();
  53. this.renderPosition( x );
  54. };
  55. // setDefaultTarget v1 method, backwards compatibility, remove in v3
  56. proto.updateTarget = proto.setDefaultTarget = function() {
  57. var marginProperty = this.parent.originSide == 'left' ? 'marginLeft' : 'marginRight';
  58. this.target = this.x + this.size[ marginProperty ] +
  59. this.size.width * this.parent.cellAlign;
  60. };
  61. proto.renderPosition = function( x ) {
  62. // render position of cell with in slider
  63. var side = this.parent.originSide;
  64. this.element.style[ side ] = this.parent.getPositionValue( x );
  65. };
  66. proto.select = function() {
  67. this.element.classList.add('is-selected');
  68. this.element.removeAttribute('aria-hidden');
  69. };
  70. proto.unselect = function() {
  71. this.element.classList.remove('is-selected');
  72. this.element.setAttribute( 'aria-hidden', 'true' );
  73. };
  74. /**
  75. * @param {Integer} factor - 0, 1, or -1
  76. **/
  77. proto.wrapShift = function( shift ) {
  78. this.shift = shift;
  79. this.renderPosition( this.x + this.parent.slideableWidth * shift );
  80. };
  81. proto.remove = function() {
  82. this.element.parentNode.removeChild( this.element );
  83. };
  84. return Cell;
  85. }));