-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdepot-browser.js
More file actions
174 lines (152 loc) · 4.83 KB
/
depot-browser.js
File metadata and controls
174 lines (152 loc) · 4.83 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
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
/**
* Public Depot Browser - file browser component for content pages
*/
const previewTypes = {
image: new Set(['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg']),
video: new Set(['mp4', 'webm', 'ogg']),
audio: new Set(['mp3', 'wav']),
pdf: new Set(['pdf']),
};
function isPreviewable(ext) {
return Object.values(previewTypes).some(s => s.has(ext));
}
function createPreviewElement(ext, url, name) {
if (previewTypes.image.has(ext)) {
const img = document.createElement("img");
img.src = url;
img.alt = name;
return img;
}
if (previewTypes.video.has(ext)) {
const video = document.createElement("video");
video.src = url;
video.controls = true;
video.autoplay = true;
return video;
}
if (previewTypes.audio.has(ext)) {
const audio = document.createElement("audio");
audio.src = url;
audio.controls = true;
audio.autoplay = true;
return audio;
}
if (previewTypes.pdf.has(ext)) {
const obj = document.createElement("object");
obj.data = url;
obj.type = "application/pdf";
obj.className = "preview-pdf";
return obj;
}
return null;
}
function applyStripes(container) {
const tree = container.querySelector(".depot-browser-tree");
if (!tree) return;
let index = 0;
const walk = (ul) => {
for (const li of ul.children) {
if (li.tagName !== "LI") continue;
if (li.classList.contains("filtered-out")) {
li.classList.remove("stripe");
continue;
}
li.classList.toggle("stripe", index % 2 === 1);
index++;
// Only walk into open folder contents
const details = li.querySelector(":scope > details");
if (details?.open) {
const nested = details.querySelector(":scope > .depot-browser-tree");
if (nested) walk(nested);
}
}
};
walk(tree);
}
function filterBrowser(container, input) {
const query = input.value.toLowerCase();
const tree = container.querySelector(".depot-browser-tree");
const allLi = tree.querySelectorAll("li");
if (query.length === 0) {
allLi.forEach(li => li.classList.remove("filtered-out"));
applyStripes(container);
return;
}
// First pass: filter file items by name, comments, and tags
allLi.forEach(li => {
if (li.querySelector("details")) return; // folder
const fileEl = li.querySelector(".file");
if (!fileEl) return;
let text = fileEl.textContent;
const comments = li.querySelector(".file-comments");
if (comments) text += ' ' + comments.textContent;
const tags = li.querySelector(".file-tags");
if (tags) text += ' ' + tags.textContent;
const match = text.toLowerCase().includes(query);
li.classList.toggle("filtered-out", !match);
});
// Second pass: filter folders based on visible children
const filterFolders = (ul) => {
for (const li of ul.children) {
if (li.tagName !== "LI" || !li.querySelector("details")) continue;
const contents = li.querySelector(".depot-browser-tree");
if (contents) filterFolders(contents);
const hasVisible = contents && Array.from(contents.children).some(
child => child.tagName === "LI" && !child.classList.contains("filtered-out")
);
li.classList.toggle("filtered-out", !hasVisible);
if (hasVisible) {
li.querySelector("details")?.setAttribute("open", "");
}
}
};
filterFolders(tree);
applyStripes(container);
}
function initBrowser(container) {
const options = JSON.parse(container.dataset.settings || "{}");
const filterInput = container.querySelector(".depot-browser-filter input");
const dialog = container.querySelector(".depot-browser-preview");
const preview = dialog?.querySelector(".preview-content");
// Alternating row stripes
applyStripes(container);
// Re-stripe when folders are toggled
container.querySelectorAll("details").forEach(details => {
details.addEventListener("toggle", () => applyStripes(container));
});
// Filter — "input" for typing, "search" for native clear button
if (filterInput) {
const onFilter = () => filterBrowser(container, filterInput);
filterInput.addEventListener("input", onFilter);
filterInput.addEventListener("search", onFilter);
}
// Preview
if (options.preview && dialog) {
container.querySelectorAll(".action-preview").forEach(btn => {
const item = btn.closest(".depot-browser-item");
const ext = item?.dataset.ext;
if (!ext || !isPreviewable(ext)) {
btn.remove();
return;
}
btn.addEventListener("click", () => {
const url = item.dataset.streamUrl;
const name = item.querySelector(".file")?.textContent || "";
preview.innerHTML = "";
const el = createPreviewElement(ext, url, name);
if (!el) return;
preview.appendChild(el);
dialog.showModal();
});
});
dialog.addEventListener("close", () => {
preview.innerHTML = "";
});
dialog.addEventListener("click", (e) => {
if (e.target === dialog) dialog.close();
});
}
}
export default function initDepotBrowsers() {
document.querySelectorAll(".cms-depot-browser").forEach(initBrowser);
}