-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathinventory.js
More file actions
105 lines (90 loc) · 3.64 KB
/
Copy pathinventory.js
File metadata and controls
105 lines (90 loc) · 3.64 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
// Smartest not to use!!!
import { safeHtml } from '../../core/packages/dompurify.js';
export function createInventoryOverlay(options) {
const { title, stat, rolimonsUrl, onSearch, onLoadMore } = options;
let isLoading = false;
const overlay = document.createElement('div');
overlay.className = 'rovalra-inventory-overlay';
overlay.style.display = 'none';
const rolimonsLink = rolimonsUrl
? `<a href="${rolimonsUrl}" target="_blank" rel="noopener noreferrer" class="rovalra-rolimons-link">
<div class="rovalra-tooltip">Open in Rolimon's</div>
<svg focusable="false" aria-hidden="true" viewBox="0 0 24 24"><path d="M19 19H5V5h7V3H5c-1.11 0-2 .9-2 2v14c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2v-7h-2zM14 3v2h3.59l-9.83 9.83 1.41 1.41L19 6.41V10h2V3z"></path></svg>
</a>`
: '';
overlay.innerHTML = safeHtml`
<div class="rovalra-inventory-content">
<div class="rovalra-inventory-header">
<div class="rovalra-inventory-header-main">
<h3>${title} <span class="total-rap">(${stat || ''})</span></h3>
${rolimonsLink}
</div>
<div class="rovalra-inventory-search-container">
<input type="text" class="rovalra-inventory-search" placeholder="Search by item name...">
</div>
<button class="rovalra-inventory-close">×</button>
</div>
<div class="rovalra-inventory-list padding-large overflow-y-auto flex-grow"></div>
</div>
`;
const itemListEl = overlay.querySelector('.rovalra-inventory-list');
const searchInput = overlay.querySelector('.rovalra-inventory-search');
const show = () => {
document.body.appendChild(overlay);
overlay.style.display = 'flex';
document.body.style.overflow = 'hidden';
};
const close = () => {
overlay.remove();
document.body.style.overflow = '';
};
overlay.querySelector('.rovalra-inventory-close').addEventListener('click', close);
overlay.addEventListener('click', (e) => {
if (e.target === overlay) close();
});
if (onSearch) {
searchInput.addEventListener('input', () => onSearch(searchInput.value));
}
if (onLoadMore) {
itemListEl.addEventListener('scroll', () => {
const isNearBottom = itemListEl.scrollTop + itemListEl.clientHeight >= itemListEl.scrollHeight - 250;
if (isNearBottom && !isLoading) {
onLoadMore();
}
});
}
const addItems = (items, thumbnailCache, itemConfig) => {
const fragment = document.createDocumentFragment();
items.forEach(item => {
const card = createItemCard(item, thumbnailCache, itemConfig);
fragment.appendChild(card);
});
itemListEl.appendChild(fragment);
isLoading = false;
};
const clearItems = () => {
itemListEl.innerHTML = '';
};
const setLoading = (message = 'Loading...') => {
isLoading = true;
const loadingEl = document.createElement('p');
loadingEl.className = 'rovalra-inventory-message';
loadingEl.innerText = message;
itemListEl.appendChild(loadingEl);
};
const setEmpty = (message = 'No items found.') => {
clearItems();
const emptyEl = document.createElement('p');
emptyEl.className = 'rovalra-inventory-message';
emptyEl.innerText = message;
itemListEl.appendChild(emptyEl);
};
return {
show,
close,
addItems,
clearItems,
setLoading,
setEmpty,
};
}