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
132 lines (105 loc) · 3.38 KB
/
button.js
File metadata and controls
132 lines (105 loc) · 3.38 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
/**
* 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',
supportsTouch = 'ontouchstart' in document.documentElement,
mouseDownEvents = (supportsTouch) ? 'touchstart' : 'mousedown',
mouseUpEvents = (supportsTouch) ? 'touchend' : 'mouseup mouseleave';
angular.module(moduleName, [])
.directive('muiButton', function() {
return {
restrict: 'AE',
replace: true,
template: '<button class="mui-btn" mui-ripple>' +
'<ng-transclude></ng-transclude>' +
'<span class="mui-btn__ripple-container">' +
'<span class="mui-ripple"></span>' +
'</span>' +
'</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) {
var buttonEl = element[0];
// cache reference to ripple element
buttonEl._rippleEl = buttonEl.querySelector('.mui-ripple');
// add mousedown and mouseup event ripple effect handlers
element.on(mouseDownEvents, mouseDownHandler);
}
}
}]);
/**
* MouseDown event handler.
* @param {Event} ev - The DOM event
*/
function mouseDownHandler(ev) {
var buttonEl = this,
rippleEl = buttonEl._rippleEl;
// exit if disabled
if (buttonEl.disabled) return;
// add mouseup handler on first-click
if (!rippleEl._init) {
jqLite.on(buttonEl, mouseUpEvents, mouseUpHandler);
rippleEl._init = true;
}
// get (x, y) position of click
var offset = jqLite.offset(buttonEl),
clickEv = (ev.type === 'touchstart') ? ev.touches[0] : ev,
radius,
diameter;
// calculate radius
radius = Math.sqrt(offset.width * offset.width +
offset.height * offset.height);
diameter = radius * 2 + 'px';
// set position and dimensions
jqLite.css(rippleEl, {
width: diameter,
height: diameter,
top: Math.round(clickEv.pageY - offset.top - radius) + 'px',
left: Math.round(clickEv.pageX - offset.left - radius) + 'px'
});
jqLite.removeClass(rippleEl, 'mui--is-animating');
jqLite.addClass(rippleEl, 'mui--is-visible');
// start animation
util.requestAnimationFrame(function() {
jqLite.addClass(rippleEl, 'mui--is-animating');
});
}
/**
* MouseUp event handler.
* @param {Event} ev - The DOM event
*/
function mouseUpHandler(ev) {
// get ripple element
var rippleEl = this._rippleEl;
// allow a repaint to occur before removing class so animation shows for
// tap events
util.requestAnimationFrame(function() {
jqLite.removeClass(rippleEl, 'mui--is-visible');
});
}
/** Define module API */
export default moduleName;