-
Notifications
You must be signed in to change notification settings - Fork 21
/
v-button.js
executable file
·159 lines (120 loc) · 4.11 KB
/
v-button.js
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
149
150
151
152
153
154
155
156
157
158
159
/**
* vButton - AngularJS pressable button with a busy indicator
* @version v1.2.1
* @link http://lukaszwatroba.github.io/v-button
* @author Łukasz Wątroba <l@lukaszwatroba.com>
* @license MIT License, http://www.opensource.org/licenses/MIT
*/
(function(window, angular) {
'use strict';
// Config
angular.module('vButton.config', [])
.constant('buttonConfig', {
busyLabel: 'Loading',
states: {
busy: 'is-busy',
pressed: 'is-pressed'
}
});
// Modules
angular.module('vButton.directives', []);
angular.module('vButton',
[
'vButton.config',
'vButton.directives'
]);
// vBusy directive
angular.module('vButton.directives')
.directive('vBusy', vBusyDirective);
function vBusyDirective ($document, buttonConfig) {
return {
restrict: 'A',
scope: {
isBusy: '=vBusy',
busyLabel: '@vBusyLabel',
busyText: '@vBusyText'
},
compile: function (tElement, tAttrs) {
var labelElement = angular.element(tElement.find('span'));
if (!labelElement[0]) {
tElement.html( '<span>' + tElement.html() + '</span>' );
labelElement = angular.element(tElement.find('span'));
}
return function postLink (scope, iElement) {
var idleLabelHtml = labelElement.html(),
busyLabelHtml = scope.busyLabel || buttonConfig.busyLabel,
busyTextHtml = scope.busyText;
scope.$watch('isBusy', function (value) {
if (value) {
iElement.addClass(buttonConfig.states.busy);
labelElement.html(busyLabelHtml);
} else {
iElement.removeClass(buttonConfig.states.busy);
labelElement.html(busyTextHtml || idleLabelHtml);
}
});
tAttrs.$observe('vBusyLabel', function(value) {
busyLabelHtml = value;
});
tAttrs.$observe('vBusyText', function(value) {
busyTextHtml = value;
});
};
}
};
}
vBusyDirective.$inject = ['$document', 'buttonConfig'];
function isTouchDevice () {
return (('ontouchstart' in window) || (window.navigator.MaxTouchPoints > 0) || (window.navigator.msMaxTouchPoints > 0));
}
// vPressable directive
angular.module('vButton.directives')
.directive('vPressable', vPressableDirective);
function vPressableDirective ($document, buttonConfig) {
return {
restrict: 'A',
link: function (scope, iElement) {
var isTouch = isTouchDevice(),
pressEvent = (isTouch) ? 'touchstart' : 'mousedown',
releaseEvent = (isTouch) ? 'touchend' : 'mouseup';
var bodyElement = angular.element($document[0].body);
function makeRipple (posX, posY) {
var rect = iElement[0].getBoundingClientRect(),
ripple = iElement[0].querySelector('v-ripple');
var top, left;
angular.element(ripple).remove();
ripple = $document[0].createElement('v-ripple');
ripple.style.height = ripple.style.width = Math.max(rect.width, rect.height) + 'px';
iElement.append(ripple);
left = posX - rect.left - ripple.offsetWidth / 2 - bodyElement[0].scrollLeft;
top = posY - rect.top - ripple.offsetHeight / 2 - bodyElement[0].scrollTop;
ripple.style.left = left + 'px';
ripple.style.top = top + 'px';
}
function pressButton () {
iElement.addClass(buttonConfig.states.pressed);
bodyElement.on(releaseEvent, releaseButton);
}
function releaseButton (event) {
var posX, posY;
if (isTouch) {
posX = event.changedTouches[0].pageX;
posY = event.changedTouches[0].pageY;
} else {
posX = event.pageX;
posY = event.pageY;
}
makeRipple(posX, posY);
iElement.removeClass(buttonConfig.states.pressed);
bodyElement.off(releaseEvent, releaseButton);
}
iElement.on(pressEvent, pressButton);
scope.$on('$destroy', function () {
iElement.off(pressEvent, pressButton);
bodyElement.off(releaseEvent, releaseButton);
});
}
};
}
vPressableDirective.$inject = ['$document', 'buttonConfig'];
}(window, angular));