forked from Becklyn/mojave
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmount.js
More file actions
59 lines (50 loc) · 1.38 KB
/
mount.js
File metadata and controls
59 lines (50 loc) · 1.38 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
import {h, render} from "preact";
import {find} from "./dom/traverse";
import {merge} from "./extend";
/**
* Mounts the given class on all elements of the given selector
*
* @param {string} selector
* @param {function(new:*, HTMLElement)} ComponentToMount
* @param {Array} constructorParameters
*/
export function mount (selector, ComponentToMount, constructorParameters = [])
{
const elements = find(selector);
for (let i = 0; i < elements.length; i++)
{
constructorParameters.unshift(elements[i]);
const mounted = new ComponentToMount(...constructorParameters);
mounted.init();
}
}
/**
* Mounts the component as JSX Component.
* The matched node must contain the data as JSON encoded content.
*
* @param {string} selector
* @param {preact.Component} ComponentToMount
* @param {Object} additionalProps
*/
export function mountJsx (selector, ComponentToMount, additionalProps = {})
{
const elements = find(selector);
for (let i = 0; i < elements.length; i++)
{
try
{
const node = elements[i];
let data = JSON.parse(node.textContent);
data = merge(data, additionalProps);
render(
h(ComponentToMount, data),
node.parentElement,
node
);
}
catch (e)
{
// ignore
}
}
}