bootstrap-input-spinner 4.0.4 → 4.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,375 +0,0 @@
1
- /**
2
- * Author and copyright: Stefan Haack (https://shaack.com)
3
- * Repository: https://github.com/shaack/bootstrap-input-spinner
4
- * License: MIT, see file 'LICENSE'
5
- */
6
-
7
- ;(function ($) {
8
- "use strict"
9
-
10
- // the default editor for parsing and rendering
11
- const I18nEditor = function (props, element) {
12
- const locale = props.locale || "en-US"
13
-
14
- this.parse = function (customFormat) {
15
- const numberFormat = new Intl.NumberFormat(locale)
16
- const thousandSeparator = numberFormat.format(11111).replace(/1/g, '') || '.'
17
- const decimalSeparator = numberFormat.format(1.1).replace(/1/g, '')
18
- return parseFloat(customFormat
19
- .replace(new RegExp(' ', 'g'), '')
20
- .replace(new RegExp('\\' + thousandSeparator, 'g'), '')
21
- .replace(new RegExp('\\' + decimalSeparator), '.')
22
- )
23
- }
24
-
25
- this.render = function (number) {
26
- const decimals = parseInt(element.getAttribute("data-decimals")) || 0
27
- const digitGrouping = !(element.getAttribute("data-digit-grouping") === "false")
28
- const numberFormat = new Intl.NumberFormat(locale, {
29
- minimumFractionDigits: decimals,
30
- maximumFractionDigits: decimals,
31
- useGrouping: digitGrouping
32
- })
33
- return numberFormat.format(number)
34
- }
35
- }
36
-
37
- let triggerKeyPressed = false
38
- const originalVal = $.fn.val
39
- $.fn.val = function (value) {
40
- if (arguments.length >= 1) {
41
- for (let i = 0; i < this.length; i++) {
42
- if (this[i]["bootstrap-input-spinner"] && this[i].setValue) {
43
- const element = this[i]
44
- setTimeout(function () {
45
- element.setValue(value)
46
- })
47
- }
48
- }
49
- }
50
- return originalVal.apply(this, arguments)
51
- }
52
-
53
- $.fn.inputSpinner = function (methodOrProps) {
54
-
55
- if (methodOrProps === "destroy") {
56
- this.each(function () {
57
- if (this["bootstrap-input-spinner"]) {
58
- this.destroyInputSpinner()
59
- } else {
60
- console.warn("element", this, "is no bootstrap-input-spinner")
61
- }
62
- })
63
- return this
64
- }
65
-
66
- const props = {
67
- decrementButton: "<strong>&minus;</strong>", // button text
68
- incrementButton: "<strong>&plus;</strong>", // ..
69
- groupClass: "", // css class of the resulting input-group
70
- buttonsClass: "btn-outline-secondary",
71
- buttonsWidth: "2.5rem",
72
- textAlign: "center", // alignment of the entered number
73
- autoDelay: 500, // ms threshold before auto value change
74
- autoInterval: 50, // speed of auto value change, set to `undefined` to disable auto-change
75
- buttonsOnly: false, // set this `true` to disable the possibility to enter or paste the number via keyboard
76
- keyboardStepping: true, // set this to `false` to disallow the use of the up and down arrow keys to step
77
- locale: navigator.language, // the locale, per default detected automatically from the browser
78
- editor: I18nEditor, // the editor (parsing and rendering of the input)
79
- template: // the template of the input
80
- '<div class="input-group ${groupClass}">' +
81
- '<button style="min-width: ${buttonsWidth}" class="btn btn-decrement ${buttonsClass} btn-minus" type="button">${decrementButton}</button>' +
82
- '<input type="text" inputmode="decimal" style="text-align: ${textAlign}" class="form-control form-control-text-input"/>' +
83
- '<button style="min-width: ${buttonsWidth}" class="btn btn-increment ${buttonsClass} btn-plus" type="button">${incrementButton}</button>' +
84
- '</div>'
85
- }
86
-
87
- for (let option in methodOrProps) {
88
- // noinspection JSUnfilteredForInLoop
89
- props[option] = methodOrProps[option]
90
- }
91
-
92
- const html = props.template
93
- .replace(/\${groupClass}/g, props.groupClass)
94
- .replace(/\${buttonsWidth}/g, props.buttonsWidth)
95
- .replace(/\${buttonsClass}/g, props.buttonsClass)
96
- .replace(/\${decrementButton}/g, props.decrementButton)
97
- .replace(/\${incrementButton}/g, props.incrementButton)
98
- .replace(/\${textAlign}/g, props.textAlign)
99
-
100
- this.each(function () {
101
-
102
- if (this["bootstrap-input-spinner"]) {
103
- console.warn("element", this, "is already a bootstrap-input-spinner")
104
- } else {
105
-
106
- var $original = $(this)
107
- $original[0]["bootstrap-input-spinner"] = true
108
- $original.hide()
109
- $original[0].inputSpinnerEditor = new props.editor(props, this)
110
-
111
- var autoDelayHandler = null
112
- var autoIntervalHandler = null
113
-
114
- var $inputGroup = $(html)
115
- var $buttonDecrement = $inputGroup.find(".btn-decrement")
116
- var $buttonIncrement = $inputGroup.find(".btn-increment")
117
- var $input = $inputGroup.find("input")
118
- var $label = $("label[for='" + $original.attr("id") + "']")
119
- if (!$label[0]) {
120
- $label = $original.closest("label")
121
- }
122
-
123
- var min = null
124
- var max = null
125
- var step = null
126
-
127
- updateAttributes()
128
-
129
- var value = parseFloat($original[0].value)
130
- let pointerState = false
131
-
132
- const prefix = $original.attr("data-prefix") || ""
133
- const suffix = $original.attr("data-suffix") || ""
134
-
135
- if (prefix) {
136
- const prefixElement = $('<span class="input-group-text">' + prefix + '</span>')
137
- $inputGroup.find("input").before(prefixElement)
138
- }
139
- if (suffix) {
140
- const suffixElement = $('<span class="input-group-text">' + suffix + '</span>')
141
- $inputGroup.find("input").after(suffixElement)
142
- }
143
-
144
- $original[0].setValue = function (newValue) {
145
- setValue(newValue)
146
- }
147
- $original[0].destroyInputSpinner = function () {
148
- destroy()
149
- }
150
-
151
- var observer = new MutationObserver(function () {
152
- updateAttributes()
153
- setValue(value, true)
154
- })
155
- observer.observe($original[0], {attributes: true})
156
-
157
- $original.after($inputGroup)
158
-
159
- setValue(value)
160
-
161
- $input.on("paste input change focusout", function (event) {
162
- let newValue = $input[0].value
163
- const focusOut = event.type === "focusout"
164
- newValue = $original[0].inputSpinnerEditor.parse(newValue)
165
- setValue(newValue, focusOut)
166
- dispatchEvent($original, event.type)
167
- if (props.keyboardStepping && focusOut) { // stop stepping
168
- resetTimer()
169
- }
170
- }).on("keydown", function (event) {
171
- if (props.keyboardStepping) {
172
- if (event.which === 38) { // up arrow pressed
173
- event.preventDefault()
174
- if (!$buttonDecrement.prop("disabled")) {
175
- stepHandling(step)
176
- }
177
- } else if (event.which === 40) { // down arrow pressed
178
- event.preventDefault()
179
- if (!$buttonIncrement.prop("disabled")) {
180
- stepHandling(-step)
181
- }
182
- }
183
- }
184
- }).on("keyup", function (event) {
185
- // up/down arrow released
186
- if (props.keyboardStepping && (event.which === 38 || event.which === 40)) {
187
- event.preventDefault()
188
- resetTimer()
189
- }
190
- })
191
-
192
- // decrement button
193
- onPointerDown($buttonDecrement[0], function () {
194
- if (!$buttonDecrement.prop("disabled")) {
195
- pointerState = true
196
- stepHandling(-step)
197
- }
198
- })
199
- // increment button
200
- onPointerDown($buttonIncrement[0], function () {
201
- if (!$buttonIncrement.prop("disabled")) {
202
- pointerState = true
203
- stepHandling(step)
204
- }
205
- })
206
- onPointerUp(document.body, function () {
207
- if(pointerState === true) {
208
- resetTimer()
209
- dispatchEvent($original, "change")
210
- pointerState = false
211
- }
212
- })
213
- }
214
-
215
- function setValue(newValue, updateInput) {
216
- if (updateInput === undefined) {
217
- updateInput = true
218
- }
219
- if (isNaN(newValue) || newValue === "") {
220
- $original[0].value = ""
221
- if (updateInput) {
222
- $input[0].value = ""
223
- }
224
- value = NaN
225
- } else {
226
- newValue = parseFloat(newValue)
227
- newValue = Math.min(Math.max(newValue, min), max)
228
- $original[0].value = newValue
229
- if (updateInput) {
230
- $input[0].value = $original[0].inputSpinnerEditor.render(newValue)
231
- }
232
- value = newValue
233
- }
234
- }
235
-
236
- function destroy() {
237
- $original.prop("required", $input.prop("required"))
238
- observer.disconnect()
239
- resetTimer()
240
- $input.off("paste input change focusout")
241
- $inputGroup.remove()
242
- $original.show()
243
- $original[0]["bootstrap-input-spinner"] = undefined
244
- if ($label[0]) {
245
- $label.attr("for", $original.attr("id"))
246
- }
247
- }
248
-
249
- function dispatchEvent($element, type) {
250
- if (type) {
251
- setTimeout(function () {
252
- let event
253
- if (typeof (Event) === 'function') {
254
- event = new Event(type, {bubbles: true})
255
- } else { // IE
256
- event = document.createEvent('Event')
257
- event.initEvent(type, true, true)
258
- }
259
- $element[0].dispatchEvent(event)
260
- })
261
- }
262
- }
263
-
264
- function stepHandling(step) {
265
- calcStep(step)
266
- resetTimer()
267
- if(props.autoInterval !== undefined) {
268
- autoDelayHandler = setTimeout(function () {
269
- autoIntervalHandler = setInterval(function () {
270
- calcStep(step)
271
- }, props.autoInterval)
272
- }, props.autoDelay)
273
- }
274
- }
275
-
276
- function calcStep(step) {
277
- if (isNaN(value)) {
278
- value = 0
279
- }
280
- setValue(Math.round(value / step) * step + step)
281
- dispatchEvent($original, "input")
282
- }
283
-
284
- function resetTimer() {
285
- clearTimeout(autoDelayHandler)
286
- clearTimeout(autoIntervalHandler)
287
- }
288
-
289
- function updateAttributes() {
290
- // copy properties from original to the new input
291
- if ($original.prop("required")) {
292
- $input.prop("required", $original.prop("required"))
293
- $original.removeAttr('required')
294
- }
295
- $input.prop("placeholder", $original.prop("placeholder"))
296
- $input.attr("inputmode", $original.attr("inputmode") || "decimal")
297
- const disabled = $original.prop("disabled")
298
- const readonly = $original.prop("readonly")
299
- $input.prop("disabled", disabled)
300
- $input.prop("readonly", readonly || props.buttonsOnly)
301
- $buttonIncrement.prop("disabled", disabled || readonly)
302
- $buttonDecrement.prop("disabled", disabled || readonly)
303
- if (disabled || readonly) {
304
- resetTimer()
305
- }
306
- const originalClass = $original.prop("class")
307
- let groupClass = ""
308
- // sizing
309
- if (/form-control-sm/g.test(originalClass)) {
310
- groupClass = "input-group-sm"
311
- } else if (/form-control-lg/g.test(originalClass)) {
312
- groupClass = "input-group-lg"
313
- }
314
- const inputClass = originalClass.replace(/form-control(-(sm|lg))?/g, "")
315
- $inputGroup.prop("class", "input-group " + groupClass + " " + props.groupClass)
316
- $input.prop("class", "form-control " + inputClass)
317
-
318
- // update the main attributes
319
- min = isNaN($original.prop("min")) || $original.prop("min") === "" ? -Infinity : parseFloat($original.prop("min"))
320
- max = isNaN($original.prop("max")) || $original.prop("max") === "" ? Infinity : parseFloat($original.prop("max"))
321
- step = parseFloat($original.prop("step")) || 1
322
- if ($original.attr("hidden")) {
323
- $inputGroup.attr("hidden", $original.attr("hidden"))
324
- } else {
325
- $inputGroup.removeAttr("hidden")
326
- }
327
- if ($original.attr("id")) {
328
- $input.attr("id", $original.attr("id") + ":input_spinner") // give the spinner a unique id...
329
- if ($label[0]) {
330
- $label.attr("for", $input.attr("id")) // ...to rewire the label
331
- }
332
- }
333
- }
334
- })
335
-
336
- return this
337
- }
338
-
339
- function onPointerUp(element, callback) {
340
- element.addEventListener("mouseup", function (e) {
341
- callback(e)
342
- })
343
- element.addEventListener("touchend", function (e) {
344
- callback(e)
345
- })
346
- element.addEventListener("keyup", function (e) {
347
- if ((e.keyCode === 32 || e.keyCode === 13)) {
348
- triggerKeyPressed = false
349
- callback(e)
350
- }
351
- })
352
- }
353
-
354
- function onPointerDown(element, callback) {
355
- element.addEventListener("mousedown", function (e) {
356
- if (e.button === 0) {
357
- e.preventDefault()
358
- callback(e)
359
- }
360
- })
361
- element.addEventListener("touchstart", function (e) {
362
- if (e.cancelable) {
363
- e.preventDefault()
364
- }
365
- callback(e)
366
- }, {passive: false})
367
- element.addEventListener("keydown", function (e) {
368
- if ((e.keyCode === 32 || e.keyCode === 13) && !triggerKeyPressed) {
369
- triggerKeyPressed = true
370
- callback(e)
371
- }
372
- })
373
- }
374
-
375
- }(jQuery))
@@ -1,51 +0,0 @@
1
- /**
2
- * Author and copyright: Stefan Haack (https://shaack.com)
3
- * Repository: https://github.com/shaack/bootstrap-input-spinner
4
- * License: MIT, see file 'LICENSE'
5
- */
6
- const customEditors = {
7
- RawEditor: function (props, element) {
8
- this.parse = function (customFormat) {
9
- // parse nothing
10
- return customFormat
11
- }
12
- this.render = function (number) {
13
- // render raw
14
- return number
15
- }
16
- },
17
- TimeEditor: function (props, element) {
18
- // could be implemented more elegant maybe, but works
19
- this.parse = function (customFormat) {
20
- let trimmed = customFormat.trim()
21
- let sign = 1
22
- if (trimmed.charAt(0) === "-") {
23
- sign = -1
24
- trimmed = trimmed.replace("-", "")
25
- }
26
- const parts = trimmed.split(":")
27
- let hours = 0, minutes
28
- if (parts[1]) {
29
- hours = parseInt(parts[0], 10)
30
- minutes = parseInt(parts[1], 10)
31
- } else {
32
- minutes = parseInt(parts[0], 10)
33
- }
34
- return (hours * 60 + minutes) * sign
35
- }
36
- this.render = function (number) {
37
- let minutes = Math.abs(number % 60)
38
- if (minutes < 10) {
39
- minutes = "0" + minutes
40
- }
41
- let hours
42
- if (number >= 0) {
43
- hours = Math.floor(number / 60)
44
- return hours + ":" + minutes
45
- } else {
46
- hours = Math.ceil(number / 60)
47
- return "-" + Math.abs(hours) + ":" + minutes
48
- }
49
- }
50
- }
51
- }
@@ -1,17 +0,0 @@
1
- import {describe, it, assert} from "../node_modules/teevi/src/teevi.js"
2
-
3
- describe('bootstrap-input-spinner', function () {
4
- it('Should display and destroy the spinner', function () {
5
- addInput()
6
- const $input = $("input[type='number']")
7
- $input.inputSpinner()
8
- $input.inputSpinner("destroy")
9
- })
10
- })
11
-
12
- function addInput() {
13
- var testContainer = document.getElementById("testContainer")
14
- var input = document.createElement("input")
15
- input.type = "number"
16
- testContainer.append(input)
17
- }
@@ -1,19 +0,0 @@
1
- <!doctype html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="UTF-8">
5
- <meta http-equiv="X-UA-Compatible" content="ie=edge">
6
- <title>bootstrap-input-spinner test</title>
7
- </head>
8
- <body>
9
- <div id="testContainer"></div>
10
- <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
11
- <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-/bQdsTh/da6pkI1MST/rWKFNjaCP5gBSY4sEBT38Q/9RBh9AH40zEOg7Hlq2THRZ" crossorigin="anonymous"></script>
12
- <script src="../src/bootstrap-input-spinner.js"></script>
13
- <script type="module">
14
- import {teevi} from "../../node_modules/teevi/src/teevi.js"
15
- import "./Test.js"
16
- teevi.run()
17
- </script>
18
- </body>
19
- </html>
@@ -1,55 +0,0 @@
1
- <!doctype html>
2
- <html lang="en">
3
- <head>
4
- <title>input type="number"</title>
5
- <link rel="stylesheet" href="../../node_modules/bootstrap/dist/css/bootstrap.min.css"/>
6
- <link rel="stylesheet" href="test.css"/>
7
- <meta name="viewport" content="width=device-width, initial-scale=1"/>
8
- <body>
9
- <h1>This is for testing the behaviour of the native number input, compared to the bootstrap-input-spinner.</h1>
10
- First: Native Element, second: bootstrap-input-spinner.
11
-
12
- <h3>No attributes</h3>
13
- <p>
14
- <input type="number"/>
15
- <input type="number" class="bootstrap-input-spinner"/>
16
- </p>
17
-
18
-
19
- <h3>step 1, value 1.01</h3>
20
- <p>
21
- <input type="number" step="1" value="1.01"/>
22
- <input type="number" class="bootstrap-input-spinner" step="1" value="1.01"/>
23
- </p>
24
-
25
- <h3>step 2, value 6, min 5.01</h3>
26
- <p>
27
- <input type="number" step="2" min="5.01" value="6"/>
28
- <input type="number" class="bootstrap-input-spinner" step="2" min="5.01" value="6"/>
29
- </p>
30
-
31
- <h3>step 2, min 0, max 9.5</h3>
32
- <p>
33
- <input type="number" step="2" min="0" max="9.5"/>
34
- <input type="number" class="bootstrap-input-spinner" step="2" min="0" max="9.5"/>
35
- </p>
36
-
37
- <h3>step 10, min 1, max 49</h3>
38
- <p>
39
- <input type="number" step="10" min="1" max="49"/>
40
- <input type="number" class="bootstrap-input-spinner" step="10" min="1" max="49"/>
41
- </p>
42
- <h3>step 20, min -99, max 100</h3>
43
- <p>
44
- <input type="number" step="20" min="-99" max="100"/>
45
- <input type="number" class="bootstrap-input-spinner" step="20" min="-99" max="100"/>
46
- </p>
47
-
48
- <script src="../../node_modules/jquery/dist/jquery.min.js"></script>
49
- <script src="../../node_modules/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
50
- <script src="../../src/bootstrap-input-spinner.js"></script>
51
- <script>
52
- $(".bootstrap-input-spinner").inputSpinner()
53
- </script>
54
- </body>
55
- </html>
@@ -1,43 +0,0 @@
1
- <!doctype html>
2
- <html lang="en">
3
- <head>
4
- <title>Test bug #98</title>
5
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
6
- <link rel="stylesheet" href="test.css"/>
7
- <meta name="viewport" content="width=device-width, initial-scale=1"/>
8
- </head>
9
- <body>
10
- <h1>Test page for fixing <a href="https://github.com/shaack/bootstrap-input-spinner/issues/98">issue #98</a></h1>
11
- <div style="max-width: 520px">
12
- <div class="form-row">
13
- <label class="col-4"><b>Change this:</b></label>
14
- <div class="col-8 mb-1">
15
- <input type="number" value="0" id="source_b" class="form-control"/>
16
- </div>
17
- </div>
18
- <div class="box_b">
19
- <div class="form-row">
20
- <label class="col-4">Mirror 1:</label>
21
- <div class="col-8 mb-1">
22
- <input type="number" value="0" id="mirror_1b" class="form-control"/>
23
- </div>
24
- </div>
25
- <div class="form-row">
26
- <label class="col-4">Mirror 2:</label>
27
- <div class="col-8">
28
- <input type="number" value="0" id="mirror_2b" class="form-control"/>
29
- </div>
30
- </div>
31
- </div>
32
- </div>
33
- <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
34
- <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-/bQdsTh/da6pkI1MST/rWKFNjaCP5gBSY4sEBT38Q/9RBh9AH40zEOg7Hlq2THRZ" crossorigin="anonymous"></script>
35
- <script src="../../src/bootstrap-input-spinner.js"></script>
36
- <script>
37
- $('input').inputSpinner()
38
- $(document).on('change', 'input#source_b', function (e) {
39
- $('.box_b input').val($(e.target).val())
40
- })
41
- </script>
42
- </body>
43
- </html>
@@ -1,20 +0,0 @@
1
- body {
2
- padding: 2rem;
3
- }
4
-
5
- h1 {
6
- margin-bottom: 2rem;
7
- }
8
-
9
- h3 {
10
- margin-top: 2rem;
11
- }
12
-
13
- .input-group, input.test-value-input {
14
- max-width: 250px;
15
- }
16
-
17
- p input:first-child {
18
- margin-bottom: 0.5rem;
19
- width: 250px;
20
- }