Project

General

Profile

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

haketilo / html / item_preview.js @ 1f9ccef9

1
/**
2
 * This file is part of Haketilo.
3
 *
4
 * Function: Showing resource/mapping details in a browser.
5
 *
6
 * Copyright (C) 2022 Wojtek Kosior
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * As additional permission under GNU GPL version 3 section 7, you
19
 * may distribute forms of that code without the copy of the GNU
20
 * GPL normally required by section 4, provided you include this
21
 * license notice and, in case of non-source distribution, a URL
22
 * through which recipients can access the Corresponding Source.
23
 * If you modify file(s) with this exception, you may extend this
24
 * exception to your version of the file(s), but you are not
25
 * obligated to do so. If you do not wish to do so, delete this
26
 * exception statement from your version.
27
 *
28
 * As a special exception to the GPL, any HTML file which merely
29
 * makes function calls to this code, and for that purpose
30
 * includes it by reference shall be deemed a separate work for
31
 * copyright law purposes. If you modify this code, you may extend
32
 * this exception to your version of the code, but you are not
33
 * obligated to do so. If you do not wish to do so, delete this
34
 * exception statement from your version.
35
 *
36
 * You should have received a copy of the GNU General Public License
37
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
38
 *
39
 * I, Wojtek Kosior, thereby promise not to sue for violation of this file's
40
 * license. Although I request that you do not make use of this code in a
41
 * proprietary program, I am not going to enforce this in court.
42
 */
43

    
44
#IMPORT common/indexeddb.js AS haketilodb
45
#IMPORT html/dialog.js
46

    
47
#FROM common/browser.js IMPORT browser
48
#FROM html/DOM_helpers.js IMPORT clone_template
49

    
50
function populate_list(ul, items) {
51
    for (const item of items) {
52
	const li = document.createElement("li");
53
	li.append(item);
54
	ul.append(li);
55
    }
56
}
57

    
58
const file_preview_link = browser.runtime.getURL("html/file_preview.html");
59

    
60
/*
61
 * The default function to use to create file preview link. Links it creates can
62
 * be used to view files from IndexedDB.
63
 */
64
function make_file_link(preview_object, file_ref) {
65
    const a = document.createElement("a");
66
    a.href = `${file_preview_link}#${file_ref.sha256}`;
67
    a.innerText = file_ref.file;
68
    a.target = "_blank";
69
    return a;
70
}
71

    
72
function resource_preview(resource, preview_object, link_cb=make_file_link) {
73
    if (preview_object === undefined)
74
	preview_object = clone_template("resource_preview");
75

    
76
    preview_object.conforms_to.innerHTML = "";
77
    const schema_link     = document.createElement("a");
78
    schema_link.href      = resource.$schema;
79
    schema_link.innerText = resource.$schema;
80
    preview_object.conforms_to.append(schema_link);
81

    
82
    preview_object.identifier.innerText  = resource.identifier;
83
    preview_object.long_name.innerText   = resource.long_name;
84
    preview_object.uuid.innerText        = resource.uuid || "not set";
85
    preview_object.version.innerText     =
86
	`${resource.version.join(".")}-${resource.revision}`;
87
    preview_object.description.innerText = resource.description;
88
    preview_object.source_name.innerText = resource.source_name;
89

    
90
    [...preview_object.dependencies.childNodes].forEach(n => n.remove());
91

    
92
    const deps_refs = resource.dependencies || [];
93
    populate_list(preview_object.dependencies,
94
		  deps_refs.map(res_ref => res_ref.identifier));
95

    
96
    const link_maker = file_ref => link_cb(preview_object, file_ref);
97

    
98
    [...preview_object.scripts.childNodes].forEach(n => n.remove());
99
    populate_list(preview_object.scripts, resource.scripts.map(link_maker));
100

    
101
    [...preview_object.copyright.childNodes].forEach(n => n.remove());
102
    populate_list(preview_object.copyright,
103
		  resource.source_copyright.map(link_maker));
104

    
105
    return preview_object;
106
}
107
#EXPORT resource_preview
108

    
109
function mapping_preview(mapping, preview_object, link_cb=make_file_link) {
110
    if (preview_object === undefined)
111
	preview_object = clone_template("mapping_preview");
112

    
113
    preview_object.conforms_to.innerHTML = "";
114
    const schema_link     = document.createElement("a");
115
    schema_link.href      = mapping.$schema;
116
    schema_link.innerText = mapping.$schema;
117
    preview_object.conforms_to.append(schema_link);
118

    
119
    preview_object.identifier.innerText  = mapping.identifier;
120
    preview_object.long_name.innerText   = mapping.long_name;
121
    preview_object.uuid.innerText        = mapping.uuid || "not set";
122
    preview_object.version.innerText     = mapping.version.join(".");
123
    preview_object.description.innerText = mapping.description;
124
    preview_object.source_name.innerText = mapping.source_name;
125

    
126
    [...preview_object.payloads.childNodes].forEach(n => n.remove());
127
    const payload_entries = Object.entries(mapping.payloads || {}).sort();
128
    for (const [pattern, payload] of payload_entries) {
129
	/* We use a non-breaking space because normal space would be ignored. */
130
	const [nbsp, rarrow] = [160, 0x2192].map(n => String.fromCodePoint(n));
131
	const texts = [`${pattern}${nbsp}`, `${rarrow}  ${payload.identifier}`];
132
	const payload_entry = clone_template("pattern_payload_entry");
133
	payload_entry.pattern.innerText = pattern;
134
	payload_entry.payload.innerText = payload.identifier;
135
	preview_object.payloads.append(payload_entry.main_div);
136
    }
137

    
138
    const link_maker = file_ref => link_cb(preview_object, file_ref);
139

    
140
    [...preview_object.copyright.childNodes].forEach(n => n.remove());
141
    populate_list(preview_object.copyright,
142
		  mapping.source_copyright.map(link_maker));
143

    
144
    return preview_object;
145
}
146
#EXPORT mapping_preview
(15-15/28)