« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/static/js/client-util.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/static/js/client-util.js')
-rw-r--r--src/static/js/client-util.js43
1 files changed, 33 insertions, 10 deletions
diff --git a/src/static/js/client-util.js b/src/static/js/client-util.js
index 71112313..74d63ad6 100644
--- a/src/static/js/client-util.js
+++ b/src/static/js/client-util.js
@@ -1,5 +1,3 @@
-/* eslint-env browser */
-
 export function rebase(href, rebaseKey = 'rebaseLocalized') {
   let result = document.documentElement.dataset[rebaseKey] || './';
 
@@ -37,7 +35,7 @@ export function cssProp(el, ...args) {
   }
 }
 
-export function templateContent(el) {
+export function templateContent(el, slots = {}) {
   if (el === null) {
     return null;
   }
@@ -46,7 +44,25 @@ export function templateContent(el) {
     throw new Error(`Expected a <template> element`);
   }
 
-  return el.content.cloneNode(true);
+  const content = el.content.cloneNode(true);
+
+  for (const [key, value] of Object.entries(slots)) {
+    const slot = content.querySelector(`slot[name="${key}"]`);
+
+    if (!slot) {
+      console.warn(`Slot ${key} missing in template:`, el);
+      continue;
+    }
+
+    if (value === null || value === undefined) {
+      console.warn(`Valueless slot ${key} in template:`, el);
+      continue;
+    }
+
+    slot.replaceWith(value);
+  }
+
+  return content;
 }
 
 // Curry-style, so multiple points can more conveniently be tested at once.
@@ -81,12 +97,12 @@ export function getVisuallyContainingElement(child) {
 const getLinkHref = (type, directory) => rebase(`${type}/${directory}`);
 */
 
-export const openAlbum = d => rebase(`album/${d}`);
-export const openArtTag = d => rebase(`tag/${d}`);
-export const openArtist = d => rebase(`artist/${d}`);
-export const openFlash = d => rebase(`flash/${d}`);
-export const openGroup = d => rebase(`group/${d}`);
-export const openTrack = d => rebase(`track/${d}`);
+export const openAlbum = d => rebase(`album/${d}/`);
+export const openArtTag = d => rebase(`tag/${d}/`);
+export const openArtist = d => rebase(`artist/${d}/`);
+export const openFlash = d => rebase(`flash/${d}/`);
+export const openGroup = d => rebase(`group/${d}/`);
+export const openTrack = d => rebase(`track/${d}/`);
 
 // TODO: This should also use urlSpec.
 
@@ -127,3 +143,10 @@ export function dispatchInternalEvent(event, eventName, ...args) {
 
   return results;
 }
+
+const languageCode = document.documentElement.getAttribute('lang');
+
+export function formatDate(inputDate) {
+  const date = new Date(inputDate);
+  return date.toLocaleDateString(languageCode);
+}