Skip to content
This repository was archived by the owner on Apr 20, 2024. It is now read-only.

Commit a9c3601

Browse files
Merge pull request #111 from nodes-vapor/feature/confirm-modal-updates
Feature/confirm modal updates
2 parents 2e138f8 + 7a7d862 commit a9c3601

File tree

2 files changed

+85
-8
lines changed

2 files changed

+85
-8
lines changed

Public/AdminPanel/js/modal-confirmation.js

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,19 @@ let modalConfirmation = (function() {
1010
modalText = !modalText ? 'Are you sure you want to continue?' : modalText;
1111

1212
// Confirmation path
13-
let modalConfirmationPath = $(element).attr('href')
13+
let modalConfirmationPath = $(element).attr('href');
1414

1515
// Confirmation type
1616
let modalConfirmButtonType = $(element).data('button');
17-
modalConfirmButtonType = !modalConfirmButtonType ? 'primary' : modalConfirmButtonType
17+
modalConfirmButtonType = !modalConfirmButtonType ? 'primary' : modalConfirmButtonType;
18+
19+
// Confirmation button text
20+
let confirmButtonText = $(element).data('confirm-btn');
21+
confirmButtonText = !confirmButtonText ? 'Confirm' : confirmButtonText;
22+
23+
// Dismiss button text
24+
let dismissButtonText = $(element).data('dismiss-btn');
25+
dismissButtonText = !dismissButtonText ? 'Close' : dismissButtonText;
1826

1927
let closure = function(e) {
2028
e.preventDefault();
@@ -23,7 +31,7 @@ let modalConfirmation = (function() {
2331
<div class="modal fade" id="modalConfirmation" tabindex="-1" role="dialog" aria-labelledby="modalConfirmationLabel" aria-hidden="true">
2432
<div class="modal-dialog" role="document">
2533
<div class="modal-content">
26-
<form method="POST" action="${modalConfirmationPath}">
34+
<form method="POST" action="${modalConfirmationPath}" id="confirmModalForm">
2735
<div class="modal-header">
2836
<h5 class="modal-title">${modalTitle}</h5>
2937
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
@@ -34,23 +42,52 @@ let modalConfirmation = (function() {
3442
<p>${modalText}</p>
3543
</div>
3644
<div class="modal-footer">
37-
<button type="submit" class="btn btn-${modalConfirmButtonType}">Confirm</button>
38-
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
45+
<button type="submit" class="btn btn-${modalConfirmButtonType}">${confirmButtonText}</button>
46+
<button type="button" class="btn btn-secondary" data-dismiss="modal">${dismissButtonText}</button>
3947
</div>
4048
</form>
4149
</div>
4250
</div>
4351
</div>
4452
`;
4553

46-
$('body').append(modal)
47-
$('#modalConfirmation').modal()
54+
$('body').append(modal);
55+
$('#modalConfirmation').modal();
56+
57+
// Custom "Confirm"-action
58+
if (typeof modalConfirmation.actions.confirm === "function") {
59+
// Identify confirm button, interrupt default behavior, execute custom action
60+
let confirmBtn = $("#modalConfirmation .modal-footer button[type='submit']");
61+
62+
confirmBtn.click(function(event) {
63+
event.preventDefault();
64+
event.stopPropagation();
65+
modalConfirmation.actions.confirm(event);
66+
});
67+
}
68+
69+
// Custom "Dismiss"-action
70+
if (typeof(modalConfirmation.actions.dismiss) === "function") {
71+
// Identify dismiss button, interrupt default behavior, execute custom action
72+
let dismissBtn = $("#modalConfirmation .modal-footer button[type='button']");
73+
74+
dismissBtn.click(function(event) {
75+
event.stopPropagation();
76+
event.preventDefault();
77+
modalConfirmation.actions.dismiss(event);
78+
});
79+
}
80+
4881
$('#modalConfirmation').on('hidden.bs.modal', function (e) {
49-
$('#modalConfirmation').remove()
82+
$('#modalConfirmation').remove();
5083
})
5184
};
5285

5386
$(element).click(closure);
87+
},
88+
actions: {
89+
confirm: null,
90+
dismiss: null
5491
}
5592
}
5693
})();

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,43 @@
11
# admin-panel
22

33
A description of this package.
4+
5+
6+
#### Confirm Modal
7+
8+
Admin Panel includes a generic confirmation modal for links, out of the box. Using HTML data attributes on `<a>`-tags the modal can be configured in different ways. Just add a data attribute to your link and you're all set.
9+
10+
Triggering the modal will append a HTML-element form to the DOM, containing title, text, confirm button and dismiss button.
11+
12+
By default confirm submits the form and dismiss will remove the HTML-element from the DOM
13+
14+
**Basic usage**
15+
16+
```HTML
17+
<a href="#" data-confirm="true">Open modal</a>
18+
```
19+
20+
**Data Attributes**
21+
22+
|Attribute|Description|example|
23+
|---------|-----------|-------|
24+
|data-confirm|Initialize the modal|`data-confirm="true"`|
25+
|data-title|Sets the title of the modal|`data-title="Please confirm"`|
26+
|data-text|Sets the text of the modal|`data-text="Are you sure you want to continue?"`|
27+
|data-button|Sets bootstrap css selector for the confirm button|`data-button="danger"` _[primary,secondary,success,danger,warning,info,light,dark]_|
28+
|data-confirm-btn|Set the text label on the "confirm"-button|`data-confirm-btn="Yes"`|
29+
|data-dismiss-btn|Set the text label on the "dismiss"-button|`data-confirm-btn="No"`|
30+
31+
**Override default behavior**
32+
33+
```javascript
34+
// Override modal confirm action
35+
modalConfirmation.actions.confirm = function(event) {
36+
alert("Confirmed");
37+
}
38+
39+
// Overríde modal dismiss action
40+
modalConfirmation.actions.dismiss = function(event) {
41+
alert("Dismissed");
42+
}
43+
```

0 commit comments

Comments
 (0)