forked from aces/Loris
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[configuration] adds confirmation prompt when deleting a field. (aces…
…#7069) Adds confirmation prompt when deleting a field in the configuration module. Related issue aces#7010
- Loading branch information
Showing
6 changed files
with
162 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import swal from 'sweetalert2'; | ||
|
||
$(function() { | ||
'use strict'; | ||
|
||
$('div').tooltip(); | ||
|
||
let count = 0; | ||
$('.add').click(function(e) { | ||
e.preventDefault(); | ||
|
||
count = count + 1; | ||
|
||
// Field that will be copied | ||
let currentField = $(this).parent().find('.entry:first-child'); | ||
|
||
let id = $(currentField).parent().attr('id'); | ||
let name = 'add-' + id + '-' + count; | ||
|
||
// Setup the new form field | ||
let newField = currentField.clone(); | ||
newField.find('.form-control').attr('name', name); | ||
$(newField).find('.btn-remove') | ||
.addClass('remove-new') | ||
.removeClass('btn-remove'); | ||
resetForm(newField); | ||
|
||
newField.appendTo($(this).parent().children(':first')); | ||
}); | ||
|
||
$('body').on('click', '.remove-new', function() { | ||
if ($(this).parent().parent().parent().children().length > 1) { | ||
$(this).parent().parent().remove(); | ||
} else { | ||
resetForm($(this).parent().parent()); | ||
} | ||
}); | ||
|
||
$('.btn-remove').click(function(e) { | ||
e.preventDefault(); | ||
|
||
let options = $(this).parent().parent().children().prop('options'); | ||
let selectedIndex = $(this) | ||
.parent().parent().children() | ||
.prop('selectedIndex'); | ||
let selectedOption = options[selectedIndex].text; | ||
let fieldName = $(this) | ||
.parent().parent().parent().parent().parent().children() | ||
.attr('data-original-title'); | ||
|
||
swal.fire({ | ||
text: 'Please confirm you want to delete the option "' + | ||
selectedOption | ||
+ '" of the field "' + fieldName + '".', | ||
type: 'warning', | ||
showCancelButton: true, | ||
confirmButtonText: 'Confirm', | ||
cancelButtonText: 'Cancel', | ||
}).then((result) => { | ||
if (result.value) { | ||
let id = $(this).attr('name'); | ||
let button = this; | ||
|
||
$.ajax({ | ||
type: 'post', | ||
url: loris.BaseURL + '/configuration/ajax/process.php', | ||
data: {remove: id}, | ||
success: function() { | ||
if ($(button) | ||
.parent().parent().parent().children() | ||
.length > 1 | ||
) { | ||
$(button).parent().parent().remove(); | ||
} else { | ||
let parentID = $(button) | ||
.parent().parent().parent() | ||
.attr('id'); | ||
let name = 'add-' + parentID; | ||
|
||
resetForm($(button).parent().parent()); | ||
$(button) | ||
.parent().parent().children('.form-control') | ||
.attr('name', name); | ||
$(button) | ||
.addClass('remove-new') | ||
.removeClass('btn-remove'); | ||
} | ||
}, | ||
error: function(xhr, desc, err) { | ||
console.error(xhr); | ||
console.error('Details: ' + desc + '\nError:' + err); | ||
}, | ||
}); | ||
} | ||
}); | ||
}); | ||
|
||
// On form submit, process the changes through an AJAX call | ||
$('form').on('submit', function(e) { | ||
e.preventDefault(); | ||
|
||
let form = $(this).serialize(); | ||
|
||
// Clear previous feedback | ||
$('.submit-area > label').remove(); | ||
|
||
$.ajax({ | ||
type: 'post', | ||
url: loris.BaseURL + '/configuration/ajax/process.php', | ||
data: form, | ||
success: function() { | ||
let html = '<label>Submitted</label>'; | ||
$(html) | ||
.hide() | ||
.appendTo('.submit-area') | ||
.fadeIn(500).delay(1000).fadeOut(500); | ||
location.reload(); | ||
}, | ||
error: function(xhr, desc, err) { | ||
let html = '<label>' + xhr.responseText + '</label>'; | ||
$(html).hide().appendTo('.submit-area').fadeIn(500).delay(1000); | ||
}, | ||
}); | ||
}); | ||
|
||
// On form reset, to delete the elements added with the "Add field" button that were not submitted. | ||
$('form').on('reset', function(e) { | ||
$('.tab-pane.active').find('select[name^="add-"]').parent().remove(); | ||
}); | ||
}); | ||
|
||
/* | ||
function validate(form) { | ||
// age | ||
// year | ||
// email - this should be done already | ||
// not same instrument twice | ||
} | ||
*/ | ||
|
||
/** | ||
* Reset form | ||
* | ||
* @param {Element} form A DOM form element | ||
*/ | ||
function resetForm(form) { | ||
'use strict'; | ||
|
||
$(form).find( | ||
'input:text, input:password, input:file, select, textarea' | ||
).val(''); | ||
$(form).find('input:radio, input:checkbox') | ||
.removeAttr('checked').removeAttr('selected'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters