-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
161 lines (147 loc) · 6.75 KB
/
index.html
File metadata and controls
161 lines (147 loc) · 6.75 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JsUpdatr Example</title>
<meta name="description" content="JsUpdatr observer example page">
<link rel="prefetch" href="https://unpkg.com/open-props" />
<link rel="prefetch" href="https://unpkg.com/open-props/normalize.min.css" />
<style>
@import "https://unpkg.com/open-props";
@import "https://unpkg.com/open-props/normalize.min.css";
main {
max-width: 1200px;
margin: auto;
padding: var(--size-10) var(--size-3);
}
h3 {
padding: var(--size-3) 0;
}
label {
display: block;
padding: var(--size-3) 0;
}
dd::before {
content: 'data.' attr(data-watch) ' = ';
}
/* responsive without media queries */
.full-section {
margin: 3vh 0;
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
grid-gap: 2vh;
}
</style>
<!-- JsUpdatr example State -->
<script type="text/javascript">
document.state = {
name: '',
showName: false,
text: 'fruit salad',
favorites: '🥥 🍓 🍍',
number: 22,
largest: 23,
smallest: 11,
}
</script>
</head>
<body>
<main>
<h2><a href="https://github.com/hertonwork/JsUpdatr">JsUpdatr</a></h2>
<section class="full-section">
<article>
<h3>Data Models</h3>
<p>
<label for="data_name">Name</label>
<input id="data_name" type="text" name="data_name" data-model="name" value="">
</p>
<p>
<label for="data_favorites">favorites</label>
<input id="data_favorites" type="text" name="data_favorites" data-model="favorites" value="">
</p>
<p>
<label for="data_text">Some text</label>
<input id="data_text" type="text" name="data_text" data-model="text" value="">
</p>
<p>
<label for="data_text">Number limited by the min max fields</label>
<input id="data_number" type="number" name="data_number"
data-watch-attributes="smallest:min,largest:max" data-model="number">
</p>
<p>
<label for="data_min">Min number</label>
<input id="data_min" type="number" name="data_min" data-model="smallest"
data-watch-attributes="largest:max">
</p>
<p>
<label for="data_max">Max number</label>
<input id="data_max" type="number" name="data_max" data-model="largest"
data-watch-attributes="smallest:min">
</p>
</article>
<article>
<h3>Data Views</h3>
<dl>
<dt>Text</dt>
<dd data-watch="text"></dd>
<dd data-watch="favorites"></dd>
<dt>Numbers</dt>
<dd data-watch="number"></dd>
<dd data-watch="largest"></dd>
<dd data-watch="smallest"></dd>
</dl>
</article>
<article>
<h3>Inline Views</h3>
<p>
I like <em data-watch="text"></em> and my favorites are <em data-watch="favorites"></em>
</p>
<p>
Sagittis purus sit amet volutpat <em data-watch="text"></em> mauris nunc. Sed risus ultricies tristique nulla aliquet. Aliquet nibh praesent tristique magna sit amet purus. Leo integer malesuada nunc vel.
Aliquet porttitor lacus luctus accumsan tortor <em data-watch="favorites"></em>.
<em data-watch="name"></em> Sed pulvinar proin gravida hendrerit lectus a. Blandit turpis cursus in hac habitasse platea dictumst quisque.
<em data-watch="name"></em> Pellentesque habitant morbi tristique senectus et netus et malesuada. Enim nec dui nunc mattis enim. Vel quam elementum pulvinar etiam non quam lacus.
<em data-watch="name"></em> Pellentesque dignissim enim sit amet. Et molestie ac feugiat sed lectus <em data-watch="favorites"></em>.
<em data-watch="name"></em> Rhoncus urna neque viverra justo nec ultrices dui. Aliquet lectus proin nibh nisl condimentum id venenatis. Vulputate enim nulla aliquet porttitor lacus luctus accumsan. Sodales ut eu sem integer vitae justo eget.
Feugiat sed lectus vestibulum mattis. Eget mauris pharetra et ultrices neque ornare aenean.
In eu mi bibendum neque egestas congue quisque <em data-watch="favorites"></em>.
</p>
<p aria-live="polite" data-watch="name"></p>
</article>
</section>
</main>
<!-- JsUpdatr Setup -->
<script type="module">
import { JsUpdatr } from './dist/index.js'
JsUpdatr.observe(document.state);
const modelExist = model => document.state.hasOwnProperty(model);
// Finding elements that will be used to control the data or "model"
document.querySelectorAll('[data-model]').forEach(element => {
element.value = document.state[element.dataset.model];
element.addEventListener('input', () => {
document.state[element.dataset.model] = element.value;
});
})
// Finding elements that will be used to show the data or "view"
document.querySelectorAll('[data-watch]').forEach(element => {
if (modelExist(element.dataset.watch)) JsUpdatr.run(() => {
element.textContent = document.state[element.dataset.watch];
})
})
// Finding element attributes that will be set based on the model data
// Example: data-watch-attributes="smallest:min,largest:max"
// Where max and min are the attributes and smallest anj largest are the models
document.querySelectorAll('[data-watch-attributes]').forEach(element => {
element.dataset.watchAttributes.split(',').forEach(attributesSet => {
const [model, attribute] = attributesSet.split(':');
if (modelExist(model) && attribute) {
JsUpdatr.run(() => {
element.setAttribute(attribute, document.state[model]);
})
}
});
})
</script>
</body>
</html>