|
1 | | -import options from './options'; |
2 | | -import { NumberFormat, setCursor } from './utils' |
| 1 | +import * as core from './core' |
3 | 2 |
|
4 | | -export default function (el, binding) { |
5 | | - const { value } = binding |
6 | | - if (!value) return false |
7 | | - const config = Object.assign(options, value) |
8 | | - // console.log('src/components/directive:init()', config) |
| 3 | +const CONFIG_KEY = core.CONFIG_KEY |
9 | 4 |
|
10 | | - // v-number used on a component that's not a input element |
11 | | - if (el.localName !== 'input') { |
12 | | - const input = el.getElementsByTagName('input') |
13 | | - if (input.length < 1) { |
14 | | - throw new Error(`v-number requires 1 input element, found ${input.length}`) |
15 | | - } else { |
16 | | - el = input[0] |
17 | | - } |
18 | | - } |
| 5 | +export default { |
| 6 | + bind: (el, { value }, vnode) => { |
| 7 | + el = core.getInputElement(el) |
| 8 | + const config = core.normalizeConfig(value) |
| 9 | + el[CONFIG_KEY] = { config } |
| 10 | + // set initial value |
| 11 | + core.updateValue(el, vnode, { force: config.prefill }) |
| 12 | + }, |
19 | 13 |
|
20 | | - // change the input type to {text} because we can't use it to any other input types |
21 | | - el.setAttribute('type', 'text') |
| 14 | + inserted: (el) => { |
| 15 | + el = core.getInputElement(el) |
| 16 | + const config = el[CONFIG_KEY] |
| 17 | + // prefer adding event listener to parent element to avoid Firefox bug which does not |
| 18 | + // execute `useCapture: true` event handlers before non-capturing event handlers |
| 19 | + const handlerOwner = el.parentElement || el |
22 | 20 |
|
23 | | - el.oninput = () => { |
24 | | - // console.log('src/directive.js:oninput()', evt) |
25 | | - var positionFromEnd = el.value.length - el.selectionEnd |
26 | | - el.value = new NumberFormat(config).format(el.value) |
27 | | - positionFromEnd = Math.max(positionFromEnd, config.suffix.length) |
28 | | - positionFromEnd = el.value.length - positionFromEnd |
29 | | - positionFromEnd = Math.max(positionFromEnd, config.prefix.length + 1) |
30 | | - setCursor(el, positionFromEnd) |
31 | | - } |
| 21 | + // use anonymous event handler to avoid inadvertently removing masking for all inputs within a container |
| 22 | + const handler = (e) => core.inputHandler(e) |
32 | 23 |
|
33 | | - el.onblur = () => { |
34 | | - // clean up after end the input |
35 | | - // console.log('src/directive.js:onblur()') |
36 | | - el.value = new NumberFormat(config).clean().format(el.value) |
37 | | - el.dispatchEvent(new Event('change')) |
38 | | - } |
| 24 | + handlerOwner.addEventListener('input', handler, true) |
39 | 25 |
|
40 | | - el.onfocus = () => { |
41 | | - // console.log('src/directive.js:onfocus()') |
42 | | - setCursor(el, el.value.length - config.suffix.length) |
43 | | - } |
| 26 | + config.cleanup = () => handlerOwner.removeEventListener('input', handler, true) |
| 27 | + }, |
44 | 28 |
|
45 | | - el.onkeydown = (evt) => { |
46 | | - // console.log('src/directive.js:onkeydown()') |
47 | | - // Check deciaml |
48 | | - if (evt.key === config.decimal && evt.target.value.includes(config.decimal)) { |
49 | | - evt.preventDefault() |
50 | | - } |
51 | | - // Allow these keys only |
52 | | - if ( |
53 | | - // backspace, delete, tab, escape, enter |
54 | | - [46, 8, 9, 27, 13].indexOf(evt.keyCode) >= 0 |
55 | | - // Ctrl/cmd+A |
56 | | - || (evt.keyCode === 65 && (evt.ctrlKey || evt.metaKey)) |
57 | | - // Ctrl/cmd+C |
58 | | - || (evt.keyCode === 67 && (evt.ctrlKey || evt.metaKey)) |
59 | | - // Ctrl/cmd+V |
60 | | - || (evt.keyCode === 86 && (evt.ctrlKey || evt.metaKey)) |
61 | | - // Ctrl/cmd+R |
62 | | - || (evt.keyCode === 82 && (evt.ctrlKey || evt.metaKey)) |
63 | | - // Ctrl/cmd+X |
64 | | - || (evt.keyCode === 88 && (evt.ctrlKey || evt.metaKey)) |
65 | | - // home, end, left, right |
66 | | - || (evt.keyCode >= 35 && evt.keyCode <= 39) |
67 | | - || (evt.keyCode >= 48 && evt.keyCode <= 57) |
68 | | - || evt.keyCode === 109 |
69 | | - || evt.key === config.decimal |
70 | | - ) { |
71 | | - return true |
| 29 | + update: (el, { value, oldValue }, vnode) => { |
| 30 | + el = core.getInputElement(el) |
| 31 | + |
| 32 | + if (value !== oldValue) { |
| 33 | + el[CONFIG_KEY].config = core.normalizeConfig(value) |
| 34 | + core.updateValue(el, vnode, { force: true }) |
| 35 | + } else { |
| 36 | + core.updateValue(el, vnode) |
72 | 37 | } |
73 | | - evt.preventDefault() |
74 | | - } |
| 38 | + }, |
75 | 39 |
|
76 | | - // force format after initialization |
77 | | - el.oninput() |
78 | | - el.dispatchEvent(new Event('input')) |
| 40 | + unbind: (el) => { |
| 41 | + core.getInputElement(el)[CONFIG_KEY].cleanup() |
| 42 | + } |
79 | 43 | } |
0 commit comments