forked from fians/Waves
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaves.js
195 lines (139 loc) · 5.96 KB
/
waves.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*!
* Waves v0.2.1
* https://publicis-indonesia.github.io/Waves
*
* Copyright 2014 Publicis Metro Indonesia, PT. and other contributors
* Released under the BSD license
* https://github.com/publicis-indonesia/Waves/blob/master/LICENSE
*/
;(function(window) {
'use strict';
function Waves() {
var $$ = document.querySelectorAll.bind(document);
// Find exact position of element
function position(obj) {
var left = 0;
var top = 0;
if (obj.offsetParent) {
do {
left += obj.offsetLeft;
top += obj.offsetTop;
} while (obj = obj.offsetParent);
}
return {
top: top,
left: left
};
}
var Effect = {
// Trigger element action if they are waves-button
action: function(e) {
// catch window.event
e = e || window.event;
var el = this;
// Disable action if right click triggered this event
if (e.which !== 1) {
return false;
}
if (el.className.indexOf('waves-button') !== -1) {
// Get the child
var child = el.childNodes[1];
var tag = child.tagName.toLowerCase();
// Redirect page based on href attribute
if (tag === 'a') {
if (!child.href.length) {
return false;
}
if (child.target === '_blank') {
var win = window.open(child.href, '_blank');
return win.focus();
}
return window.location.href = child.href;
}
// Click the element or submit the form if exist
if (tag === 'button' || tag === 'input') {
if (child.submit) {
return child.submit();
}
return child.click();
}
}
},
show: function(e) {
var el = this;
// Create ripple
var ripple = document.createElement('div');
ripple.className = ripple.className + 'waves-ripple';
el.appendChild(ripple);
// Get click coordinate and element witdh
var pos = position(el);
var relativeY = (e.pageY - pos.top);
var relativeX = (e.pageX - pos.left);
var width = el.clientWidth;
// Attach data to element
ripple.setAttribute('data-hold', Date.now());
ripple.setAttribute('data-x', relativeX);
ripple.setAttribute('data-y', relativeY);
// Start ripple
var positionStyle = 'top:'+relativeY+'px;left:'+relativeX+'px;';
var flowStyle = 'border-width:'+width+'px;margin-top:-'+width+'px;margin-left:-'+width+'px;opacity:1;';
ripple.className = ripple.className + ' waves-notransition';
ripple.setAttribute('style', positionStyle);
ripple.offsetHeight;
ripple.className = ripple.className.replace('waves-notransition', '');
ripple.setAttribute('style', positionStyle+flowStyle);
},
hide: function(e) {
var el = this;
var width = el.clientWidth;
// Get first ripple
var ripple = null;
for (var a = 0; a < el.children.length; a++) {
if (el.children[a].className.indexOf('waves-ripple') !== -1) {
ripple = el.children[a];
continue;
}
}
if (!ripple) {
return false;
}
var relativeX = ripple.getAttribute('data-x');
var relativeY = ripple.getAttribute('data-y');
// Get delay beetween mousedown and mouse leave
var diff = Date.now() - Number(ripple.getAttribute('data-hold'));
var delay = 500 - diff;
if (delay < 0) {
delay = 0;
}
// Fade out ripple after delay
setTimeout(function() {
var style = 'top:'+relativeY+'px;left:'+relativeX+'px;border-width:'+width+'px;margin-top:-'+width+'px;margin-left:-'+width+'px;opacity:0;';
ripple.setAttribute('style', style);
setTimeout(function() {
try {
el.removeChild(ripple);
} catch(e) {
return false;
}
}, 300);
}, delay);
},
};
return {
displayEffect: function() {
Array.prototype.forEach.call($$('.waves-effect'), function(i) {
if (window.Touch) {
i.addEventListener('touchstart', Effect.show, false);
i.addEventListener('touchend', Effect.hide, false);
i.addEventListener('touchend', Effect.action, false);
}
i.addEventListener('mousedown', Effect.show, false);
i.addEventListener('mouseup', Effect.hide, false);
i.addEventListener('mouseleave', Effect.hide, false);
i.addEventListener('mouseup', Effect.action, false);
});
}
}
}
window.Waves = Waves;
})(window);