|
1 | | -/** |
2 | | - * @see https://unsplash.com/ |
3 | | - */ |
4 | | -class UnsplashImage { |
5 | | - |
6 | | - constructor(imageID, authorID, authorName) { |
7 | | - |
8 | | - this.imageID = imageID |
9 | | - this.imageURL = `https://source.unsplash.com/${imageID}/1920x1080` |
10 | | - this.authorID = authorID |
11 | | - this.authorName = authorName |
12 | | - this.authorURL = `https://unsplash.com/@${authorID}` |
13 | | - |
14 | | - } |
15 | | - |
16 | | - static getRandom() { |
17 | | - |
18 | | - const images = [ |
19 | | - new UnsplashImage('pVq6YhmDPtk', 'danranwanghao', 'hao wang'), |
20 | | - new UnsplashImage('E8Ufcyxz514', 'fakurian', 'Fakurian Design'), |
21 | | - new UnsplashImage('PGdW_bHDbpI', 'fakurian', 'Fakurian Design'), |
22 | | - new UnsplashImage('26WixHTutxc', 'gradienta', 'Gradienta'), |
23 | | - new UnsplashImage('u8Jn2rzYIps', 'fakurian', 'Fakurian Design'), |
24 | | - new UnsplashImage('FBQcPsBL2Zo', 'fakurian', 'Fakurian Design'), |
25 | | - new UnsplashImage('Hlkuojv_P6I', 'enginakyurt', 'engin akyurt'), |
26 | | - new UnsplashImage('YWIOwHvRBvU', 'pawel_czerwinski', 'Pawel Czerwinski') |
27 | | - ] |
28 | | - // Select a random image from the list above. |
29 | | - const image = images[Math.floor(Math.random() * images.length)] |
30 | | - |
31 | | - return image |
32 | | - |
33 | | - } |
34 | | - |
35 | | -} |
36 | | - |
37 | 1 | /** |
38 | 2 | * MongoDB PHP GUI login. |
39 | 3 | */ |
40 | 4 | class Login { |
41 | 5 |
|
42 | 6 | constructor() { |
43 | 7 |
|
44 | | - this.background = document.getElementById('mpg-background') |
45 | | - this.backgroundCreditLink = this.background.querySelector('.credit-link') |
46 | 8 | this.cardsContainer = document.getElementById('mpg-cards') |
47 | 9 | this.flipCardButtons = document.querySelectorAll('.mpg-flip-card-button') |
48 | 10 | this.requiredInputs = document.querySelectorAll('input[required]') |
49 | 11 | this.forms = document.querySelectorAll('form') |
50 | 12 |
|
51 | 13 | } |
52 | 14 |
|
53 | | - /** |
54 | | - * Defines background. |
55 | | - */ |
56 | | - setBackground() { |
57 | | - |
58 | | - const randomImage = UnsplashImage.getRandom() |
59 | | - const abortController = new AbortController() |
60 | | - |
61 | | - // We will abort fetch request if it takes more than one second. |
62 | | - const timeoutID = setTimeout(() => abortController.abort(), 1000) |
63 | | - |
64 | | - fetch(randomImage.imageURL, { signal: abortController.signal }) |
65 | | - .then(response => { |
66 | | - clearTimeout(timeoutID) |
67 | | - return response.blob() |
68 | | - }) |
69 | | - .then(blob => { |
70 | | - const blobURL = URL.createObjectURL(blob) |
71 | | - this.background.style.backgroundImage = `url(${blobURL})` |
72 | | - this.backgroundCreditLink.textContent = `Image by ${randomImage.authorName}` |
73 | | - this.backgroundCreditLink.href = randomImage.authorURL |
74 | | - this.cardsContainer.classList.add('reveal') |
75 | | - }) |
76 | | - .catch(_error => { |
77 | | - console.warn('Failed to fetch unsplash.com. Fallback to local image.') |
78 | | - this.background.classList.add('fallback') |
79 | | - this.cardsContainer.classList.add('reveal') |
80 | | - }) |
81 | | - |
82 | | - } |
83 | | - |
84 | 15 | /** |
85 | 16 | * Adds an event listener on each "Flip card" button. |
86 | 17 | */ |
@@ -138,7 +69,6 @@ class Login { |
138 | 69 |
|
139 | 70 | const login = new Login() |
140 | 71 |
|
141 | | - login.setBackground() |
142 | 72 | login.listenFlipCardButtons() |
143 | 73 | login.listenRequiredInputs() |
144 | 74 | login.listenForms() |
|
0 commit comments