|
| 1 | +/* global Inputmask */ |
| 2 | +import { OneWayInput } from 'ember-one-way-controls'; |
| 3 | +import { computed, get, set } from '@ember/object'; |
| 4 | +import { schedule } from '@ember/runloop'; |
| 5 | + |
| 6 | +const DEFAULT_OPTIONS = { |
| 7 | + rightAlign: false, |
| 8 | +}; |
| 9 | + |
| 10 | +/** |
| 11 | + * Displays an input with the specified mask applied to it |
| 12 | + * using Inputmask library. Follows Data-down actions up pattern |
| 13 | + * |
| 14 | + * @param {string} value The unmasked value to display in the input |
| 15 | + * @param {action} update The function to perform when the value changes. Will be passed the |
| 16 | + * unmasked value and the masked values |
| 17 | + * @param {string} mask The mask to use on the input |
| 18 | + * @param {object} options The options to pass into the Inputmask library |
| 19 | + */ |
| 20 | +export default OneWayInput.extend({ |
| 21 | + /** |
| 22 | + * Set the `_value` to be whatever the `element.value` is |
| 23 | + */ |
| 24 | + attributeBindings: [ |
| 25 | + 'type', |
| 26 | + '_value:value' |
| 27 | + ], |
| 28 | + |
| 29 | + // In ember-one-way-controls all attributes are bound dynamically via a mixin, except for |
| 30 | + // the ones specified in this property. We need to include 'mask', and 'options' to the list |
| 31 | + NON_ATTRIBUTE_BOUND_PROPS: [ |
| 32 | + 'keyEvents', |
| 33 | + 'classNames', |
| 34 | + 'positionalParamValue', |
| 35 | + 'update', |
| 36 | + 'mask', |
| 37 | + 'options', |
| 38 | + ], |
| 39 | + |
| 40 | + /** |
| 41 | + * mask - Pass in the `mask` string to set it on the element |
| 42 | + * |
| 43 | + * @public |
| 44 | + */ |
| 45 | + mask: '', |
| 46 | + |
| 47 | + /** |
| 48 | + * options - Options accepted by the Inputmask library |
| 49 | + */ |
| 50 | + options: null, |
| 51 | + |
| 52 | + /** |
| 53 | + * Setup _value to be a positional param or the passed param if that is not defined |
| 54 | + * |
| 55 | + * @private |
| 56 | + */ |
| 57 | + _value: computed('positionalParamValue', 'value', { |
| 58 | + get() { |
| 59 | + let value = get(this, 'positionalParamValue'); |
| 60 | + if (value === undefined) { |
| 61 | + value = get(this, 'value'); |
| 62 | + } |
| 63 | + |
| 64 | + return value; |
| 65 | + } |
| 66 | + }), |
| 67 | + |
| 68 | + init() { |
| 69 | + this._super(...arguments); |
| 70 | + |
| 71 | + // Give the mask some default options that can be overridden |
| 72 | + let options = get(this, 'options'); |
| 73 | + set(this, 'options', Object.assign({}, DEFAULT_OPTIONS, options)); |
| 74 | + }, |
| 75 | + |
| 76 | + /** |
| 77 | + * update - This action will be called when the value changes and will be passed the unmasked value |
| 78 | + * and the masked value |
| 79 | + * |
| 80 | + * @public |
| 81 | + */ |
| 82 | + update() {}, |
| 83 | + |
| 84 | + didInsertElement() { |
| 85 | + this._setupMask(); |
| 86 | + }, |
| 87 | + |
| 88 | + willDestroyElement() { |
| 89 | + this._destroyMask(); |
| 90 | + this.element.removeEventListener('input', this._changeEventListener); |
| 91 | + }, |
| 92 | + |
| 93 | + /** |
| 94 | + * Disabling this so we don't have conflicts with manual addEventListener in case something |
| 95 | + * changes one day |
| 96 | + * |
| 97 | + * @override |
| 98 | + */ |
| 99 | + change(){}, |
| 100 | + |
| 101 | + /** |
| 102 | + * Disabling thi so we don't have conflicts with manual addEventListener in case something |
| 103 | + * changes one day |
| 104 | + * |
| 105 | + * @override |
| 106 | + */ |
| 107 | + input(){}, |
| 108 | + |
| 109 | + /** |
| 110 | + * _changeEventListener - A place to store the event listener we setup to listen to the 'input' |
| 111 | + * events, because the Inputmask library events don't play nice with the Ember components event |
| 112 | + * |
| 113 | + * @private |
| 114 | + */ |
| 115 | + _changeEventListener() {}, |
| 116 | + |
| 117 | + /** |
| 118 | + * _processNewValue - Handle when a new value changes |
| 119 | + * |
| 120 | + * @private |
| 121 | + * @param {string} value - The masked value visible in the element |
| 122 | + */ |
| 123 | + _processNewValue(value) { |
| 124 | + let cursorStart = this.element.selectionStart; |
| 125 | + let cursorEnd = this.element.selectionEnd; |
| 126 | + let unmaskedValue = this._getUnmaskedValue(); |
| 127 | + let oldUnmaskedValue = get(this, '_value'); |
| 128 | + let options = get(this, 'options'); |
| 129 | + |
| 130 | + // We only want to make changes if something is different so we don't cause infinite loops or |
| 131 | + // double renders. |
| 132 | + // We want to make sure that that values we compare are going to come out the same through |
| 133 | + // the masking algorithm, to ensure that we only call `update` if the values are actually different |
| 134 | + // (e.g. '1234.' will be masked as '1234' and so when `update` is called and passed back |
| 135 | + // into the component the decimal will be removed, we don't want this) |
| 136 | + if (Inputmask.format(String(oldUnmaskedValue), options) !== Inputmask.format(unmaskedValue, options)) { |
| 137 | + get(this, 'update')(unmaskedValue, value); |
| 138 | + |
| 139 | + // When the value is updated, and then sent back down the cursor moves to the end of the field. |
| 140 | + // We therefore need to put it back to where the user was typing so they don't get janked around |
| 141 | + schedule('afterRender', () => { |
| 142 | + this.element.setSelectionRange(cursorStart, cursorEnd); |
| 143 | + }); |
| 144 | + } |
| 145 | + }, |
| 146 | + |
| 147 | + /** |
| 148 | + * _setupMask - Connect the 3rd party input masking library to the element |
| 149 | + * |
| 150 | + * @private |
| 151 | + */ |
| 152 | + _setupMask() { |
| 153 | + let mask = get(this, 'mask'), options = get(this, 'options'); |
| 154 | + let inputmask = new Inputmask(mask, options); |
| 155 | + inputmask.mask(this.element); |
| 156 | + |
| 157 | + // We need to setup a manual event listener for the change event instead of using the Ember |
| 158 | + // Component event methods, because the Inputmask events don't play nice with the Component |
| 159 | + // ones. Similar issue happens in React.js as well |
| 160 | + // https://github.com/RobinHerbots/Inputmask/issues/1377 |
| 161 | + let eventListener = event => this._processNewValue(event.target.value); |
| 162 | + set(this, '_changeEventListener', eventListener); |
| 163 | + this.element.addEventListener('input', eventListener); |
| 164 | + }, |
| 165 | + |
| 166 | + /** |
| 167 | + * _getUnmaskedValue - Get the value of the element without the mask |
| 168 | + * |
| 169 | + * @private |
| 170 | + * @return {string} The unmasked value |
| 171 | + */ |
| 172 | + _getUnmaskedValue() { |
| 173 | + return this.element.inputmask.unmaskedvalue(); |
| 174 | + }, |
| 175 | + |
| 176 | + _destroyMask() { |
| 177 | + this.element.inputmask.remove(); |
| 178 | + }, |
| 179 | +}); |
0 commit comments