/* * Admin - Organisation page scripts. */ (function (win, doc, undef) { "use strict"; let op = sax.mdl.organisations = sax.mdl.organisations || { data: {}, ajax: {} }; let $pnl = sax.$pnl, $frm = sax.$frm, $dlg = sax.$dlg; sax.itemIdPrefix = 'Itm_'; //-------------------------------------------------------------- sax.refreshResults = function (delay) { if (!op.awaitingRefreshResponse) { var $items = $('.sax-panel-item-list', $pnl.results).fadeOut(200).empty(); var $msgs = $('.sax-panel-messages', $pnl.results).hide().empty().append($('
')).fadeIn(200); } op.ajax.refreshResults = op.ajax.refreshResults || { url: sax.url.base + 'Organisation/Ajax/GetList', formId: 'frmOrganisationFilter', callbackFn: op.doRefreshResults }; sax.doAjaxRequest(op.ajax.refreshResults, delay || 50); op.awaitingRefreshResponse = true; } op.getItemTemplate = function () { return $('.sax-panel-item-template .sax-panel-item.Organisation', $pnl.results); } op.getDropdownItemTemplate = function () { return $('.sax-panel-item-template .sax-dropdown-menu.Organisation li.sax-dropdown-item', $pnl.results); } op.doRefreshResults = function (response) { //sax.debug('doRefreshResults', response); delete op.awaitingRefreshResponse; sax.refreshControlValues(response.controlValues); var $msgs = $('.sax-panel-messages', $pnl.results).hide().empty(); var $items = $('.sax-panel-item-list', $pnl.results).hide().empty(); var $tmpl = op.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($('')).fadeIn(500); for (let itemIdx = 0; itemIdx < items.length; itemIdx++) { let item = items[itemIdx]; let $item = $tmpl.clone(); op.populateItem($item, item); $items.append($item); } $items.fadeIn(50); } op.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(op.getDropdownItemTemplate().clone().find('div.sax-label').text(sax.lbl.edit).end().on('click', function () { sax.updateItem(item.id) })); if (item.awaiting) $mnu.append(op.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', $pnl.results).hide().empty(); return $item; } //-------------------------------------------------------------- op.selectFilterSport = function () { if (!op.data.sports) op.populateSports(op.showSelectFilterSport); else op.showSelectFilterSport(); } op.showSelectFilterSport = function () { sax.data.selectItem.fn.selectItem({ caption: sax.dlg.title.selectSport, items: op.data.sports, fnCallback: op.onFilterSportSelected }); } op.onFilterSportSelected = function (spt) { let frm = $frm.filter; $('.FilterOrganisationSportId', frm).val(spt.id); $('.FilterOrganisationSportCaption', frm).val(spt.name); sax.refreshResults(); } op.clearFilterSport = function () { $('.FilterOrganisationSportId,.FilterOrganisationSportCaption', $frm.filter).val(''); sax.refreshResults(); } op.populateSports = function (nextFn) { let handleResponse = function (response) { if (response && response.success && response.sports) op.data.sports = response.sports; if (nextFn) nextFn(); } op.ajax.sports = op.ajax.sports || { url: sax.url.base + 'Sport/Ajax/GetList', formId: 'frmNavigate', callbackFn: handleResponse }; sax.doAjaxRequest(op.ajax.sports, 50); } //-------------------------------------------------------------- sax.selectSport = function () { if (!op.data.sports) op.populateSports(sax.showSelectSport); else sax.showSelectSport(); } sax.showSelectSport = function () { sax.data.selectItem.fn.selectItem({ caption: sax.dlg.title.selectSport, items: op.data.sports, fnCallback: sax.onSportSelected }); } sax.onSportSelected = function (spt) { let frm = $frm.org; $('.OrganisationSportId', frm).val(spt.id); $('.OrganisationSportCaption', frm).val(spt.name); sax.clearControlError($('.OrganisationSportCaption', frm)); } //-------------------------------------------------------------- op.selectFilterRegion = function () { if (!op.data.regions) op.populateRegions(op.showSelectFilterRegion); else op.showSelectFilterRegion(); } op.showSelectFilterRegion = function () { sax.data.selectItem.fn.selectItem({ caption: sax.dlg.title.selectFilterRegion, items: op.data.regions, fnCallback: op.onFilterRegionSelected }); } op.onFilterRegionSelected = function (rgn) { //sax.debug('onFilterRegionSelected', rgn); let frm = $frm.filter; $('.FilterOrganisationRegionId', frm).val(rgn.id); $('.FilterOrganisationRegionCaption', frm).val(rgn.name); sax.refreshResults(); } op.clearFilterRegion = function () { $('.FilterOrganisationRegionId,.FilterOrganisationRegionCaption', $frm.filter).val(''); sax.refreshResults(); } op.populateRegions = function (nextFn) { let handleResponse = function (response) { if (response && response.success && response.regions) op.data.regions = response.regions; if (nextFn) nextFn(); } op.ajax.regions = op.ajax.regions || { url: sax.url.base + 'Region/Ajax/GetList', formId: 'frmNavigate', callbackFn: handleResponse }; sax.doAjaxRequest(op.ajax.regions, 50); } //-------------------------------------------------------------- sax.selectRegion = function () { if (!op.data.regions) op.populateRegions(sax.showSelectRegion); else sax.showSelectRegion(); } sax.showSelectRegion = function () { sax.data.selectItem.fn.selectItem({ caption: sax.dlg.title.selectRegion, items: op.data.regions, fnCallback: sax.onRegionSelected }); } sax.onRegionSelected = function (rgn) { //sax.debug('onRegionSelected', rgn); let frm = $frm.org; $('.OrganisationRegionIdCountry', frm).val(rgn.id); $('.OrganisationRegionCaptionCountry', frm).val(rgn.name); sax.clearControlError($('.OrganisationRegionCaptionCountry', frm)); } //-------------------------------------------------------------- sax.createItem = function () { let frm = $frm.org; $('.OrganisationId,.OrganisationSportId,.OrganisationRegionIdCountry,input[type="text"],textarea,select', frm).val(''); $('.OrganisationStatus', frm).val(sax.data.defaultStatusCode); sax.clearControlErrors(); $dlg.organisation.dialog('option', 'title', sax.dlg.title.createOrganisation); sax.openDialog($dlg.organisation, 600, 600); } //-------------------------------------------------------------- sax.updateItem = function (id) { $('.OrganisationId', $frm.orgId).val(id); op.ajax.updateItem = op.ajax.updateItem || { url: sax.url.base + 'Organisation/Ajax/GetItem', formId: 'frmOrganisationId', callbackFn: sax.onUpdateItem }; sax.doAjaxRequest(op.ajax.updateItem, 50); } sax.onUpdateItem = function (response) { //sax.debug('onUpdateItem', response); let frm = $frm.org; $('.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(); $dlg.organisation.dialog('option', 'title', sax.dlg.title.updateOrganisation); sax.openDialog($dlg.organisation, 600, 600); } //-------------------------------------------------------------- sax.storeItem = function () { op.ajax.storeItem = op.ajax.storeItem || { url: sax.url.base + 'Organisation/Ajax/StoreItem', formId: 'frmOrganisation', callbackFn: sax.onStoreItem }; sax.doAjaxRequest(op.ajax.storeItem, 50); } sax.onStoreItem = function (response) { //sax.debug('onStoreItem', response); sax.refreshControlValues(response.controlValues); if (!response.success) return; $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', $pnl.results).clone(); $('.sax-panel-item-list', $pnl.results).prepend($item).show(); } $item.replaceWith(op.populateItem(op.getItemTemplate(), item)); } //-------------------------------------------------------------- sax.activateItem = function (id) { //sax.debug('activateItem(' + id + ')') $('.OrganisationId', $frm.orgId).val(id); op.ajax.getActivationInfo = op.ajax.getActivationInfo || { url: sax.url.base + 'Organisation/Ajax/GetActivationInfo', formId: 'frmOrganisationId', callbackFn: sax.showActivationInfo }; sax.doAjaxRequest(op.ajax.getActivationInfo, 50); } sax.showActivationInfo = function (response) { sax.debug('showActivationInfo', response); if (!response.success || !response.organisation) return; let item = response.organisation; let $dlg = $dlg.activate; let frm = $('#frmOrganisationActivation'); $('.OrganisationId', frm).val($('.OrganisationId', $frm.orgId).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'); op.ajax.activateOrganisation = op.ajax.activateOrganisation || { url: sax.url.base + 'Organisation/Ajax/Activate', formId: frm.prop('id'), callbackFn: sax.handleAtivateResponse }; sax.doAjaxRequest(op.ajax.activateOrganisation, 50); } sax.handleAtivateResponse = function (response) { sax.debug('handleAtivateResponse', response); if (!response.success) return; $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 () { $pnl.orgs = $('.sax-panel.Organisations'); $pnl.filter = $('.sax-panel-filter', $pnl.orgs); $pnl.results = $('.sax-panel-results', $pnl.orgs); $('.sax-action.sax-item-create', $pnl.filter).on('click', function () { sax.createItem(); }); $('.sax-action.sax-results-refresh', $pnl.filter).on('click', function () { sax.refreshResults(); }); $frm.filter = $('#frmOrganisationFilter'); $('.FilterOrganisation', $frm.filter).on('input', function () { sax.refreshResults(250); }); $('.FilterOrganisationCategory,.FilterOrganisationStatusCategory', $frm.filter).on('change', function () { sax.refreshResults(50); }); $frm.filter.on('dblclick', '.FilterOrganisationSportCaption', op.selectFilterSport).on('click', '.FilterSportSelect', op.selectFilterSport).on('click', '.FilterSportClear', op.clearFilterSport); $frm.filter.on('dblclick', '.FilterOrganisationRegionCaption', op.selectFilterRegion).on('click', '.FilterRegionSelect', op.selectFilterRegion).on('click', '.FilterRegionClear', op.clearFilterRegion); $frm.orgId = $('#frmOrganisationId'); $frm.org = $('#frmOrganisation'); $('.sax-input, .sax-textarea', $frm.org).on('input', function () { sax.clearControlError(this); }); $('.sax-select', $frm.org).on('change', function () { sax.clearControlError(this); }); var opt = sax.dlgOpts(); opt.buttons[sax.dlg.btn.ok] = function () { sax.storeItem(); }; opt.buttons[sax.dlg.btn.cancel] = function () { $(this).dialog('close'); }; $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'); }; $dlg.activate = $('#dlgOrganisationActivation').dialog(opt); setTimeout(sax.refreshResults, 250); }); }(window, window.document)); /* The End */