waves.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. /*!
  2. * Waves v0.7.6
  3. * http://fian.my.id/Waves
  4. *
  5. * Copyright 2014-2018 Alfiana E. Sibuea and other contributors
  6. * Released under the MIT license
  7. * https://github.com/fians/Waves/blob/master/LICENSE
  8. */
  9. ;(function(window, factory) {
  10. 'use strict';
  11. // AMD. Register as an anonymous module. Wrap in function so we have access
  12. // to root via `this`.
  13. if (typeof define === 'function' && define.amd) {
  14. define([], function() {
  15. window.Waves = factory.call(window);
  16. return window.Waves;
  17. });
  18. }
  19. // Node. Does not work with strict CommonJS, but only CommonJS-like
  20. // environments that support module.exports, like Node.
  21. else if (typeof exports === 'object') {
  22. module.exports = factory.call(window);
  23. }
  24. // Browser globals.
  25. else {
  26. window.Waves = factory.call(window);
  27. }
  28. })(typeof global === 'object' ? global : this, function() {
  29. 'use strict';
  30. var Waves = Waves || {};
  31. var $$ = document.querySelectorAll.bind(document);
  32. var toString = Object.prototype.toString;
  33. var isTouchAvailable = 'ontouchstart' in window;
  34. // Find exact position of element
  35. function isWindow(obj) {
  36. return obj !== null && obj === obj.window;
  37. }
  38. function getWindow(elem) {
  39. return isWindow(elem) ? elem : elem.nodeType === 9 && elem.defaultView;
  40. }
  41. function isObject(value) {
  42. var type = typeof value;
  43. return type === 'function' || type === 'object' && !!value;
  44. }
  45. function isDOMNode(obj) {
  46. return isObject(obj) && obj.nodeType > 0;
  47. }
  48. function getWavesElements(nodes) {
  49. var stringRepr = toString.call(nodes);
  50. if (stringRepr === '[object String]') {
  51. return $$(nodes);
  52. } else if (isObject(nodes) && /^\[object (Array|HTMLCollection|NodeList|Object)\]$/.test(stringRepr) && nodes.hasOwnProperty('length')) {
  53. return nodes;
  54. } else if (isDOMNode(nodes)) {
  55. return [nodes];
  56. }
  57. return [];
  58. }
  59. function offset(elem) {
  60. var docElem, win,
  61. box = { top: 0, left: 0 },
  62. doc = elem && elem.ownerDocument;
  63. docElem = doc.documentElement;
  64. if (typeof elem.getBoundingClientRect !== typeof undefined) {
  65. box = elem.getBoundingClientRect();
  66. }
  67. win = getWindow(doc);
  68. return {
  69. top: box.top + win.pageYOffset - docElem.clientTop,
  70. left: box.left + win.pageXOffset - docElem.clientLeft
  71. };
  72. }
  73. function convertStyle(styleObj) {
  74. var style = '';
  75. for (var prop in styleObj) {
  76. if (styleObj.hasOwnProperty(prop)) {
  77. style += (prop + ':' + styleObj[prop] + ';');
  78. }
  79. }
  80. return style;
  81. }
  82. var Effect = {
  83. // Effect duration
  84. duration: 750,
  85. // Effect delay (check for scroll before showing effect)
  86. delay: 200,
  87. show: function(e, element, velocity) {
  88. // Disable right click
  89. if (e.button === 2) {
  90. return false;
  91. }
  92. element = element || this;
  93. // Create ripple
  94. var ripple = document.createElement('div');
  95. ripple.className = 'waves-ripple waves-rippling';
  96. element.appendChild(ripple);
  97. // Get click coordinate and element width
  98. var pos = offset(element);
  99. var relativeY = 0;
  100. var relativeX = 0;
  101. // Support for touch devices
  102. if('touches' in e && e.touches.length) {
  103. relativeY = (e.touches[0].pageY - pos.top);
  104. relativeX = (e.touches[0].pageX - pos.left);
  105. }
  106. //Normal case
  107. else {
  108. relativeY = (e.pageY - pos.top);
  109. relativeX = (e.pageX - pos.left);
  110. }
  111. // Support for synthetic events
  112. relativeX = relativeX >= 0 ? relativeX : 0;
  113. relativeY = relativeY >= 0 ? relativeY : 0;
  114. var scale = 'scale(' + ((element.clientWidth / 100) * 3) + ')';
  115. var translate = 'translate(0,0)';
  116. if (velocity) {
  117. translate = 'translate(' + (velocity.x) + 'px, ' + (velocity.y) + 'px)';
  118. }
  119. // Attach data to element
  120. ripple.setAttribute('data-hold', Date.now());
  121. ripple.setAttribute('data-x', relativeX);
  122. ripple.setAttribute('data-y', relativeY);
  123. ripple.setAttribute('data-scale', scale);
  124. ripple.setAttribute('data-translate', translate);
  125. // Set ripple position
  126. var rippleStyle = {
  127. top: relativeY + 'px',
  128. left: relativeX + 'px'
  129. };
  130. ripple.classList.add('waves-notransition');
  131. ripple.setAttribute('style', convertStyle(rippleStyle));
  132. ripple.classList.remove('waves-notransition');
  133. // Scale the ripple
  134. rippleStyle['-webkit-transform'] = scale + ' ' + translate;
  135. rippleStyle['-moz-transform'] = scale + ' ' + translate;
  136. rippleStyle['-ms-transform'] = scale + ' ' + translate;
  137. rippleStyle['-o-transform'] = scale + ' ' + translate;
  138. rippleStyle.transform = scale + ' ' + translate;
  139. rippleStyle.opacity = '1';
  140. var duration = e.type === 'mousemove' ? 2500 : Effect.duration;
  141. rippleStyle['-webkit-transition-duration'] = duration + 'ms';
  142. rippleStyle['-moz-transition-duration'] = duration + 'ms';
  143. rippleStyle['-o-transition-duration'] = duration + 'ms';
  144. rippleStyle['transition-duration'] = duration + 'ms';
  145. ripple.setAttribute('style', convertStyle(rippleStyle));
  146. },
  147. hide: function(e, element) {
  148. element = element || this;
  149. var ripples = element.getElementsByClassName('waves-rippling');
  150. for (var i = 0, len = ripples.length; i < len; i++) {
  151. removeRipple(e, element, ripples[i]);
  152. }
  153. if (isTouchAvailable) {
  154. element.removeEventListener('touchend', Effect.hide);
  155. element.removeEventListener('touchcancel', Effect.hide);
  156. }
  157. element.removeEventListener('mouseup', Effect.hide);
  158. element.removeEventListener('mouseleave', Effect.hide);
  159. }
  160. };
  161. /**
  162. * Collection of wrapper for HTML element that only have single tag
  163. * like <input> and <img>
  164. */
  165. var TagWrapper = {
  166. // Wrap <input> tag so it can perform the effect
  167. input: function(element) {
  168. var parent = element.parentNode;
  169. // If input already have parent just pass through
  170. if (parent.tagName.toLowerCase() === 'span' && parent.classList.contains('waves-effect')) {
  171. return;
  172. }
  173. // Put element class and style to the specified parent
  174. var wrapper = document.createElement('span');
  175. wrapper.className = 'waves-input-wrapper';
  176. // element.className = 'waves-button-input';
  177. // Put element as child
  178. parent.replaceChild(wrapper, element);
  179. wrapper.appendChild(element);
  180. },
  181. // Wrap <img> tag so it can perform the effect
  182. img: function(element) {
  183. var parent = element.parentNode;
  184. // If input already have parent just pass through
  185. if (parent.tagName.toLowerCase() === 'i' && parent.classList.contains('waves-effect')) {
  186. return;
  187. }
  188. // Put element as child
  189. var wrapper = document.createElement('i');
  190. parent.replaceChild(wrapper, element);
  191. wrapper.appendChild(element);
  192. }
  193. };
  194. /**
  195. * Hide the effect and remove the ripple. Must be
  196. * a separate function to pass the JSLint...
  197. */
  198. function removeRipple(e, el, ripple) {
  199. // Check if the ripple still exist
  200. if (!ripple) {
  201. return;
  202. }
  203. ripple.classList.remove('waves-rippling');
  204. var relativeX = ripple.getAttribute('data-x');
  205. var relativeY = ripple.getAttribute('data-y');
  206. var scale = ripple.getAttribute('data-scale');
  207. var translate = ripple.getAttribute('data-translate');
  208. // Get delay beetween mousedown and mouse leave
  209. var diff = Date.now() - Number(ripple.getAttribute('data-hold'));
  210. var delay = 350 - diff;
  211. if (delay < 0) {
  212. delay = 0;
  213. }
  214. if (e.type === 'mousemove') {
  215. delay = 150;
  216. }
  217. // Fade out ripple after delay
  218. var duration = e.type === 'mousemove' ? 2500 : Effect.duration;
  219. setTimeout(function() {
  220. var style = {
  221. top: relativeY + 'px',
  222. left: relativeX + 'px',
  223. opacity: '0',
  224. // Duration
  225. '-webkit-transition-duration': duration + 'ms',
  226. '-moz-transition-duration': duration + 'ms',
  227. '-o-transition-duration': duration + 'ms',
  228. 'transition-duration': duration + 'ms',
  229. '-webkit-transform': scale + ' ' + translate,
  230. '-moz-transform': scale + ' ' + translate,
  231. '-ms-transform': scale + ' ' + translate,
  232. '-o-transform': scale + ' ' + translate,
  233. 'transform': scale + ' ' + translate
  234. };
  235. ripple.setAttribute('style', convertStyle(style));
  236. setTimeout(function() {
  237. try {
  238. el.removeChild(ripple);
  239. } catch (e) {
  240. return false;
  241. }
  242. }, duration);
  243. }, delay);
  244. }
  245. /**
  246. * Disable mousedown event for 500ms during and after touch
  247. */
  248. var TouchHandler = {
  249. /* uses an integer rather than bool so there's no issues with
  250. * needing to clear timeouts if another touch event occurred
  251. * within the 500ms. Cannot mouseup between touchstart and
  252. * touchend, nor in the 500ms after touchend. */
  253. touches: 0,
  254. allowEvent: function(e) {
  255. var allow = true;
  256. if (/^(mousedown|mousemove)$/.test(e.type) && TouchHandler.touches) {
  257. allow = false;
  258. }
  259. return allow;
  260. },
  261. registerEvent: function(e) {
  262. var eType = e.type;
  263. if (eType === 'touchstart') {
  264. TouchHandler.touches += 1; // push
  265. } else if (/^(touchend|touchcancel)$/.test(eType)) {
  266. setTimeout(function() {
  267. if (TouchHandler.touches) {
  268. TouchHandler.touches -= 1; // pop after 500ms
  269. }
  270. }, 500);
  271. }
  272. }
  273. };
  274. /**
  275. * Delegated click handler for .waves-effect element.
  276. * returns null when .waves-effect element not in "click tree"
  277. */
  278. function getWavesEffectElement(e) {
  279. if (TouchHandler.allowEvent(e) === false) {
  280. return null;
  281. }
  282. var element = null;
  283. var target = e.target || e.srcElement;
  284. while (target.parentElement) {
  285. if ( (!(target instanceof SVGElement)) && target.classList.contains('waves-effect')) {
  286. element = target;
  287. break;
  288. }
  289. target = target.parentElement;
  290. }
  291. return element;
  292. }
  293. /**
  294. * Bubble the click and show effect if .waves-effect elem was found
  295. */
  296. function showEffect(e) {
  297. // Disable effect if element has "disabled" property on it
  298. // In some cases, the event is not triggered by the current element
  299. // if (e.target.getAttribute('disabled') !== null) {
  300. // return;
  301. // }
  302. var element = getWavesEffectElement(e);
  303. if (element !== null) {
  304. // Make it sure the element has either disabled property, disabled attribute or 'disabled' class
  305. if (element.disabled || element.getAttribute('disabled') || element.classList.contains('disabled')) {
  306. return;
  307. }
  308. TouchHandler.registerEvent(e);
  309. if (e.type === 'touchstart' && Effect.delay) {
  310. var hidden = false;
  311. var timer = setTimeout(function () {
  312. timer = null;
  313. Effect.show(e, element);
  314. }, Effect.delay);
  315. var hideEffect = function(hideEvent) {
  316. // if touch hasn't moved, and effect not yet started: start effect now
  317. if (timer) {
  318. clearTimeout(timer);
  319. timer = null;
  320. Effect.show(e, element);
  321. }
  322. if (!hidden) {
  323. hidden = true;
  324. Effect.hide(hideEvent, element);
  325. }
  326. removeListeners();
  327. };
  328. var touchMove = function(moveEvent) {
  329. if (timer) {
  330. clearTimeout(timer);
  331. timer = null;
  332. }
  333. hideEffect(moveEvent);
  334. removeListeners();
  335. };
  336. element.addEventListener('touchmove', touchMove, false);
  337. element.addEventListener('touchend', hideEffect, false);
  338. element.addEventListener('touchcancel', hideEffect, false);
  339. var removeListeners = function() {
  340. element.removeEventListener('touchmove', touchMove);
  341. element.removeEventListener('touchend', hideEffect);
  342. element.removeEventListener('touchcancel', hideEffect);
  343. };
  344. } else {
  345. Effect.show(e, element);
  346. if (isTouchAvailable) {
  347. element.addEventListener('touchend', Effect.hide, false);
  348. element.addEventListener('touchcancel', Effect.hide, false);
  349. }
  350. element.addEventListener('mouseup', Effect.hide, false);
  351. element.addEventListener('mouseleave', Effect.hide, false);
  352. }
  353. }
  354. }
  355. Waves.init = function(options) {
  356. var body = document.body;
  357. options = options || {};
  358. if ('duration' in options) {
  359. Effect.duration = options.duration;
  360. }
  361. if ('delay' in options) {
  362. Effect.delay = options.delay;
  363. }
  364. if (isTouchAvailable) {
  365. body.addEventListener('touchstart', showEffect, false);
  366. body.addEventListener('touchcancel', TouchHandler.registerEvent, false);
  367. body.addEventListener('touchend', TouchHandler.registerEvent, false);
  368. }
  369. body.addEventListener('mousedown', showEffect, false);
  370. };
  371. /**
  372. * Attach Waves to dynamically loaded inputs, or add .waves-effect and other
  373. * waves classes to a set of elements. Set drag to true if the ripple mouseover
  374. * or skimming effect should be applied to the elements.
  375. */
  376. Waves.attach = function(elements, classes) {
  377. elements = getWavesElements(elements);
  378. if (toString.call(classes) === '[object Array]') {
  379. classes = classes.join(' ');
  380. }
  381. classes = classes ? ' ' + classes : '';
  382. var element, tagName;
  383. for (var i = 0, len = elements.length; i < len; i++) {
  384. element = elements[i];
  385. tagName = element.tagName.toLowerCase();
  386. if (['input', 'img'].indexOf(tagName) !== -1) {
  387. TagWrapper[tagName](element);
  388. element = element.parentElement;
  389. }
  390. if (element.className.indexOf('waves-effect') === -1) {
  391. element.className += ' waves-effect' + classes;
  392. }
  393. }
  394. };
  395. /**
  396. * Cause a ripple to appear in an element via code.
  397. */
  398. Waves.ripple = function(elements, options) {
  399. elements = getWavesElements(elements);
  400. var elementsLen = elements.length;
  401. options = options || {};
  402. options.wait = options.wait || 0;
  403. options.position = options.position || null; // default = centre of element
  404. if (elementsLen) {
  405. var element, pos, off, centre = {}, i = 0;
  406. var mousedown = {
  407. type: 'mousedown',
  408. button: 1
  409. };
  410. var hideRipple = function(mouseup, element) {
  411. return function() {
  412. Effect.hide(mouseup, element);
  413. };
  414. };
  415. for (; i < elementsLen; i++) {
  416. element = elements[i];
  417. pos = options.position || {
  418. x: element.clientWidth / 2,
  419. y: element.clientHeight / 2
  420. };
  421. off = offset(element);
  422. centre.x = off.left + pos.x;
  423. centre.y = off.top + pos.y;
  424. mousedown.pageX = centre.x;
  425. mousedown.pageY = centre.y;
  426. Effect.show(mousedown, element);
  427. if (options.wait >= 0 && options.wait !== null) {
  428. var mouseup = {
  429. type: 'mouseup',
  430. button: 1
  431. };
  432. setTimeout(hideRipple(mouseup, element), options.wait);
  433. }
  434. }
  435. }
  436. };
  437. /**
  438. * Remove all ripples from an element.
  439. */
  440. Waves.calm = function(elements) {
  441. elements = getWavesElements(elements);
  442. var mouseup = {
  443. type: 'mouseup',
  444. button: 1
  445. };
  446. for (var i = 0, len = elements.length; i < len; i++) {
  447. Effect.hide(mouseup, elements[i]);
  448. }
  449. };
  450. /**
  451. * Deprecated API fallback
  452. */
  453. Waves.displayEffect = function(options) {
  454. console.error('Waves.displayEffect() has been deprecated and will be removed in future version. Please use Waves.init() to initialize Waves effect');
  455. Waves.init(options);
  456. };
  457. return Waves;
  458. });
  459. //Initialization
  460. Waves.attach('.btn:not(.btn-flat), .btn-floating', ['waves-light']);
  461. Waves.attach('.btn-flat', ['waves-effect']);
  462. Waves.attach('.chip', ['waves-effect']);
  463. Waves.attach('.view a .mask', ['waves-light']);
  464. Waves.attach('.waves-light', ['waves-light']);
  465. Waves.attach('.navbar-nav a:not(.navbar-brand), .nav-icons li a, .nav-tabs .nav-item:not(.dropdown)', ['waves-light']);
  466. Waves.attach('.pager li a', ['waves-light']);
  467. Waves.attach('.pagination .page-item .page-link', ['waves-effect']);
  468. Waves.init();