You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**van_dml.js** adds a new flavour of composition to VanJS. This is heavily inspired by the [DML](https://github.com/efpage/dml)-framework for dynamic page creation.
6
6
7
7
Van_DML extends the core of VanJS. You can enable van_dml like this:
const { begin, end, base, sp, css } = van // new functions of van_dml
15
14
```
16
-
17
15
## How does it work?
18
-
19
16
`begin(ID)` selects an element "ID" as a base. ID can be a DOM element or a HTML-ID. All elements created after `begin()` are automatically appended as childs to this object.
20
17
`end()` finishes the automatic appending.
21
18
22
19
`begin()` and `end()` can be nested like this:
23
-
24
20
```JS
25
21
const { h1, h2, div, p, button } =van.tags;
26
22
const { begin, end, base, sp, css } = van
27
-
23
+
28
24
begin(document.body)
29
25
h1("Headline")
30
26
begin(div({style:"border: 1px solid black"}))
@@ -33,38 +29,30 @@ Van_DML extends the core of VanJS. You can enable van_dml like this:
33
29
end()
34
30
end()
35
31
```
36
-
37
32
This is similar to using plain HTML, where a tag create elements directly.
38
33
39
34
## transparent operation of "begin()"
40
35
41
36
Van-Tags return a DOM Reference that can be stored in a variable:
42
-
43
37
```JS
44
38
let d =div()
45
39
d.style.color="red"
46
40
d.appendChild(h1("Headline");
47
41
```
48
-
49
42
With VAN_DML, this can be rewritten:
50
-
51
43
```JS
52
44
let d =begin(div()) // -> d is the <div>-element
53
45
begin(div()).style.color="red"// Access the style of div
54
46
```
55
-
56
47
`begin()` is "transparent", it just returns the DOM reference of the first child. This let´s you create functions like this:
57
-
58
48
```JS
59
49
constbdiv= (...args) =>begin(div(...args))
60
-
50
+
61
51
bdiv() // Create a div and open for append
62
52
h1()
63
53
end()
64
54
```
65
-
66
55
begin(ID) can also digest Strings:
67
-
68
56
```JS
69
57
<div id="ID"></div>
70
58
<script>
@@ -76,57 +64,49 @@ begin(ID) can also digest Strings:
76
64
77
65
## end(n)
78
66
79
-
`end()` finishes the append mode and returns to the previous base. For each `begin()` there should be one call of `end()`. You can verify this by checking `SP() ===0` (see below).
80
-
If no base is selected (or the calls `begin()` and `end()` are balanced), auto-Append is disabled.
67
+
`end()` finishes the append mode and returns to the previous base. For each `begin()` there should be one call of `end()`. You can verify this by checking `SP() ===0` (see below).
68
+
If no base is selected (or the calls `begin()` and `end()` are balanced), auto-Append is disabled.
81
69
82
70
end(n) runs `end()` n times. May be convenient to return from deep nested functions.
83
71
84
72
## base() and sp()
85
73
86
74
base() returns the current base:
87
-
88
75
```JS
89
-
begin(div())
76
+
begin(div())
90
77
let b =base(); // returns the current base (div())
91
78
```
92
79
93
80
sp() is the current Stack-Position. Initially sp() is 0. With each call of `begin()` sp() is incremented, `end()` decrements sp().
94
81
To check your code, you can add this line to the end of Javascript:
95
-
96
82
```JS
97
-
begin(document.body)
83
+
begin(document.body)
98
84
.... some code here
99
85
end()
100
-
86
+
101
87
if (sp() !==0) alert(`baseStack mismatch: stacksize ${sp()} at end of Javascript`)
102
88
```
103
89
104
90
## css(): create CSS-rules dynamically with Javascript
105
91
106
92
`css(s)` adds new CSS-rules during runtime.
107
-
s can be
108
-
109
-
- a string css(".myClass { color: red; }")
93
+
s can be
94
+
- a string css(".myClass { color: red; }")
110
95
- a template string with multiple rules
111
-
112
96
```JS
113
97
css(`
114
98
.myClass { color: red; }
115
99
.myClass: hover { color: green }
116
100
`)
101
+
```
102
+
- It can contain template literals:
103
+
```JS
104
+
let color = [ "red", "green", "blue" ]
105
+
css(`
106
+
.myClass { color: ${mycolor[1]}; }
107
+
`)
117
108
```
118
-
119
-
- It can contain template literals:
120
-
121
-
```JS
122
-
let color = [ "red", "green", "blue" ]
123
-
css(`
124
-
.myClass { color: ${mycolor[1]}; }
125
-
`)
126
-
```
127
-
128
109
If a rule already exists, it will be overwritten by the new definition. Results are immediately shown by the browser. Definitions with css() work like any other CSS you provide with static definitions, they just are added from within Javascript. This makes it easy to control CSS programmatically. css can be used anywhere and anytime in the script. New css-rules will become active immediately and might even be changed as shown in the example below:
129
-
130
110
```JS
131
111
const { button } =van.tags
132
112
const { add, css } = van
@@ -135,8 +115,7 @@ If a rule already exists, it will be overwritten by the new definition. Results
**!Attention:**: css() adds new rules to the first stylesheet (`window.document.styleSheets[0]`). If the first stylesheet was loaded from an external source, this may cause CORS errors. For more information see [here](https://davidwalsh.name/add-rules-stylesheets) and [here](https://stackoverflow.com/questions/49088507/cannot-access-rules-in-external-cssstylesheet). Always add a **_local CSS-file_** first, as the dynamic rules are included in the first style sheet.
118
+
**!Attention:**: css() adds new rules to the first stylesheet (`window.document.styleSheets[0]`). If the first stylesheet was loaded from an external source, this may cause CORS errors. For more information see [here](https://davidwalsh.name/add-rules-stylesheets) and [here](https://stackoverflow.com/questions/49088507/cannot-access-rules-in-external-cssstylesheet). Always add a ***local CSS-file*** first, as the dynamic rules are included in the first style sheet.
0 commit comments