|
| 1 | +/*! |
| 2 | +* @erwinstone/input-touchspin v1.0.0 (https://input-touchspin.vercel.app/) |
| 3 | +* Copyright 2021 erwinstone |
| 4 | +* Licensed under MIT (https://github.com/erwinstone/input-touchspin/blob/master/LICENSE) |
| 5 | +*/ |
| 6 | +(function (global, factory) { |
| 7 | + typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : |
| 8 | + typeof define === 'function' && define.amd ? define(factory) : |
| 9 | + (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.BsToast = factory()); |
| 10 | +})(this, (function () { 'use strict'; |
| 11 | + |
| 12 | + class InputTouchspin { |
| 13 | + constructor(target) { |
| 14 | + this.styleId = "input-touchspin-style"; |
| 15 | + this.input = target.querySelector("[data-touchspin-input]"); |
| 16 | + this.btnUp = target.querySelector("[data-touchspin-up]"); |
| 17 | + this.btnDown = target.querySelector("[data-touchspin-down]"); |
| 18 | + this.style(); |
| 19 | + if (!this.input.readOnly && !this.input.disabled) { |
| 20 | + this.events(); |
| 21 | + } |
| 22 | + } |
| 23 | + style() { |
| 24 | + if (document.getElementById(this.styleId) === null) { |
| 25 | + const style = document.createElement("style"); |
| 26 | + style.id = this.styleId; |
| 27 | + style.textContent = "[data-touchspin-input]::-webkit-inner-spin-button,[data-touchspin-input]::-webkit-outer-spin-button{margin:0;-webkit-appearance:none}[data-touchspin-input]{-moz-appearance:textfield}"; |
| 28 | + document.head.appendChild(style); |
| 29 | + } |
| 30 | + } |
| 31 | + events() { |
| 32 | + this.btnUp.addEventListener("mousedown", () => this.spin()); |
| 33 | + this.btnDown.addEventListener("mousedown", () => this.spin(false)); |
| 34 | + this.btnUp.addEventListener("touchstart", (e) => { |
| 35 | + this.spin(); |
| 36 | + e.cancelable && e.preventDefault(); |
| 37 | + }); |
| 38 | + this.btnDown.addEventListener("touchstart", (e) => { |
| 39 | + this.spin(false); |
| 40 | + e.cancelable && e.preventDefault(); |
| 41 | + }); |
| 42 | + Array.from(["mouseup", "mouseleave", "touchend"]).forEach((e) => { |
| 43 | + this.btnUp.addEventListener(e, () => this.clearTimers()); |
| 44 | + this.btnDown.addEventListener(e, () => this.clearTimers()); |
| 45 | + }); |
| 46 | + this.input.addEventListener("wheel", (e) => { |
| 47 | + Math.sign(e.deltaY) < 1 ? this.step() : this.step(false); |
| 48 | + e.preventDefault(); |
| 49 | + }); |
| 50 | + } |
| 51 | + clearTimers() { |
| 52 | + clearTimeout(this.timeout); |
| 53 | + clearInterval(this.interval); |
| 54 | + } |
| 55 | + step(up = true) { |
| 56 | + const before = this.input.value; |
| 57 | + up ? this.input.stepUp() : this.input.stepDown(); |
| 58 | + const after = this.input.value; |
| 59 | + before !== after && this.input.dispatchEvent(new Event("change")); |
| 60 | + } |
| 61 | + spin(up = true) { |
| 62 | + this.step(up); |
| 63 | + this.timeout = setTimeout(() => this.interval = setInterval(() => this.step(up), 50), 300); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return InputTouchspin; |
| 68 | + |
| 69 | +})); |
0 commit comments