142 lines
4.6 KiB
JavaScript
142 lines
4.6 KiB
JavaScript
/*
|
|
* Region selection.
|
|
*/
|
|
|
|
(function (win, doc, undef)
|
|
{
|
|
"use strict";
|
|
let sr = sax.data.selectRegion;
|
|
sr.regions = {}; sr.fn = {}; sr.ajax = {}; sr.itemIdPrefix = 'Rgn_';
|
|
|
|
//--------------------------------------------------------------
|
|
/*
|
|
* Region selection config:
|
|
* - caption - dialog title.
|
|
* - regionId - currently selected region.
|
|
* - typesInc[] - included region types.
|
|
* - typesExc[] - excluded region types.
|
|
* - fnCallback - method to call on completion to store selected region.
|
|
*/
|
|
sr.fn.selectRegion = function (cfg)
|
|
{
|
|
sr.fn.init();
|
|
sr.cfg = cfg;
|
|
sr.regionId = cfg.regionId;
|
|
if (sr.regions.length) sr.fn.showSelectRegion();
|
|
else
|
|
{
|
|
sr.ajax.regions = sr.ajax.regions || { url: sax.url.base + 'Reference/Ajax/GetRegions', formId: 'frmNavigate', callbackFn: sr.fn.refreshRegionsAndSelect };
|
|
sax.doAjaxRequest(sr.ajax.regions, 20);
|
|
}
|
|
}
|
|
sr.fn.refreshRegionsAndSelect = function (response)
|
|
{
|
|
if (!response || !response.success) return;
|
|
sr.regions = response.regions;
|
|
sax.debug(sr.cfg);
|
|
sr.fn.showSelectRegion();
|
|
}
|
|
sr.fn.showSelectRegion = function ()
|
|
{
|
|
$('.SelectedRegionName', sr.$dlg).text(sr.fn.getRegionName(sr.regionId));
|
|
$('#ifRegionFilter', sr.$dlg).val('');
|
|
sr.fn.displayRegions();
|
|
sax.openDialog(sr.$dlg.dialog('option', { title: sr.cfg.caption || 'Select Region' }), sr.cfg.width || 400, sr.cfg.height || 800);
|
|
}
|
|
|
|
//--------------------------------------------------------------
|
|
sr.fn.compareRegions = function (a, b)
|
|
{
|
|
var comparison = a.name.toLowerCase().localeCompare(b.name.toLowerCase());
|
|
if (comparison === 0)
|
|
return a.name.localeCompare(b.name);
|
|
return comparison;
|
|
}
|
|
|
|
//--------------------------------------------------------------
|
|
sr.fn.displayRegions = function ()
|
|
{
|
|
let filter = $('#ifRegionFilter', sr.$dlg).val();
|
|
let reFilter = new RegExp(sax.escapeRegExp((filter || '').trim()), 'i');
|
|
let $regionsAvail = $('.sax-selectRegion-regions.AvailableRegions', sr.$dlg).empty();
|
|
let $regionTmpl = $('.sax-panel-item-template .sax-panel-item.Region', sr.$dlg);
|
|
let regions = [...sr.regions];
|
|
let typesInc = sr.cfg.typesInc||[];
|
|
let typesExc = sr.cfg.typesExc||[];
|
|
regions.sort(sr.fn.compareRegions);
|
|
for (let rgnIdx = 0; rgnIdx < regions.length; rgnIdx++)
|
|
{
|
|
let region = regions[rgnIdx];
|
|
let id = region.id;
|
|
if (filter && !region.name.match(reFilter)) continue;
|
|
if (typesInc.length && !typesInc.includes(region.regionType)) continue;
|
|
if (typesExc.length && typesExc.includes(region.regionType)) continue;
|
|
let $region = $regionTmpl.clone();
|
|
$region.prop('id', sr.itemIdPrefix + id).on('click', function () { sr.fn.onClickRegion(id); }).on('dblclick', function () { sr.fn.onDblClickRegion(id); }).find('.RegionName').text(region.name).end();
|
|
if (id == sr.regionId) $region.addClass('sel');
|
|
$regionsAvail.append($region);
|
|
}
|
|
}
|
|
sr.fn.getRegion = function (id)
|
|
{
|
|
for (let rgnIdx = 0; rgnIdx < sr.regions.length; rgnIdx++)
|
|
{
|
|
let region = sr.regions[rgnIdx];
|
|
if (region.id == id) return region;
|
|
}
|
|
}
|
|
sr.fn.getRegionName = function (id)
|
|
{
|
|
let region = sr.fn.getRegion(id);
|
|
return region ? region.name : '';
|
|
}
|
|
|
|
//--------------------------------------------------------------
|
|
sr.fn.onClickRegion = function (id)
|
|
{
|
|
$('.SelectedRegionName', sr.$dlg).text(sr.fn.getRegionName(sr.regionId = id));
|
|
$('.sax-panel-item.Region', sr.$dlg).removeClass('sel');
|
|
$('.sax-panel-item.Region#' + sr.itemIdPrefix + id, sr.$dlg).addClass('sel');
|
|
}
|
|
sr.fn.onDblClickRegion = function (id)
|
|
{
|
|
sr.regionId = id;
|
|
sr.fn.storeSelectRegion();
|
|
}
|
|
sr.fn.storeSelectRegion = function ()
|
|
{
|
|
if (!sr.regionId) return;
|
|
let region = sr.fn.getRegion(sr.regionId);
|
|
if (!region) return;
|
|
if (sr.cfg.fnCallback) sr.cfg.fnCallback(region);
|
|
sr.$dlg.dialog('close');
|
|
sr.fn.cleanUp();
|
|
}
|
|
sr.fn.cleanUp = function ()
|
|
{
|
|
delete sr.cfg;
|
|
delete sr.regionId;
|
|
}
|
|
|
|
//--------------------------------------------------------------
|
|
sr.fn.onDlgSize = function ()
|
|
{
|
|
let $pnl = $('.sax-selectRegion-regions.AvailableRegions', sr.$dlg);
|
|
$pnl.outerHeight(Math.floor(sr.$dlg.innerHeight() - $pnl.position().top - ($pnl.outerHeight(true) - $pnl.innerHeight())));
|
|
}
|
|
|
|
//--------------------------------------------------------------
|
|
sr.fn.init = function ()
|
|
{
|
|
if (sr.$dlg) return;
|
|
var opt = sax.dlgOpts();
|
|
opt.open = function (evt, ui) { sr.fn.onDlgSize(); };
|
|
opt.resize = function (evt, ui) { sr.fn.onDlgSize(); };
|
|
opt.buttons[sax.dlg.btn.ok] = function () { sr.fn.storeSelectRegion(); };
|
|
opt.buttons[sax.dlg.btn.cancel] = function () { sr.fn.cleanUp(); $(this).dialog('close'); };
|
|
sr.$dlg = $('#dlgSelectRegion').dialog(opt);
|
|
}
|
|
|
|
}(window, window.document));
|
|
|
|
/* The End */ |