-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmart-number-inputs.min.js
10 lines (10 loc) · 1.74 KB
/
smart-number-inputs.min.js
1
2
3
4
5
6
7
8
9
10
/*
* smart-number-inputs.js
*
* Increment/decrement number values in text inputs using up and down arrow keys.
*
* Author: Martti Laine (@codeclown)
* License: MIT
* URL: https://github.com/codeclown/smart-number-inputs
*/
"use strict";var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};!function(e){"object"===("undefined"==typeof exports?"undefined":_typeof(exports))&&"object"===("undefined"==typeof module?"undefined":_typeof(module))?module.exports=e():"function"==typeof define&&define.amd?define(e):window.smartNumberInputs=e()}(function(){function n(e){if(38===e.which||40===e.which){e.preventDefault();var t=e.currentTarget,n=t.value,o=t.selectionStart,r=t.selectionEnd,i=38===e.which?1:-1;e.shiftKey&&(i*=10);var f=l(n,o,r,i);t.value=f.value,t.setSelectionRange(f.start,f.end),function(e,t){if("createEvent"in document){var n=document.createEvent("HTMLEvents");n.initEvent(t,!1,!0),e.dispatchEvent(n)}else e.fireEvent("on"+t)}(t,"change")}}var a=/([^\-\w0-9]+)/,l=function(e,t,n,o){for(var r,i=e.split(a),f=null,l=null,u=0,c=0;c<i.length;c++)if(u+=(r=i[c]).length,!r.match(a)&&(t<=u&&(null===f&&(f=u-r.length),i[c]=r.replace(/-?[0-9]+/,parseInt(r.replace(/^.*?(-?[0-9]+).*/,"$1"),10)+o)),u-=r.length-i[c].length,(n-=r.length-i[c].length)<=u)){l=u;break}return{value:i.join(""),start:f,end:l}};return{eventHandler:n,enable:function e(t){if(function(e){return"[object Array]"===Object.prototype.toString.call(e)}(t)||t instanceof NodeList||t instanceof HTMLCollection)return function(e,t){for(var n=0;n<e.length;n++)t(e[n])}(t,e);!function(e,t,n){e.addEventListener(t,n)}(t,"keydown",n)},modify:l}});