aboutsummaryrefslogtreecommitdiff
path: root/resources
diff options
context:
space:
mode:
authordakkar <dakkar@thenautilus.net>2021-12-27 15:06:06 +0000
committerdakkar <dakkar@thenautilus.net>2021-12-27 15:06:06 +0000
commitbb0e76dcaa1dc529d4f8c460a929b905ec781964 (patch)
tree711ee59ed9bd3690919718596d3348117ff6a9ed /resources
parentlet's not mix md and rst… (diff)
downloadmedia-control-bb0e76dcaa1dc529d4f8c460a929b905ec781964.tar.gz
media-control-bb0e76dcaa1dc529d4f8c460a929b905ec781964.tar.bz2
media-control-bb0e76dcaa1dc529d4f8c460a929b905ec781964.zip
mini-templating in js+html
Diffstat (limited to 'resources')
-rw-r--r--resources/index.html41
1 files changed, 41 insertions, 0 deletions
diff --git a/resources/index.html b/resources/index.html
index cbf0a75..de19121 100644
--- a/resources/index.html
+++ b/resources/index.html
@@ -7,6 +7,44 @@
<title>IR</title>
<script>
"use strict"
+ function fillTemplate(template,values) {
+ const newElement=template.content.cloneNode(true);
+
+ newElement.querySelectorAll('*').forEach(
+ (e) => {
+ if (e.tagName == 'SLOT') {
+ const valueKey=e.textContent.trim();
+ e.replaceWith(e.ownerDocument.createTextNode(values[valueKey]));
+ return;
+ }
+
+ const attrs = e.attributes;
+ for (var i = attrs.length - 1; i >= 0; i--) {
+ if (attrs[i].name.startsWith('slot:')) {
+ const name=attrs[i].name.replace('slot:','');
+ const valueKey=attrs[i].value;
+ e.removeAttribute(attrs[i].name);
+ e.setAttribute(name,values[valueKey]);
+ }
+ }
+ },
+ );
+
+ return newElement;
+ }
+ // example usage
+ function addRow() {
+ document.querySelector('#list').appendChild(
+ fillTemplate(
+ document.querySelector('#row'),
+ {
+ 'play-url':'http://foo.bar',
+ 'name':'something',
+ },
+ )
+ );
+ }
+
function call(method,path,args) {
args ||= {};
let url=new URL(path,location.href);
@@ -215,5 +253,8 @@
</table>
</div>
+ <template id="row">
+ <li><a slot:href="play-url"><span><slot>name</slot></span></a></li>
+ </template>
</body>
</html>