-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo5.html
More file actions
321 lines (300 loc) · 14.6 KB
/
Copy pathdemo5.html
File metadata and controls
321 lines (300 loc) · 14.6 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Demo 5: Component Isolation - 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 #059669;
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, select, textarea {
width: 100%;
padding: 8px 12px;
border: 1px solid #d1d5db;
border-radius: 4px;
font-size: 14px;
box-sizing: border-box;
}
input:focus, select: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 5: Component Isolation - Line Item Calculation ⭐</h1>
<div class="nav">
<a href="index.html">← All Demos</a>
<a href="demo4.html">← Previous</a>
</div>
<div class="note" style="background: #e0f2fe; border-color: #0284c7;">
<strong>The Killer Feature!</strong> This demonstrates using <code>hx-name</code> (excluded from form) vs <code>name</code> (included in form) for component isolation.
</div>
<div class="demo-section">
<h2>Component Isolation - Line Item Calculation</h2>
<p>Multiple isolated components with reusable input names inside a single form.</p>
<div class="note" style="background: #e0f2fe; border-color: #0284c7; margin-bottom: 20px;">
<strong>Key Concept:</strong> Each line item uses <code>hx-name="quantity"</code> and <code>hx-name="price"</code> - the same names!
These are <strong>NOT</strong> submitted with the form. Only the calculated totals (with <code>name</code> attribute) are submitted.
</div>
<form hx-ext="scoped-inputs" id="checkout-form">
<h3 style="margin-top: 0;">Shopping Cart</h3>
<!-- Line Item 1 -->
<div class="line-item item-1" style="background: #f9fafb; padding: 15px; margin: 15px 0; border-radius: 6px; border: 1px solid #e5e7eb;">
<h4 style="margin-top: 0; color: #1f2937;">Product A - Laptop <span class="scope-indicator">.item-1</span></h4>
<div style="display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; align-items: end;">
<div class="form-group">
<label>Quantity <span class="scope-indicator">hx-name</span></label>
<input type="number" hx-name="quantity" value="2" min="1" style="width: 100%;">
</div>
<div class="form-group">
<label>Price ($) <span class="scope-indicator">hx-name</span></label>
<input type="number" hx-name="price" value="899.99" step="0.01" readonly style="width: 100%; background: #f3f4f6;">
</div>
<div class="form-group">
<button hx-post="/calculate?item_id=1&product_id=laptop" hx-scope=".item-1 input[hx-name]" hx-target="#line-1-result" hx-swap="outerHTML" style="width: 100%; margin: 0;">
Calculate
</button>
</div>
</div>
<div id="line-1-result">
<div style="margin-top: 10px; font-size: 16px; font-weight: 500; color: #059669;">
Line Total: $1,799.98
</div>
<input type="hidden" name="line_total_1" id="line_total_1" value="1799.98">
<input type="hidden" name="product_id_1" value="laptop">
</div>
</div>
<!-- Line Item 2 -->
<div class="line-item item-2" style="background: #f9fafb; padding: 15px; margin: 15px 0; border-radius: 6px; border: 1px solid #e5e7eb;">
<h4 style="margin-top: 0; color: #1f2937;">Product B - Mouse <span class="scope-indicator">.item-2</span></h4>
<div style="display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; align-items: end;">
<div class="form-group">
<label>Quantity <span class="scope-indicator">hx-name</span></label>
<input type="number" hx-name="quantity" value="1" min="1" style="width: 100%;">
</div>
<div class="form-group">
<label>Price ($) <span class="scope-indicator">hx-name</span></label>
<input type="number" hx-name="price" value="49.99" step="0.01" readonly style="width: 100%; background: #f3f4f6;">
</div>
<div class="form-group">
<button hx-post="/calculate?item_id=2&product_id=mouse" hx-scope=".item-2 input[hx-name]" hx-target="#line-2-result" hx-swap="outerHTML" style="width: 100%; margin: 0;">
Calculate
</button>
</div>
</div>
<div id="line-2-result">
<div style="margin-top: 10px; font-size: 16px; font-weight: 500; color: #059669;">
Line Total: $49.99
</div>
<input type="hidden" name="line_total_2" id="line_total_2" value="49.99">
<input type="hidden" name="product_id_2" value="mouse">
</div>
</div>
<!-- Line Item 3 -->
<div class="line-item item-3" style="background: #f9fafb; padding: 15px; margin: 15px 0; border-radius: 6px; border: 1px solid #e5e7eb;">
<h4 style="margin-top: 0; color: #1f2937;">Product C - Keyboard <span class="scope-indicator">.item-3</span></h4>
<div style="display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; align-items: end;">
<div class="form-group">
<label>Quantity <span class="scope-indicator">hx-name</span></label>
<input type="number" hx-name="quantity" value="3" min="1" style="width: 100%;">
</div>
<div class="form-group">
<label>Price ($) <span class="scope-indicator">hx-name</span></label>
<input type="number" hx-name="price" value="79.99" step="0.01" readonly style="width: 100%; background: #f3f4f6;">
</div>
<div class="form-group">
<button hx-post="/calculate?item_id=3&product_id=keyboard" hx-scope=".item-3 input[hx-name]" hx-target="#line-3-result" hx-swap="outerHTML" style="width: 100%; margin: 0;">
Calculate
</button>
</div>
</div>
<div id="line-3-result">
<div style="margin-top: 10px; font-size: 16px; font-weight: 500; color: #059669;">
Line Total: $239.97
</div>
<input type="hidden" name="line_total_3" id="line_total_3" value="239.97">
<input type="hidden" name="product_id_3" value="keyboard">
</div>
</div>
<hr style="margin: 20px 0;">
<div style="text-align: right; font-size: 18px; font-weight: 600; margin: 20px 0;">
Cart Total: $2,089.94
</div>
<button type="button" hx-post="/api/checkout" hx-include="[name]" hx-target="#checkout-result" style="background: #059669; font-size: 16px;">
Proceed to Checkout (Submit Form Data)
</button>
<div id="checkout-result" class="response"></div>
</form>
<div style="margin-top: 20px; padding: 15px; background: #fef3c7; border-left: 4px solid #f59e0b; border-radius: 4px; font-size: 13px;">
<strong>What's happening:</strong>
<ul style="margin: 10px 0; padding-left: 20px;">
<li><strong>Calculate buttons:</strong> Use <code>hx-scope</code> to send only <code>quantity</code> and <code>price</code> from their specific line item to <code>/calculate</code></li>
<li><strong>Each line item:</strong> Uses the same <code>hx-name="quantity"</code> and <code>hx-name="price"</code> - no conflicts!</li>
<li><strong>Checkout button:</strong> Only sends the hidden inputs with <code>name</code> attribute: <code>line_total_1</code>, <code>product_id_1</code>, <code>line_total_2</code>, etc.</li>
<li><strong>NOT sent on checkout:</strong> The intermediate <code>quantity</code> and <code>price</code> values (they use <code>hx-name</code>, not <code>name</code>)</li>
</ul>
<p style="margin: 10px 0 0 0;"><strong>Try it:</strong> Change a quantity, click Calculate to update that line's total, then click Checkout to see only the final totals are submitted!</p>
</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>