foundation.slider.js 6.3 KB

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