2017-04-28 13:04:29 +00:00
|
|
|
import {module} from '../module';
|
|
|
|
import Component from '../lib/component';
|
|
|
|
import * as resolveFactory from '../lib/resolveDefaultComponents';
|
|
|
|
import * as normalizerFactory from '../lib/inputAttrsNormalizer';
|
|
|
|
import './style.scss';
|
|
|
|
import './index.mdl';
|
|
|
|
|
|
|
|
export default class Textfield extends Component {
|
|
|
|
constructor($element, $scope, $attrs) {
|
|
|
|
super($element);
|
|
|
|
|
|
|
|
let input = this.input = this.element.querySelector('input');
|
|
|
|
input.addEventListener('input',
|
|
|
|
() => this.checkValue());
|
|
|
|
input.addEventListener('focus',
|
|
|
|
() => this.checkValue());
|
|
|
|
input.addEventListener('blur',
|
|
|
|
() => this.showClear(false));
|
|
|
|
|
|
|
|
let clearButton = this.element.querySelector('button');
|
|
|
|
clearButton.addEventListener('click',
|
|
|
|
() => this.onClearClick());
|
|
|
|
clearButton.addEventListener('mousedown',
|
|
|
|
event => event.preventDefault());
|
|
|
|
|
|
|
|
let div = this.element.firstChild;
|
|
|
|
componentHandler.upgradeElement(div);
|
|
|
|
}
|
|
|
|
link($scope, $attrs) {
|
|
|
|
let mdlTextField = this.element.firstChild.MaterialTextfield;
|
2017-05-05 10:54:47 +00:00
|
|
|
mdlTextField.updateClasses_();
|
|
|
|
|
2017-04-28 13:04:29 +00:00
|
|
|
$scope.$watch($attrs.model,
|
|
|
|
() => mdlTextField.updateClasses_());
|
|
|
|
}
|
|
|
|
onClearClick() {
|
|
|
|
this.input.value = '';
|
|
|
|
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-04-28 13:04:29 +00:00
|
|
|
let clearButton = this.element.querySelector('button');
|
|
|
|
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
|
|
|
}
|
|
|
|
Textfield.$inject = ['$element', '$scope', '$attrs'];
|
|
|
|
|
|
|
|
directive.$inject = [resolveFactory.NAME, normalizerFactory.NAME];
|
|
|
|
export function directive(resolve, normalizer) {
|
|
|
|
return {
|
|
|
|
restrict: 'E',
|
|
|
|
template: function(_, attrs) {
|
|
|
|
normalizer.normalize(attrs);
|
|
|
|
return resolve.getTemplate('textfield', attrs);
|
|
|
|
},
|
|
|
|
link: function($scope, $element, $attrs, $ctrl) {
|
|
|
|
$ctrl.link($scope, $attrs);
|
|
|
|
},
|
|
|
|
controller: Textfield
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.directive('vnTextfield', directive);
|