forked from muicss/mui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton.js
More file actions
148 lines (121 loc) · 3.53 KB
/
button.js
File metadata and controls
148 lines (121 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* MUI Angular Button Component
* @module angular/button
*/
import angular from 'angular';
import * as jqLite from '../js/lib/jqLite';
import * as util from '../js/lib/util';
const moduleName = 'mui.button',
rippleClass = 'mui-ripple-effect',
supportsTouch = 'ontouchstart' in document.documentElement,
mouseDownEvents = (supportsTouch) ? 'touchstart' : 'mousedown',
mouseUpEvents = (supportsTouch) ? 'touchend' : 'mouseup mouseleave',
animationDuration = 600;
angular.module(moduleName, [])
.directive('muiButton', function() {
return {
restrict: 'AE',
replace: true,
template: '<button class="mui-btn" mui-ripple ng-transclude></button>',
transclude: true,
link: function(scope, element, attrs) {
var isUndef = angular.isUndefined,
el = element[0];
// disable MUI js
el._muiDropdown = true;
el._muiRipple = true;
// handle disabled attribute
if (!isUndef(attrs.disabled) && isUndef(attrs.ngDisabled)) {
element.prop('disabled', true);
}
// set button styles
angular.forEach(['variant', 'color', 'size'], function(attrName) {
var attrVal = attrs[attrName];
if (attrVal) element.addClass('mui-btn--' + attrVal);
});
}
};
})
.directive('muiRipple', ['$timeout', function($timeout) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
// add mousedown event handler
element.on(mouseDownEvents, mouseDownHandler);
}
}
}]);
/**
* MouseDown event handler.
* @param {Event} ev - The DOM event
*/
function mouseDownHandler(ev) {
var element = angular.element(this);
// exit if disabled
if (element.prop('disabled')) return;
// add mouseup event handler once
if (!this.muiMouseUp) {
element.on(mouseUpEvents, mouseUpHandler);
this.muiMouseUp = true;
}
// get (x, y) position of click
var offset = jqLite.offset(this),
clickEv = (ev.type === 'touchstart') ? ev.touches[0] : ev,
xPos = clickEv.pageX - offset.left,
yPos = clickEv.pageY - offset.top,
diameter,
radius,
rippleEl;
// calculate diameter
diameter = Math.sqrt(offset.width * offset.width +
offset.height * offset.height) * 2;
// create ripple element
rippleEl = angular.element('<div class="' + rippleClass + '"></div>');
radius = diameter / 2;
rippleEl.css({
height: diameter + 'px',
width: diameter + 'px',
top: (yPos - radius) + 'px',
left: (xPos - radius) + 'px'
});
// add to DOM
element.append(rippleEl);
// start animation
util.requestAnimationFrame(function() {
rippleEl.addClass('mui--animate-in mui--active');
});
}
/**
* MouseUp event handler.
* @param {Event} ev - The DOM event
*/
function mouseUpHandler(ev) {
var children = this.children,
i = children.length,
rippleEls = [],
el;
// animate out ripples
while (i--) {
el = children[i];
if (jqLite.hasClass(el, rippleClass)) {
jqLite.addClass(el, 'mui--animate-out');
rippleEls.push(el);
}
}
// remove ripples after animation
if (rippleEls.length) {
setTimeout(function() {
var i = rippleEls.length,
el,
parentNode;
// remove elements
while (i--) {
el = rippleEls[i];
parentNode = el.parentNode;
if (parentNode) parentNode.removeChild(el);
}
}, animationDuration);
}
}
/** Define module API */
export default moduleName;