UNPKG

7.98 kBJavaScriptView Raw
1var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5 return c > 3 && r && Object.defineProperty(target, key, r), r;
6};
7var __metadata = (this && this.__metadata) || function (k, v) {
8 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9};
10import { NgModule, Directive, ElementRef, HostBinding, Input } from '@angular/core';
11import { RippleRenderer, ForegroundRippleState } from './ripple-renderer';
12export var MdRipple = (function () {
13 function MdRipple(_elementRef) {
14 var _this = this;
15 /**
16 * If set, the radius in pixels of foreground ripples when fully expanded. If unset, the radius
17 * will be the distance from the center of the ripple to the furthest corner of the host element's
18 * bounding rectangle.
19 */
20 this.maxRadius = 0;
21 /**
22 * If set, the normal duration of ripple animations is divided by this value. For example,
23 * setting it to 0.5 will cause the animations to take twice as long.
24 */
25 this.speedFactor = 1;
26 // These event handlers are attached to the element that triggers the ripple animations.
27 var eventHandlers = new Map();
28 eventHandlers.set('mousedown', function (event) { return _this._mouseDown(event); });
29 eventHandlers.set('click', function (event) { return _this._click(event); });
30 eventHandlers.set('mouseleave', function (event) { return _this._mouseLeave(event); });
31 this._rippleRenderer = new RippleRenderer(_elementRef, eventHandlers);
32 }
33 /** TODO: internal */
34 MdRipple.prototype.ngOnInit = function () {
35 // If no trigger element was explicity set, use the host element
36 if (!this.trigger) {
37 this._rippleRenderer.setTriggerElementToHost();
38 }
39 };
40 /** TODO: internal */
41 MdRipple.prototype.ngOnDestroy = function () {
42 // Remove event listeners on the trigger element.
43 this._rippleRenderer.clearTriggerElement();
44 };
45 /** TODO: internal */
46 MdRipple.prototype.ngOnChanges = function (changes) {
47 // If the trigger element changed (or is being initially set), add event listeners to it.
48 var changedInputs = Object.keys(changes);
49 if (changedInputs.indexOf('trigger') !== -1) {
50 this._rippleRenderer.setTriggerElement(this.trigger);
51 }
52 };
53 /**
54 * Responds to the start of a ripple animation trigger by fading the background in.
55 */
56 MdRipple.prototype.start = function () {
57 this._rippleRenderer.fadeInRippleBackground(this.backgroundColor);
58 };
59 /**
60 * Responds to the end of a ripple animation trigger by fading the background out, and creating a
61 * foreground ripple that expands from the event location (or from the center of the element if
62 * the "centered" property is set or forceCenter is true).
63 */
64 MdRipple.prototype.end = function (left, top, forceCenter) {
65 var _this = this;
66 if (forceCenter === void 0) { forceCenter = true; }
67 this._rippleRenderer.createForegroundRipple(left, top, this.color, this.centered || forceCenter, this.maxRadius, this.speedFactor, function (ripple, e) { return _this._rippleTransitionEnded(ripple, e); });
68 this._rippleRenderer.fadeOutRippleBackground();
69 };
70 MdRipple.prototype._rippleTransitionEnded = function (ripple, event) {
71 if (event.propertyName === 'opacity') {
72 // If the ripple finished expanding, start fading it out. If it finished fading out,
73 // remove it from the DOM.
74 switch (ripple.state) {
75 case ForegroundRippleState.EXPANDING:
76 this._rippleRenderer.fadeOutForegroundRipple(ripple.rippleElement);
77 ripple.state = ForegroundRippleState.FADING_OUT;
78 break;
79 case ForegroundRippleState.FADING_OUT:
80 this._rippleRenderer.removeRippleFromDom(ripple.rippleElement);
81 break;
82 }
83 }
84 };
85 /**
86 * Called when the trigger element receives a mousedown event. Starts the ripple animation by
87 * fading in the background.
88 */
89 MdRipple.prototype._mouseDown = function (event) {
90 if (!this.disabled && event.button === 0) {
91 this.start();
92 }
93 };
94 /**
95 * Called when the trigger element receives a click event. Creates a foreground ripple and
96 * runs its animation.
97 */
98 MdRipple.prototype._click = function (event) {
99 if (!this.disabled && event.button === 0) {
100 // If screen and page positions are all 0, this was probably triggered by a keypress.
101 // In that case, use the center of the bounding rect as the ripple origin.
102 // FIXME: This fails on IE11, which still sets pageX/Y and screenX/Y on keyboard clicks.
103 var isKeyEvent = (event.screenX === 0 && event.screenY === 0 && event.pageX === 0 && event.pageY === 0);
104 this.end(event.pageX, event.pageY, isKeyEvent);
105 }
106 };
107 /**
108 * Called when the trigger element receives a mouseleave event. Fades out the background.
109 */
110 MdRipple.prototype._mouseLeave = function (event) {
111 // We can always fade out the background here; It's a no-op if it was already inactive.
112 this._rippleRenderer.fadeOutRippleBackground();
113 };
114 __decorate([
115 Input('md-ripple-trigger'),
116 __metadata('design:type', Object)
117 ], MdRipple.prototype, "trigger", void 0);
118 __decorate([
119 Input('md-ripple-centered'),
120 __metadata('design:type', Boolean)
121 ], MdRipple.prototype, "centered", void 0);
122 __decorate([
123 Input('md-ripple-disabled'),
124 __metadata('design:type', Boolean)
125 ], MdRipple.prototype, "disabled", void 0);
126 __decorate([
127 Input('md-ripple-max-radius'),
128 __metadata('design:type', Number)
129 ], MdRipple.prototype, "maxRadius", void 0);
130 __decorate([
131 Input('md-ripple-speed-factor'),
132 __metadata('design:type', Number)
133 ], MdRipple.prototype, "speedFactor", void 0);
134 __decorate([
135 Input('md-ripple-color'),
136 __metadata('design:type', String)
137 ], MdRipple.prototype, "color", void 0);
138 __decorate([
139 Input('md-ripple-background-color'),
140 __metadata('design:type', String)
141 ], MdRipple.prototype, "backgroundColor", void 0);
142 __decorate([
143 HostBinding('class.md-ripple-focused'),
144 Input('md-ripple-focused'),
145 __metadata('design:type', Boolean)
146 ], MdRipple.prototype, "focused", void 0);
147 __decorate([
148 HostBinding('class.md-ripple-unbounded'),
149 Input('md-ripple-unbounded'),
150 __metadata('design:type', Boolean)
151 ], MdRipple.prototype, "unbounded", void 0);
152 MdRipple = __decorate([
153 Directive({
154 selector: '[md-ripple]',
155 }),
156 __metadata('design:paramtypes', [ElementRef])
157 ], MdRipple);
158 return MdRipple;
159}());
160export var MdRippleModule = (function () {
161 function MdRippleModule() {
162 }
163 MdRippleModule.forRoot = function () {
164 return {
165 ngModule: MdRippleModule,
166 providers: []
167 };
168 };
169 MdRippleModule = __decorate([
170 NgModule({
171 exports: [MdRipple],
172 declarations: [MdRipple],
173 }),
174 __metadata('design:paramtypes', [])
175 ], MdRippleModule);
176 return MdRippleModule;
177}());
178
179//# sourceMappingURL=ripple.js.map