Project

General

Profile

Download (1.28 KB) Statistics
| Branch: | Tag: | Revision:

haketilo / html / DOM_helpers.js @ 2bd35bc4

1
/**
2
 * This file is part of Haketilo.
3
 *
4
 * Function: Operations on DOM elements.
5
 *
6
 * Copyright (C) 2021 Wojtek Kosior
7
 * Redistribution terms are gathered in the `copyright' file.
8
 */
9

    
10
function by_id(id)
11
{
12
    return document.getElementById(id);
13
}
14

    
15
const known_templates = new Map();
16

    
17
function get_template(template_id)
18
{
19
    let template = known_templates.get(template_id) || null;
20
    if (template)
21
	return template;
22

    
23
    for (const template_node of document.getElementsByTagName("TEMPLATE")) {
24
	template = template_node.content.getElementById(template_id);
25
	if (template)
26
	    break;
27
    }
28

    
29
    known_templates.set(template_id, template);
30
    return template;
31
}
32

    
33
function clone_template(template_id)
34
{
35
    const clone = get_template(template_id).cloneNode(true);
36
    const result_object = {};
37
    const to_process = [clone];
38

    
39
    while (to_process.length > 0) {
40
	const element = to_process.pop();
41
	const template_key = element.getAttribute("data-template");
42

    
43
	if (template_key)
44
	    result_object[template_key] = element;
45

    
46
	element.removeAttribute("id");
47
	element.removeAttribute("data-template");
48

    
49
	for (const child of element.children)
50
	    to_process.push(child);
51
    }
52

    
53
    return result_object;
54
}
55

    
56
/*
57
 * EXPORTS_START
58
 * EXPORT by_id
59
 * EXPORT get_template
60
 * EXPORT clone_template
61
 * EXPORTS_END
62
 */
(1-1/14)