Files
Saxonite/SaxoniteCoreWebAdmin/wwwroot/js/sax-organisation.js
T
2021-07-23 14:01:49 +01:00

376 lines
15 KiB
JavaScript

/*
* Admin - Organisation page scripts.
*/
(function (win, doc, undef)
{
"use strict";
sax.itemIdPrefix = 'Itm_';
var $results;
//--------------------------------------------------------------
sax.refreshResults = function (delay)
{
if (!sax.awaitingRefreshResponse)
{
var $items = $('.sax-panel-item-list', $results).fadeOut(200).empty();
var $msgs = $('.sax-panel-messages', $results).hide().empty().append($('<div class="sax-message">' + sax.msg.dataQuerying + '</div>')).fadeIn(200);
}
sax.ajax.refreshResults = sax.ajax.refreshResults || { url: sax.url.base + 'Organisation/Ajax/GetList', formId: 'frmOrganisationFilter', callbackFn: sax.doRefreshResults };
sax.doAjaxRequest(sax.ajax.refreshResults, delay || 50);
sax.awaitingRefreshResponse = true;
}
sax.getItemTemplate = function ()
{
return $('.sax-panel-item-template .sax-panel-item.Organisation', $results);
}
sax.getDropdownItemTemplate = function ()
{
return $('.sax-panel-item-template .sax-dropdown-menu.Organisation li.sax-dropdown-item', $results);
}
sax.doRefreshResults = function (response)
{
//sax.debug('doRefreshResults', response);
delete sax.awaitingRefreshResponse;
sax.refreshControlValues(response.controlValues);
var $msgs = $('.sax-panel-messages', $results).hide().empty();
var $items = $('.sax-panel-item-list', $results).hide().empty();
var $tmpl = sax.getItemTemplate();
sax.refreshPaging(response, '#frmOrganisationFilter', sax.refreshResults);
if (!response.success) return;
// Reference data - Statuses.
if (response.defaultStatusCode) sax.data.defaultStatusCode = response.defaultStatusCode;
if (response.statuses)
sax.populateSelectCodeDesc('#cbOrganisationStatus', response.statuses);
// Items.
var items = response.organisations;
if (!items.length)
return $msgs.append($('<div class="sax-message">' + sax.msg.dataNoneFound + '</div>')).fadeIn(500);
for (let itemIdx = 0; itemIdx < items.length; itemIdx++)
{
let item = items[itemIdx];
let $item = $tmpl.clone();
sax.populateItem($item, item);
$items.append($item);
}
$items.fadeIn(50);
}
sax.populateItem = function ($item, item)
{
if (!item) return;
$item.prop('id', sax.itemIdPrefix + item.id);
$('.OrganisationName', $item).text(item.name);
$('.OrganisationType', $item).text(item.typeDesc);
$('.OrganisationCategory', $item).text(item.categoryDesc);
$('.OrganisationSport', $item).text(item.sportName);
$('.OrganisationRegion', $item).text(item.regionName);
$('.OrganisationMemberCount', $item).on('click', function () { sax.onClickMemberCount(item.id); }).find('.MemberCount').text(item.memberCount);
$('.StatusDesc', $item).text(item.statusDesc).addClass('sax-status-' + item.statusCode.toLowerCase());
$('.sax-caption .sax-action').toggleClass('sax-action', !item.deleted);
if (!item.deleted)
{
let $mnu = $('ul.sax-dropdown-menu', $item);
$mnu.append(sax.getDropdownItemTemplate().clone().find('div.sax-label').text(sax.lbl.edit).end().on('click', function () { sax.updateItem(item.id) }));
if (item.awaiting)
$mnu.append(sax.getDropdownItemTemplate().clone().find('div.sax-label').text(sax.lbl.activate).end().on('click', function () { sax.activateItem(item.id) }));
$('.sax-header .sax-action.sax-item-update', $item).off('click').on('click', function () { sax.updateItem(item.id) });
$item.off('dblclick').on('dblclick', function () { sax.updateItem(item.id) });
}
$('.sax-burger', $item).toggle(!!$('.sax-burger .sax-dropdown-menu .sax-dropdown-item', $item).length);
$('.sax-panel-messages', $results).hide().empty();
return $item;
}
//--------------------------------------------------------------
sax.selectSport = function ()
{
if (!sax.data.sports)
{
let handleSportsResponse = function (response)
{
if (response && response.success && response.sports)
sax.data.sports = response.sports;
sax.showSelectSport();
}
sax.ajax.sports = sax.ajax.sports || { url: sax.url.base + 'Sport/Ajax/GetList', formId: 'frmNavigate', callbackFn: handleSportsResponse };
sax.doAjaxRequest(sax.ajax.sports, 50);
}
else sax.showSelectSport();
}
sax.showSelectSport = function ()
{
sax.data.selectItem.fn.selectItem({ caption: sax.dlg.title.selectSport, items: sax.data.sports, fnCallback: sax.onSportSelected });
}
sax.onSportSelected = function (spt)
{
let $frm = sax.$frm.organisation;
$('.OrganisationSportId', $frm).val(spt.id);
$('.OrganisationSportCaption', $frm).val(spt.name);
sax.clearControlError($('.OrganisationSportCaption', $frm));
}
//--------------------------------------------------------------
sax.selectFilterSport = function ()
{
if (!sax.data.sports)
{
let handleFilterSportsResponse = function (response)
{
if (response && response.success && response.sports)
sax.data.sports = response.sports;
sax.showSelectFilterSport();
}
sax.ajax.filterSports = sax.ajax.filterSports || { url: sax.url.base + 'Sport/Ajax/GetList', formId: 'frmNavigate', callbackFn: handleFilterSportsResponse };
sax.doAjaxRequest(sax.ajax.filterSports, 50);
}
else sax.showSelectFilterSport();
}
sax.showSelectFilterSport = function ()
{
sax.data.selectItem.fn.selectItem({ caption: sax.dlg.title.selectSport, items: sax.data.sports, fnCallback: sax.onFilterSportSelected });
}
sax.onFilterSportSelected = function (spt)
{
//sax.debug('onFilterSportSelected', sax.$frm.filter)
let $frm = sax.$frm.filter;
$('.FilterOrganisationSportId', $frm).val(spt.id);
$('.FilterOrganisationSportCaption', $frm).val(spt.name);
sax.refreshResults();
}
sax.clearFilterSport = function ()
{
$('.FilterOrganisationSportId,.FilterOrganisationSportCaption', sax.$frm.filter).val('');
sax.refreshResults();
}
//--------------------------------------------------------------
sax.selectRegion = function ()
{
if (!sax.data.regions)
{
let handleRegionsResponse = function (response)
{
if (response && response.success && response.regions)
sax.data.regions = response.regions;
sax.showSelectRegion();
}
sax.ajax.regions = sax.ajax.regions || { url: sax.url.base + 'Region/Ajax/GetList', formId: 'frmNavigate', callbackFn: handleRegionsResponse };
sax.doAjaxRequest(sax.ajax.regions, 50);
}
else sax.showSelectRegion();
}
sax.showSelectRegion = function ()
{
sax.data.selectItem.fn.selectItem({ caption: sax.dlg.title.selectRegion, items: sax.data.regions, fnCallback: sax.onRegionSelected });
}
sax.onRegionSelected = function (rgn)
{
//sax.debug('onRegionSelected', rgn);
let $frm = sax.$frm.organisation;
$('.OrganisationRegionIdCountry', $frm).val(rgn.id);
$('.OrganisationRegionCaptionCountry', $frm).val(rgn.name);
sax.clearControlError($('.OrganisationRegionCaptionCountry', $frm));
}
//--------------------------------------------------------------
sax.selectFilterRegion = function ()
{
if (!sax.data.regions)
{
let handleFilterRegionsResponse = function (response)
{
if (response && response.success && response.regions)
sax.data.regions = response.regions;
sax.showSelectFilterRegion();
}
sax.ajax.regions = sax.ajax.regions || { url: sax.url.base + 'Region/Ajax/GetList', formId: 'frmNavigate', callbackFn: handleFilterRegionsResponse };
sax.doAjaxRequest(sax.ajax.regions, 50);
}
else sax.showSelectFilterRegion();
}
sax.showSelectFilterRegion = function ()
{
sax.data.selectItem.fn.selectItem({ caption: sax.dlg.title.selectFilterRegion, items: sax.data.regions, fnCallback: sax.onFilterRegionSelected });
}
sax.onFilterRegionSelected = function (rgn)
{
//sax.debug('onFilterRegionSelected', rgn);
let $frm = sax.$frm.filter;
$('.FilterOrganisationRegionId', $frm).val(rgn.id);
$('.FilterOrganisationRegionCaption', $frm).val(rgn.name);
sax.refreshResults();
}
sax.clearFilterRegion = function ()
{
$('.FilterOrganisationRegionId,.FilterOrganisationRegionCaption', sax.$frm.filter).val('');
sax.refreshResults();
}
//--------------------------------------------------------------
sax.createItem = function ()
{
let $frm = sax.$frm.organisation;
$('.OrganisationId,.OrganisationSportId,.OrganisationRegionIdCountry,input[type="text"],textarea,select', $frm).val('');
$('.OrganisationStatus', $frm).val(sax.data.defaultStatusCode);
sax.clearControlErrors();
sax.$dlg.organisation.dialog('option', 'title', sax.dlg.title.createOrganisation);
sax.openDialog(sax.$dlg.organisation, 600, 600);
}
sax.doCreateItem = function ()
{
sax.storeItem();
}
//--------------------------------------------------------------
sax.updateItem = function (id)
{
$('.OrganisationId', sax.$frm.organisationId).val(id);
sax.ajax.updateItem = sax.ajax.updateItem || { url: sax.url.base + 'Organisation/Ajax/GetItem', formId: 'frmOrganisationId', callbackFn: sax.onUpdateItem };
sax.doAjaxRequest(sax.ajax.updateItem, 50);
}
sax.onUpdateItem = function (response)
{
//sax.debug('onUpdateItem', response);
let $frm = sax.$frm.organisation;
$('.OrganisationId,input[type="text"],textarea,select', $frm).val('');
if (!response.success) return;
let item = response.organisation;
$('.OrganisationId', $frm).val(item.id);
$('.OrganisationName', $frm).val(item.name);
$('.OrganisationType', $frm).val(item.typeCode);
$('.OrganisationCategory', $frm).val(item.categoryCode);
$('.OrganisationSportId', $frm).val(item.sportId);
$('.OrganisationSportCaption', $frm).val(item.sportName);
$('.OrganisationRegionIdCountry', $frm).val(item.regionIdCountry);
$('.OrganisationRegionCaptionCountry', $frm).val(item.regionNameCountry);
$('.OrganisationStatus', $frm).val(item.statusCode);
sax.clearControlErrors();
sax.$dlg.organisation.dialog('option', 'title', sax.dlg.title.updateOrganisation);
sax.openDialog(sax.$dlg.organisation, 600, 600);
}
sax.doUpdateItem = function ()
{
sax.storeItem();
}
//--------------------------------------------------------------
sax.storeItem = function ()
{
sax.ajax.storeItem = sax.ajax.storeItem || { url: sax.url.base + 'Organisation/Ajax/StoreItem', formId: 'frmOrganisation', callbackFn: sax.onStoreItem };
sax.doAjaxRequest(sax.ajax.storeItem, 50);
}
sax.onStoreItem = function (response)
{
//sax.debug('onStoreItem', response);
sax.refreshControlValues(response.controlValues);
if (!response.success) return;
sax.$dlg.organisation.dialog('close');
sax.refreshItem(response.organisation);
}
sax.refreshItem=function(item)
{
var $item = $('#' + sax.itemIdPrefix + item.id);
if (!$item.length)
{
$item = $('.sax-panel-item-template .sax-panel-item.Organisation', $results).clone();
$('.sax-panel-item-list', $results).prepend($item).show();
}
$item.replaceWith(sax.populateItem(sax.getItemTemplate(), item));
}
//--------------------------------------------------------------
sax.activateItem = function (id)
{
//sax.debug('activateItem(' + id + ')')
$('.OrganisationId', sax.$frm.organisationId).val(id);
sax.ajax.getActivationInfo = sax.ajax.getActivationInfo || { url: sax.url.base + 'Organisation/Ajax/GetActivationInfo', formId: 'frmOrganisationId', callbackFn: sax.showActivationInfo };
sax.doAjaxRequest(sax.ajax.getActivationInfo, 50);
}
sax.showActivationInfo = function (response)
{
sax.debug('showActivationInfo', response);
if (!response.success || !response.organisation) return;
let item = response.organisation;
let $dlg = sax.$dlg.activate;
let $frm = $('#frmOrganisationActivation');
$('.OrganisationId', $frm).val($('.OrganisationId', sax.$frm.organisationId).val());
let popItemValue = function (sel, val)
{
$(sel, $frm).toggle(!!val).find('.sax-value').text(val);
}
popItemValue('.OrganisationName', item.name);
popItemValue('.OrganisationSport', item.sportName);
popItemValue('.OrganisationRegion', item.regionNameCountry);
popItemValue('.OrganisationCategory', item.categoryDesc);
popItemValue('.OrganisationAddress', item.addressSummary);
let $orgMbrs = $('.OrganisationMembers', $dlg).empty();
let $mbrTmpl = $('.sax-panel-item-template tr.sax-item.Member');
sax.debug(response.members, $orgMbrs, $mbrTmpl);
for (let idx = 0; idx < response.members.length; idx++)
{
let item = response.members[idx];
let $item = $mbrTmpl.clone();
$('.MemberId', $item).val(item.id);
$('.MemberActivate', $item).prop('name','MemberActivate_' + item.id)
$('.MemberEmail', $item).text(item.emailAddress);
$('.MemberName', $item).text(item.displayName);
$orgMbrs.append($item);
sax.debug($item);
}
sax.openDialog($dlg, 480, 640);
}
sax.activateOrganisation = function ()
{
let $frm = $('#frmOrganisationActivation');
sax.ajax.activateOrganisation = sax.ajax.activateOrganisation || { url: sax.url.base + 'Organisation/Ajax/Activate', formId: $frm.prop('id'), callbackFn: sax.handleAtivateResponse };
sax.doAjaxRequest(sax.ajax.activateOrganisation, 50);
}
sax.handleAtivateResponse = function (response)
{
sax.debug('handleAtivateResponse', response);
if (!response.success) return;
sax.$dlg.activate.dialog('close');
sax.refreshItem(response.organisation);
}
//--------------------------------------------------------------
sax.onClickMemberCount = function (id)
{
if (!(id * 1)) return;
sax.doPleaseWait();
let $frm = $('#frmOrganisationId');
$('.OrganisationId', $frm).val(id);
$frm.prop('action', sax.url.base + 'Organisation/Members').submit();
}
//--------------------------------------------------------------
$(function ()
{
$('.sax-panel-filter .sax-action.sax-item-create').on('click', function () { sax.createItem(); });
$('.sax-panel-filter .sax-action.sax-results-refresh').on('click', function () { sax.refreshResults(); });
$('.sax-input, .sax-textarea').on('input', function () { sax.clearControlError(this); });
$('.sax-select').on('change', function () { sax.clearControlError(this); });
sax.$frm.filter = $('#frmOrganisationFilter');
$('.FilterOrganisation', sax.$frm.filter).on('input', function () { sax.refreshResults(250); });
$('.FilterOrganisationCategory,.FilterOrganisationStatusCategory', sax.$frm.filter).on('change', function () { sax.refreshResults(50); });
sax.$frm.organisationId = $('#frmOrganisationId');
sax.$frm.organisation = $('#frmOrganisation');
$results = $('.sax-panel-results');
var opt = sax.dlgOpts();
opt.buttons[sax.dlg.btn.ok] = function () { sax.doCreateItem(); };
opt.buttons[sax.dlg.btn.cancel] = function () { $(this).dialog('close'); };
sax.$dlg.organisation = $('#dlgOrganisation').dialog(opt);
var opt = sax.dlgOpts();
opt.buttons[sax.dlg.btn.ok] = function () { sax.activateOrganisation(); };
opt.buttons[sax.dlg.btn.cancel] = function () { $(this).dialog('close'); };
sax.$dlg.activate = $('#dlgOrganisationActivation').dialog(opt);
setTimeout(sax.refreshResults, 250);
});
}(window, window.document));
/* The End */