|
| 1 | +import {getHslColor} from "../utils/colors"; |
| 2 | + |
| 3 | +;(function (window) { |
| 4 | + 'use strict'; |
| 5 | + |
| 6 | + const Darken = Darken || {}; |
| 7 | + |
| 8 | + const Effect = { |
| 9 | + //click |
| 10 | + elements: [], |
| 11 | + // Effect delay |
| 12 | + duration: 1500, |
| 13 | + |
| 14 | + show: function (e, element) { |
| 15 | + |
| 16 | + // Disable right click |
| 17 | + if (e.button === 2) { |
| 18 | + return false; |
| 19 | + } |
| 20 | + |
| 21 | + const el = element || this; |
| 22 | + Effect.elements.push(el) |
| 23 | + |
| 24 | + const background = el.style.backgroundColor, |
| 25 | + border = el.style.borderColor, |
| 26 | + shadow = el.style.boxShadow |
| 27 | + |
| 28 | + let {h, s, l, a} = getHslColor(background) |
| 29 | + if (h === 0 && s === 0) { |
| 30 | + let hsla |
| 31 | + if (border) |
| 32 | + hsla = getHslColor(border) |
| 33 | + else |
| 34 | + hsla = {h: 0, s: 0, l: 0, a: 0} |
| 35 | + h = hsla.h |
| 36 | + s = hsla.s |
| 37 | + l = hsla.l |
| 38 | + a = hsla.a |
| 39 | + } |
| 40 | + el.style.background = `hsla(${h}, ${s - 5}%, ${l - 15}%, ${a})` |
| 41 | + el.style.borderColor = `hsla(${h + 2}, ${s - 10}%, ${l - 15}%, ${a})` |
| 42 | + el.style.boxShadow = `1px 0px 5px hsla(${h + 2}, ${s - 10}%, ${l - 15}%, ${a})`; |
| 43 | + |
| 44 | + setTimeout(() => { |
| 45 | + el.style.backgroundColor = background |
| 46 | + el.style.borderColor = border |
| 47 | + el.style.boxShadow = shadow |
| 48 | + const index = Effect.elements.findIndex(element => element === el) |
| 49 | + Effect.elements.splice(index, 1) |
| 50 | + }, Effect.duration) |
| 51 | + }, |
| 52 | + |
| 53 | + }; |
| 54 | + |
| 55 | + const TouchHandler = { |
| 56 | + touches: 0, |
| 57 | + allowEvent: function (e) { |
| 58 | + let allow = true; |
| 59 | + |
| 60 | + if (e.type === 'touchstart') { |
| 61 | + TouchHandler.touches += 1; |
| 62 | + } else if (e.type === 'touchend' || e.type === 'touchcancel') { |
| 63 | + setTimeout(function () { |
| 64 | + if (TouchHandler.touches > 0) { |
| 65 | + TouchHandler.touches -= 1; |
| 66 | + } |
| 67 | + }, Effect.duration); |
| 68 | + } else if (e.type === 'mousedown' && TouchHandler.touches > 0) { |
| 69 | + allow = false; |
| 70 | + } |
| 71 | + |
| 72 | + return allow; |
| 73 | + }, |
| 74 | + touchup: function (e) { |
| 75 | + TouchHandler.allowEvent(e); |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + function getWavesEffectElement(e) { |
| 80 | + if (TouchHandler.allowEvent(e) === false) { |
| 81 | + return null; |
| 82 | + } |
| 83 | + |
| 84 | + let element = null; |
| 85 | + let target = e.target || e.srcElement; |
| 86 | + |
| 87 | + while (target.parentNode !== null) { |
| 88 | + if (!(target instanceof SVGElement) && target.className.indexOf('darken-effect') !== -1) { |
| 89 | + element = target; |
| 90 | + break; |
| 91 | + } |
| 92 | + target = target.parentNode; |
| 93 | + } |
| 94 | + return element; |
| 95 | + } |
| 96 | + |
| 97 | + function showEffect(e) { |
| 98 | + const element = getWavesEffectElement(e); |
| 99 | + |
| 100 | + if (element !== null && Effect.elements.findIndex(el => el === element) === -1) { |
| 101 | + Effect.show(e, element); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + Darken.displayEffect = function (options) { |
| 106 | + options = options || {}; |
| 107 | + |
| 108 | + if ('duration' in options) { |
| 109 | + Effect.duration = options.duration; |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + if ('ontouchstart' in window) { |
| 114 | + document.body.addEventListener('touchstart', showEffect, false); |
| 115 | + } |
| 116 | + |
| 117 | + document.body.addEventListener('mousedown', showEffect, false); |
| 118 | + }; |
| 119 | + |
| 120 | + Darken.attach = function (element) { |
| 121 | + if ('ontouchstart' in window) { |
| 122 | + element.addEventListener('touchstart', showEffect, false); |
| 123 | + } |
| 124 | + |
| 125 | + element.addEventListener('mousedown', showEffect, false); |
| 126 | + }; |
| 127 | + |
| 128 | + window.Darken = Darken; |
| 129 | + |
| 130 | + document.addEventListener('DOMContentLoaded', function () { |
| 131 | + Darken.displayEffect(); |
| 132 | + }, false); |
| 133 | + |
| 134 | +})(window); |
0 commit comments