-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(finder): Remove jQuery dep from admin
Remove jQuery dependency from admins scripts
- Loading branch information
Showing
1 changed file
with
47 additions
and
25 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
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); | ||
}); | ||
}); | ||
}); |