-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitems.js
252 lines (197 loc) · 8.96 KB
/
items.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
function createItemsList(classification) {
const listContainer = document.createElement('div');
const itemsListHeader = document.createElement('h4');
itemsListHeader.setAttribute('class', 'items-list-header');
itemsListHeader.setAttribute('id', `${classification}-items`);
const capitalizeListName = classification.charAt(0).toUpperCase() + classification.slice(1);
itemsListHeader.textContent = capitalizeListName;
listContainer.appendChild(itemsListHeader);
const icon = getListHeaderIcon(classification);
itemsListHeader.prepend(icon);
const itemsList = document.createElement('ul');
const listItems = items.filter(item => item?.type === classification);
listItems.forEach(item => {
const itemsLi = document.createElement('li');
const itemAnchor = document.createElement('a');
itemAnchor.setAttribute('href', `#${item.id}`);
itemAnchor.setAttribute('class', 'list-item');
itemAnchor.textContent = item.name;
itemAnchor.addEventListener('click', (event) => {
const itemCard = createItemCard(event.target.textContent);
const cardContainer = document.getElementById('items-card-container');
const instructionCard = document.querySelector('.instruction-card');
if (instructionCard) {
instructionCard.remove();
} else {
const currentItemCard = document.querySelector('.item-card');
currentItemCard.remove();
}
cardContainer.appendChild(itemCard);
});
itemsLi.appendChild(itemAnchor);
itemsList.appendChild(itemsLi);
});
listContainer.appendChild(itemsList);
return listContainer;
}
(function addItems() {
const itemsContainer = document.getElementById('items-container');
const listsWrapper = document.createElement('div');
listsWrapper.setAttribute('class', 'lists');
const treasureList = createItemsList('treasure');
listsWrapper.appendChild(treasureList);
const artifactList = createItemsList('artifact');
listsWrapper.appendChild(artifactList);
const itemsList = createItemsList('equipment');
listsWrapper.appendChild(itemsList);
itemsContainer.appendChild(listsWrapper);
})();
function createInstructionCard() {
const instructionCard = document.createElement('div');
instructionCard.setAttribute('class', 'instruction-card');
const instructionCardHeader = document.createElement('h3');
instructionCardHeader.textContent = 'Welcome to the Items Library';
instructionCard.appendChild(instructionCardHeader);
const instructionCardP = document.createElement('p');
instructionCardP.textContent = 'Select an item from the items list to reveal it\'s power and expand your understanding of it\'s capabilities... or to simply refresh your weary heroes memory.';
instructionCard.appendChild(instructionCardP);
return instructionCard;
}
(function displayInstructionCard() {
const itemsCardContainer = document.getElementById('items-card-container');
const instructionCard = createInstructionCard();
itemsCardContainer.appendChild(instructionCard);
})();
function getListHeaderIcon(classification) {
const icon = document.createElement('img');
icon.setAttribute('class', 'items-header-icon');
switch (classification) {
case 'treasure':
icon.setAttribute('src', 'images/treasure-icon-blk.png');
icon.setAttribute('alt', 'Treasure chest');
break;
case 'artifact':
icon.setAttribute('src', 'images/artifact-icon-blk.png');
icon.setAttribute('alt', 'Hands holding up a vase');
break;
case 'equipment':
icon.setAttribute('src', 'images/equipment-icon-blk.png');
icon.setAttribute('alt', 'A shield with a pair of crossed swords behind it');
}
return icon;
}
function createItemCard(itemName) {
const item = items.find(item => item.name === itemName);
const itemCard = document.createElement('div');
itemCard.setAttribute('class', 'item-card');
const cardHeader = document.createElement('h3');
cardHeader.textContent = item.name;
itemCard.appendChild(cardHeader);
const cardImage = document.createElement('img');
cardImage.setAttribute('src', `images/${item.image}`);
cardImage.setAttribute('alt', item.imageDescription);
cardImage.setAttribute('class', 'item-card-image');
itemCard.appendChild(cardImage);
const cardDescription = document.createElement('p');
cardDescription.textContent = item.description;
itemCard.appendChild(cardDescription);
if (!item.cost) {
return itemCard;
}
const itemCost = document.createElement('span');
itemCost.setAttribute('class', 'item-card-cost');
const importantText = document.createElement('strong');
importantText.textContent = 'Cost: ';
itemCost.append(importantText, item.cost);
itemCard.appendChild(itemCost);
return itemCard;
}
(function searchItems() {
const searchInput = document.querySelector('[data-search]');
const treasureItems = document.querySelector('#treasure-items + ul').querySelectorAll('a');
const artifactItems = document.querySelector('#artifact-items + ul').querySelectorAll('a');
const equipmentItems = document.querySelector('#equipment-items + ul').querySelectorAll('a');
const itemsListElements = [treasureItems, artifactItems, equipmentItems];
const treasureItemsHeader = document.getElementById('treasure-items');
const artifactItemsHeader = document.getElementById('artifact-items')
const equipmentItemsHeader = document.getElementById('equipment-items');
searchInput.addEventListener('input', (e) => {
const value = e.target.value.toLowerCase();
itemsListElements.forEach(itemsList => {
let allHidden = true;
itemsList.forEach(item => {
const lowerCaseItemName = item.innerText.toLowerCase();
const isVisible = lowerCaseItemName.includes(value);
item.classList.toggle('hide', !isVisible);
if (isVisible) {
allHidden = false;
}
});
switch (itemsListElements.indexOf(itemsList)) {
case 0:
treasureItemsHeader.classList.toggle('hide', allHidden);
break;
case 1:
artifactItemsHeader.classList.toggle('hide', allHidden);
break;
case 2:
equipmentItemsHeader.classList.toggle('hide', allHidden);
break;
}
});
});
})();
(function createItemsMobileDropdownUi() {
const itemsDropdown = document.getElementById('items-dropdown');
const itemsLabel = document.createElement('label');
itemsLabel.setAttribute('for', 'items-menu');
itemsLabel.textContent = 'HeroQuest Items List';
itemsDropdown.appendChild(itemsLabel);
const itemsMenu = document.createElement('select');
itemsMenu.setAttribute('name', 'items-menu');
itemsMenu.setAttribute('id', 'items-menu');
itemsDropdown.appendChild(itemsMenu);
const defaultOption = document.createElement('option');
defaultOption.setAttribute('value', '');
defaultOption.textContent = '--Please Choose an Item--';
itemsMenu.appendChild(defaultOption);
const treasure = document.createElement('optgroup');
treasure.setAttribute('label', 'Treasure');
itemsMenu.appendChild(treasure);
const artifacts = document.createElement('optgroup');
artifacts.setAttribute('label', 'Artifacts');
itemsMenu.appendChild(artifacts);
const equipment = document.createElement('optgroup');
equipment.setAttribute('label', 'Equipment');
itemsMenu.appendChild(equipment);
items.forEach( item => {
const menuItem = document.createElement('option');
menuItem.setAttribute('value', item.id);
menuItem.textContent = item.name;
switch (item.type) {
case 'treasure':
treasure.appendChild(menuItem);
break;
case 'artifact':
artifacts.appendChild(menuItem);
break;
case 'equipment':
equipment.appendChild(menuItem);
break;
}
});
itemsMenu.addEventListener('change', event => {
const SelectedItemId = event.target.value;
const selectedItem = items.find( item => item.id === SelectedItemId);
const itemCard = createItemCard(selectedItem.name);
const cardContainer = document.getElementById('items-card-container');
const instructionCard = document.querySelector('.instruction-card');
if (instructionCard) {
instructionCard.remove();
} else {
const currentItemCard = document.querySelector('.item-card');
currentItemCard.remove();
}
cardContainer.appendChild(itemCard);
});
})();