Files
Saxonite/SaxoniteCoreWebAdmin/wwwroot/js/sax-organisation.js
T
2021-07-02 09:28:37 +01:00

228 lines
8.7 KiB
JavaScript

/*
* Admin - Organisation page scripts.
*/
(function (win, doc, undef)
{
"use strict";
sax.itemIdPrefix = 'Itm_';
//--------------------------------------------------------------
sax.refreshResults = function (delay)
{
var $results = $('.sax-panel-results');
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.ajaxRefreshResults = sax.ajaxRefreshResults || { url: sax.url.base + 'Organisation/Ajax/GetList', formId: 'frmOrganisationFilter', callbackFn: sax.doRefreshResults };
sax.doAjaxRequest(sax.ajaxRefreshResults, delay || 50);
sax.awaitingRefreshResponse = true;
}
sax.doRefreshResults = function (response)
{
//sax.debug('doRefreshResults', response);
delete sax.awaitingRefreshResponse;
sax.refreshControlValues(response.controlValues);
var $results = $('.sax-panel-results');
var $msgs = $('.sax-panel-messages', $results).hide().empty();
var $items = $('.sax-panel-item-list', $results).hide().empty();
var $tmpl = $('.sax-panel-item-template .sax-panel-item.Organisation', $results);
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);
$('.sax-caption .sax-action, .sax-actions .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-panel-results .sax-panel-messages').hide().empty();
}
//--------------------------------------------------------------
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.ajaxSports = sax.ajaxSports || { url: sax.url.base + 'Sport/Ajax/GetList', formId: 'frmNavigate', callbackFn: handleSportsResponse };
sax.doAjaxRequest(sax.ajaxSports, 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 (rgn)
{
//sax.debug('onSportSelected', rgn);
let $frm = sax.$frm.organisation;
$('.OrganisationSportId', $frm).val(rgn.id);
$('.OrganisationSportCaption', $frm).val(rgn.name);
sax.clearControlError($('.OrganisationSportCaption', $frm));
}
//--------------------------------------------------------------
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.ajaxRegions = sax.ajaxRegions || { url: sax.url.base + 'Region/Ajax/GetList', formId: 'frmNavigate', callbackFn: handleRegionsResponse };
sax.doAjaxRequest(sax.ajaxRegions, 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.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.ajaxUpdateItem = sax.ajaxUpdateItem || { url: sax.url.base + 'Organisation/Ajax/GetItem', formId: 'frmOrganisationId', callbackFn: sax.onUpdateItem };
sax.doAjaxRequest(sax.ajaxUpdateItem, 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.ajaxStoreItem = sax.ajaxStoreItem || { url: sax.url.base + 'Organisation/Ajax/StoreItem', formId: 'frmOrganisation', callbackFn: sax.onStoreItem };
sax.doAjaxRequest(sax.ajaxStoreItem, 50);
}
sax.onStoreItem = function (response)
{
//sax.debug('onStoreItem', response);
sax.refreshControlValues(response.controlValues);
if (!response.success) return;
sax.$dlg.organisation.dialog('close');
var item = response.organisation;
var $item = $('#' + sax.itemIdPrefix + item.id);
if (!$item.length)
{
var $results = $('.sax-panel-results');
$item = $('.sax-panel-item-template .sax-panel-item.Organisation', $results).clone();
$('.sax-panel-item-list', $results).prepend($item).show();
}
sax.populateItem($item, item)
}
//--------------------------------------------------------------
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').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); });
sax.$frm.organisationId = $('#frmOrganisationId');
sax.$frm.organisation = $('#frmOrganisation');
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);
setTimeout(sax.refreshResults, 250);
});
}(window, window.document));
/* The End */