114 lines
3.8 KiB
JavaScript
114 lines
3.8 KiB
JavaScript
/*
|
|
* Organisation selection.
|
|
*/
|
|
(function (win, doc, undef)
|
|
{
|
|
"use strict";
|
|
let so = sax.selectOrganisation = sax.selectOrganisation || {};
|
|
so.data = {}; so.fn = {};
|
|
//--------------------------------------------------------------
|
|
/*
|
|
* Organisation selection config:
|
|
* - caption dialog title.
|
|
* - items restricted list of organisations.
|
|
* - width preferred dialog width (default: 400).
|
|
* - height preferred dialog height (default: 400).
|
|
* - fnCallback method to call on completion.
|
|
*/
|
|
so.fn.selectOrganisation = function (cfg)
|
|
{
|
|
//sax.debug('openDialog', cfg);
|
|
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.refreshResults = function (delay)
|
|
{
|
|
//sax.debug('refreshResults');
|
|
if (so.cfg && so.cfg.items && so.cfg.items.length)
|
|
{
|
|
so.fn.displayResults(so.cfg.items);
|
|
return;
|
|
}
|
|
so.$results.empty();
|
|
so.ajaxSearch = so.ajaxSearch || { url: sax.url.base + 'Organisation/Ajax/GetOrganisationList', formId: 'frmSelectOrganisationFilter', callbackFn: so.fn.doRefreshResults };
|
|
sax.doAjaxRequest(so.ajaxSearch, delay || 50);
|
|
}
|
|
so.fn.doRefreshResults = function (response)
|
|
{
|
|
//sax.debug('so.doRefreshResults', response);
|
|
if (!response.success) return;
|
|
so.fn.displayResults(response.organisations);
|
|
}
|
|
so.fn.displayResults = function (items)
|
|
{
|
|
so.$results.empty().hide();
|
|
let $tmpl = $('.sax-panel-item-template .sax-panel-item.Organisation', so.$dlg);
|
|
if (items)
|
|
{
|
|
for (let itemIdx = 0; itemIdx < items.length; itemIdx++)
|
|
{
|
|
let item = items[itemIdx];
|
|
if (!so.fn.matchesFilter(item)) continue;
|
|
let $item = $tmpl.clone();
|
|
$item.on('click', function () { so.fn.onClickOrganisation(item, $item); }).on('dblclick', function () { so.fn.onDblClickOrganisation(item, $item); });
|
|
$('.Name', $item).text(item.name);
|
|
so.$results.append($item);
|
|
}
|
|
so.$results.fadeIn(200);
|
|
}
|
|
}
|
|
so.fn.matchesFilter = function (item)
|
|
{
|
|
let filterName = $('.sax-input.FilterOrganisation', so.$filter).val();
|
|
if (filterName.length)
|
|
{
|
|
let reFilterName = new RegExp(sax.escapeRegExp((filterName || '').trim()), 'i');
|
|
if (!item.name.match(reFilterName)) return false;
|
|
}
|
|
return true;
|
|
}
|
|
so.fn.onClickOrganisation = function (item, $item)
|
|
{
|
|
//sax.debug('onClickOrganisation', item, $item);
|
|
so.data.item = item;
|
|
$('.sax-panel-item.Organisation', so.$dlg).removeClass('sel');
|
|
$item.addClass('sel');
|
|
}
|
|
so.fn.onDblClickOrganisation = function (item, $item)
|
|
{
|
|
//sax.debug('onDblClickCode', item, $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.debug('confirmSelection', so.data.item);
|
|
$('.sax-input.FilterOrganisation', so.$filter).val('');
|
|
so.$results.empty();
|
|
delete so.cfg;
|
|
delete so.data.item;
|
|
}
|
|
//--------------------------------------------------------------
|
|
$(function ()
|
|
{
|
|
var opt = sax.dlgOpts();
|
|
opt.open = function () { so.fn.refreshResults(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);
|
|
|
|
so.$results = $('.sax-selection-results.Organisations', so.$dlg);
|
|
so.$filter = $('.sax-panel-filter', so.$dlg);
|
|
$('.sax-input.FilterOrganisation', so.$filter).on('input', function () { sax.selectOrganisation.fn.refreshResults(250); });
|
|
});
|
|
//--------------------------------------------------------------
|
|
}(window, window.document));
|
|
/* The End */ |