owl.carousel2.thumbs.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * Thumbs Plugin
  3. * @version 2.0.0
  4. * @author Gijs Rogé
  5. * @license The MIT License (MIT)
  6. */
  7. (function ($, window, document, undefined) {
  8. 'use strict';
  9. /**
  10. * Creates the thumbs plugin.
  11. * @class The thumbs Plugin
  12. * @param {Owl} carousel - The Owl Carousel
  13. */
  14. var Thumbs = function (carousel) {
  15. /**
  16. * Reference to the core.
  17. * @protected
  18. * @type {Owl}
  19. */
  20. this.owl = carousel;
  21. /**
  22. * All DOM elements for thumbnails
  23. * @protected
  24. * @type {Object}
  25. */
  26. this._thumbcontent = [];
  27. /**
  28. * Instance identiefier
  29. * @type {number}
  30. * @private
  31. */
  32. this._identifier = 0;
  33. /**
  34. * Return current item regardless of clones
  35. * @protected
  36. * @type {Object}
  37. */
  38. this.owl_currentitem = this.owl.options.startPosition;
  39. /**
  40. * The carousel element.
  41. * @type {jQuery}
  42. */
  43. this.$element = this.owl.$element;
  44. /**
  45. * All event handlers.
  46. * @protected
  47. * @type {Object}
  48. */
  49. this._handlers = {
  50. 'prepared.owl.carousel': $.proxy(function (e) {
  51. if (e.namespace && this.owl.options.thumbs && !this.owl.options.thumbImage && !this.owl.options.thumbsPrerendered && !this.owl.options.thumbImage) {
  52. if ($(e.content).find('[data-thumb]').attr('data-thumb') !== undefined) {
  53. this._thumbcontent.push($(e.content).find('[data-thumb]').attr('data-thumb'));
  54. }
  55. } else if (e.namespace && this.owl.options.thumbs && this.owl.options.thumbImage) {
  56. var innerImage = $(e.content).find('img');
  57. this._thumbcontent.push(innerImage);
  58. }
  59. }, this),
  60. 'initialized.owl.carousel': $.proxy(function (e) {
  61. if (e.namespace && this.owl.options.thumbs) {
  62. this.render();
  63. this.listen();
  64. this._identifier = this.owl.$element.data('slider-id');
  65. this.setActive();
  66. }
  67. }, this),
  68. 'changed.owl.carousel': $.proxy(function (e) {
  69. if (e.namespace && e.property.name === 'position' && this.owl.options.thumbs) {
  70. this._identifier = this.owl.$element.data('slider-id');
  71. this.setActive();
  72. }
  73. }, this)
  74. };
  75. // set default options
  76. this.owl.options = $.extend({}, Thumbs.Defaults, this.owl.options);
  77. // register the event handlers
  78. this.owl.$element.on(this._handlers);
  79. };
  80. /**
  81. * Default options.
  82. * @public
  83. */
  84. Thumbs.Defaults = {
  85. thumbs: true,
  86. thumbImage: false,
  87. thumbContainerClass: 'owl-thumbs',
  88. thumbItemClass: 'owl-thumb-item',
  89. moveThumbsInside: false
  90. };
  91. /**
  92. * Listen for thumbnail click
  93. * @protected
  94. */
  95. Thumbs.prototype.listen = function () {
  96. //set default options
  97. var options = this.owl.options;
  98. if (options.thumbsPrerendered) {
  99. this._thumbcontent._thumbcontainer = $('.' + options.thumbContainerClass);
  100. }
  101. //check what thumbitem has been clicked and move slider to that item
  102. $(this._thumbcontent._thumbcontainer).on('click', this._thumbcontent._thumbcontainer.children(), $.proxy(function (e) {
  103. // find relative slider
  104. this._identifier = $(e.target).closest('.' + options.thumbContainerClass).data('slider-id');
  105. // get index of clicked thumbnail
  106. var index = $(e.target).parent().is(this._thumbcontent._thumbcontainer) ? $(e.target).index() : $(e.target).closest('.'+options.thumbItemClass).index();
  107. if (options.thumbsPrerendered) {
  108. // slide to slide :)
  109. $('[data-slider-id=' + this._identifier + ']').trigger('to.owl.carousel', [index, options.dotsSpeed, true]);
  110. } else {
  111. this.owl.to(index, options.dotsSpeed);
  112. }
  113. e.preventDefault();
  114. }, this));
  115. };
  116. /**
  117. * Builds thumbnails
  118. * @protected
  119. */
  120. Thumbs.prototype.render = function () {
  121. //set default options
  122. var options = this.owl.options;
  123. //create thumbcontainer
  124. if (!options.thumbsPrerendered) {
  125. this._thumbcontent._thumbcontainer = $('<div>').addClass(options.thumbContainerClass).appendTo(this.$element);
  126. } else {
  127. this._thumbcontent._thumbcontainer = $('.' + options.thumbContainerClass + '');
  128. if(options.moveThumbsInside){
  129. this._thumbcontent._thumbcontainer.appendTo(this.$element);
  130. }
  131. }
  132. //create thumb items
  133. var i;
  134. if (!options.thumbImage) {
  135. for (i = 0; i < this._thumbcontent.length; ++i) {
  136. this._thumbcontent._thumbcontainer.append('<button class=' + options.thumbItemClass + '>' + this._thumbcontent[i] + '</button>');
  137. }
  138. } else {
  139. for (i = 0; i < this._thumbcontent.length; ++i) {
  140. this._thumbcontent._thumbcontainer.append('<button class=' + options.thumbItemClass + '><img src="' + this._thumbcontent[i].attr('src') + '" alt="' + this._thumbcontent[i].attr('alt') + '" /></button>');
  141. }
  142. }
  143. };
  144. /**
  145. * Updates active class on thumbnails
  146. * @protected
  147. */
  148. Thumbs.prototype.setActive = function () {
  149. // get startslide
  150. this.owl_currentitem = this.owl._current - (this.owl._clones.length / 2);
  151. if (this.owl_currentitem === this.owl._items.length) {
  152. this.owl_currentitem = 0;
  153. }
  154. //set default options
  155. var options = this.owl.options;
  156. // set relative thumbnail container
  157. var thumbContainer = options.thumbsPrerendered ? $('.' + options.thumbContainerClass + '[data-slider-id="' + this._identifier + '"]') : this._thumbcontent._thumbcontainer;
  158. thumbContainer.children().filter('.active').removeClass('active');
  159. thumbContainer.children().eq(this.owl_currentitem).addClass('active');
  160. };
  161. /**
  162. * Destroys the plugin.
  163. * @public
  164. */
  165. Thumbs.prototype.destroy = function () {
  166. var handler, property;
  167. for (handler in this._handlers) {
  168. this.owl.$element.off(handler, this._handlers[handler]);
  169. }
  170. for (property in Object.getOwnPropertyNames(this)) {
  171. typeof this[property] !== 'function' && (this[property] = null);
  172. }
  173. };
  174. $.fn.owlCarousel.Constructor.Plugins.Thumbs = Thumbs;
  175. })(window.Zepto || window.jQuery, window, document);