Project

General

Profile

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

haketilo / html / item_preview.js @ 1c65dd5c

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.identifier.innerText  = resource.identifier;
77
    preview_object.long_name.innerText   = resource.long_name;
78
    preview_object.uuid.innerText        = resource.uuid;
79
    preview_object.version.innerText     =
80
	`${resource.version.join(".")}-${resource.revision}`;
81
    preview_object.description.innerText = resource.description;
82
    preview_object.source_name.innerText = resource.source_name;
83

    
84
    [...preview_object.dependencies.childNodes].forEach(n => n.remove());
85
    populate_list(preview_object.dependencies, resource.dependencies);
86

    
87
    const link_maker = file_ref => link_cb(preview_object, file_ref);
88

    
89
    [...preview_object.scripts.childNodes].forEach(n => n.remove());
90
    populate_list(preview_object.scripts, resource.scripts.map(link_maker));
91

    
92
    [...preview_object.copyright.childNodes].forEach(n => n.remove());
93
    populate_list(preview_object.copyright,
94
		  resource.source_copyright.map(link_maker));
95

    
96
    return preview_object;
97
}
98
#EXPORT resource_preview
99

    
100
function mapping_preview(mapping, preview_object, link_cb=make_file_link) {
101
    if (preview_object === undefined)
102
	preview_object = clone_template("mapping_preview");
103

    
104
    preview_object.identifier.innerText  = mapping.identifier;
105
    preview_object.long_name.innerText   = mapping.long_name;
106
    preview_object.uuid.innerText        = mapping.uuid;
107
    preview_object.version.innerText     = mapping.version.join(".");
108
    preview_object.description.innerText = mapping.description;
109
    preview_object.source_name.innerText = mapping.source_name;
110

    
111
    [...preview_object.payloads.childNodes].forEach(n => n.remove());
112
    for (const [pattern, payload] of Object.entries(mapping.payloads).sort()) {
113
	/* We use a non-breaking space because normal space would be ignored. */
114
	const [nbsp, rarrow] = [160, 0x2192].map(n => String.fromCodePoint(n));
115
	const texts = [`${pattern}${nbsp}`, `${rarrow}  ${payload.identifier}`];
116
	for (let i = 0; i < texts.length; i++) {
117
	    const span = document.createElement("span");
118
	    span.innerText = texts[i];
119
	    span.classList.add(`grid_col_${i + 1}`);
120
	    preview_object.payloads.append(span);
121
	}
122
    }
123

    
124
    const link_maker = file_ref => link_cb(preview_object, file_ref);
125

    
126
    [...preview_object.copyright.childNodes].forEach(n => n.remove());
127
    populate_list(preview_object.copyright,
128
		  mapping.source_copyright.map(link_maker));
129

    
130
    return preview_object;
131
}
132
#EXPORT mapping_preview
(15-15/26)