Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
giswqs
GitHub Repository: giswqs/geemap
Path: blob/master/js/utils.ts
2313 views
1
// TODO(sufyanAbbasi): Write tests for this file.
2
3
import type { AnyModel } from "@anywidget/types";
4
import { IWidgetManager, WidgetModel } from "@jupyter-widgets/base";
5
import { html, TemplateResult } from "lit";
6
7
async function unpackModels(
8
modelIds: Array<string>,
9
manager: IWidgetManager
10
): Promise<Array<WidgetModel>> {
11
return Promise.all(
12
modelIds.map((id) => manager.get_model(id.slice("IPY_MODEL_".length)))
13
);
14
}
15
16
export function loadFonts() {
17
if (!document.querySelector(".custom-fonts")) {
18
const styleElement = document.createElement("style");
19
styleElement.classList.add("custom-fonts");
20
styleElement.textContent =
21
'@import "https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined";';
22
document.body.appendChild(styleElement);
23
}
24
}
25
26
export async function updateChildren(
27
container: HTMLElement,
28
model: AnyModel<any>,
29
property = "children"
30
) {
31
let children = model.get(property);
32
if (!Array.isArray(children)) {
33
children = [children];
34
}
35
const child_models = await unpackModels(children, model.widget_manager);
36
const child_views = await Promise.all(
37
child_models.map((model) => model.widget_manager.create_view(model))
38
);
39
container.innerHTML = ``;
40
for (const child_view of child_views) {
41
container.appendChild(child_view.el);
42
}
43
}
44
45
export function reverseMap<K, V>(map: Map<K, V>): Map<V, K> {
46
const reversedMap = new Map<V, K>();
47
for (const [key, value] of map.entries()) {
48
if (value != null) {
49
reversedMap.set(value, key);
50
}
51
}
52
return reversedMap;
53
}
54
55
export interface SelectOption {
56
label: string;
57
value: string;
58
}
59
60
export function renderSelect(
61
options: Array<SelectOption> | Array<string>,
62
value: string,
63
callback: (event: Event) => void
64
): TemplateResult {
65
const emptyOptions = options.length === 0;
66
const newOptions = (emptyOptions ? ["---"] : options).map((option) => {
67
const isObject = typeof option === "object";
68
const optValue = `${isObject ? option.value : option}`;
69
const optLabel = `${isObject ? option.label : option}`;
70
return html`
71
<option value="${optValue}" ?selected="${value === optValue}">
72
${optLabel}
73
</option>
74
`;
75
});
76
return html`
77
<select
78
class="legacy-select"
79
@change="${callback}"
80
?disabled=${emptyOptions}
81
>
82
${newOptions}
83
</select>
84
`;
85
}
86
87