Files
2015-12-08 15:48:40 -06:00

92 lines
1.9 KiB
TypeScript

import {Directive, Optional, ElementRef, Renderer} from 'angular2/angular2';
import {Config} from '../../config/config';
import {TextInput} from './text-input';
import {pointerCoord, hasPointerMoved} from '../../util/dom';
/**
* @name Label
* @description
* Labels describe the data that the user should enter in to an input element.
* @usage
* ```html
* <ion-input>
* <ion-label>Username</ion-label>
* <input type="text" value="">
* </ion-input>
* ```
*
* @see {@link ../../../../components#inputs Input Component Docs}
* @see {@link ../Input Input API Docs}
*
*/
@Directive({
selector: 'ion-label',
inputs: [
'id'
],
host: {
'[attr.id]': 'id',
'(touchstart)': 'pointerStart($event)',
'(touchend)': 'pointerEnd($event)',
'(mousedown)': 'pointerStart($event)',
'(mouseup)': 'pointerEnd($event)'
}
})
export class Label {
constructor(
config: Config,
@Optional() container: TextInput,
private elementRef: ElementRef,
private renderer: Renderer
) {
this.scrollAssist = config.get('scrollAssist');
if (!this.id) {
this.id = 'lbl-' + (++labelIds);
}
this.container = container;
container && container.registerLabel(this);
}
/**
* @private
*/
pointerStart(ev) {
if (this.scrollAssist) {
// remember where the touchstart/mousedown started
this.startCoord = pointerCoord(ev);
}
}
/**
* @private
*/
pointerEnd(ev) {
if (this.container) {
// get where the touchend/mouseup ended
let endCoord = pointerCoord(ev);
// focus this input if the pointer hasn't moved XX pixels
if (!hasPointerMoved(20, this.startCoord, endCoord)) {
ev.preventDefault();
ev.stopPropagation();
this.container.initFocus();
}
this.startCoord = null;
}
}
/**
* @private
*/
addClass(className) {
this.renderer.setElementClass(this.elementRef, className, true);
}
}
let labelIds = -1;