ui.slider.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. (function($) {
  2. //Web Forms 2.0
  3. window.webforms = 1;
  4. if(window['webforms']) {
  5. $(document).ready(function() {
  6. $("input").each(function() {
  7. if(this.getAttribute("type") == "range") {
  8. var cur = $(this);
  9. var slider = $("<div class='ui-slider'></div>").css({ width: cur.innerWidth()+"px", height: cur.innerHeight()+"px" }).insertAfter(cur);
  10. var handle = $("<div class='ui-slider-handle'></div>").appendTo(slider);
  11. slider.css({
  12. "position": cur.css("position") == "absolute" ? "absolute" : "relative",
  13. "left": cur.css("left"),
  14. "right": cur.css("right"),
  15. "zIndex": cur.css("zIndex"),
  16. "float": cur.css("float"),
  17. "clear": cur.css("clear")
  18. });
  19. cur.css({ position: "absolute", opacity: 0, top: "-1000px", left: "-1000px" });
  20. slider.slider({
  21. maxValue: cur.attr("max"),
  22. minValue: cur.attr("min"),
  23. startValue: this.getAttribute("value"),
  24. stepping: cur.attr("step"),
  25. change: function(e, ui) { cur[0].value = ui.value; cur[0].setAttribute("value", ui.value); },
  26. });
  27. slider = slider.sliderInstance();
  28. cur.bind("keydown", function(e) {
  29. var o = slider.interaction.options;
  30. switch(e.keyCode) {
  31. case 37:
  32. slider.moveTo(slider.interaction.curValue+o.minValue-(o.stepping || 1));
  33. break;
  34. case 39:
  35. slider.moveTo(slider.interaction.curValue+o.minValue+(o.stepping || 1));
  36. break;
  37. }
  38. if(e.keyCode != 9) return false;
  39. });
  40. };
  41. });
  42. });
  43. }
  44. //Make nodes selectable by expression
  45. $.extend($.expr[':'], { slider: "(' '+a.className+' ').indexOf(' ui-slider ')" });
  46. $.fn.slider = function(o) {
  47. return this.each(function() {
  48. new $.ui.slider(this, o);
  49. });
  50. }
  51. //Macros for external methods that support chaining
  52. var methods = "destroy,enable,disable,moveTo".split(",");
  53. for(var i=0;i<methods.length;i++) {
  54. var cur = methods[i], f;
  55. eval('f = function() { var a = arguments; return this.each(function() { if(jQuery(this).is(".ui-slider")) jQuery.data(this, "ui-slider")["'+cur+'"](a); }); }');
  56. $.fn["slider"+cur.substr(0,1).toUpperCase()+cur.substr(1)] = f;
  57. };
  58. //get instance method
  59. $.fn.sliderInstance = function() {
  60. if($(this[0]).is(".ui-slider")) return $.data(this[0], "ui-slider");
  61. return false;
  62. };
  63. $.ui.slider = function(el, o) {
  64. var options = {};
  65. o = o || {};
  66. $.extend(options, o);
  67. $.extend(options, {
  68. axis: o.axis || (el.offsetWidth < el.offsetHeight ? 'vertical' : 'horizontal'),
  69. maxValue: parseInt(o.maxValue) || 100,
  70. minValue: parseInt(o.minValue) || 0,
  71. startValue: parseInt(o.startValue) || 0,
  72. _start: function(h, p, c, t, e) {
  73. self.start.apply(t, [self, e]); // Trigger the start callback
  74. },
  75. _beforeStop: function(h, p, c, t, e) {
  76. self.stop.apply(t, [self, e]); // Trigger the start callback
  77. },
  78. _drag: function(h, p, c, t, e) {
  79. self.drag.apply(t, [self, e]); // Trigger the start callback
  80. },
  81. startCondition: function() {
  82. return !self.disabled;
  83. }
  84. });
  85. var self = this;
  86. var o = options;
  87. $.data(el, "ui-slider", this);
  88. o.stepping = parseInt(o.stepping) || (o.steps ? o.maxValue/o.steps : 0);
  89. o.realValue = (o.maxValue - o.minValue);
  90. this.handle = options.handle ? $(options.handle, el) : $('.ui-slider-handle', el);
  91. if(this.handle.length == 1) {
  92. this.interaction = new $.ui.mouseInteraction(this.handle[0], options);
  93. this.multipleHandles = false;
  94. } else {
  95. this.interactions = [];
  96. this.handle.each(function() {
  97. self.interactions.push(new $.ui.mouseInteraction(this, options));
  98. });
  99. this.multipleHandles = true;
  100. }
  101. this.element = el;
  102. $(this.element).addClass("ui-slider");
  103. if(o.axis == 'horizontal') {
  104. this.parentSize = $(this.element).outerWidth() - this.handle.outerWidth();
  105. this.prop = 'left';
  106. }
  107. if(o.axis == 'vertical') {
  108. this.parentSize = $(this.element).outerHeight() - this.handle.outerHeight();
  109. this.prop = 'top';
  110. }
  111. if(!this.multipleHandles) {
  112. $(el).bind('click', function(e) { self.click.apply(self, [e]); });
  113. if(!isNaN(o.startValue)) this.moveTo(o.startValue,options.realValue, null, false);
  114. }
  115. }
  116. $.extend($.ui.slider.prototype, {
  117. currentTarget: null,
  118. lastTarget: null,
  119. destroy: function() {
  120. $(this.element).removeClass("ui-slider").removeClass("ui-slider-disabled");
  121. this.interaction.destroy();
  122. },
  123. enable: function() {
  124. $(this.element).removeClass("ui-slider-disabled");
  125. this.disabled = false;
  126. },
  127. disable: function() {
  128. $(this.element).addClass("ui-slider-disabled");
  129. this.disabled = true;
  130. },
  131. nonvalidRange: function(self) {
  132. for(var i=0;i<this.interactions.length;i++) {
  133. if(self == this.interactions[i]) {
  134. if(this.interactions[i-1]) {
  135. if(this.interactions[i-1].curValue > this.interactions[i].curValue) return this.interactions[i-1].curValue;
  136. }
  137. if(this.interactions[i+1]) {
  138. if(this.interactions[i+1].curValue < this.interactions[i].curValue) return this.interactions[i+1].curValue;
  139. }
  140. }
  141. }
  142. return false;
  143. },
  144. prepareCallbackObj: function(self,m) {
  145. var cur = this;
  146. var func = function() {
  147. var retVal = [];
  148. for(var i=0;i<cur.interactions.length;i++) {
  149. retVal.push((cur.interactions[i].curValue || 0)+self.options.minValue);
  150. }
  151. return retVal;
  152. };
  153. return {
  154. handle: self.helper,
  155. pixel: m,
  156. value: self.curValue+self.options.minValue,
  157. values: this.multipleHandles ? func() : self.curValue+self.options.minValue,
  158. slider: self
  159. }
  160. },
  161. click: function(e) {
  162. var o = this.interaction.options;
  163. var pointer = [e.pageX,e.pageY];
  164. var offset = $(this.interaction.element).offsetParent().offset({ border: false });
  165. if(this.interaction.element == e.target || this.disabled) return;
  166. this.interaction.pickValue = this.interaction.curValue;
  167. this.drag.apply(this.interaction, [this, e, [pointer[0]-offset.left-this.handle[0].offsetWidth/2,pointer[1]-offset.top-this.handle[0].offsetHeight/2]]);
  168. if(this.interaction.pickValue != this.interaction.curValue)
  169. $(this.element).triggerHandler("slidechange", [e, this.prepareCallbackObj(this.interaction)], o.change);
  170. },
  171. start: function(that, e) {
  172. var o = this.options;
  173. $(that.element).triggerHandler("slidestart", [e, that.prepareCallbackObj(this)], o.start);
  174. this.pickValue = this.curValue;
  175. return false;
  176. },
  177. stop: function(that, e) {
  178. var o = this.options;
  179. $(that.element).triggerHandler("slidestop", [e, that.prepareCallbackObj(this)], o.stop);
  180. if(this.pickValue != this.curValue) $(that.element).triggerHandler("slidechange", [e, that.prepareCallbackObj(this)], o.change);
  181. return false;
  182. },
  183. drag: function(that, e, pos) {
  184. var o = this.options;
  185. this.pos = pos || [this.pos[0]-this.element.offsetWidth/2, this.pos[1]-this.element.offsetHeight/2];
  186. if(o.axis == 'horizontal') var m = this.pos[0];
  187. if(o.axis == 'vertical') var m = this.pos[1];
  188. var p = that.parentSize;
  189. var prop = that.prop;
  190. if(m < 0) m = 0;
  191. if(m > p) m = p;
  192. this.curValue = (Math.round((m/p)*o.realValue));
  193. if(o.stepping) {
  194. this.curValue = Math.round(this.curValue/o.stepping)*o.stepping;
  195. m = ((this.curValue)/o.realValue) * p;
  196. }
  197. if(that.interactions) {
  198. nonvalidRange = that.nonvalidRange(this);
  199. if(nonvalidRange) {
  200. this.curValue = nonvalidRange;
  201. m = ((this.curValue)/o.realValue) * p;
  202. }
  203. }
  204. $(this.element).css(prop, m+'px');
  205. $(that.element).triggerHandler("slide", [e, that.prepareCallbackObj(this,m)], o.slide);
  206. return false;
  207. },
  208. moveTo: function(value,scale,changeslide,p) { // renamed from goto to moveTo as goto is reserved javascript word
  209. if(this.multipleHandles) return false; //TODO: Multiple handle moveTo function
  210. var o = this.interaction.options;
  211. var offset = $(this.interaction.element).offsetParent().offset({ border: false });
  212. this.interaction.pickValue = this.interaction.curValue;
  213. value = value-o.minValue;
  214. var modifier = scale || o.realValue;
  215. var p = this.parentSize;
  216. var prop = this.prop;
  217. m = Math.round(((value)/modifier) * p);
  218. if(m < 0) m = 0;
  219. if(m > p) m = p;
  220. this.interaction.curValue = (Math.round((m/p)*o.realValue));
  221. if(o.stepping) {
  222. this.interaction.curValue = Math.round(this.interaction.curValue/o.stepping)*o.stepping;
  223. m = ((this.interaction.curValue)/o.realValue) * p;
  224. }
  225. $(this.interaction.element).css(prop, m+'px');
  226. if(!changeslide && this.interaction.pickValue != this.interaction.curValue && !p)
  227. $(this.element).triggerHandler("slidechange", [e, this.prepareCallbackObj(this.interaction)], o.change);
  228. if(changeslide)
  229. $(this.element).triggerHandler("slide", [e, this.prepareCallbackObj(this.interaction)], o.slide);
  230. }
  231. });
  232. })($);