Alerts
Alerts can be either generated by the alert container or created and controlled manually. Allowing the container to generate them means that the positioning, stacking order and display timing will all be managed automatically and determined by the component initialisation properties. Theme colors are available in the WebUI primary signal colors (success, info, warning and danger).
Simple Example
The following example shows how to use alerts.
NOTE: Component instance properties are optional and can be omitted if the default settings are acceptable.
<div id="alertContainer1"></div> <button type="button" class="button-sm button-info" id="commandShowAlert"> Show Alert </button>
webui.ready( () => {
var alertContainer1 = ui("#alertContainer1").alertControl({
position: "top-center",
duration: 6000,
transitionDuration: 1000,
displayOrder: "ascending",
width: "60rem",
showHeader: true,
inline: true,
style: "outline-rounded",
autoHide: false,
showIcon: true,
showClose: true
});
ui("#commandShowAlert").click( () => {
alertContainer1.showInfoAlert("Alert - " + new Date().toTimeString(), true);
});
});
Alert Close ButtonWhen an alert close button is selected the alert container will take care of closing the alert.
Alert Classes
The alert classes available are described below.
Class group | Class | Description |
---|---|---|
- | alert-container | The element used to contain all component generated alerts. |
- | alert | Defines an alert |
- | alert-cancel-button | Defines an close button that will close an alert when selected. |
alert-success, alert-success-* | alert-success alert-success-icon alert-success-outline |
Used to implement an alert success theme. |
alert-info, alert-info-* | alert-info alert-info-icon alert-info-outline |
Used to implement an alert info theme. |
alert-warning, alert-warning-* | alert-warning alert-warning-icon alert-warning-outline |
Used to implement an alert warning theme. |
alert-danger, alert-danger-* | alert-danger alert-danger-icon alert-danger-outline |
Used to implement an alert danger theme. |
Functions
WebUI provides a set of standard JavaScript alert functions for displaying alerts - this means you don't have to write any HTML. The behaviour is controlled by the JavaScript initialisation as shown in the example above.
Function | Description |
---|---|
showAlert(message, type, auto, icon, close) | Shows an alert. |
showSuccessAlert(message, auto, icon, close) | Shows an alert styled in the success theme. |
showInfoAlert(message, auto, icon, close) | Shows an alert styled in the info theme. |
showWarningAlert(message, auto, icon, close) | Shows an alert styled in the warning theme. |
showDangerAlert(message, auto, icon, close) | Shows an alert styled in the danger theme. |
hideAlert(alert) | Hides an alert. |
update(newSettings) | Updates the alert container instance at runtime using the configuration settings. |
Function Arguments
Argument | Description |
---|---|
alert | A reference to manually created alerts such as an id or class name. (Not recommended for container generated alerts). |
message | The string or HTML message to be shown in an alert. |
type | The alert theme to use: Can be "success", "info", "warning", or "danger". |
auto | Whether the alert should hide automatically after the duration period.
Can be true or false. Default: true. Overrides the autoHide component setting. |
icon | Whether to show an icon corresponding to the type argument specified.
Can be true or false. Default: true. Overrides the showIcon component setting. |
close | Whether to show a close button.
Can be true or false. Default: true. Overrides the showClose component setting. |
newSettings | An object literal containing a comma separated list of settings. |
Settings
Javascript settings available for the alert control.
Property | Description |
---|---|
autoHide | Whether to hide the alert automatically using the duration and transition settings. Can be true or false. Default: true. |
displayOrder | The order in which the alerts are stacked. Can be either "ascending" or "descending". Default: "ascending". |
duration | The amount of time in milliseconds that the alert is visible. An integer value. Default: 3000 |
inline | Whether the header including the icon and close button is collapsed inline with the message. Can be true or false. Default: true. |
position | The initial starting position of the alert batch. Can be "top-left", "top-center", "top-right", "bottom-left", "bottom-center", or "bottom-right". Default: "top-right". |
showClose | Whether to show a close button. Can be true or false. Default: true. |
showHeader | Whether the header including the icon and close button is shown. Can be true or false. Default: true. |
showIcon | Whether to show an icon corresponding to the type argument specified. Can be true or false. Default: true. |
style | The basic alert style. Can be "outline-square", "ouline-rounded", "solid-square", "solid-rounded". Default: "outline-square". |
transitionDuration | The time taken in milliseconds for the alert to fade in and out. An integer value. Default: 300. |
width | The initial width of the alert container as a CSS string value, e.g. "60rem". Default: "25rem". |
Customisation
Alert Customisation
Alerts can also be written inline using markup. Any of the WebUI styles and JavaScript functions can be used to create the style and behaviour required.
NOTE: Because manually created custom alerts are not controlled by the alert container additional JavaScript is required to close the alert.
Alert Close ButtonThe alert close button can include the class alert-cancel-button which will automatically adjust the color to match the alert theme specified.
<div class="alert alert-info-outline rounded-md hidden" role="alert" id="alert1"> <div class="flex justify-content-space-between align-items-center height-xs-sm pad-left-md pad-right-md"> <div><span class='text-bold'>Alert</span> - Configure behaviour, structure, and style.</div> <div class="alert-cancel-button alert-close" role="button"></div> </div> </div> <div class="row-spacing-md"></div> <div class="alert alert-danger-outline rounded-md hidden" role="alert" id="alert2"> <div class="flex justify-content-space-between align-items-center height-sm pad-left-md pad-right-md"> <div class="alert-danger-icon"></div> <div><span class='text-bold'>Alert</span> - Configure behaviour, structure, and style.</div> <div class="alert-cancel-button alert-close" role="button"></div> </div> </div> <div class="row-spacing-md"></div> <div class="alert alert-success rounded-md pad-xs pad-left-md pad-right-md hidden" role="alert" id="alert3"> <div class="flex justify-content-space-between align-items-center"> <div class="alert-success-icon"></div> <div class="alert-cancel-button alert-close" role="button"></div> </div> <div class="flex align-items-center"> <div><span class='text-bold'>Alert</span> - Configure behaviour, structure, and style.</div> </div> </div>
webui.ready( () => { ui("#alert1, #alert2, #alert3").fadeIn(1000); ui(".alert-close").click( function() { var alert = ui(this).closest(".alert"); alert.hide(); }); });
Component Updates
Run-time Alert Updates
Settings specified for the alertControl instance can be updated programatically at run-time. See the following example, which updates the position and displayOrder settings. Use the restore button to revert the change.
<div class="row gap-md text-sm"> <div class="col-fixed"> <button id="alertUpdateShowCommand" class="button-sm menu-button-info">Show Alert</button> </div> <div class="col-fixed"> <button id="alertUpdateCommand" class="button-sm menu-button-info">Update</button> </div> <div class="col-fixed"> <button id="alertRestoreCommand" class="button-sm menu-button-info">Restore</button> </div> </div>
webui.ready( () => {
var alertContainer1 = ui("#alertContainer1").alertControl({
position: "top-center",
duration: 6000,
transitionDuration: 1000,
displayOrder: "ascending",
width: "60rem",
showHeader: true,
inline: true,
style: "outline-rounded",
autoHide: false,
showIcon: true,
showClose: true
});
ui("#alertUpdateShowCommand").click( () => {
alertContainer1.showSuccessAlert("Alert - " + new Date().toLocaleTimeString(), true);
});
ui("#alertUpdateCommand").click( () => {
alertContainer1.update({ position: "bottom-left", displayOrder: "descending" });
});
ui("#alertRestoreCommand").click( () => {
alertContainer1.update({ position: "top-center", displayOrder: "ascending" });
});
});
Functions
WebUI provides a single function that can be used to update the alert instance settings at runtime.
Function | Description |
---|---|
update(newSettings) | Updates the alert instance at runtime using the configuration settings. |
Function Arguments
Argument | Description |
---|---|
newSettings | An object literal containing a comma separated list of settings. |
Events
Alert Event Callbacks
There are 4 event callbacks that can be used, ui.alert.show.before, ui.alert.show.after, ui.alert.hide.before, and ui.alert.hide.after. The following example shows how to setup the event callbacks.
NOTE: If custom alerts are created and controlled outside the alert container, as in the example above, the following events are not triggered.
webui.ready( () => { alertContainer1.on("ui.alert.show.before", (e) => { // Take some action }); alertContainer1.on("ui.alert.show.after", (e) => { // Take some action }); alertContainer1.on("ui.alert.hide.before", (e) => { // Take some action }); alertContainer1.on("ui.alert.hide.after", (e) => { // Take some action }); });