Files
Saxonite/SaxoniteCoreWebOnline/wwwroot/js/sax-pro-org.js
T
2022-12-15 16:58:20 +00:00

245 lines
11 KiB
JavaScript

/*
* Account page scripts.
*/
(function (win, doc, undef)
{
"use strict";
let po = sax.mdl.profileOrganisations = sax.mdl.profileOrganisations || { data: {}, ajax: {} };
let $pnl = po.$pnl = {}, $dlg = po.$dlg = {}, $frm = po.$frm = {};
//--------------------------------------------------------------
sax.refreshProfileOrganisationsInfo = function (delay)
{
$pnl.section.removeClass('sax-edit').addClass('sax-populating');
$('.sax-property', $pnl.section).hide();
sax.showDataRefreshingMsg($pnl.section);
sax.refreshProfileOrganisations(po.onRefreshProfileOrganisationsInfo);
}
sax.refreshProfileOrganisations = function (callbackFn)
{
//sax.debug('refreshProfileOrganisations');
delete sax.page.profileOrganisationsChanged;
po.ajax.profileOrganisations = po.ajax.profileOrganisations || { url: sax.url.base + 'Account/Ajax/GetProfileOrganisations', formId: 'frmNavigate', noWaitIcon: true };
po.ajax.profileOrganisations.callbackFn = callbackFn;
sax.doAjaxRequest(po.ajax.profileOrganisations, 50);
}
po.onRefreshProfileOrganisationsInfo = function (response)
{
//sax.debug('onRefreshProfileOrganisationsInfo', response);
if (!response || !response.success) return;
$('.sax-message', $pnl.section).stop(true, false).fadeOut(200).empty();
po.data.contactMethods = response.contactMethods;
let items = response.organisations;
if (!items) return;
let $items = $('.sax-panel-results.Organisations', $pnl.section).empty();
for (let itemIdx = 0; itemIdx < items.length; itemIdx++)
$items.append(po.populateItem(po.getItemTemplate(), items[itemIdx]));
$items.fadeIn(50);
sax.showNoInfoMsg($pnl.section);
$pnl.section.removeClass('sax-populating');
}
po.populateItem = function ($item, item)
{
if (!item) return;
$('.OrganisationName', $item).text(item.name);
$('.OrganisationSport', $item).text(item.sportName);
$('.OrganisationRegionPrimary', $item).text(item.regionPrimary.name);
$('.OrganisationMemberCount', $item).toggle(!!item.memberCount).find('.MemberCount').text(item.memberCount);
$('.OrganisationOpportunityCount', $item).toggle(!!item.opportunityCount).find('.MemberCount').text(item.opportunityCount);
let $bgr = $('.sax-burger', $item);
let $mnu = $('ul.sax-dropdown-menu', $bgr).empty();
if (item.admin)
{
let fnUpd = function () { po.updateOrganisation($item, item); };
$mnu.append(po.getItemMenuOption(sax.lbl.edit, fnUpd, 'sax-edit'));
$('.sax-caption .sax-action', $item).off('click').on('click', fnUpd);
$item.off('dblclick').on('dblclick', fnUpd);
}
else $('.sax-caption .sax-action', $item).removeClass('sax-action');
$bgr.toggle(!!$('.sax-dropdown-menu .sax-dropdown-item', $item).length);
return $item;
}
//--------------------------------------------------------------
po.getHeaderDropdownTemplate = function ()
{
return $('.sax-panel-item-template .sax-dropdown-menu.ProfileOrganisations li.sax-dropdown-item', $pnl.section).clone();
}
po.getItemTemplate = function ()
{
return $('.sax-panel-item-template .sax-panel-item.Organisation', $pnl.section).clone();
}
po.getItemDropdownTemplate = function ()
{
return $('.sax-panel-item-template .sax-dropdown-menu.ProfileOrganisation li.sax-dropdown-item', $pnl.section).clone();
}
po.getHeaderMenuOption = function (lbl, fn, cls)
{
return po.getMenuOption(po.getHeaderDropdownTemplate(), lbl, fn, cls);
}
po.getItemMenuOption = function (lbl, fn, cls)
{
return po.getMenuOption(po.getItemDropdownTemplate(), lbl, fn, cls);
}
po.getMenuOption = function ($opt, lbl, fn, cls)
{
return $opt.find('div.sax-label').text(lbl).end().on('click', fn).addClass(cls);
}
//--------------------------------------------------------------
po.updateOrganisation = function ($item, item)
{
//sax.debug('po.updateOrganisation', $item, item);
$('.OrganisationId', $frm.orgId).val(item.id);
po.data.$item = $item; po.data.item = item;
po.ajax.profileOrganisation = po.ajax.profileOrganisation || { url: sax.url.base + 'Account/Ajax/GetProfileOrganisation', formId: 'frmProfileOrganisationId', callbackFn: po.doUpdateOrganisation };
sax.doAjaxRequest(po.ajax.profileOrganisation, 50);
}
po.doUpdateOrganisation = function (response)
{
//sax.debug('doUpdateOrganisation', response);
if (!response || !response.success) return;
let item = response.organisation;
if (!item) return;
$('.OrganisationId', $frm.org).val(item.id);
$('.OrganisationName', $frm.org).val(item.name);
if (response.categories) sax.populateSelectCodeDesc($('.sax-select.OrganisationCategory', $frm.org), response.categories);
$('.OrganisationCategory', $frm.org).val(item.categoryCode);
if (response.types) sax.populateSelectCodeDesc($('.sax-select.OrganisationType', $frm.org), response.types);
$('.OrganisationType', $frm.org).val(item.typeCode);
$('.OrganisationSportId', $frm.org).val(item.sportId);
$('.OrganisationSportName', $frm.org).val(item.sportName);
$('.OrganisationRegionIdPrimary', $frm.org).val(item.regionPrimary.id);
$('.OrganisationRegionNamePrimary', $frm.org).val(item.regionPrimary.name);
$('.OrganisationAbout', $frm.org).val(item.about);
$('.OrganisationFacilities', $frm.org).val(item.facilities);
$pnl.tblconMths.empty();
let conMths = item.contactMethods || [];
for (let idx = 0; idx < conMths.length; idx++)
{
let conMth = conMths[idx];
$pnl.tblconMths.append(po.getConMthEl(conMth.method, conMth.methodCaption, conMth.value));
}
po.sortConMths();
let address = item.address || {};
$('input.OrganisationAddressId', $frm.org).val(address.id);
$('input.OrganisationAddressLine1', $frm.org).val(address.line1);
$('input.OrganisationAddressLine2', $frm.org).val(address.line2);
$('input.OrganisationAddressLine3', $frm.org).val(address.line3);
$('input.OrganisationAddressTown', $frm.org).val(address.town);
$('input.OrganisationAddressProvince', $frm.org).val(address.regionNameProvince);
$('input.OrganisationAddressCountry', $frm.org).val(address.regionNameCountry);
$('input.OrganisationAddressPostalCode', $frm.org).val(address.postalCode);
sax.openDialog($dlg.org, 720, 840);
}
po.storeOrganisation = function ()
{
let fn = function (response)
{
//sax.debug('handleStoreOrganisationResponse', response);
if (!response) return;
sax.refreshControlValues(response.controlValues, $frm.org);
if (!response.success) return;
po.populateItem(po.data.$item, response.organisation);
$dlg.org.dialog('close');
po.cleanUp();
}
or.ajax.storeOrg = or.ajax.storeOrg || { url: sax.url.base + 'Account/Ajax/StoreProfileOrganisation', formId: 'frmProfileOrganisation', callbackFn: fn };
sax.doAjaxRequest(or.ajax.storeOrg, 50);
}
po.cleanUp = function ()
{
delete po.data.item;
delete po.data.$item;
}
//--------------------------------------------------------------
po.selectOrgSport = function ()
{
sax.mdl.selectSport.fn.selectSport({ caption: sax.dlg.title.selectSport, sportId: $('.OrganisationSportId', $frm.org).val(), fnCallback: po.onSelectOrgSport })
}
po.onSelectOrgSport = function (sport)
{
sax.clearControlError($('.OrganisationSportId', $frm.org).val(sport.id));
$('.OrganisationSportName', $frm.org).val(sport ? sport.name : '');
}
//--------------------------------------------------------------
po.selectOrgRegionPrimary = function ()
{
sax.mdl.selectRegion.fn.selectRegion({ caption: sax.dlg.title.selectRegion, regionId: $('.OrganisationRegionIdPrimary', $frm.org).val(), typesInc: [30], fnCallback: po.onSelectOrgRegionPrimary })
}
po.onSelectOrgRegionPrimary = function (region)
{
sax.clearControlError($('.OrganisationRegionIdPrimary', $frm.org).val(region.id));
$('.OrganisationRegionNamePrimary', $frm.org).val(region ? region.name : '');
}
//--------------------------------------------------------------
po.getConMthCfg = function (usedMths, mth, val)
{
return { fnCallback: po.applyConMth, methods: po.data.contactMethods, methodsUsed: usedMths, method: mth, value: val };
}
po.getUsedMths = function ()
{
return $('input[name="Organisation.ContactMethod"]', $frm.org).map(function () { return $(this).val(); }).get();
}
po.addConMth = function ()
{
sax.editContactMethod(po.getConMthCfg(po.getUsedMths()));
}
po.editConMth = function (mth, val)
{
sax.editContactMethod(po.getConMthCfg(po.getUsedMths(), mth, val));
}
po.getConMthId = function (mth)
{
return 'ConMth_' + mth;
}
po.applyConMth = function (mth, mthDsc, val)
{
//sax.debug('applyConMth(\'' + mth + '\',\'' + mthDsc + '\',\'' + val + '\')');
$('#' + po.getConMthId(mth), $pnl.conMths).remove();
$pnl.tblconMths.append(po.getConMthEl(mth, mthDsc, val));
po.sortConMths();
}
po.sortConMths = function ()
{
let $rows = $pnl.tblconMths.find('tr').detach().sort(function (a, b)
{
return $('.sax-label', a).text().localeCompare($('.sax-label', b).text());
}).appendTo($pnl.tblconMths);
$('input[name="Organisation.ContactMethods"]', $pnl.conMths).val($rows.length);
}
po.getConMthEl = function (mth, mthDsc, val)
{
let $conMth = $('.sax-panel-item-template .ContactMethod', $pnl.conMths).clone().prop('id', po.getConMthId(mth));
$('.sax-label', $conMth).text(mthDsc);
$('.sax-value', $conMth).text(val);
$('input[name="Organisation.ContactMethod"]', $conMth).val(mth);
$('input[name="Organisation.ContactValue"]', $conMth).val(val);
$conMth.on('click', '.sax-action.sax-edit', function () { po.editConMth(mth, val); });
$conMth.on('click', '.sax-action.sax-remove', function () { po.clearConMth(mth); });
return $conMth;
}
po.clearConMth = function (mth)
{
$('#' + po.getConMthId(mth), $pnl.conMths).remove();
po.sortConMths();
}
//--------------------------------------------------------------
$(function ()
{
$pnl.section = $('.sax-panel-section.ProfileOrganisations');
$frm.orgId = $('#frmProfileOrganisationId', $pnl.section);
var opt = sax.dlgOpts();
opt.buttons[sax.dlg.btn.ok] = function () { po.storeOrganisation(); };
opt.buttons[sax.dlg.btn.cancel] = function () { $(this).dialog('close'); };
$dlg.org = $('#dlgProfileOrganisation').dialog(opt);
$frm.org = $('#frmProfileOrganisation', $dlg.org);
$frm.org.on('dblclick', 'input.OrganisationSportName', po.selectOrgSport).on('click', 'button.SelectSport', po.selectOrgSport);
$frm.org.on('dblclick', 'input.OrganisationRegionNamePrimary', po.selectOrgRegionPrimary).on('click', 'button.SelectOrganisationRegionPrimary', po.selectOrgRegionPrimary);
$frm.org.on('click', '.OrganisationContactMethods .sax-action.sax-add', po.addConMth);
$pnl.conMths = $('.OrganisationContactMethods', $frm.org);
$pnl.tblconMths = $('table.sax-data', $pnl.conMths);
});
//--------------------------------------------------------------
}(window, window.document));
/* The End */