diff --git a/History.md b/History.md index 0093723..807fbe0 100644 --- a/History.md +++ b/History.md @@ -1,4 +1,11 @@ +0.0.4 / 2014-07-02 +================== + + * allow tapping anywhere on the slider, not just the handle + * fix empty strings when the element has no min/max set + * use the build files in example, as to not rely on crappy grunt for development + 0.0.2 / 2014-04-26 ================== @@ -11,4 +18,4 @@ 0.0.1 / 2014-03-17 ================== - * Initial release \ No newline at end of file + * Initial release diff --git a/bower.json b/bower.json index 6d0c678..db1cc66 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "powerange" , "main": "dist/powerange.js" - , "version": "0.0.2" + , "version": "0.0.4" , "description": "iOS 7 style range slider" , "keywords": [ "range" diff --git a/component.json b/component.json index 5b1e699..18b4226 100644 --- a/component.json +++ b/component.json @@ -2,7 +2,7 @@ "name": "powerange" , "repo": "abpetkov/powerange" , "description": "iOS 7 style range slider" - , "version": "0.0.2" + , "version": "0.0.4" , "keywords": [ "range" , "slider" @@ -32,4 +32,4 @@ , "https://cdn.rawgit.com" , "https://rawgit.com" ] -} \ No newline at end of file +} diff --git a/example/example.html b/example/example.html index 5bbadbe..b385b09 100644 --- a/example/example.html +++ b/example/example.html @@ -5,7 +5,7 @@ Powerange - iOS 7 style range slider - + @@ -391,9 +391,11 @@

Development

- + - \ No newline at end of file + diff --git a/lib/horizontal.js b/lib/horizontal.js index d98a63e..4258cbc 100644 --- a/lib/horizontal.js +++ b/lib/horizontal.js @@ -75,10 +75,20 @@ Horizontal.prototype.setPosition = function(val) { Horizontal.prototype.onmousedown = function(e) { if (e.touches) e = e.touches[0]; - this.startX = e.clientX; + // snap to the touched position + if (e.target.className !== 'range-handle') { + var offset = 0; + var parent = this.handle; + while (parent = parent.offsetParent) + offset += parent.offsetLeft; + this.startX = offset - window.scrollX + this.handle.offsetLeft + this.handle.clientWidth / 2; + } else + this.startX = e.clientX; this.handleOffsetX = this.handle.offsetLeft; this.restrictHandleX = this.slider.offsetWidth - this.handle.offsetWidth; this.unselectable(this.slider, true); + // trigger a move right away, to reposition the handle + this.onmousemove(e); }; /** @@ -115,4 +125,4 @@ Horizontal.prototype.onmousemove = function(e) { Horizontal.prototype.onmouseup = function(e) { this.unselectable(this.slider, false); -}; \ No newline at end of file +}; diff --git a/lib/main.js b/lib/main.js index bfc0d47..acaddd8 100644 --- a/lib/main.js +++ b/lib/main.js @@ -28,9 +28,9 @@ function Powerange(element, options) { this.element = element; this.options = options || {}; - this.slider = this.create('span', 'range-bar'); + this.slider = this.create('div', 'range-container'); - if (this.element !== null && this.element.type === 'text') this.init(); + if (this.element !== null) this.init(); } /** @@ -41,11 +41,11 @@ function Powerange(element, options) { Powerange.prototype.bindEvents = function () { this.handle = this.slider.querySelector('.range-handle'); - this.touch = events(this.handle, this); + this.touch = events(this.slider, this); this.touch.bind('touchstart', 'onmousedown'); this.touch.bind('touchmove', 'onmousemove'); this.touch.bind('touchend', 'onmouseup'); - this.mouse = mouse(this.handle, this); + this.mouse = mouse(this.slider, this); this.mouse.bind(); }; @@ -97,10 +97,13 @@ Powerange.prototype.generate = function() { } }; + var bar = this.create('span', 'range-bar'); + this.slider.appendChild(bar); + for (var key in elems) { if (elems.hasOwnProperty(key)) { var temp = this.create(elems[key].type, elems[key].selector); - this.slider.appendChild(temp); + bar.appendChild(temp); } } @@ -295,4 +298,4 @@ Powerange.prototype.init = function() { this.checkValues(this.options.start); this.setRange(this.options.min, this.options.max); this.disable(); -}; \ No newline at end of file +}; diff --git a/lib/powerange.css b/lib/powerange.css index 7aebfa7..1d2a40c 100644 --- a/lib/powerange.css +++ b/lib/powerange.css @@ -9,6 +9,11 @@ * Horizontal slider style (default). */ +.range-container { + height: 4px; + padding: 13px 0px; +} + .range-bar { background-color: #a9acb1; border-radius: 15px; @@ -64,9 +69,14 @@ * Vertical slider style. */ -.vertical { +.vertical.range-container { + width: 4px; + padding: 0px 13px; height: 100%; +} +.vertical .range-bar { width: 4px; + height: 100%; } .vertical .range-quantity { @@ -116,4 +126,4 @@ .range-disabled { cursor: default; -} \ No newline at end of file +} diff --git a/lib/powerange.js b/lib/powerange.js index 569b0b3..339f2a2 100644 --- a/lib/powerange.js +++ b/lib/powerange.js @@ -33,6 +33,18 @@ var defaults = { module.exports = function(element, options) { options = options || {}; + // if we have an element, fill in some values based on that input element + if (element) { + if (typeof options.min === 'undefined' && element.min) + options.min = element.min; + if (typeof options.max === 'undefined' && element.max) + options.max = element.max; + if (typeof options.step === 'undefined' && element.step) + options.step = element.step; + if (typeof options.start === 'undefined' && element.value) + options.start = element.value; + } + for (var i in defaults) { if (options[i] == null) { options[i] = defaults[i]; @@ -44,4 +56,4 @@ module.exports = function(element, options) { } else { return new Horizontal(element, options); } -}; \ No newline at end of file +}; diff --git a/lib/vertical.js b/lib/vertical.js index 68cc119..2c7df99 100644 --- a/lib/vertical.js +++ b/lib/vertical.js @@ -77,10 +77,20 @@ Vertical.prototype.setPosition = function(val) { Vertical.prototype.onmousedown = function(e) { if (e.touches) e = e.touches[0]; - this.startY = e.clientY; + // snap to the touched position + if (e.target.className !== 'range-handle') { + var offset = 0; + var parent = this.handle; + while (parent = parent.offsetParent) + offset += parent.offsetTop; + this.startY = offset - window.scrollY + this.handle.offsetTop + this.handle.clientHeight / 2; + } else + this.startY = e.clientY; this.handleOffsetY = this.slider.offsetHeight - this.handle.offsetHeight - this.handle.offsetTop; this.restrictHandleY = this.slider.offsetHeight - this.handle.offsetHeight; this.unselectable(this.slider, true); + // trigger a move right away, to reposition the handle + this.onmousemove(e); }; /** @@ -117,4 +127,4 @@ Vertical.prototype.onmousemove = function(e) { Vertical.prototype.onmouseup = function(e) { this.unselectable(this.slider, false); -}; \ No newline at end of file +}; diff --git a/package.json b/package.json index 7cbfe97..72a42f1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "powerange" - , "version": "0.0.2" + , "version": "0.0.4" , "description": "iOS 7 style range slider" , "main": "powerange.js" , "repository": {