-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo2.html
More file actions
240 lines (222 loc) · 8.65 KB
/
demo2.html
File metadata and controls
240 lines (222 loc) · 8.65 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Demo 2: Multiple Selectors - hx-scope</title>
<script src="https://unpkg.com/htmx.org@1.9.10"></script>
<script src="../hx-scope.js"></script>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
background: #f5f5f5;
}
h1 {
color: #333;
border-bottom: 3px solid #3b82f6;
padding-bottom: 10px;
}
.nav {
background: white;
padding: 15px;
margin-bottom: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.nav a {
color: #3b82f6;
text-decoration: none;
margin-right: 15px;
font-weight: 500;
}
.nav a:hover {
text-decoration: underline;
}
.demo-section {
background: white;
padding: 20px;
margin: 20px 0;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.demo-section h2 {
margin-top: 0;
color: #1f2937;
}
.form-group {
margin: 15px 0;
}
label {
display: block;
margin-bottom: 5px;
font-weight: 500;
color: #374151;
}
input, textarea {
width: 100%;
padding: 8px 12px;
border: 1px solid #d1d5db;
border-radius: 4px;
font-size: 14px;
box-sizing: border-box;
}
input:focus, textarea:focus {
outline: none;
border-color: #3b82f6;
box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}
button {
background: #3b82f6;
color: white;
border: none;
padding: 10px 20px;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
font-weight: 500;
margin: 5px 5px 5px 0;
}
button:hover {
background: #2563eb;
}
button:active {
background: #1d4ed8;
}
.scope-indicator {
display: inline-block;
padding: 2px 8px;
background: #dbeafe;
color: #1e40af;
border-radius: 3px;
font-size: 12px;
font-weight: 500;
margin-left: 8px;
}
.response {
margin-top: 15px;
padding: 15px;
background: #f3f4f6;
border-left: 4px solid #3b82f6;
border-radius: 4px;
font-family: monospace;
font-size: 13px;
}
.response:empty {
display: none;
}
code {
background: #1f2937;
color: #e5e7eb;
padding: 2px 6px;
border-radius: 3px;
font-size: 13px;
}
.note {
background: #fef3c7;
border-left: 4px solid #f59e0b;
padding: 12px;
margin: 15px 0;
border-radius: 4px;
}
</style>
</head>
<body>
<h1>Demo 2: Multiple Selectors (Comma-Separated)</h1>
<div class="nav">
<a href="index.html">← All Demos</a>
<a href="demo1.html">← Previous</a>
<a href="demo3.html">Next: Attribute Selectors →</a>
</div>
<div class="note">
<strong>Note:</strong> This demo uses a Service Worker to intercept requests and display the parameters being sent.
<span id="reload-note" style="display: none; color: #dc2626; font-weight: 600;">Please reload the page for the Service Worker to take effect!</span>
</div>
<div class="demo-section" hx-ext="scoped-inputs">
<h2>Multiple Selectors (Comma-Separated)</h2>
<p>Use comma-separated CSS selectors to include inputs from multiple groups.</p>
<div class="form-group">
<label>First Name <span class="scope-indicator">.profile</span></label>
<input type="text" hx-name="first_name" class="profile" value="John" placeholder="First name">
</div>
<div class="form-group">
<label>Last Name <span class="scope-indicator">.profile</span></label>
<input type="text" hx-name="last_name" class="profile" value="Doe" placeholder="Last name">
</div>
<div class="form-group">
<label>Bio <span class="scope-indicator">.profile .extended</span></label>
<textarea hx-name="bio" class="profile extended" rows="3" placeholder="Tell us about yourself">Software developer passionate about web technologies.</textarea>
</div>
<div class="form-group">
<label>Website <span class="scope-indicator">.extended</span></label>
<input type="url" hx-name="website" class="extended" value="https://example.com" placeholder="Your website">
</div>
<button hx-post="/api/profile/basic" hx-scope=".profile" hx-target="#response2">
Save Basic Profile (selector: .profile)
</button>
<button hx-post="/api/profile/full" hx-scope=".profile, .extended" hx-target="#response2">
Save Full Profile (selector: .profile, .extended)
</button>
<div id="response2" class="response"></div>
</div>
<script>
// Register service worker to intercept API requests
if ('serviceWorker' in navigator) {
let reloadOnce = false;
window.addEventListener('load', async () => {
try {
const registration = await navigator.serviceWorker.register('./sw.js');
console.log('Service Worker registered:', registration);
// Check for updates on page load
registration.update();
// Listen for new service worker taking over
registration.addEventListener('updatefound', () => {
const newWorker = registration.installing;
newWorker.addEventListener('statechange', () => {
if (newWorker.state === 'activated' && !navigator.serviceWorker.controller) {
console.log('New Service Worker activated, reloading page...');
window.location.reload();
}
});
});
// Wait for service worker to be ready
await navigator.serviceWorker.ready;
console.log('Service Worker ready!');
// If this is the first visit and SW just got ready, reload to let it take control
if (!navigator.serviceWorker.controller && !reloadOnce) {
reloadOnce = true;
console.log('First visit - reloading to activate Service Worker...');
sessionStorage.setItem('sw-reloaded', 'true');
window.location.reload();
return;
}
const note = document.querySelector('.note');
if (note) {
note.innerHTML = '<strong>Service Worker Active!</strong> Requests are being intercepted and their details are shown below each button click.';
note.style.background = '#d1fae5';
note.style.borderColor = '#22c55e';
}
} catch (error) {
console.error('Service Worker registration failed:', error);
const note = document.querySelector('.note');
if (note) {
note.innerHTML = '<strong>⚠️ Service Worker Failed!</strong> ' + error.message + '. Make sure you are accessing via http:// (not file://)';
note.style.background = '#fee2e2';
note.style.borderColor = '#dc2626';
}
}
});
// Check if service worker is controlling the page
if (navigator.serviceWorker.controller) {
console.log('Service Worker already controlling the page');
} else {
console.log('Service Worker NOT controlling the page yet');
}
} else {
console.error('Service Workers not supported in this browser');
}
</script>
</body>
</html>