2015-07-24 15:28:44 -07:00
|
|
|
import {Component, View, LifecycleEvent, ViewEncapsulation} from 'angular2/angular2';
|
2015-07-31 14:11:35 -07:00
|
|
|
|
|
|
|
import {TimerWrapper} from 'angular2/src/facade/async';
|
2015-04-21 11:47:53 -07:00
|
|
|
import {isPresent} from 'angular2/src/facade/lang';
|
2015-02-17 11:56:24 -08:00
|
|
|
|
2015-07-31 14:11:35 -07:00
|
|
|
// TODO(jelbourn): Ink ripples.
|
|
|
|
// TODO(jelbourn): Make the `isMosueDown` stuff done with one global listener.
|
2015-02-17 11:56:24 -08:00
|
|
|
|
2015-07-31 14:11:35 -07:00
|
|
|
@Component({
|
|
|
|
selector: '[md-button]:not(a), [md-fab]:not(a), [md-raised-button]:not(a)',
|
|
|
|
host: {
|
|
|
|
'(^mousedown)': 'onMousedown()',
|
|
|
|
'(focus)': 'onFocus()',
|
|
|
|
'(blur)': 'onBlur()',
|
|
|
|
'[class.md-button-focus]': 'isKeyboardFocused',
|
|
|
|
},
|
|
|
|
})
|
2015-07-24 15:28:44 -07:00
|
|
|
@View({
|
|
|
|
templateUrl: 'package:angular2_material/src/components/button/button.html',
|
2015-07-31 14:11:35 -07:00
|
|
|
encapsulation: ViewEncapsulation.NONE,
|
2015-07-24 15:28:44 -07:00
|
|
|
})
|
2015-02-17 11:56:24 -08:00
|
|
|
export class MdButton {
|
2015-07-31 14:11:35 -07:00
|
|
|
/** Whether a mousedown has occured on this element in the last 100ms. */
|
|
|
|
isMouseDown: boolean = false;
|
|
|
|
|
|
|
|
/** Whether the button has focus from the keyboard (not the mouse). Used for class binding. */
|
|
|
|
isKeyboardFocused: boolean = false;
|
|
|
|
|
|
|
|
onMousedown() {
|
|
|
|
// We only *show* the focus style when focus has come to the button via the keyboard.
|
|
|
|
// The Material Design spec is silent on this topic, and without doing this, the
|
|
|
|
// button continues to look :active after clicking.
|
|
|
|
// @see http://marcysutton.com/button-focus-hell/
|
|
|
|
this.isMouseDown = true;
|
|
|
|
TimerWrapper.setTimeout(() => {this.isMouseDown = false}, 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
onFocus() {
|
|
|
|
this.isKeyboardFocused = !this.isMouseDown;
|
|
|
|
}
|
|
|
|
|
|
|
|
onBlur() {
|
|
|
|
this.isKeyboardFocused = false;
|
|
|
|
}
|
2015-02-17 11:56:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Component({
|
2015-07-31 14:11:35 -07:00
|
|
|
selector: 'a[md-button], a[md-raised-button], a[md-fab]',
|
2015-05-26 15:54:10 +02:00
|
|
|
properties: ['disabled'],
|
2015-07-31 14:11:35 -07:00
|
|
|
lifecycle: [LifecycleEvent.onChange],
|
|
|
|
host: {
|
|
|
|
'(^click)': 'onClick($event)',
|
|
|
|
'(^mousedown)': 'onMousedown()',
|
|
|
|
'(focus)': 'onFocus()',
|
|
|
|
'(blur)': 'onBlur()',
|
|
|
|
'[tabIndex]': 'tabIndex',
|
|
|
|
'[class.md-button-focus]': 'isKeyboardFocused',
|
|
|
|
'[attr.aria-disabled]': 'disabled',
|
|
|
|
},
|
2015-02-17 11:56:24 -08:00
|
|
|
})
|
2015-07-24 15:28:44 -07:00
|
|
|
@View({
|
|
|
|
templateUrl: 'package:angular2_material/src/components/button/button.html',
|
|
|
|
encapsulation: ViewEncapsulation.NONE
|
|
|
|
})
|
2015-07-31 14:11:35 -07:00
|
|
|
export class MdAnchor extends MdButton {
|
2015-04-21 11:47:53 -07:00
|
|
|
tabIndex: number;
|
2015-02-17 11:56:24 -08:00
|
|
|
|
|
|
|
/** Whether the component is disabled. */
|
2015-07-31 14:11:35 -07:00
|
|
|
disabled_: boolean;
|
|
|
|
|
|
|
|
get disabled(): boolean {
|
|
|
|
return this.disabled_;
|
|
|
|
}
|
|
|
|
|
|
|
|
set disabled(value) {
|
|
|
|
// The presence of *any* disabled value makes the component disabled, *except* for false.
|
|
|
|
this.disabled_ = isPresent(value) && this.disabled !== false;
|
|
|
|
}
|
2015-02-17 11:56:24 -08:00
|
|
|
|
|
|
|
onClick(event) {
|
|
|
|
// A disabled anchor shouldn't navigate anywhere.
|
2015-07-31 14:11:35 -07:00
|
|
|
if (this.disabled) {
|
2015-02-17 11:56:24 -08:00
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Invoked when a change is detected. */
|
|
|
|
onChange(_) {
|
|
|
|
// A disabled anchor should not be in the tab flow.
|
2015-04-21 11:47:53 -07:00
|
|
|
this.tabIndex = this.disabled ? -1 : 0;
|
2015-02-17 11:56:24 -08:00
|
|
|
}
|
|
|
|
}
|