2017-04-28 13:04:29 +00:00
|
|
|
import {module} from '../module';
|
2017-09-12 09:44:56 +00:00
|
|
|
|
|
|
|
// import * as resolveFactory from '../lib/resolveDefaultComponents';
|
|
|
|
// import * as normalizerFactory from '../lib/inputAttrsNormalizer';
|
2017-04-28 13:04:29 +00:00
|
|
|
import './style.scss';
|
2017-09-12 09:44:56 +00:00
|
|
|
// import './textfield.mdl';
|
2017-04-28 13:04:29 +00:00
|
|
|
|
2017-09-12 09:44:56 +00:00
|
|
|
export default class TextfieldController {
|
2017-04-28 13:04:29 +00:00
|
|
|
constructor($element, $scope, $attrs) {
|
|
|
|
|
2017-09-12 09:44:56 +00:00
|
|
|
this.$scope = $scope;
|
|
|
|
this.$attrs = $attrs;
|
|
|
|
let input = this.input = this.$element.querySelector('input');
|
2017-04-28 13:04:29 +00:00
|
|
|
input.addEventListener('input',
|
|
|
|
() => this.checkValue());
|
|
|
|
input.addEventListener('focus',
|
|
|
|
() => this.checkValue());
|
|
|
|
input.addEventListener('blur',
|
|
|
|
() => this.showClear(false));
|
|
|
|
|
2017-09-12 09:44:56 +00:00
|
|
|
let clearButton = this.$element.querySelector('button');
|
2017-04-28 13:04:29 +00:00
|
|
|
clearButton.addEventListener('click',
|
|
|
|
() => this.onClearClick());
|
|
|
|
clearButton.addEventListener('mousedown',
|
|
|
|
event => event.preventDefault());
|
|
|
|
|
2017-09-12 09:44:56 +00:00
|
|
|
let div = this.$element.firstChild;
|
2017-04-28 13:04:29 +00:00
|
|
|
componentHandler.upgradeElement(div);
|
2017-09-07 17:35:17 +00:00
|
|
|
|
2017-09-12 09:44:56 +00:00
|
|
|
Object.assign(this, $attrs);
|
2017-04-28 13:04:29 +00:00
|
|
|
}
|
2017-09-12 09:44:56 +00:00
|
|
|
|
|
|
|
$onInit() {
|
|
|
|
let mdlTextField = this.$element.firstChild.MaterialTextfield;
|
2017-05-05 10:54:47 +00:00
|
|
|
mdlTextField.updateClasses_();
|
|
|
|
|
2017-09-12 09:44:56 +00:00
|
|
|
this.$scope.$watch(this.$attrs.model,
|
2017-04-28 13:04:29 +00:00
|
|
|
() => mdlTextField.updateClasses_());
|
2017-09-12 09:44:56 +00:00
|
|
|
|
|
|
|
let $input = angular.$element(this.input);
|
2017-09-07 17:35:17 +00:00
|
|
|
$input.controller('ng-model').$parsers.push(value => value === '' ? null : value);
|
2017-04-28 13:04:29 +00:00
|
|
|
}
|
|
|
|
onClearClick() {
|
2017-09-07 17:35:17 +00:00
|
|
|
this.input.value = null;
|
2017-04-28 13:04:29 +00:00
|
|
|
this.checkValue();
|
|
|
|
|
|
|
|
let event = this.document.createEvent('HTMLEvents');
|
|
|
|
event.initEvent('change', false, true);
|
|
|
|
this.input.dispatchEvent(event);
|
|
|
|
}
|
|
|
|
checkValue() {
|
|
|
|
this.showClear(this.input.value);
|
|
|
|
}
|
|
|
|
showClear(show) {
|
2017-05-26 11:14:47 +00:00
|
|
|
show = show && document.activeElement === this.input;
|
2017-09-12 09:44:56 +00:00
|
|
|
let clearButton = this.$element.querySelector('button');
|
2017-04-28 13:04:29 +00:00
|
|
|
clearButton.style.visibility = show ? 'visible' : 'hidden';
|
|
|
|
}
|
2017-05-18 15:35:07 +00:00
|
|
|
/**
|
|
|
|
* Selects the textfield.
|
|
|
|
*/
|
|
|
|
select() {
|
|
|
|
this.input.select();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Puts the focus on the textfield.
|
|
|
|
*/
|
|
|
|
focus() {
|
|
|
|
this.input.focus();
|
|
|
|
}
|
2017-04-28 13:04:29 +00:00
|
|
|
}
|
2017-09-12 09:44:56 +00:00
|
|
|
TextfieldController.$inject = ['$element', '$scope', '$attrs'];
|
2017-04-28 13:04:29 +00:00
|
|
|
|
2017-09-12 09:44:56 +00:00
|
|
|
module.component('vnTextfield', {
|
|
|
|
template: require('./textfield.html'),
|
|
|
|
controller: TextfieldController
|
|
|
|
});
|