2018-09-25 16:28:26 +00:00
|
|
|
import Component from '@ember/component';
|
|
|
|
import { computed } from '@ember/object';
|
2018-06-14 18:52:00 +00:00
|
|
|
import autosize from 'autosize';
|
2019-06-21 21:05:45 +00:00
|
|
|
import layout from '../templates/components/masked-input';
|
2018-06-14 18:52:00 +00:00
|
|
|
|
2019-04-05 21:58:55 +00:00
|
|
|
/**
|
|
|
|
* @module MaskedInput
|
|
|
|
* `MaskedInput` components are textarea inputs where the input is hidden. They are used to enter sensitive information like passwords.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* <MaskedInput
|
|
|
|
* @value={{attr.options.defaultValue}}
|
|
|
|
* @placeholder="secret"
|
|
|
|
* @allowCopy={{true}}
|
2019-04-15 22:53:44 +00:00
|
|
|
* @onChange={{action "someAction"}}
|
2019-04-05 21:58:55 +00:00
|
|
|
* />
|
|
|
|
*
|
|
|
|
* @param [value] {String} - The value to display in the input.
|
|
|
|
* @param [placeholder=value] {String} - The placeholder to display before the user has entered any input.
|
|
|
|
* @param [allowCopy=null] {bool} - Whether or not the input should render with a copy button.
|
|
|
|
* @param [displayOnly=false] {bool} - Whether or not to display the value as a display only `pre` element or as an input.
|
2019-04-15 22:53:44 +00:00
|
|
|
* @param [onChange=Function.prototype] {Function|action} - A function to call when the value of the input changes.
|
|
|
|
*
|
2019-04-05 21:58:55 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
export default Component.extend({
|
2019-06-21 21:05:45 +00:00
|
|
|
layout,
|
2018-09-25 16:28:26 +00:00
|
|
|
value: null,
|
2018-12-07 21:23:42 +00:00
|
|
|
placeholder: 'value',
|
2018-09-25 16:28:26 +00:00
|
|
|
didInsertElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
autosize(this.element.querySelector('textarea'));
|
|
|
|
},
|
|
|
|
didUpdate() {
|
|
|
|
this._super(...arguments);
|
|
|
|
autosize.update(this.element.querySelector('textarea'));
|
|
|
|
},
|
|
|
|
willDestroyElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
autosize.destroy(this.element.querySelector('textarea'));
|
|
|
|
},
|
|
|
|
shouldObscure: computed('isMasked', 'isFocused', 'value', function() {
|
|
|
|
if (this.get('value') === '') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (this.get('isFocused') === true) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return this.get('isMasked');
|
|
|
|
}),
|
|
|
|
displayValue: computed('shouldObscure', function() {
|
|
|
|
if (this.get('shouldObscure')) {
|
|
|
|
return '■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■';
|
|
|
|
} else {
|
|
|
|
return this.get('value');
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
isMasked: true,
|
|
|
|
isFocused: false,
|
|
|
|
displayOnly: false,
|
|
|
|
onKeyDown() {},
|
|
|
|
onChange() {},
|
|
|
|
actions: {
|
|
|
|
toggleMask() {
|
|
|
|
this.toggleProperty('isMasked');
|
|
|
|
},
|
|
|
|
updateValue(e) {
|
2019-04-15 22:53:44 +00:00
|
|
|
let value = e.target.value;
|
|
|
|
this.set('value', value);
|
|
|
|
this.onChange(value);
|
2018-09-25 16:28:26 +00:00
|
|
|
},
|
|
|
|
},
|
2018-06-14 18:52:00 +00:00
|
|
|
});
|