Newer
Older
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
/* global cozy */
import '../../styles/index.scss'
import React from 'react'
import CozyClient, { CozyProvider } from 'cozy-client'
import { render } from 'react-dom'
import { I18n } from 'cozy-ui/react/I18n'
import schema from 'doctypes'
const manifest = require('../../../manifest.webapp')
let appLocale
const renderApp = function(client) {
const App = require('components/App').default
render(
<I18n
lang={appLocale}
dictRequire={appLocale => require(`locales/${appLocale}`)}
>
<CozyProvider client={client}>
<App />
</CozyProvider>
</I18n>,
document.querySelector('[role=application]')
)
}
// return a defaultData if the template hasn't been replaced by cozy-stack
const getDataOrDefault = function(toTest, defaultData) {
const templateRegex = /^\{\{\.[a-zA-Z]*\}\}$/ // {{.Example}}
return templateRegex.test(toTest) ? defaultData : toTest
}
// initial rendering of the application
document.addEventListener('DOMContentLoaded', () => {
const root = document.querySelector('[role=application]')
const data = root.dataset
const appIcon = getDataOrDefault(
data.cozyIconPath,
require('../vendor/assets/icon.svg')
)
const appNamePrefix = getDataOrDefault(
data.cozyAppNamePrefix || manifest.name_prefix,
''
)
const appName = getDataOrDefault(data.cozyAppName, manifest.name)
const appSlug = getDataOrDefault(data.cozyAppSlug, manifest.slug)
const appVersion = getDataOrDefault(data.cozyAppVersion, manifest.version)
appLocale = getDataOrDefault(data.cozyLocale, 'en')
const protocol = window.location ? window.location.protocol : 'https:'
// initialize the client to interact with the cozy stack
const client = new CozyClient({
uri: `${protocol}//${data.cozyDomain}`,
token: data.cozyToken,
schema,
appMetadata: {
slug: appSlug,
version: appVersion,
},
})
// initialize the bar, common of all applications, it allows
// platform features like apps navigation without doing anything
cozy.bar.init({
appName: appName,
appNamePrefix: appNamePrefix,
iconPath: appIcon,
lang: appLocale,
replaceTitleOnMobile: true,
cozyClient: client,
})
const { setTheme } = cozy.bar
setTheme('primary', { primaryColor: 'transparent' })
renderApp(client)
})