Skip to content

Commit

Permalink
refactor(finder): Remove jQuery dep from admin
Browse files Browse the repository at this point in the history
Remove jQuery dependency from admins scripts
  • Loading branch information
zooley committed May 2, 2024
1 parent 5ddbe75 commit c8c6292
Showing 1 changed file with 47 additions and 25 deletions.
72 changes: 47 additions & 25 deletions app/Modules/Finder/Resources/assets/js/admin.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,67 @@
/* global $ */ // jquery.js
/* global Halcyon */ // core.js
/* global Handlebars */ // handlebars.js

document.addEventListener('DOMContentLoaded', function () {
$('body').on('click', '.remove-choice', function (e) {
document.querySelector('body').addEventListener('click', function (e) {
if (!e.target.matches('.remove-choice')) {
return;
}
e.preventDefault();

var result = confirm($(this).data('confirm'));
var result = confirm(this.getAttribute('data-confirm'));

if (result) {
var field = $($(this).attr('href'));
var field = document.getElementById(this.getAttribute('href').replace('#', ''));

// delete relationship
$.ajax({
url: $(this).data('api'),
type: 'delete',
dataType: 'json',
async: false,
success: function () {
if (this.getAttribute('data-api')) {
fetch(this.getAttribute('data-api'), {
method: 'DELETE',
headers: {
'Authorization': 'Bearer ' + document.querySelector('meta[name="api-token"]').getAttribute('content'),
'Content-Type': 'application/json'
}
})
.then(function (response) {
if (response.ok) {
return response.json();
}

return response.json().then(function (data) {
var msg = data.message;
if (typeof msg === 'object') {
msg = Object.values(msg).join('<br />');
}
throw msg;
});
})
.then(function () {
Halcyon.message('success', 'Item removed');
field.remove();
},
error: function (xhr) {
Halcyon.message('danger', xhr.responseJSON.message);
}
});
})
.catch(function (error) {
Halcyon.message('danger', error);
});
} else {
field.remove();
}
}
});

$('.add-choice').on('click', function (e) {
e.preventDefault();
document.querySelectorAll('.add-choice').forEach(function (el) {
el.addEventListener('click', function (e) {
e.preventDefault();

var source = $($(this).attr('data-template')).html();
var container = $($(this).attr('data-container'));
var source = document.getElementById(this.getAttribute('data-template').replace('#', '')).html();
var container = document.getElementById(this.getAttribute('data-container').replace('#', ''));

var template = Handlebars.compile(source),
context = {
"i": container.find('fieldset').length
},
html = template(context);
var template = Handlebars.compile(source),
context = {
"i": container.find('fieldset').length
},
html = template(context);

container.append(html);
container.append(html);
});
});
});

0 comments on commit c8c6292

Please sign in to comment.