-
Notifications
You must be signed in to change notification settings - Fork 16
/
dom.js
70 lines (58 loc) · 1.65 KB
/
dom.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
const APP_SELECTOR = '[role=application]'
// return an empty object by default to avoid checking existance
const getAppNodeDataSet = () => {
const appNode = document.querySelector(APP_SELECTOR)
if (!appNode || !appNode.dataset) return {}
return appNode.dataset
}
const getCozyData = () => {
const dataset = getAppNodeDataSet()
if (!dataset.cozy) return { app: {} }
return JSON.parse(dataset.cozy)
}
const getDefaultIcon = () => {
const cozy = getCozyData()
const dataset = getAppNodeDataSet()
if (cozy.app.icon) {
return cozy.app.icon
} else if (dataset.cozyIconPath) {
return dataset.cozyIconPath
} else {
return 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'
}
}
const getAppName = () => {
const cozy = getCozyData()
const dataset = getAppNodeDataSet()
return cozy.app.name || dataset.cozyAppName || null
}
const getAppNamePrefix = () => {
const cozy = getCozyData()
const dataset = getAppNodeDataSet()
return cozy.app.prefix || dataset.cozyAppNamePrefix || null
}
const getAppSlug = () => {
const cozy = getCozyData()
const dataset = getAppNodeDataSet()
return cozy.app.slug || dataset.cozyAppSlug || null
}
const getUserActionRequired = () => {
const meta = document.querySelector('meta[name=user-action-required]')
const data = meta && meta.dataset
if (data) {
const { title, code, detail, links } = data
if (code) {
// we suppose that at least code will always exist
return { title, code, detail, links }
}
}
return undefined
}
export {
getDefaultIcon,
getAppName,
getAppNamePrefix,
getAppSlug,
getUserActionRequired,
APP_SELECTOR
}