Files
Saxonite/SaxoniteCoreWebAdmin/wwwroot/js/sax-selectOrganisation.js
T
2021-05-20 19:22:52 +01:00

94 lines
3.3 KiB
JavaScript

/*
* Admin - User page scripts.
*/
(function (win, doc, undef)
{
"use strict";
let so = sax.selectOrganisation = sax.selectOrganisation || { data: {}, fn: {}, $dlg: {}, itemIdPrefix: 'Org_' };
//--------------------------------------------------------------
/*
* Organisation selection config:
* - caption - dialog title.
* - width: preferred dialog width (default: 400).
* - height: preferred dialog height (default: 400).
* - fnCallback - method to call on completion.
*/
so.fn.openDialog = function (cfg)
{
//sax.debug('openDialog', cfg);
so.fn.init();
so.cfg = cfg;
sax.openDialog(so.$dlg.dialog('option', { title: cfg.caption || so.dlgTitleSelectOrganisation || 'Select Organisation' }), so.cfg.width || 480, so.cfg.height || 600);
}
so.fn.refreshOrganisations = function (delay)
{
//sax.debug('refreshOrganisations');
$('.sax-selection-results.Organisations', so.$dlg).empty().hide();
so.ajaxRefreshResults = so.ajaxRefreshResults || { url: sax.url.base + 'Organisation/Ajax/GetList', formId: 'frmSelectOrganisationFilter', callbackFn: so.fn.doRefreshResults };
sax.doAjaxRequest(so.ajaxRefreshResults, delay || 50);
}
so.fn.doRefreshResults = function (response)
{
//sax.debug('so.doRefreshResults', response);
if (!response.success) return;
let $results = $('.sax-selection-results.Organisations', so.$dlg).empty().hide();
let $tmpl = $('.sax-panel-item-template .sax-panel-item.Organisation', so.$dlg);
let items = response.organisations;
if (items)
{
for (let itemIdx = 0; itemIdx < items.length; itemIdx++)
{
let item = items[itemIdx];
let $item = $tmpl.clone().prop('id', so.itemIdPrefix + item.id).on('click', function () { so.fn.onClickOrganisation(item); }).on('dblclick', function () { so.fn.onDblClickOrganisation(item); });
$('.Name', $item).text(item.name);
$results.append($item);
}
}
$results.fadeIn(200);
}
so.fn.onClickOrganisation = function (item)
{
//sax.debug('onClickOrganisation', item);
so.data.item = item;
$('.sax-panel-item.Organisation', so.$dlg).removeClass('sel');
$('.sax-panel-item.Organisation#' + so.itemIdPrefix + item.id, so.$dlg).addClass('sel');
}
so.fn.onDblClickOrganisation = function (item)
{
//sax.debug('onDblClickCode', item);
so.data.item = item;
so.fn.confirmSelection();
}
so.fn.confirmSelection = function ()
{
//sax.debug('confirmSelection', so.data.item);
if (!so.data.item) return;
if (so.cfg.fnCallback) so.cfg.fnCallback(so.data.item.id, so.data.item.name);
so.$dlg.dialog('close');
so.fn.cleanUp();
}
so.fn.cleanUp = function ()
{
$('.sax-selection-results.Organisations', so.$dlg).empty().hide();
delete so.cfg;
delete so.data.item;
}
//--------------------------------------------------------------
so.fn.init = function ()
{
if (!so.$dlg) return;
var opt = sax.dlgOpts();
opt.open = function () { so.fn.refreshOrganisations(200); };
opt.buttons[sax.dlg.btn.ok] = function () { so.fn.confirmSelection(); };
opt.buttons[sax.dlg.btn.cancel] = function () { so.fn.cleanUp(); $(this).dialog('close'); };
so.$dlg = $('#dlgSelectOrganisation').dialog(opt);
$('.sax-input.FilterOrganisation', so.$dlg).on('input', function () { sax.selectOrganisation.fn.refreshOrganisations(250); });
}
}(window, window.document));
/* The End */