|
1 | | -const App = { |
2 | | - init() { |
3 | | - this.instanceEl = document.querySelector('input[name="instance"]'); |
4 | | - if (!this.instanceEl) return; |
5 | | - |
6 | | - this.searchEl = document.querySelector('input[name="search"]'); |
7 | | - this.configsEl = document.querySelector("#configs"); |
8 | | - this.cards = this.configsEl.querySelectorAll(".card"); |
9 | | - this.bindEvents(); |
10 | | - this.updateInstanceUrl(); |
11 | | - this.initDynamicForms(); |
12 | | - }, |
13 | | - |
14 | | - bindEvents() { |
15 | | - this.searchEl.addEventListener("input", this.filterConfigs.bind(this)); |
16 | | - this.configsEl.addEventListener("click", (event) => { |
17 | | - const { bindClick } = event.target.dataset; |
18 | | - if (bindClick === "show") { |
19 | | - this.handleShowClick(event); |
20 | | - } else if (bindClick === "copy") { |
21 | | - this.handleCopyClick(event); |
| 1 | +document.addEventListener("alpine:init", () => { |
| 2 | + Alpine.data("feedDirectory", () => ({ |
| 3 | + instanceUrl: "https://html2rss.herokuapp.com/", |
| 4 | + searchQuery: "", |
| 5 | + configs: window.feedDirectoryData, |
| 6 | + |
| 7 | + filterConfig(configName) { |
| 8 | + if (!this.searchQuery) { |
| 9 | + return true; |
22 | 10 | } |
23 | | - }); |
24 | | - |
25 | | - this.instanceEl.addEventListener("blur", this.updateInstanceUrl.bind(this)); |
26 | | - }, |
27 | | - |
28 | | - filterConfigs() { |
29 | | - const query = this.searchEl.value.toLowerCase(); |
30 | | - this.cards.forEach((card) => { |
31 | | - const title = card.querySelector(".card-title").innerText.toLowerCase(); |
32 | | - if (title.includes(query)) { |
33 | | - card.style.display = ""; |
34 | | - } else { |
35 | | - card.style.display = "none"; |
| 11 | + return configName.toLowerCase().includes(this.searchQuery.toLowerCase()); |
| 12 | + }, |
| 13 | + |
| 14 | + getFeedUrl(config, params = {}) { |
| 15 | + let url = this.instanceUrl.endsWith("/") |
| 16 | + ? this.instanceUrl |
| 17 | + : `${this.instanceUrl}/`; |
| 18 | + url += `${config.domain}/${config.name}.rss`; |
| 19 | + |
| 20 | + const queryParams = new URLSearchParams(); |
| 21 | + if (Array.isArray(config.url_parameters)) { |
| 22 | + config.url_parameters.forEach((param) => { |
| 23 | + const key = param[0]; |
| 24 | + if (params[key]) { |
| 25 | + queryParams.append(key, params[key]); |
| 26 | + } |
| 27 | + }); |
36 | 28 | } |
37 | | - }); |
38 | | - }, |
39 | | - |
40 | | - getInstanceUrl() { |
41 | | - const url = this.instanceEl.value; |
42 | | - return url.endsWith("/") ? url : `${url}/`; |
43 | | - }, |
44 | 29 |
|
45 | | - updateInstanceUrl() { |
46 | | - const url = this.getInstanceUrl(); |
47 | | - document.querySelectorAll(".instance").forEach((el) => { |
48 | | - if (el instanceof HTMLElement) { |
49 | | - el.innerText = url; |
| 30 | + const queryString = queryParams.toString(); |
| 31 | + if (queryString) { |
| 32 | + url += `?${queryString}`; |
50 | 33 | } |
51 | | - }); |
52 | | - }, |
53 | | - |
54 | | - handleShowClick(event) { |
55 | | - const { target } = event; |
56 | | - const path = this.getPath(target); |
57 | | - if (path) { |
58 | | - target.href = `${this.getInstanceUrl()}${path}`; |
59 | | - } else { |
60 | | - event.preventDefault(); |
61 | | - } |
62 | | - }, |
63 | | - |
64 | | - async handleCopyClick(event) { |
65 | | - const { target } = event; |
66 | | - const path = this.getPath(target); |
67 | | - if (!path) return; |
68 | | - |
69 | | - const href = `${this.getInstanceUrl()}${path}`; |
70 | | - |
71 | | - try { |
72 | | - await navigator.clipboard.writeText(href); |
73 | | - this.showCopiedState(target); |
74 | | - } catch (err) { |
75 | | - console.error("Failed to copy: ", err); |
76 | | - } |
77 | | - }, |
78 | | - |
79 | | - showCopiedState(triggerEl) { |
80 | | - triggerEl.classList.add("copied"); |
81 | | - setTimeout(() => { |
82 | | - triggerEl.classList.remove("copied"); |
83 | | - triggerEl.blur(); |
84 | | - }, 1000); |
85 | | - }, |
86 | | - |
87 | | - initDynamicForms() { |
88 | | - document |
89 | | - .querySelectorAll("[data-dynamic-form]") |
90 | | - .forEach((formContainer) => { |
91 | | - const form = formContainer.querySelector("form"); |
92 | | - const pathPreview = formContainer.querySelector("[data-path-preview]"); |
93 | | - const actionButtons = formContainer.querySelectorAll( |
94 | | - "[data-path-template]", |
95 | | - ); |
96 | | - |
97 | | - if (!form || !pathPreview || !(pathPreview instanceof HTMLElement)) |
98 | | - return; |
99 | | - |
100 | | - const updateDynamicPath = () => { |
101 | | - const formData = new FormData(form); |
102 | | - const firstButton = actionButtons[0]; |
103 | | - if (!(firstButton instanceof HTMLElement)) return; |
104 | 34 |
|
105 | | - let pathTemplate = firstButton.dataset.pathTemplate || ""; |
106 | | - let pathPreviewTemplate = pathTemplate; |
107 | | - |
108 | | - for (const [key, value] of formData.entries()) { |
109 | | - if (typeof value === "string") { |
110 | | - const placeholder = `{${key}}`; |
111 | | - pathTemplate = pathTemplate.replace( |
112 | | - placeholder, |
113 | | - encodeURIComponent(value), |
114 | | - ); |
115 | | - pathPreviewTemplate = pathPreviewTemplate.replace( |
116 | | - placeholder, |
117 | | - value || `{${key}}`, |
118 | | - ); |
119 | | - } |
120 | | - } |
121 | | - |
122 | | - actionButtons.forEach((button) => { |
123 | | - if (button instanceof HTMLElement) { |
124 | | - button.dataset.path = pathTemplate; |
125 | | - } |
126 | | - }); |
127 | | - pathPreview.innerText = pathPreviewTemplate; |
128 | | - }; |
129 | | - |
130 | | - form.addEventListener("input", updateDynamicPath); |
131 | | - updateDynamicPath(); // Initial update |
| 35 | + return url; |
| 36 | + }, |
| 37 | + |
| 38 | + copyUrl(config, params = {}, event) { |
| 39 | + const url = this.getFeedUrl(config, params); |
| 40 | + navigator.clipboard.writeText(url).then(() => { |
| 41 | + const originalText = event.target.innerHTML; |
| 42 | + event.target.innerHTML = "Copied!"; |
| 43 | + setTimeout(() => { |
| 44 | + event.target.innerHTML = originalText; |
| 45 | + }, 2000); |
132 | 46 | }); |
133 | | - }, |
134 | | - |
135 | | - getPath(target) { |
136 | | - if (!(target instanceof HTMLElement)) return null; |
137 | | - |
138 | | - if (target.dataset.path) { |
139 | | - return target.dataset.path; |
140 | | - } |
141 | | - |
142 | | - if (target.dataset.pathTemplate) { |
143 | | - const formContainer = target.closest("[data-dynamic-form]"); |
144 | | - if (!formContainer) return null; |
145 | | - |
146 | | - const form = formContainer.querySelector("form"); |
147 | | - if (!form) return null; |
148 | | - |
149 | | - const formData = new FormData(form); |
150 | | - let path = target.dataset.pathTemplate; |
151 | | - let allParamsFilled = true; |
152 | | - |
153 | | - for (const [key, value] of formData.entries()) { |
154 | | - if (!value) { |
155 | | - allParamsFilled = false; |
156 | | - } |
157 | | - if (typeof value === "string") { |
158 | | - path = path.replace(`{${key}}`, encodeURIComponent(value)); |
159 | | - } |
160 | | - } |
161 | | - |
162 | | - return allParamsFilled ? path : null; |
163 | | - } |
164 | | - |
165 | | - return null; |
166 | | - }, |
167 | | -}; |
168 | | - |
169 | | -document.addEventListener("DOMContentLoaded", () => { |
170 | | - App.init(); |
| 47 | + }, |
| 48 | + })); |
171 | 49 | }); |
0 commit comments