forked from SolidOS/solid-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheaderFooterHelpers.ts
More file actions
111 lines (108 loc) · 3.34 KB
/
Copy pathheaderFooterHelpers.ts
File metadata and controls
111 lines (108 loc) · 3.34 KB
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
/*
Copied from mashlib/src/global/metadata.ts
*/
import { IndexedFormula, NamedNode, sym } from 'rdflib'
import { ns } from '..'
import { styleMap as headerStyleMap } from '../header/styleMap'
import { styleMap as footerStyleMap } from '../footer/styleMap'
import { getClasses } from '../jss'
type ThrottleOptions = {
leading?: boolean;
throttling?: boolean;
trailing?: boolean;
}
/**
* @internal
*/
function getStyle (styleClass, type?) {
if (type && type === 'footer') {
return footerStyleMap[styleClass]
} else {
return headerStyleMap[styleClass]
}
}
/**
* @ignore exporting this only for the unit test
*/
export function addStyleClassToElement (element: any, styleClasses: string[], type?: string) {
styleClasses.forEach((styleClass) => {
const style = getStyle(styleClass, type)
const { classes } = getClasses(document.head, { [styleClass]: style })
element.classList.add(classes[styleClass])
})
}
/**
* @ignore exporting this only for the unit test
*/
export function getPod (): NamedNode {
// @@ TODO: This is given that mashlib runs on NSS - might need to change when we want it to run on other Pod servers
return sym(document.location.origin).site()
}
/**
* @ignore exporting this only for the unit test
*/
export async function getPodOwner (pod: NamedNode, store: IndexedFormula): Promise<NamedNode | null> {
// @@ TODO: This is given the structure that NSS provides - might need to change for other Pod servers
const podOwner = sym(`${pod.uri}profile/card#me`)
try {
if (store.fetcher) {
await store.fetcher.load(podOwner.doc())
} else {
throw new Error('There was a problem loading the Fetcher')
}
// @@ TODO: check back links to storage
} catch (err) {
throw new Error('Did NOT find pod owners profile at ' + podOwner)
}
if (podOwner) {
const storageIsListedInPodOwnersProfile = store.holds(podOwner, ns.space('storage'), pod, podOwner.doc())
if (!storageIsListedInPodOwnersProfile) {
throw new Error(`** Pod owner ${podOwner} does NOT list pod ${pod} as storage`)
}
}
return podOwner
}
/**
* @ignore exporting this only for the unit test
*/
export function getName (store: IndexedFormula, user: NamedNode): string {
return store.anyValue(user, ns.vcard('fn'), null, user.doc()) ||
store.anyValue(user, ns.foaf('name'), null, user.doc()) ||
user.uri
}
/**
* @ignore exporting this only for the unit test
*/
export function throttle (func: Function, wait: number, options: ThrottleOptions = {}): (...args: any[]) => any {
let context: any,
args: any,
result: any
let timeout: any = null
let previous = 0
const later = function () {
previous = !options.leading ? 0 : Date.now()
timeout = null
result = func.apply(context, args)
if (!timeout) context = args = null
}
return function () {
const now = Date.now()
if (!previous && !options.leading) previous = now
const remaining = wait - (now - previous)
// @ts-ignore
context = this
args = arguments
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout)
timeout = null
}
previous = now
result = func.apply(context, args)
if (!timeout) context = args = null
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining)
}
return result
}
}