Last active
April 15, 2019 12:38
-
-
Save Daniel217D/d524bfd2145dfe4b25665bfea6867e1a to your computer and use it in GitHub Desktop.
Fast input for number with plus and minus buttons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
function Btn(element, settings) { | |
return new Btn.prototype.init(element, settings); | |
} | |
Btn.prototype.init = function(element, settings) { | |
const defaults = { | |
min: 0, | |
max: 10, | |
step: 1, | |
start: 1 | |
} | |
const inputRegex = new RegExp(/^\d+$/); | |
if (settings) { | |
for (let i in defaults) { | |
if (settings[i] === undefined) | |
settings[i] = defaults[i]; | |
} | |
} else { | |
settings = defaults; | |
} | |
if (settings.min > settings.max) { | |
[settings.max, settings.min] = [settings.min, settings.max]; | |
} | |
if (!element) { | |
element = document.body; | |
} | |
const el = document.createElement('div'); | |
el.classList.add('cButton'); | |
const plus = document.createElement('button'); | |
plus.innerText = '+' | |
const minus = document.createElement('button'); | |
minus.innerText = '-' | |
const input = document.createElement('input'); | |
let data = input.value = settings.start; | |
el.appendChild(minus); | |
el.appendChild(input); | |
el.appendChild(plus); | |
plus.addEventListener('mousedown', function() { | |
data += settings.step; | |
timer(plus, '+'); | |
update(); | |
}); | |
minus.addEventListener('mousedown', function() { | |
data -= settings.step; | |
timer(minus, '-'); | |
update(); | |
}); | |
input.oninput = function(e) { | |
if (this.value === '') { | |
return; | |
} | |
if (inputRegex.test(this.value)) { | |
data = parseInt(this.value); | |
update(); | |
} else if (this.value.length === 1) { | |
data = 0; | |
this.value = ''; | |
} | |
}; | |
input.onchange = function() { | |
if (this.value === '') { | |
data = settings.start < settings.min ? settings.start : settings.min; | |
} | |
update(); | |
} | |
this.add = function(n) { | |
n = n ? n : 1; | |
data = data + n; | |
update(); | |
return this; | |
} | |
function timer(el, sign) { | |
let c_interval; | |
let c_timeout; | |
let time_interval = 100; | |
let step = 1; | |
c_timeout = setTimeout(function() { | |
c_interval = setInterval(function() { | |
if (sign === '+') { | |
data += settings.step; | |
} else { | |
data -= settings.step; | |
} | |
time_interval = time_interval > 50 ? time_interval - step++ : time_interval; | |
update(); | |
}, time_interval); | |
}, 250); | |
el.addEventListener('mouseleave', function() { | |
clearTimeout(c_timeout); | |
clearInterval(c_interval); | |
}) | |
el.addEventListener('mouseup', function() { | |
clearTimeout(c_timeout); | |
clearInterval(c_interval); | |
}) | |
}; | |
function update() { | |
if (data < settings.min) { | |
data = settings.min; | |
} | |
if (data > settings.max) { | |
data = settings.max; | |
} | |
input.value = data; | |
} | |
element.appendChild(el); | |
} | |
window.Btn = Btn; | |
})() | |
const $btn = Btn(false, { | |
max: 100, | |
min: 1, | |
start: 50 | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment