forked from radiantearth/stac-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfields.config.js
More file actions
237 lines (207 loc) · 7.42 KB
/
fields.config.js
File metadata and controls
237 lines (207 loc) · 7.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import { Registry } from "@radiantearth/stac-fields";
// Please consult https://github.com/radiantearth/stac-browser/blob/main/README.md#additional-metadata-fields for details.
// ============================================================================
// Decentralized Geospatial (dgeo) Extension - v1.2
// ============================================================================
Registry.addExtension("dgeo", "Decentralized Geospatial");
// ============================================================================
// Helper Functions
// ============================================================================
/**
* Escape HTML special characters to prevent XSS
*/
const escapeHtml = (str) => {
if (str === null || str === undefined) return "";
if (typeof str !== "string") return String(str);
return str
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
};
/**
* Detect CID version from prefix
* @param {string} cid - Content Identifier
* @returns {string} Version identifier (v0, v1, or ?)
*/
const detectCidVersion = (cid) => {
if (!cid || typeof cid !== "string") return "?";
if (cid.startsWith("Qm")) return "v0";
if (cid.startsWith("b") || cid.startsWith("z")) return "v1";
return "?";
};
// Removed shortenCid function - CIDs now display in full with CSS-based responsive truncation
// ============================================================================
// Task 1: Properties-Level CID Array Formatter (dgeo:cids)
// ============================================================================
Registry.addMetadataField("dgeo:cids", {
label: "Content Identifiers",
formatter: (cids) => {
if (!Array.isArray(cids) || cids.length === 0) {
return "<em>No CIDs defined</em>";
}
// Generate HTML list with clickable CID values and version badges
const items = cids
.map((cid) => {
const version = detectCidVersion(cid);
return `
<div class="dgeo-cid-item">
<code class="dgeo-cid dgeo-cid-clickable" data-copy="${escapeHtml(
cid
)}" title="${escapeHtml(cid)} (Click to copy)">${escapeHtml(
cid
)}</code>
<span class="badge badge-info cid-version">CID${version}</span>
</div>
`;
})
.join("");
const count = cids.length;
const plural = count > 1 ? "s" : "";
return `
<div class="dgeo-cids-container">
<div class="dgeo-section-header">
<strong>${count} Content Identifier${plural}</strong>
<small class="text-muted"> (Queryable via STAC API)</small>
</div>
<div class="dgeo-cids-list">
${items}
</div>
</div>
`;
},
});
// ============================================================================
// Task 2: Properties-Level Piece CID Array Formatter (dgeo:piece_cids)
// ============================================================================
Registry.addMetadataField("dgeo:piece_cids", {
label: "Filecoin Piece CIDs",
formatter: (pieceCids) => {
// Hide section if empty
if (!Array.isArray(pieceCids) || pieceCids.length === 0) {
return null;
}
// Generate HTML list with clickable CID values (no badge needed)
const items = pieceCids
.map((cid) => {
return `
<div class="dgeo-cid-item dgeo-piece-cid-item">
<code class="dgeo-cid dgeo-cid-clickable" data-copy="${escapeHtml(
cid
)}" title="${escapeHtml(cid)} (Click to copy)">${escapeHtml(
cid
)}</code>
</div>
`;
})
.join("");
const count = pieceCids.length;
const plural = count > 1 ? "s" : "";
return `
<div class="dgeo-cids-container dgeo-piece-cids-container">
<div class="dgeo-section-header">
<strong>${count} Filecoin Piece CID${plural}</strong>
<small class="text-muted"> (Storage verification)</small>
</div>
<div class="dgeo-cids-list">
${items}
</div>
</div>
`;
},
});
// ============================================================================
// Task 3: Asset-Level CID Formatter (dgeo:cid)
// ============================================================================
Registry.addMetadataField("dgeo:cid", {
label: "Associated CID",
formatter: (cid) => {
if (!cid) return null;
const version = detectCidVersion(cid);
return `
<div class="dgeo-asset-cid">
<code class="dgeo-cid dgeo-cid-clickable" data-copy="${escapeHtml(
cid
)}" title="${escapeHtml(cid)} (Click to copy)">${escapeHtml(cid)}</code>
<span class="badge badge-info cid-version">CID${version}</span>
</div>
`;
},
});
// ============================================================================
// Task 4: Asset-Level CID Profile Formatter (dgeo:cid_profile)
// ============================================================================
const cidProfileFieldLabels = {
cid_version: "CID Version",
hash_function: "Hash Function",
chunk_algorithm: "Chunk Algorithm",
dag_layout: "DAG Layout",
multibase_encoding: "Multibase Encoding",
chunk_size: "Chunk Size (bytes)",
dag_width: "DAG Width",
directory_wrapping: "Directory Wrapping",
hamt_directory_fanout: "HAMT Fanout",
hamt_directory_threshold: "HAMT Threshold",
leaf_envelope: "Leaf Envelope",
};
Registry.addMetadataField("dgeo:cid_profile", {
label: "CID Profile",
formatter: (profile) => {
if (
!profile ||
typeof profile !== "object" ||
Object.keys(profile).length === 0
) {
return null;
}
// Build table rows for non-empty values
const rows = Object.entries(cidProfileFieldLabels)
.filter(([key]) => profile[key] !== undefined && profile[key] !== null)
.map(([key, label]) => {
const val = profile[key];
let displayVal;
if (typeof val === "boolean") {
displayVal = val ? "Yes" : "No";
} else {
displayVal = escapeHtml(String(val));
}
return `
<tr>
<td class="dgeo-profile-label"><strong>${escapeHtml(
label
)}</strong></td>
<td class="dgeo-profile-value">${displayVal}</td>
</tr>
`;
})
.join("");
if (!rows) return null;
// Create summary text from key fields
const summaryParts = [];
if (profile.cid_version !== undefined)
summaryParts.push(`v${profile.cid_version}`);
if (profile.hash_function) summaryParts.push(profile.hash_function);
if (profile.dag_layout) summaryParts.push(profile.dag_layout);
const summaryText =
summaryParts.length > 0 ? summaryParts.join(" • ") : "View Details";
return `
<details class="dgeo-cid-profile">
<summary class="dgeo-profile-summary">${escapeHtml(
summaryText
)}</summary>
<table class="table table-sm table-bordered cid-profile-table">
<tbody>${rows}</tbody>
</table>
</details>
`;
},
});
// ============================================================================
// Example: Custom field registration (commented out)
// ============================================================================
// Registry.addExtension('radiant', 'Radiant Earth');
// Registry.addMetadataField('radiant:public_access', {
// label: "Data Access",
// formatter: value => value ? "Public" : "Private"
// });