-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
94 lines (67 loc) · 2.2 KB
/
app.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
/***********************************************************
What_is_your_tag? v 1.1.0
Made by Wonkook Lee (oneook)
© All Rights Reserved
************************************************************/
"use strict"
const dataModule = (function() {
const DOM = {
addTag: document.querySelector('.addTag'),
container: document.querySelector('.container'),
tagContainer: document.querySelector('.container__tag'),
tags: document.querySelectorAll('.tag'),
};
return {
getDOM() {
return DOM;
},
}
})();
const controller = (function() {
const tagHTML = function (keyword, [R, G, B]) {
return `<article style="background-color: rgba(${R}, ${G}, ${B}, 0.7);" class="tag" data-keyword="${keyword}">#${keyword}</article>`
};
const randomRGB = function(min, max) {
return Math.floor(Math.random() * (max - min) + 1) + min;
}
const getRandomRGB = function(min, max) {
let arrRGB = [];
arrRGB.push(randomRGB(min, max));
arrRGB.push(randomRGB(min, max));
arrRGB.push(randomRGB(min, max));
return arrRGB;
}
return {
searchByTag(keyword) {
fetch(`https://source.unsplash.com/featured/?${keyword.toLowerCase()}`)
.then((response) => {
document.body.style.backgroundImage = `url(${response.url})`;
});
},
addNewTag(target, parentNode) {
const newColor = getRandomRGB(180, 230);
const newHTML = tagHTML(target.value, newColor);
parentNode.insertAdjacentHTML('beforeend', newHTML);
this.searchByTag(target.value);
target.value = '';
target.focus();
},
}
})();
const UIController = (function() {
const DOM = dataModule.getDOM();
DOM.tagContainer.addEventListener('click', event => {
if (!event.target.classList.contains('tag')) return;
const keyword = event.target.dataset.keyword;
controller.searchByTag(keyword)
});
DOM.addTag.addEventListener('keydown', e => {
if (e.key === 'Enter') {
controller.addNewTag(e.target, DOM.tagContainer);
}
})
DOM.addTag.focus();
})();
const msg = "%cWonkook Lee ⓒ oneook";
const css = "font-size: 2em; color: #FEDC45; background-color: #000;font-family: 'Libre Baskerville', serif;";
console.log(msg, css);