Files
Saxonite/SaxoniteCoreWebAdmin/wwwroot/js/sax-org-ass.js
T
2022-12-12 14:20:49 +00:00

217 lines
8.8 KiB
JavaScript

/*
* Admin - Organisation Association scripts.
*/
(function (win, doc, undef)
{
"use strict";
let oa = sax.mdl.organisationAssociations = { data: {}, ajax: {} };
let $pnl = oa.$pnl = {}, $frm = oa.$frm = {}, $dlg = oa.$dlg = {};
let mdlOrg = function () { return sax.mdl.organisations; }
//--------------------------------------------------------------
oa.refreshResults = function (delay)
{
if (!oa.awaitingRefreshResponse)
{
var $items = $('.sax-panel-item-list', $pnl.results).fadeOut(200).empty();
var $msgs = $('.sax-panel-messages', $pnl.results).hide().empty().append($('<div class="sax-message">' + sax.msg.dataQuerying + '</div>')).fadeIn(200);
}
oa.ajax.refreshResults = oa.ajax.refreshResults || { url: sax.url.base + 'Organisation/Ajax/GetAssociationList', formId: 'frmAssociationFilter', callbackFn: oa.doRefreshResults };
sax.doAjaxRequest(oa.ajax.refreshResults, delay || 50);
oa.awaitingRefreshResponse = true;
}
oa.doRefreshResults = function (response)
{
//sax.debug('doRefreshResults', response);
delete oa.awaitingRefreshResponse;
sax.refreshControlValues(response.controlValues);
var $msgs = $('.sax-panel-messages', $pnl.results).hide().empty();
var $items = $('.sax-panel-item-list', $pnl.results).hide().empty();
sax.refreshPaging(response, '#frmAssociationFilter', oa.refreshResults);
if (!response.success) return;
// Reference data - association types.
if (response.associationTypes && response.associationTypes.length)
oa.data.associationTypes = response.associationTypes;
// Reference data - Statuses.
if (response.defaultStatusCode) oa.data.defaultStatusCode = response.defaultStatusCode;
if (response.statuses)
sax.populateSelectCodeDesc('#cbAssociationStatus', response.statuses);
// Menu.
var $bgr = $('.sax-burger', $pnl.filter).hide();
var $mnu = $('ul.sax-dropdown-menu', $bgr).empty();
$mnu.append(oa.getFilterMenuOption(sax.lbl.refresh, function () { oa.refreshResults(); }, 'sax-refresh'));
$mnu.append(oa.getFilterMenuOption(sax.lbl.add, function () { oa.createItem(); }, 'sax-add'));
$bgr.toggle(!!$('.sax-dropdown-item', $mnu).length);
// Items.
var items = response.associations;
if (!items.length)
return $msgs.append($('<div class="sax-message">' + sax.msg.dataNoneFound + '</div>')).fadeIn(500);
for (let itemIdx = 0; itemIdx < items.length; itemIdx++)
$items.append(sax.populateItem(oa.getItemTemplate(), items[itemIdx]));
$items.fadeIn(50);
}
sax.populateItem = function ($item, item)
{
if (!item) return;
$item.prop('id', oa.itemIdPrefix + item.id);
$('.UserEmailAddress', $item).text(item.emailAddress);
$('.UserStatusDesc', $item).text(item.userStatusDesc).addClass('sax-status-' + (item.userStatusCode || '').toLowerCase());
$('.UserStatus', $item).toggle(!item.userActive);
$('.UserDisplayName', $item).text(item.displayName);
$('.AssociationRoleCount .RoleCount', $item).text(item.roleCount);
$('.StatusDesc', $item).text(item.statusDesc).addClass('sax-status-' + (item.statusCode || '').toLowerCase());
$('.sax-caption .sax-action').toggleClass('sax-action', !item.deleted);
let $mnu = $('ul.sax-dropdown-menu', $item).empty();
if (!item.deleted)
{
let fnUpdItm = function () { oa.updateItem(item, $item) };
$mnu.append(oa.getItemMenuOption(sax.lbl.edit, fnUpdItm, 'sax-edit'));
$item.on('click', '.sax-header .sax-caption', fnUpdItm).on('dblclick', fnUpdItm);
if (item.association)
$mnu.append(oa.getItemMenuOption(sax.lbl.remove, function () { oa.removeItem(item, $item) }, 'sax-remove'));
}
$('.sax-burger', $item).toggle(!!$('.sax-dropdown-item', $mnu).length);
$('.sax-panel-messages', $pnl.results).hide().empty();
return $item;
}
//--------------------------------------------------------------
oa.getFilterDropdownTemplate = function ()
{
return $('.sax-panel-item-template .sax-dropdown-menu.AssociationFilter li.sax-dropdown-item', $pnl.filter).clone();
}
oa.getItemTemplate = function ()
{
return $('.sax-panel-item-template .sax-panel-item.Association', $pnl.results).clone();
}
oa.getItemDropdownTemplate = function ()
{
return $('.sax-panel-item-template .sax-dropdown-menu.Association li.sax-dropdown-item', $pnl.results).clone();
}
oa.getFilterMenuOption = function (lbl, fn, cls)
{
return oa.getMenuOption(oa.getFilterDropdownTemplate(), lbl, fn, cls);
}
oa.getItemMenuOption = function (lbl, fn, cls)
{
return oa.getMenuOption(oa.getItemDropdownTemplate(), lbl, fn, cls);
}
oa.getMenuOption = function ($opt, lbl, fn, cls)
{
return $opt.find('div.sax-label').text(lbl).end().on('click', fn).addClass(cls);
}
//--------------------------------------------------------------
oa.selectFilterOrganisation = function ()
{
sax.selectOrganisation.fn.openDialog({ caption: sax.dlg.title.selectOrganisation, fnCallback: oa.onFilterOrganisationSelected });
}
oa.onFilterOrganisationSelected = function (id, name)
{
$('#ifFilterOrganisationId', $frm.filter).val(id);
$('#ifFilterOrganisation', $frm.filter).val(name);
oa.refreshResults(50);
}
//--------------------------------------------------------------
oa.selectAssocOrganisation = function ()
{
sax.selectOrganisation.fn.openDialog({ caption: sax.dlg.title.selectOrganisation, fnCallback: oa.onAssocOrganisationSelected });
}
oa.onAssocOrganisationSelected = function (id, name)
{
$('.OrganisationIdAssoc', $frm.assoc).val(id);
$('.OrganisationNameAssoc', $frm.assoc).val(name);
}
//--------------------------------------------------------------
oa.createItem = function ()
{
oa.clearItem();
let frm = $frm.assoc;
$('.NewItem', frm).val(1);
$('.OrganisationId', frm).val($('.OrganisationId', $frm.filter).val());
$('.OrganisationName', frm).val($('.OrganisationName', $frm.filter).val());
sax.openAssocDialog();
}
oa.clearItem = function ()
{
let frm = $frm.assoc;
$('.ItemAssociationId,.NewItem,.OrganisationId,.OrganisationIdAssoc,input[type="text"],textarea,select', frm).val('');
sax.populateSelect($('.sax-select.AssociationTypeOption', $frm.assoc), oa.data.associationTypes, 'type', 'label');
sax.clearControlErrors(frm);
}
sax.openAssocDialog = function ()
{
sax.openDialog($dlg.assoc, 640, 480);
}
//--------------------------------------------------------------
oa.updateItem = function (item, $item)
{
$('.OrganisationId', $frm.orgId).val(item.id);
oa.ajax.getItemAssoc = oa.ajax.getItemAssoc || { url: sax.url.base + 'Organisation/Ajax/GetAssociation', formId: 'frmOrganisationId', callbackFn: oa.onUpdateItem };
sax.doAjaxRequest(oa.ajax.getItemAssoc, 50);
}
oa.onUpdateItem = function (response)
{
//sax.debug('onUpdateItemAssociation', response);
if (!response.success) return;
oa.clearItem();
// Association.
let item = response.organisation;
let assoc = item.association || {};
let frm = $frm.assoc;
$('input.ItemAssociationId', frm).val(item.id);
$('input.OrganisationId', frm).val(item.organisationId);
$('.OrganisationName', frm).val(item.name);
sax.openAssocDialog();
}
//--------------------------------------------------------------
oa.storeItem = function ()
{
oa.ajax.storeItemAssoc = oa.ajax.storeItemAssoc || { url: sax.url.base + 'Organisation/Ajax/StoreAssociation', formId: 'frmOrganisationAssociation', callbackFn: oa.onStoreItem };
sax.doAjaxRequest(oa.ajax.storeItemAssoc, 50);
}
oa.onStoreItem = function (response)
{
//sax.debug('onStoreItemAssociation', response);
sax.refreshControlValues(response.controlValues, $frm.assoc);
if (!response.success) return;
$dlg.assoc.dialog('close');
mdlOrg().refreshItem(response.organisation);
}
//--------------------------------------------------------------
$(function ()
{
$pnl.assocs = $('.sax-panel.OrganisationAssociations');
$pnl.filter = $('.sax-panel-filter', $pnl.assocs);
$pnl.results = $('.sax-panel-results', $pnl.assocs);
$frm.filter = $('#frmAssociationFilter', $pnl.assocs);
$frm.filter.on('dblclick', '#ifFilterOrganisation', oa.selectFilterOrganisation).on('click', '#pbFilterOrganisationSelect', oa.selectFilterOrganisation);
$frm.assocId = $('#frmAssociationId');
var opt = sax.dlgOpts();
opt.buttons[sax.dlg.btn.ok] = function () { oa.storeItem(); };
opt.buttons[sax.dlg.btn.cancel] = function () { $(this).dialog('close'); };
$dlg.assoc = $('#dlgOrganisationAssociation').dialog(opt);
$frm.assoc = $('#frmOrganisationAssociation', $dlg.assoc);
$frm.assoc.on('input', '.sax-input', function () { sax.clearControlError(this); });
$frm.assoc.on('change', '.sax-select', function () { sax.clearControlError(this); });
$frm.assoc.on('dblclick', '.OrganisationAssoc', oa.selectAssocOrganisation).on('click', '#pbAssocOrganisationSelect', oa.selectAssocOrganisation);
setTimeout(oa.refreshResults, 250);
});
//--------------------------------------------------------------
}(window, window.document));
/* The End */