2015-07-29 11:26:09 -07:00
|
|
|
import {Directive, LifecycleEvent, Attribute, Host, SkipSelf} from 'angular2/angular2';
|
2015-05-07 09:10:41 -07:00
|
|
|
|
|
|
|
|
import {ObservableWrapper, EventEmitter} from 'angular2/src/facade/async';
|
|
|
|
|
|
|
|
|
|
// TODO(jelbourn): validation (will depend on Forms API).
|
|
|
|
|
// TODO(jelbourn): textarea resizing
|
|
|
|
|
// TODO(jelbourn): max-length counter
|
|
|
|
|
// TODO(jelbourn): placeholder property
|
2015-02-17 11:56:24 -08:00
|
|
|
|
2015-05-22 13:01:23 -07:00
|
|
|
@Directive({
|
|
|
|
|
selector: 'md-input-container',
|
2015-07-04 15:04:50 +02:00
|
|
|
lifecycle: [LifecycleEvent.onAllChangesDone],
|
2015-08-04 15:32:17 -07:00
|
|
|
host: {
|
|
|
|
|
'[class.md-input-has-value]': 'inputHasValue',
|
|
|
|
|
'[class.md-input-focused]': 'inputHasFocus',
|
|
|
|
|
}
|
2015-05-22 13:01:23 -07:00
|
|
|
})
|
|
|
|
|
export class MdInputContainer {
|
|
|
|
|
// The MdInput or MdTextarea inside of this container.
|
|
|
|
|
_input: MdInput;
|
|
|
|
|
|
|
|
|
|
// Whether the input inside of this container has a non-empty value.
|
|
|
|
|
inputHasValue: boolean;
|
|
|
|
|
|
|
|
|
|
// Whether the input inside of this container has focus.
|
|
|
|
|
inputHasFocus: boolean;
|
|
|
|
|
|
|
|
|
|
constructor(@Attribute('id') id: string) {
|
|
|
|
|
this._input = null;
|
|
|
|
|
this.inputHasValue = false;
|
|
|
|
|
this.inputHasFocus = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onAllChangesDone() {
|
|
|
|
|
// Enforce that this directive actually contains a text input.
|
|
|
|
|
if (this._input == null) {
|
|
|
|
|
throw 'No <input> or <textarea> found inside of <md-input-container>';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Registers the child MdInput or MdTextarea. */
|
|
|
|
|
registerInput(input) {
|
|
|
|
|
if (this._input != null) {
|
|
|
|
|
throw 'Only one text input is allowed per <md-input-container>.';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._input = input;
|
|
|
|
|
this.inputHasValue = input.value != '';
|
|
|
|
|
|
|
|
|
|
// Listen to input changes and focus events so that we can apply the appropriate CSS
|
|
|
|
|
// classes based on the input state.
|
|
|
|
|
ObservableWrapper.subscribe(input.mdChange, value => { this.inputHasValue = value != ''; });
|
|
|
|
|
|
2015-08-06 09:52:33 -07:00
|
|
|
ObservableWrapper.subscribe<boolean>(input.mdFocusChange,
|
|
|
|
|
hasFocus => this.inputHasFocus = hasFocus);
|
2015-05-22 13:01:23 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-04-30 13:38:40 -07:00
|
|
|
@Directive({
|
2015-05-07 09:10:41 -07:00
|
|
|
selector: 'md-input-container input',
|
|
|
|
|
events: ['mdChange', 'mdFocusChange'],
|
2015-06-09 12:33:40 +02:00
|
|
|
host: {
|
2015-08-04 15:32:17 -07:00
|
|
|
'class': 'md-input',
|
2015-06-09 12:33:40 +02:00
|
|
|
'(input)': 'updateValue($event)',
|
|
|
|
|
'(focus)': 'setHasFocus(true)',
|
|
|
|
|
'(blur)': 'setHasFocus(false)'
|
|
|
|
|
}
|
2015-02-17 11:56:24 -08:00
|
|
|
})
|
|
|
|
|
export class MdInput {
|
2015-05-07 09:10:41 -07:00
|
|
|
value: string;
|
2015-02-17 11:56:24 -08:00
|
|
|
|
2015-05-07 09:10:41 -07:00
|
|
|
// Events emitted by this directive. We use these special 'md-' events to communicate
|
|
|
|
|
// to the parent MdInputContainer.
|
|
|
|
|
mdChange: EventEmitter;
|
|
|
|
|
mdFocusChange: EventEmitter;
|
|
|
|
|
|
2015-07-29 11:26:09 -07:00
|
|
|
constructor(@Attribute('value') value: string, @SkipSelf() @Host() container: MdInputContainer,
|
2015-05-22 13:01:23 -07:00
|
|
|
@Attribute('id') id: string) {
|
2015-05-07 09:10:41 -07:00
|
|
|
this.value = value == null ? '' : value;
|
|
|
|
|
this.mdChange = new EventEmitter();
|
|
|
|
|
this.mdFocusChange = new EventEmitter();
|
|
|
|
|
|
|
|
|
|
container.registerInput(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateValue(event) {
|
|
|
|
|
this.value = event.target.value;
|
|
|
|
|
ObservableWrapper.callNext(this.mdChange, this.value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setHasFocus(hasFocus: boolean) {
|
|
|
|
|
ObservableWrapper.callNext(this.mdFocusChange, hasFocus);
|
2015-02-17 11:56:24 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-22 13:01:23 -07:00
|
|
|
/*
|
2015-05-07 09:10:41 -07:00
|
|
|
@Directive({
|
|
|
|
|
selector: 'md-input-container textarea',
|
|
|
|
|
events: ['mdChange', 'mdFocusChange'],
|
|
|
|
|
hostProperties: {
|
|
|
|
|
'yes': 'class.md-input'
|
|
|
|
|
},
|
|
|
|
|
hostListeners: {
|
|
|
|
|
'input': 'updateValue($event)',
|
|
|
|
|
'focus': 'setHasFocus(true)',
|
|
|
|
|
'blur': 'setHasFocus(false)'
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
export class MdTextarea extends MdInput {
|
|
|
|
|
constructor(
|
2015-05-22 13:01:23 -07:00
|
|
|
@Attribute('value') value: string,
|
2015-07-29 11:26:09 -07:00
|
|
|
@SkipSelf() @Host() container: MdInputContainer,
|
2015-05-22 13:01:23 -07:00
|
|
|
@Attribute('id') id: string) {
|
|
|
|
|
super(value, container, id);
|
2015-05-07 09:10:41 -07:00
|
|
|
}
|
2015-02-17 11:56:24 -08:00
|
|
|
}
|
2015-05-22 13:01:23 -07:00
|
|
|
*/
|