Project

General

Profile

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

haketilo / html / DOM_helpers.js @ 453ba039

1
/**
2
 * Hachette operations on DOM elements
3
 *
4
 * Copyright (C) 2021 Wojtek Kosior
5
 * Redistribution terms are gathered in the `copyright' file.
6
 */
7

    
8
function by_id(id)
9
{
10
    return document.getElementById(id);
11
}
12

    
13
const known_templates = new Map();
14

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

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

    
27
    known_templates.set(template_id, template);
28
    return template;
29
}
30

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

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

    
41
	if (template_key)
42
	    result_object[template_key] = element;
43

    
44
	element.removeAttribute("id");
45
	element.removeAttribute("data-template");
46

    
47
	for (const child of element.children)
48
	    to_process.push(child);
49
    }
50

    
51
    return result_object;
52
}
53

    
54
/*
55
 * EXPORTS_START
56
 * EXPORT by_id
57
 * EXPORT get_template
58
 * EXPORT clone_template
59
 * EXPORTS_END
60
 */
(1-1/11)