forked from osbuild/cockpit-composer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
86 lines (72 loc) · 2.76 KB
/
main.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
import "core-js/stable";
// redux-saga uses generators, use regenerator-runtime/runtime to transform generators
import "regenerator-runtime/runtime";
import "whatwg-fetch";
// Contains PF3 stylesheets and base for PF4
import "./lib/patternfly/patternfly-cockpit.scss";
import React from "react";
import ReactDOM from "react-dom";
import { addLocaleData, IntlProvider } from "react-intl";
import enLocaleData from "react-intl/locale-data/en";
import FastClick from "fastclick";
import { Provider } from "react-redux";
import "@patternfly/patternfly/patternfly-addons.css";
import "@patternfly/patternfly/layouts/Flex/flex.css";
import "@patternfly/patternfly/utilities/Display/display.css";
import "./public/custom.css";
import "bootstrap";
import cockpit from "cockpit";
import store from "./core/store";
import router from "./core/router";
import history from "./core/history";
// Intialize any necessary locale data, and load translated messages
const translations = require("./build/translations.json");
const languages = [...new Set(Object.keys(translations).map((lang) => lang.split("_")[0]))];
for (const lang of languages) {
const localData = require(`react-intl/locale-data/${lang}`); // eslint-disable-line import/no-dynamic-require
addLocaleData(localData);
}
// still need english
addLocaleData(enLocaleData);
const routes = require("./routes.json");
// Loaded with utils/routes-loader.js
const container = document.getElementById("main");
const userLanguage = cockpit.language;
let messages;
if (userLanguage in translations) {
messages = translations[userLanguage];
}
let localeLang = "en";
if (userLanguage) {
localeLang = userLanguage.includes("_") ? userLanguage.replace("_", "-") : userLanguage;
}
function renderComponent(component) {
ReactDOM.render(
<Provider store={store}>
{messages !== undefined ? (
<IntlProvider locale={localeLang} messages={messages}>
{component}
</IntlProvider>
) : (
<IntlProvider locale="en">{component}</IntlProvider>
)}
</Provider>,
container
);
}
// Find and render a web page matching the current URL path,
// if such page is not found then render an error page (see routes.json, core/router.js)
function render(location) {
router
.resolve(routes, location)
.then(renderComponent)
.catch((error) => router.resolve(routes, { ...location, error }).then(renderComponent));
}
// Handle client-side navigation by using HTML5 History API
// For more information visit https://github.com/ReactJSTraining/history/tree/master/docs#readme
history.listen(render);
render(history.getCurrentLocation());
// Eliminates the 300ms delay between a physical tap
// and the firing of a click event on mobile browsers
// https://github.com/ftlabs/fastclick
FastClick.attach(document.body);