-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
138 lines (111 loc) · 4.71 KB
/
Copy pathscript.js
File metadata and controls
138 lines (111 loc) · 4.71 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
async function getList() {
const response = await fetch('https://jsonplaceholder.typicode.com/todos');
const list = await response.json();
return list;
}
(async function () {
const ulMenu = document.body.appendChild(document.createElement('ul'));
ulMenu.style.display = 'none';
ulMenu.classList.add('menu');
const ulEl = document.createElement('ul');
const list = await getList();
createHtml(list);
ulEl.addEventListener('click', deleteItem);
document.getElementById('butt-elem').addEventListener('click', addText);
ulEl.addEventListener('click', showMenu);
function createHtml(arrList) {
const divEl = document.body.appendChild(document.createElement('div'));
divEl.classList.add('wrapper');
const inputEl = divEl.appendChild(document.createElement('input'));
inputEl.setAttribute('id', 'input-elem');
inputEl.classList.add('input-elem');
const buttonEl = divEl.appendChild(document.createElement('button'));
buttonEl.setAttribute('id', 'butt-elem');
buttonEl.innerText = 'add';
buttonEl.classList.add('add')
divEl.appendChild(ulEl);
ulEl.setAttribute('id', 'ul-elem');
createList(arrList);
createMenu();
}
function createList(arrList) {
const fragment = new DocumentFragment();
for (let i = 0; i < arrList.length; i++) {
const spanEl = document.createElement('span');
spanEl.innerText = arrList[i].title;
const liEl = document.createElement('li');
liEl.appendChild(spanEl);
buttonList = document.createElement('button');
buttonList.innerText = '...';
stringWrapper = document.createElement('div');
stringWrapper.classList.add('string-wrapper');
stringWrapper.appendChild(liEl);
stringWrapper.appendChild(buttonList);
fragment.appendChild(stringWrapper);
}
ulEl.appendChild(fragment);
}
function createMenu() {
const options = [];
const optionNames = ['change', 'save'];
optionNames.forEach(item => {
const option = document.createElement('li');
option.classList.add(item);
option.innerText = item;
options.push(option);
})
ulMenu.append(...options);
document.body.appendChild(ulMenu);
}
function deleteItem(event) {
if (event.target.tagName === 'SPAN') {
const divEl = event.target.parentNode.parentNode.parentNode
divEl.removeChild(event.target.parentNode.parentNode);
}
}
function addText() {
const inputEl = document.getElementById('input-elem');
if (inputEl.value.length === 0) {
alert('Please fill in the field');
};
const array = [];
const obj = {};
obj.title = `${inputEl.value}`;
array.push(obj);
createList(array)
inputEl.value = '';
}
function showMenu(event) {
const target = event.target;
if (event.target.tagName === 'BUTTON') {
if (ulMenu.style.display === 'none') {
const coord = target.getBoundingClientRect();
ulMenu.style.cssText = `display:block; top: ${coord.bottom}px; left: ${coord.left}px`;
} else {
ulMenu.style.cssText = `display:none`;
}
ulMenu.onclick = function doChangeOrAddOrSave(event) {
if (event.target.className === 'change') {
const inputEl = document.createElement('input');
inputEl.value = target.previousElementSibling.innerText;
const coord = target.previousElementSibling.getBoundingClientRect();
inputEl.style.width = `${coord.width}px`;
const divEl = target.previousElementSibling.parentNode;
divEl.insertBefore(inputEl, target);
target.parentNode.firstElementChild.parentNode.removeChild(target.parentNode.firstElementChild);
}
if (event.target.className === 'save') {
const liEl = document.createElement('li');
spanEl = liEl.appendChild(document.createElement('span'));
spanEl.innerText = inputEl.value;
liEl.style.width = "auto";
const divEl = target.previousElementSibling.parentNode;
divEl.insertBefore(liEl, target);
target.parentNode.firstElementChild.parentNode.removeChild(target.parentNode.firstElementChild);
ulMenu.style.display = "none";
}
}
}
}
}
)();