forked from Flyzie/Art-Portfolio-Maker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
179 lines (134 loc) · 5.73 KB
/
main.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//const content = document.createElement('div');
const label = document.createElement('h1');
const description = document.createElement('p');
const image = document.createElement('img');
const container = document.querySelector('.mainGrid');
const popupAdd = document.querySelector('.popupAdd');
const piece = document.querySelector('.piece-1')
const btn = document.querySelector('#btn');
const btnClose = document.querySelector('#btnClose')
const btnFinalize = document.querySelector('#popupFinalize');
const removeBtn = document.querySelector('#btnRemove');
const name = document.querySelector('#name');
const dsc = document.querySelector('#description');
const fileSelector = document.querySelector('#Artwork');
function removeDiv(id){
const divToRemove = document.querySelector('.piece-1[uid="' + id + '"]');
divToRemove.remove();
}
function generateId(){
return "id" + Math.random().toString(16).slice(2);
}
function assignId(element, id){
element.setAttribute('uid', id);
}
function createDiv(){
const content = document.createElement('div');
const label = document.createElement('h1');
const description = document.createElement('p');
const image = document.createElement('img');
const textBox = document.createElement('div');
const removeBtn = document.createElement('button');
const closeIcon = document.createElement('img');
const imageLinker = document.createElement('a')
const uniqueId = generateId();
imageLinker.id = 'image_linker';
imageLinker.href = 'artwork.html';
content.classList.add('piece-1');
removeBtn.classList.add('btnRemove');
closeIcon.src = "cross.png";
removeBtn.id = 'btnRemove';
textBox.classList.add('textBox');
label.innerHTML = document.getElementById('name').value;
description.innerHTML = document.getElementById('description').value;
image.setAttribute('src', URL.createObjectURL(fileSelector.files[0]));
removeBtn.appendChild(closeIcon);
textBox.appendChild(label);
textBox.appendChild(description);
imageLinker.appendChild(image);
content.appendChild(imageLinker);
content.appendChild(removeBtn);
content.appendChild(textBox);
assignId(removeBtn, uniqueId);
assignId(content, uniqueId);
removeBtn.addEventListener('click', function() {
removeDiv(uniqueId);
});
container.appendChild(content);
popupAdd.classList.remove('open');
}
function createDivFromData(data) {
const content = document.createElement('div');
const label = document.createElement('h1');
const description = document.createElement('p');
const image = document.createElement('img');
const textBox = document.createElement('div');
const removeBtn = document.createElement('button');
const closeIcon = document.createElement('img');
const imageLinker = document.createElement('a');
imageLinker.id = 'image_linker';
imageLinker.href = 'artwork.html';
content.classList.add('piece-1');
removeBtn.classList.add('btnRemove');
closeIcon.src = "cross.png";
removeBtn.id = 'btnRemove';
textBox.classList.add('textBox');
label.innerHTML = data.title;
description.innerHTML = data.description;
image.setAttribute('src', data.image);
removeBtn.appendChild(closeIcon);
textBox.appendChild(label);
textBox.appendChild(description);
imageLinker.appendChild(image);
content.appendChild(imageLinker);
content.appendChild(removeBtn);
content.appendChild(textBox);
assignId(removeBtn, data.uid);
assignId(content, data.uid);
removeBtn.addEventListener('click', function () {
removeDiv(data.uid);
});
container.appendChild(content);
popupAdd.classList.remove('open');
}
// Function to load data to sessionStorage
function loadToJSON(name, description) {
const newData = {
uid: generateId(),
title: name,
description: description,
image: URL.createObjectURL(fileSelector.files[0])
};
// Get existing data from sessionStorage
const existingData = JSON.parse(sessionStorage.getItem('artworkData')) || [];
// Add new data to the existing data
existingData.push(newData);
// Save the updated data back to sessionStorage
sessionStorage.setItem('artworkData', JSON.stringify(existingData));
// Call the function to create a div from the new data
createDivFromData(newData);
}
// ... (Your existing code)
// Add this block of code to retrieve and display existing data during page load
window.onload = function () {
const existingData = JSON.parse(sessionStorage.getItem('artworkData')) || [];
existingData.forEach((data) => {
createDivFromData(data);
});
};
removeBtn.addEventListener('click', ()=> {
if(removeBtn.getAttribute('uid') == piece.getAttribute('uid')){
removeDiv(removeBtn.getAttribute('uid'));
}else{
console.log('error');
}
});
btn.addEventListener('click', () => {
popupAdd.classList.add('open');
});
btnClose.addEventListener('click', () => {
popupAdd.classList.remove('open');
});
btnFinalize.addEventListener('click', () => {
loadToJSON(name.value, dsc.value);
});