- Added an interface for organisation - Added beforeDelete and beforeMerge callbacks - Added hard/soft delete to organisations
175 lines
5.7 KiB
JavaScript
175 lines
5.7 KiB
JavaScript
/**
|
|
* Organisations widget. Sets up dropdowns, modals, etc for a table of organisations.
|
|
*/
|
|
|
|
/**
|
|
* Set up the form in a modal after being successfully attached to the body.
|
|
*/
|
|
function attachOrganisationForm() {
|
|
$("body").on('renderSuccess.ufModal', function(data) {
|
|
var modal = $(this).ufModal('getModal');
|
|
var form = modal.find('.js-form');
|
|
|
|
/**
|
|
* Set up modal widgets
|
|
*/
|
|
// Set up any widgets inside the modal
|
|
form.find(".js-select2").select2({
|
|
width: '100%'
|
|
});
|
|
|
|
// Auto-generate slug
|
|
form.find('input[name=name]').on('input change', function() {
|
|
var manualSlug = form.find('#form-organisation-slug-override').prop('checked');
|
|
if (!manualSlug) {
|
|
var slug = getSlug($(this).val());
|
|
form.find('input[name=slug]').val(slug);
|
|
}
|
|
});
|
|
|
|
form.find('#form-organisation-slug-override').on('change', function() {
|
|
if ($(this).prop('checked')) {
|
|
form.find('input[name=slug]').prop('readonly', false);
|
|
} else {
|
|
form.find('input[name=slug]').prop('readonly', true);
|
|
form.find('input[name=name]').trigger('change');
|
|
}
|
|
});
|
|
|
|
// Set up the form for submission
|
|
form.ufForm({
|
|
validator: page.validators
|
|
}).on("submitSuccess.ufForm", function() {
|
|
// Reload page on success
|
|
window.location.reload();
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Link organisation action buttons, for example in a table or on a specific organisation's page.
|
|
* @param {module:jQuery} el jQuery wrapped element to target.
|
|
* @param {{delete_redirect: string}} options Options used to modify behaviour of button actions.
|
|
*/
|
|
function bindOrganisationButtons(el, options) {
|
|
if (!options) options = {};
|
|
|
|
/**
|
|
* Link row buttons after table is loaded.
|
|
*/
|
|
|
|
/**
|
|
* Buttons that launch a modal dialog
|
|
*/
|
|
// Edit organisation details button
|
|
el.find('.js-organisation-edit').click(function(e) {
|
|
e.preventDefault();
|
|
|
|
$("body").ufModal({
|
|
sourceUrl: site.uri.public + "/modals/organisations/edit",
|
|
ajaxParams: {
|
|
slug: $(this).data('slug')
|
|
},
|
|
msgTarget: $("#alerts-page")
|
|
});
|
|
|
|
attachOrganisationForm();
|
|
});
|
|
|
|
// Manage organisation merge button
|
|
el.find('.js-organisation-merge').click(function(e) {
|
|
e.preventDefault();
|
|
|
|
var organisation_slug = $(this).data('slug');
|
|
|
|
$("body").ufModal({
|
|
sourceUrl: site.uri.public + "/modals/organisations/merge",
|
|
ajaxParams: {
|
|
slug: organisation_slug
|
|
},
|
|
msgTarget: $("#alerts-page")
|
|
});
|
|
|
|
$("body").on('renderSuccess.ufModal', function(data) {
|
|
var modal = $(this).ufModal('getModal');
|
|
var form = modal.find('.js-form');
|
|
|
|
var dropdownTemplate = modal.find('#organisation-select-option').html();
|
|
|
|
// Set up select2 dropdown
|
|
var dropdownControl = modal.find('.js-select-new');
|
|
_dropdownTemplateCompiled = Handlebars.compile(dropdownTemplate);
|
|
var options = {
|
|
ajax: {
|
|
url: site.uri.public + '/api/organisations',
|
|
processResults: function (data) {
|
|
var items = data.rows.filter((i) => i.slug != organisation_slug);
|
|
return {
|
|
results: items.map((i) => { return {
|
|
id: i.slug,
|
|
text: i.name,
|
|
name: i.name,
|
|
description: i.description,
|
|
slug: i.slug
|
|
}})
|
|
};
|
|
}
|
|
},
|
|
placeholder: "Select an organisation to merge with",
|
|
selectOnClose: true,
|
|
templateResult: $.proxy(function(item) {
|
|
// Must wrap this in a jQuery selector to render as HTML
|
|
return $(_dropdownTemplateCompiled(item));
|
|
}, this)
|
|
};
|
|
dropdownControl.select2(options);
|
|
|
|
// Set up form for submission
|
|
form.ufForm()
|
|
.on("submitSuccess.ufForm", function() {
|
|
// Reload page on success
|
|
window.location.reload();
|
|
});
|
|
});
|
|
});
|
|
|
|
// Delete organisation button
|
|
el.find('.js-organisation-delete').click(function(e) {
|
|
e.preventDefault();
|
|
|
|
$("body").ufModal({
|
|
sourceUrl: site.uri.public + "/modals/organisations/confirm-delete",
|
|
ajaxParams: {
|
|
slug: $(this).data('slug')
|
|
},
|
|
msgTarget: $("#alerts-page")
|
|
});
|
|
|
|
$("body").on('renderSuccess.ufModal', function() {
|
|
var modal = $(this).ufModal('getModal');
|
|
var form = modal.find('.js-form');
|
|
|
|
form.ufForm()
|
|
.on("submitSuccess.ufForm", function() {
|
|
// Navigate or reload page on success
|
|
if (options.delete_redirect) window.location.href = options.delete_redirect;
|
|
else window.location.reload();
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
function bindOrganisationCreationButton(el) {
|
|
// Link create button
|
|
el.find('.js-organisation-create').click(function(e) {
|
|
e.preventDefault();
|
|
|
|
$("body").ufModal({
|
|
sourceUrl: site.uri.public + "/modals/organisations/create",
|
|
msgTarget: $("#alerts-page")
|
|
});
|
|
|
|
attachOrganisationForm();
|
|
});
|
|
};
|