|
1 |
| -import observer from '@cocreate/observer'; |
| 1 | +import observer from "@cocreate/observer"; |
2 | 2 |
|
3 | 3 | function init() {
|
4 |
| - let elements = document.querySelectorAll("[background-color='random']"); |
5 |
| - initElements(elements); |
| 4 | + let elements = document.querySelectorAll("[background-color='random']"); |
| 5 | + initElements(elements); |
6 | 6 | }
|
7 | 7 |
|
8 | 8 | function initElements(elements) {
|
9 |
| - for (let el of elements) { |
10 |
| - el.style.backgroundColor = randomColor(); |
11 |
| - } |
| 9 | + for (let el of elements) { |
| 10 | + el.style.backgroundColor = randomColor(); |
| 11 | + } |
12 | 12 | }
|
13 | 13 |
|
14 | 14 | export function randomColor() {
|
15 |
| - let defaultSaturation = 75; |
16 |
| - let defaultLightness = 58; |
17 |
| - let defaultAlpha = 1; |
18 |
| - let hash = Math.floor(Math.random() * (360)); |
19 |
| - return 'hsla(' + hash + ', ' + defaultSaturation + '%, ' + defaultLightness + '%, ' + defaultAlpha + ')'; |
| 15 | + let defaultSaturation = 75; |
| 16 | + let defaultLightness = 58; |
| 17 | + let defaultAlpha = 1; |
| 18 | + let hash = Math.floor(Math.random() * 360); |
| 19 | + return ( |
| 20 | + "hsla(" + |
| 21 | + hash + |
| 22 | + ", " + |
| 23 | + defaultSaturation + |
| 24 | + "%, " + |
| 25 | + defaultLightness + |
| 26 | + "%, " + |
| 27 | + defaultAlpha + |
| 28 | + ")" |
| 29 | + ); |
20 | 30 | }
|
21 | 31 |
|
22 | 32 | observer.init({
|
23 |
| - name: 'CoCreateRandomColor', |
24 |
| - observe: ['addedNodes'], |
25 |
| - selector: '[background-color="random"]', |
26 |
| - callback: mutation => { |
27 |
| - init(mutation.target); |
28 |
| - } |
| 33 | + name: "CoCreateRandomColor", |
| 34 | + types: ["addedNodes"], |
| 35 | + selector: '[background-color="random"]', |
| 36 | + callback: (mutation) => { |
| 37 | + init(mutation.target); |
| 38 | + } |
29 | 39 | });
|
30 | 40 |
|
31 |
| - |
32 | 41 | init();
|
33 | 42 |
|
34 |
| - |
35 | 43 | export default { randomColor };
|
0 commit comments