Project

General

Profile

« Previous | Next » 

Revision ad69f9c8

Added by koszko over 1 year ago

add support for testing with other browsers (especially Abrowser and Librewolf)

There are still some spurious failures when running under those newer browsers. Those will be systematically investigated and fixed.

View differences:

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

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

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

  
58
/* Link click handler used in make_file_link(). */
59
async function file_link_clicked(preview_object, file_ref, event)
60
{
61
    event.preventDefault();
62

  
63
    const db = await haketilodb.get();
64
    const file = await haketilodb.idb_get(db.transaction("files"),
65
					  "files", file_ref.hash_key);
66
    if (file === undefined) {
67
	dialog.error(preview_object.dialog_context,
68
		     "File missing from Haketilo's internal database :(");
69
    } else {
70
	const encoded_file = encodeURIComponent(file.contents);
71
	open(`data:text/plain;charset=utf8,${encoded_file}`, '_blank');
72
    }
73
}
58
const file_preview_link = browser.runtime.getURL("html/file_preview.html");
74 59

  
75 60
/*
76 61
 * The default function to use to create file preview link. Links it creates can
77 62
 * be used to view files from IndexedDB.
78 63
 */
79
function make_file_link(preview_object, file_ref)
80
{
64
function make_file_link(preview_object, file_ref) {
81 65
    const a = document.createElement("a");
82
    a.href = "javascript:void(0)";
66
    a.href = `${file_preview_link}#${file_ref.hash_key}`;
83 67
    a.innerText = file_ref.file;
84
    a.addEventListener("click",
85
		       e => file_link_clicked(preview_object, file_ref, e));
86

  
68
    a.target = "_blank";
87 69
    return a;
88 70
}
89 71

  
90
function resource_preview(resource, preview_object, dialog_context,
91
			  make_link_cb=make_file_link)
92
{
72
function resource_preview(resource, preview_object, link_cb=make_file_link) {
93 73
    if (preview_object === undefined)
94 74
	preview_object = clone_template("resource_preview");
95 75

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

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

  
109 89
    [...preview_object.scripts.childNodes].forEach(n => n.remove());
110 90
    populate_list(preview_object.scripts, resource.scripts.map(link_maker));
......
113 93
    populate_list(preview_object.copyright,
114 94
		  resource.source_copyright.map(link_maker));
115 95

  
116
    preview_object.dialog_context = dialog_context;
117

  
118 96
    return preview_object;
119 97
}
120 98
#EXPORT resource_preview
121 99

  
122
function mapping_preview(mapping, preview_object, dialog_context,
123
			 make_link_cb=make_file_link)
124
{
100
function mapping_preview(mapping, preview_object, link_cb=make_file_link) {
125 101
    if (preview_object === undefined)
126 102
	preview_object = clone_template("mapping_preview");
127 103

  
......
145 121
	}
146 122
    }
147 123

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

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

  
154
    preview_object.dialog_context = dialog_context;
155

  
156 130
    return preview_object;
157 131
}
158 132
#EXPORT mapping_preview

Also available in: Unified diff