foundation.slider.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. ;(function ($, window, document, undefined) {
  2. 'use strict';
  3. Foundation.libs.slider = {
  4. name : 'slider',
  5. version : '5.3.3',
  6. settings: {
  7. start: 0,
  8. end: 100,
  9. step: 1,
  10. initial: null,
  11. display_selector: '',
  12. vertical: false,
  13. on_change: function(){}
  14. },
  15. cache : {},
  16. init : function (scope, method, options) {
  17. Foundation.inherit(this,'throttle');
  18. this.bindings(method, options);
  19. this.reflow();
  20. },
  21. events : function() {
  22. var self = this;
  23. $(this.scope)
  24. .off('.slider')
  25. .on('mousedown.fndtn.slider touchstart.fndtn.slider pointerdown.fndtn.slider',
  26. '[' + self.attr_name() + ']:not(.disabled, [disabled]) .range-slider-handle', function(e) {
  27. if (!self.cache.active) {
  28. e.preventDefault();
  29. self.set_active_slider($(e.target));
  30. }
  31. })
  32. .on('mousemove.fndtn.slider touchmove.fndtn.slider pointermove.fndtn.slider', function(e) {
  33. if (!!self.cache.active) {
  34. e.preventDefault();
  35. if ($.data(self.cache.active[0], 'settings').vertical) {
  36. var scroll_offset = 0;
  37. if (!e.pageY) {
  38. scroll_offset = window.scrollY;
  39. }
  40. self.calculate_position(self.cache.active, (e.pageY ||
  41. e.originalEvent.clientY ||
  42. e.originalEvent.touches[0].clientY ||
  43. e.currentPoint.y)
  44. + scroll_offset);
  45. } else {
  46. self.calculate_position(self.cache.active, e.pageX ||
  47. e.originalEvent.clientX ||
  48. e.originalEvent.touches[0].clientX ||
  49. e.currentPoint.x);
  50. }
  51. }
  52. })
  53. .on('mouseup.fndtn.slider touchend.fndtn.slider pointerup.fndtn.slider', function(e) {
  54. self.remove_active_slider();
  55. })
  56. .on('change.fndtn.slider', function(e) {
  57. self.settings.on_change();
  58. });
  59. self.S(window)
  60. .on('resize.fndtn.slider', self.throttle(function(e) {
  61. self.reflow();
  62. }, 300));
  63. },
  64. set_active_slider : function($handle) {
  65. this.cache.active = $handle;
  66. },
  67. remove_active_slider : function() {
  68. this.cache.active = null;
  69. },
  70. calculate_position : function($handle, cursor_x) {
  71. var self = this,
  72. settings = $.data($handle[0], 'settings'),
  73. handle_l = $.data($handle[0], 'handle_l'),
  74. handle_o = $.data($handle[0], 'handle_o'),
  75. bar_l = $.data($handle[0], 'bar_l'),
  76. bar_o = $.data($handle[0], 'bar_o');
  77. requestAnimationFrame(function(){
  78. var pct;
  79. if (Foundation.rtl && !settings.vertical) {
  80. pct = self.limit_to(((bar_o+bar_l-cursor_x)/bar_l),0,1);
  81. } else {
  82. pct = self.limit_to(((cursor_x-bar_o)/bar_l),0,1);
  83. }
  84. pct = settings.vertical ? 1-pct : pct;
  85. var norm = self.normalized_value(pct, settings.start, settings.end, settings.step);
  86. self.set_ui($handle, norm);
  87. });
  88. },
  89. set_ui : function($handle, value) {
  90. var settings = $.data($handle[0], 'settings'),
  91. handle_l = $.data($handle[0], 'handle_l'),
  92. bar_l = $.data($handle[0], 'bar_l'),
  93. norm_pct = this.normalized_percentage(value, settings.start, settings.end),
  94. handle_offset = norm_pct*(bar_l-handle_l)-1,
  95. progress_bar_length = norm_pct*100;
  96. if (Foundation.rtl && !settings.vertical) {
  97. handle_offset = -handle_offset;
  98. }
  99. handle_offset = settings.vertical ? -handle_offset + bar_l - handle_l + 1 : handle_offset;
  100. this.set_translate($handle, handle_offset, settings.vertical);
  101. if (settings.vertical) {
  102. $handle.siblings('.range-slider-active-segment').css('height', progress_bar_length + '%');
  103. } else {
  104. $handle.siblings('.range-slider-active-segment').css('width', progress_bar_length + '%');
  105. }
  106. $handle.parent().attr(this.attr_name(), value).trigger('change').trigger('change.fndtn.slider');
  107. $handle.parent().children('input[type=hidden]').val(value);
  108. if (settings.input_id != '') {
  109. $(settings.display_selector).each(function(){
  110. if (this.hasOwnProperty('value')) {
  111. $(this).val(value);
  112. } else {
  113. $(this).text(value);
  114. }
  115. });
  116. }
  117. },
  118. normalized_percentage : function(val, start, end) {
  119. return Math.min(1, (val - start)/(end - start));
  120. },
  121. normalized_value : function(val, start, end, step) {
  122. var range = end - start,
  123. point = val*range,
  124. mod = (point-(point%step)) / step,
  125. rem = point % step,
  126. round = ( rem >= step*0.5 ? step : 0);
  127. return (mod*step + round) + start;
  128. },
  129. set_translate : function(ele, offset, vertical) {
  130. if (vertical) {
  131. $(ele)
  132. .css('-webkit-transform', 'translateY('+offset+'px)')
  133. .css('-moz-transform', 'translateY('+offset+'px)')
  134. .css('-ms-transform', 'translateY('+offset+'px)')
  135. .css('-o-transform', 'translateY('+offset+'px)')
  136. .css('transform', 'translateY('+offset+'px)');
  137. } else {
  138. $(ele)
  139. .css('-webkit-transform', 'translateX('+offset+'px)')
  140. .css('-moz-transform', 'translateX('+offset+'px)')
  141. .css('-ms-transform', 'translateX('+offset+'px)')
  142. .css('-o-transform', 'translateX('+offset+'px)')
  143. .css('transform', 'translateX('+offset+'px)');
  144. }
  145. },
  146. limit_to : function(val, min, max) {
  147. return Math.min(Math.max(val, min), max);
  148. },
  149. initialize_settings : function(handle) {
  150. var settings = $.extend({}, this.settings, this.data_options($(handle).parent()));
  151. if (settings.vertical) {
  152. $.data(handle, 'bar_o', $(handle).parent().offset().top);
  153. $.data(handle, 'bar_l', $(handle).parent().outerHeight());
  154. $.data(handle, 'handle_o', $(handle).offset().top);
  155. $.data(handle, 'handle_l', $(handle).outerHeight());
  156. } else {
  157. $.data(handle, 'bar_o', $(handle).parent().offset().left);
  158. $.data(handle, 'bar_l', $(handle).parent().outerWidth());
  159. $.data(handle, 'handle_o', $(handle).offset().left);
  160. $.data(handle, 'handle_l', $(handle).outerWidth());
  161. }
  162. $.data(handle, 'bar', $(handle).parent());
  163. $.data(handle, 'settings', settings);
  164. },
  165. set_initial_position : function($ele) {
  166. var settings = $.data($ele.children('.range-slider-handle')[0], 'settings'),
  167. initial = (!!settings.initial ? settings.initial : Math.floor((settings.end-settings.start)*0.5/settings.step)*settings.step+settings.start),
  168. $handle = $ele.children('.range-slider-handle');
  169. this.set_ui($handle, initial);
  170. },
  171. set_value : function(value) {
  172. var self = this;
  173. $('[' + self.attr_name() + ']', this.scope).each(function(){
  174. $(this).attr(self.attr_name(), value);
  175. });
  176. if (!!$(this.scope).attr(self.attr_name())) {
  177. $(this.scope).attr(self.attr_name(), value);
  178. }
  179. self.reflow();
  180. },
  181. reflow : function() {
  182. var self = this;
  183. self.S('[' + this.attr_name() + ']').each(function() {
  184. var handle = $(this).children('.range-slider-handle')[0],
  185. val = $(this).attr(self.attr_name());
  186. self.initialize_settings(handle);
  187. if (val) {
  188. self.set_ui($(handle), parseFloat(val));
  189. } else {
  190. self.set_initial_position($(this));
  191. }
  192. });
  193. }
  194. };
  195. }(jQuery, window, window.document));