-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
117 lines (103 loc) · 3.14 KB
/
index.js
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
import { Elm } from "./src/Main.elm";
import * as Sentry from "@sentry/browser";
import Charts from "./lib/charts";
// Sentry
if (process.env.SENTRY_DSN) {
Sentry.init({
dsn: process.env.SENTRY_DSN,
integrations: [Sentry.browserTracingIntegration()],
tracesSampleRate: 0,
allowUrls: [
/^https:\/\/ecobalyse\.beta\.gouv\.fr/,
/^https:\/\/staging-ecobalyse\.incubateur\.net/,
// Review apps
/^https:\/\/ecobalyse-pr.*\.osc-fr1\.scalingo\.io/,
],
ignoreErrors: [
// Most often due to DOM-aggressive browser extensions
/_VirtualDom_applyPatch/,
],
environment: process.env.IS_REVIEW_APP ? "review-app" : process.env.NODE_ENV || "development",
});
}
function loadScript(scriptUrl) {
var d = document,
g = d.createElement("script"),
s = d.getElementsByTagName("script")[0];
g.async = true;
g.src = scriptUrl;
s.parentNode.insertBefore(g, s);
}
// The localStorage key to use to store serialized session data
const storeKey = "store";
const app = Elm.Main.init({
flags: {
clientUrl: location.origin + location.pathname,
enabledSections: {
food: process.env.ENABLE_FOOD_SECTION === "True",
objects: process.env.ENABLE_OBJECTS_SECTION === "True",
textile: true, // always enabled
veli: process.env.ENABLE_VELI_SECTION === "True",
},
rawStore: localStorage[storeKey] || "null",
matomo: {
host: process.env.MATOMO_HOST || "",
siteId: process.env.MATOMO_SITE_ID || "",
},
},
});
app.ports.copyToClipboard.subscribe((text) => {
navigator.clipboard.writeText(text).then(
function () {},
function (err) {
alert(
`Votre navigateur ne supporte pas la copie automatique;
vous pouvez copier l'adresse manuellement`,
);
},
);
});
app.ports.appStarted.subscribe(() => {
var _paq = (window._paq = window._paq || []);
_paq.push(["trackPageView"]);
_paq.push(["enableLinkTracking"]);
var u = `https://${process.env.MATOMO_HOST}/`;
_paq.push(["setTrackerUrl", u + "matomo.php"]);
_paq.push(["disableCookies"]);
_paq.push(["setSiteId", process.env.MATOMO_SITE_ID]);
loadScript(u + "matomo.js");
});
app.ports.loadRapidoc.subscribe((rapidocScriptUrl) => {
// load the rapi-doc script if the component hasn't be registered yet
if (!customElements.get("rapi-doc")) {
loadScript(rapidocScriptUrl);
}
});
app.ports.saveStore.subscribe((rawStore) => {
localStorage[storeKey] = rawStore;
});
app.ports.addBodyClass.subscribe((cls) => {
document.body.classList.add(cls);
});
app.ports.removeBodyClass.subscribe((cls) => {
document.body.classList.remove(cls);
});
app.ports.scrollTo.subscribe((pos) => {
window.scrollTo(pos.x, pos.y);
});
app.ports.scrollIntoView.subscribe((id) => {
let node = document.getElementById(id);
node?.scrollIntoView({ behavior: "smooth" });
});
// Ensure session is refreshed when it changes in another tab/window
window.addEventListener(
"storage",
(event) => {
if (event.storageArea === localStorage && event.key === storeKey) {
app.ports.storeChanged.send(event.newValue);
}
},
false,
);
// Register custom chart elements
Charts.registerElements();