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
Enhance JavaScript Patterns documentation with additional explanations on AJAX form submission, SweetAlert usage, dynamic content loading, error handling, and user input handling for improved clarity and usability.
Copy file name to clipboardExpand all lines: docs/ui/javascript-patterns.md
+48Lines changed: 48 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,6 +20,8 @@ Unraid includes these JavaScript libraries by default:
20
20
21
21
## AJAX Form Submission
22
22
23
+
Use jQuery's `$.post()` method to submit forms without a full page reload. This pattern intercepts the form's submit event, serializes the form data, and sends it to your PHP handler. The response can be JSON for programmatic handling or trigger a page reload on success.
24
+
23
25
```javascript
24
26
$(function() {
25
27
$('#myForm').on('submit', function(e) {
@@ -40,6 +42,8 @@ $(function() {
40
42
41
43
## Saving Settings with $.post()
42
44
45
+
For simpler cases where you don't need form serialization, you can build the POST data object manually. This is useful when saving individual settings or triggering actions from buttons rather than forms. Always include the CSRF token for security.
46
+
43
47
```javascript
44
48
functionsaveSettings() {
45
49
$.post('/update.php', {
@@ -56,13 +60,17 @@ function saveSettings() {
56
60
57
61
### Simple Alert
58
62
63
+
The simplest form of SweetAlert takes three arguments: title, message, and type. Use this for quick notifications that don't require user input or confirmation.
64
+
59
65
```javascript
60
66
swal('Title', 'Message text', 'info');
61
67
// Types: 'success', 'error', 'warning', 'info'
62
68
```
63
69
64
70
### Confirmation Dialog
65
71
72
+
Use confirmation dialogs before destructive actions like deletion or irreversible changes. The callback receives `true` if the user confirmed, allowing you to proceed with the action only after explicit approval.
73
+
66
74
```javascript
67
75
swal({
68
76
title:'Are you sure?',
@@ -80,6 +88,8 @@ swal({
80
88
81
89
### Input Dialog
82
90
91
+
Input dialogs collect a single text value from the user. The callback receives `false` if cancelled, an empty string if submitted empty, or the actual input value. Use `swal.showInputError()` to display validation errors without closing the dialog.
92
+
83
93
```javascript
84
94
swal({
85
95
title:'Enter value',
@@ -98,6 +108,8 @@ swal({
98
108
99
109
## Dynamic Content Loading
100
110
111
+
Load content dynamically via AJAX to update portions of the page without a full reload. This pattern is useful for status displays, logs, or any content that changes frequently. Use `setInterval()` for automatic refresh, but remember to clear the interval when the page unloads.
Conditionally show or hide form sections based on user selections. This improves UX by hiding irrelevant options. The `.trigger('change')` call at the end ensures the correct visibility state is set when the page first loads, not just when the user changes the value.
Page files can mix PHP and JavaScript, allowing you to inject server-side values into your client-side code. Use `addslashes()` when outputting PHP strings into JavaScript to prevent quote characters from breaking your code. The CSRF token is accessed from the `$var` superglobal.
142
+
127
143
```php
128
144
<?
129
145
// Your PHP code
@@ -147,6 +163,8 @@ $(function() {
147
163
148
164
## Event Handlers
149
165
166
+
These are the most common jQuery event patterns you'll use in Unraid plugins. The document ready handler ensures DOM elements exist before your code runs. Use `.on()` for event binding as it's the modern jQuery approach and supports event delegation.
Always handle AJAX errors gracefully. Network failures, server errors, and timeouts can all occur. The `$.ajax()` method provides an `error` callback for these cases. Display user-friendly error messages rather than exposing technical details.
190
+
171
191
```javascript
172
192
$.ajax({
173
193
url:'/plugins/yourplugin/action.php',
@@ -186,6 +206,8 @@ $.ajax({
186
206
187
207
### Simple Loading Spinner
188
208
209
+
Provide visual feedback during AJAX operations so users know something is happening. A simple spinner or "Loading..." message prevents users from clicking repeatedly or thinking the UI is broken.
For operations with measurable progress (file uploads, batch processing), a progress bar provides better feedback than a spinner. This example includes both a visual bar and text percentage. The CSS transition creates smooth animation as the bar width changes.
231
+
208
232
```html
209
233
<divid="progress-container"style="display:none;">
210
234
<divclass="progressbar">
@@ -243,6 +267,8 @@ function hideProgress() {
243
267
244
268
### Long-Running Task with Progress
245
269
270
+
For server-side tasks that take significant time (backup, conversion, download), use a polling pattern. Start the task and receive a task ID, then periodically query the server for progress updates. This avoids HTTP timeouts and provides real-time feedback.
271
+
246
272
```javascript
247
273
functionstartLongTask() {
248
274
updateProgress(0, 'Starting...');
@@ -278,6 +304,8 @@ SweetAlert is included in Unraid and provides attractive modal dialogs.
278
304
279
305
#### Basic Alerts
280
306
307
+
SweetAlert provides more attractive and customizable alerts than the browser's native `alert()` function. The type parameter controls the icon displayed: success (green checkmark), error (red X), warning (yellow exclamation), or info (blue i).
308
+
281
309
```javascript
282
310
// Simple alert
283
311
swal('Title', 'Message text', 'info');
@@ -294,6 +322,8 @@ swal({
294
322
295
323
#### Confirmation Dialog
296
324
325
+
Require explicit user confirmation before destructive operations. The red confirm button color (`#d33`) signals danger. Customize button text to clearly describe what will happen ("Yes, delete it!" is clearer than "OK").
326
+
297
327
```javascript
298
328
swal({
299
329
title:'Are you sure?',
@@ -312,6 +342,8 @@ swal({
312
342
313
343
#### Input Dialog
314
344
345
+
Collect user input inline without navigating to a separate form. The `inputValue` option pre-fills the field with a default. Handle three cases: cancelled (false), empty string, or valid input. Use `swal.showInputError()` for inline validation without closing the dialog.
346
+
315
347
```javascript
316
348
swal({
317
349
title:'Enter Name',
@@ -333,6 +365,8 @@ swal({
333
365
334
366
#### HTML Content Dialog
335
367
368
+
Enable the `html: true` option to render HTML markup in the dialog body. This allows formatted content like tables, lists, or styled text. Be cautious with user-provided content—sanitize it to prevent XSS attacks.
369
+
336
370
```javascript
337
371
swal({
338
372
title:'Details',
@@ -346,6 +380,8 @@ swal({
346
380
347
381
#### Loading Dialog
348
382
383
+
Display a modal loading indicator during async operations to prevent user interaction. Disable the confirm button and outside clicks to keep users from dismissing it prematurely. Call `swal.close()` when the operation completes.
When SweetAlert doesn't meet your needs (complex forms, rich content, custom layouts), build a custom modal. This pattern includes the overlay background, close button, escape key handling, and click-outside-to-close behavior that users expect from modal dialogs.
@@ -429,6 +467,8 @@ Unraid includes `dynamix.js` with useful utilities:
429
467
430
468
### Common Functions
431
469
470
+
Unraid's `dynamix.js` provides utility functions for common operations. These handle locale-aware number formatting and file size display, ensuring consistency with the rest of the Unraid UI. Use these instead of writing your own formatting code.
471
+
432
472
```javascript
433
473
// Format file size
434
474
var size =my_scale(bytes); // Returns "1.5 GB" etc.
@@ -447,6 +487,8 @@ function done() {
447
487
448
488
### Refresh and Reload
449
489
490
+
Unraid provides a `refresh()` function for updating specific page sections without a full reload. For complete page refreshes, use the standard `location.reload()`. Pass `true` to force a cache-clearing reload when you need the latest server content.
491
+
450
492
```javascript
451
493
// Refresh specific section
452
494
functionrefresh(section) {
@@ -464,6 +506,8 @@ location.reload(true);
464
506
465
507
### Basic Shortcuts
466
508
509
+
Add keyboard shortcuts for power users. Common conventions include Ctrl+S to save and Escape to close/cancel. Always call `e.preventDefault()` to stop the browser's default behavior (Ctrl+S normally opens a "Save Page" dialog).
Prevent users from accidentally submitting a form multiple times by clicking rapidly or pressing Enter repeatedly. This pattern disables the submit button after the first click and uses a data attribute to track submission state.
668
+
623
669
```javascript
624
670
$('#myForm').on('submit', function(e) {
625
671
var $button =$(this).find('input[type="submit"]');
Debouncing delays execution until the user stops typing, preventing excessive API calls on every keystroke. The 300ms delay is a good balance—short enough to feel responsive, long enough to avoid firing during normal typing. Clear the previous timer on each keystroke to reset the delay.
0 commit comments