263 lines
9.7 KiB
JavaScript
263 lines
9.7 KiB
JavaScript
/*
|
|
* SBM - Control - Param.
|
|
*/
|
|
|
|
(function (win, doc, undef)
|
|
{
|
|
"use strict";
|
|
let ip = sbc.itemParams = { data: {}, ajax: {} };
|
|
let $pnl = ip.$pnl = {}, $frm = ip.$frm = {}, $dlg = ip.$dlg = {};
|
|
|
|
//--------------------------------------------------------------
|
|
ip.refreshResults = function (delay)
|
|
{
|
|
if (!ip.awaitingRefreshResponse)
|
|
{
|
|
$pnl.bgr.hide();
|
|
$pnl.table.find('tbody tr').remove();
|
|
$pnl.msgs.hide().empty();
|
|
if (!$('.FilterItemId', $frm.filter).val())
|
|
{
|
|
$pnl.msgs.append($('<div class="sbc-message">' + sbc.msg.itemNotSelected + '</div>')).fadeIn(200);
|
|
return;
|
|
}
|
|
$pnl.msgs.append($('<div class="sbc-message">' + sbc.msg.dataQuerying + '</div>')).fadeIn(200);
|
|
}
|
|
ip.ajax.params = ip.ajax.params || { url: sbc.url.base + 'Param/Param/Ajax/GetList', formId: $frm.filter.prop('id'), callbackFn: ip.handleParams };
|
|
sbc.doAjaxRequest(ip.ajax.params, delay || 50);
|
|
ip.awaitingRefreshResponse = true;
|
|
}
|
|
ip.handleParams = function (response)
|
|
{
|
|
//sbc.debug('handleParams', response);
|
|
delete ip.awaitingRefreshResponse;
|
|
let $table = $('table', $pnl.table).find('tbody tr').remove().end();
|
|
$pnl.msgs.hide().empty();
|
|
sbc.refreshPaging(response, '#frmParamFilter', ip.refreshResults);
|
|
if (!response.success) return;
|
|
|
|
// Reference data - Statuses.
|
|
if (response.statuses) sbc.data.statuses = response.statuses;
|
|
if (response.statusesCreate) sbc.data.statusesCreate = response.statusesCreate;
|
|
if (response.statusCodeDefault) sbc.data.statusCodeDefault = response.statusCodeDefault;
|
|
|
|
// Menu.
|
|
$pnl.bgr.hide();
|
|
$pnl.mnu.empty();
|
|
$pnl.mnu.append(ip.getFilterMenuOption(sbc.lbl.refresh, function () { ip.refreshResults(); }, 'sbc-refresh'));
|
|
$pnl.mnu.append(ip.getFilterMenuOption(sbc.lbl.create, function () { ip.createItem(); }, 'sbc-add'));
|
|
$pnl.bgr.toggle(!!$('.sbc-dropdown-item', $pnl.mnu).length);
|
|
// Items.
|
|
var items = response.params || [];
|
|
if (!items.length)
|
|
return $pnl.msgs.append($('<div class="sbc-message">' + sbc.msg.dataNoneFound + '</div>')).fadeIn(500);
|
|
for (let itemIdx = 0; itemIdx < items.length; itemIdx++)
|
|
$table.append(ip.populateItem(ip.getItemRowTemplate(), items[itemIdx]));
|
|
}
|
|
ip.populateItem = function ($item, item)
|
|
{
|
|
if (!item) return;
|
|
$pnl.msgs.hide().empty();
|
|
$item.prop('id', ip.getItemId(item));
|
|
$('.ParamItem', $item).text(item.itemCaption);
|
|
$('.ParamContext', $item).text(item.context);
|
|
$('.ParamName', $item).text(item.name);
|
|
$('.ParamValue', $item).text(item.value);
|
|
$('.ParamStatusDesc', $item).text(item.statusDesc).removeStatusClasses().addClass('sbc-status-' + item.statusCode.toLowerCase());
|
|
// Actions.
|
|
let fnUpd = function () { ip.updateItem($item, item) };
|
|
var $bgr = $('.sbc-burger', $item).hide();
|
|
let $mnu = $('ul.sbc-dropdown-menu', $bgr).empty();
|
|
if (!item.deleted)
|
|
{
|
|
$mnu.append(ip.getItemMenuOption(sbc.lbl.edit, fnUpd, 'sbc-edit'));
|
|
$('.ParamName,.ParamValue', $item).off('click').on('click', fnUpd).addClass('sbc-action-text');
|
|
$item.off('dblclick').on('dblclick', fnUpd);
|
|
}
|
|
$bgr.toggle(!!$('.sbc-dropdown-item', $mnu).length);
|
|
return $item;
|
|
}
|
|
ip.getItemId = function (item)
|
|
{
|
|
return 'Itm_' + (item || {}).id;
|
|
}
|
|
|
|
//--------------------------------------------------------------
|
|
ip.getFilterDropdownTemplate = function ()
|
|
{
|
|
return $('.sbc-dropdown-menu.ParamFilter li.sbc-dropdown-item', $pnl.tmpls).clone();
|
|
}
|
|
ip.getItemRowTemplate = function ()
|
|
{
|
|
return $('table tr.sbc-row-item.Param', $pnl.tmpls).clone();
|
|
}
|
|
ip.getItemDropdownTemplate = function ()
|
|
{
|
|
return $('.sbc-dropdown-menu.Param li.sbc-dropdown-item', $pnl.tmpls).clone();
|
|
}
|
|
ip.getFilterMenuOption = function (lbl, fn, cls)
|
|
{
|
|
return ip.getMenuOption(ip.getFilterDropdownTemplate(), lbl, fn, cls);
|
|
}
|
|
ip.getItemMenuOption = function (lbl, fn, cls)
|
|
{
|
|
return ip.getMenuOption(ip.getItemDropdownTemplate(), lbl, fn, cls);
|
|
}
|
|
ip.getMenuOption = function ($opt, lbl, fn, cls)
|
|
{
|
|
return $opt.find('div.sbc-label').text(lbl).end().on('click', fn).addClass(cls);
|
|
}
|
|
|
|
//--------------------------------------------------------------
|
|
ip.createItem = function ()
|
|
{
|
|
if (!$('.FilterItemId', $frm.filter).val())
|
|
{
|
|
$sbc.dopAlert(sbc.msg.itemNotSelected);
|
|
return;
|
|
}
|
|
ip.clearItem();
|
|
$('.ParamItemType', $frm.item).val($('.FilterItemType', $frm.filter).val());
|
|
$('.ParamItemId', $frm.item).val($('.FilterItemId', $frm.filter).val());
|
|
$('.ParamItem', $frm.item).val($('.FilterItem', $frm.filter).val());
|
|
sbc.populateSelectCodeDesc('#cbParamStatus', sbc.data.statusesCreate);
|
|
$('#cbParamStatus').val(sbc.data.statusCodeDefault).prop('readonly', false);
|
|
$dlg.item.dialog('option', 'title', sbc.dlg.title.createItem);
|
|
ip.openParamDlg();
|
|
}
|
|
ip.openParamDlg = function ()
|
|
{
|
|
sbc.openDialog($dlg.item, 600, 600);
|
|
}
|
|
ip.clearItem = function ()
|
|
{
|
|
let frm = $frm.item;
|
|
$('.sbc-input,.sbc-textarea,.sbc-select', frm).val('');
|
|
sbc.populateSelectCodeDesc('#cbParamStatus', sbc.data.statuses);
|
|
sbc.clearControlErrors();
|
|
ip.setValueChanged(0);
|
|
}
|
|
ip.setValueChanged = function (changed)
|
|
{
|
|
$('.ParamValueChanged', $frm.item).val(changed ? 1 : '');
|
|
}
|
|
|
|
//--------------------------------------------------------------
|
|
ip.updateItem = function ($item, item)
|
|
{
|
|
ip.clearItem();
|
|
$('.ParamId', $frm.itemId).val(item.id);
|
|
ip.ajax.updateItem = ip.ajax.updateItem || { url: sbc.url.base + 'Param/Param/Ajax/GetItem', formId: $frm.itemId.prop('id'), callbackFn: ip.onUpdateItem };
|
|
sbc.doAjaxRequest(ip.ajax.updateItem, 50);
|
|
}
|
|
ip.onUpdateItem = function (response)
|
|
{
|
|
//sbc.debug('onUpdateItem', response);
|
|
let frm = $frm.item;
|
|
ip.clearItem();
|
|
if (!response.success) return;
|
|
let item = response.param;
|
|
$('.ParamExists', frm).val(1);
|
|
$('.ParamId', frm).val(item.id);
|
|
$('.ParamItemType', $frm.item).val(item.itemType);
|
|
$('.ParamItemId', $frm.item).val(item.itemId);
|
|
$('.ParamItem', $frm.item).val(item.itemCaption);
|
|
$('.ParamContext', frm).val(item.context);
|
|
$('.ParamName', frm).val(item.name);
|
|
$('.ParamValue', frm).val(item.value).prop('type', item.password ? 'password' : 'text');
|
|
$('.ParamNotes', frm).val(item.notes);
|
|
$('.ParamStatus', frm).val(item.statusCode).prop('readonly', true);
|
|
sbc.clearControlErrors();
|
|
$dlg.item.dialog('option', 'title', sbc.dlg.title.updateItem);
|
|
ip.openParamDlg();
|
|
}
|
|
|
|
//--------------------------------------------------------------
|
|
ip.storeItem = function ()
|
|
{
|
|
//sbc.debug('storeItem', 'Form id: ' + $frm.item.prop('id'), 'Item: ' + $('.ParamItem', $frm.item).val())
|
|
ip.ajax.storeItem = ip.ajax.storeItem || { url: sbc.url.base + 'Param/Param/Ajax/StoreItem', formId: $frm.item.prop('id'), callbackFn: ip.onStoreItem };
|
|
sbc.doAjaxRequest(ip.ajax.storeItem, 50);
|
|
}
|
|
ip.onStoreItem = function (response)
|
|
{
|
|
//sbc.debug('onStoreItem', response);
|
|
sbc.refreshControlValues(response.controlValues);
|
|
if (!response.success) return;
|
|
$dlg.item.dialog('close');
|
|
|
|
var item = response.param;
|
|
var $item = $('#' + ip.getItemId(item), $pnl.table);
|
|
if (!$item.length)
|
|
{
|
|
$item = ip.getItemRowTemplate();
|
|
$('table tbody', $pnl.table).prepend($item).show();
|
|
}
|
|
ip.populateItem($item, item);
|
|
// Force repopulation of sprts.
|
|
delete ip.data.params;
|
|
}
|
|
|
|
//--------------------------------------------------------------
|
|
ip.onFilterItemTypeChange = function ()
|
|
{
|
|
//sbc.debug('onFilterItemTypeChange');
|
|
$('.FilterItemId', $frm.filter).val('');
|
|
$('.FilterItem', $frm.filter).val('');
|
|
ip.refreshResults(250);
|
|
}
|
|
ip.selectFilterItem = function ()
|
|
{
|
|
//sbc.debug('selectFilterItem', $('.FilterItemType', $frm.filter).val());
|
|
let fnHandleFilterItems = function (response)
|
|
{
|
|
//sbc.debug('fnHandleFilterItems', response)
|
|
if (!response.success) return;
|
|
let cfg = { caption: sbc.lbl.selectItem, items: [], itemId: $('.FilterItemId', $frm.filter).val(), fnCallback: fnFilterItemSelected };
|
|
(response.items || []).forEach((o, i) => { cfg.items.push({ id: o.id, name: o.name }) });
|
|
sbc.fn.selectItem(cfg);
|
|
}
|
|
let fnFilterItemSelected = function (item)
|
|
{
|
|
//sbc.debug('fnFilterItemSelected', item);
|
|
$('.FilterItemId', $frm.filter).val(item.id);
|
|
$('.FilterItem', $frm.filter).val(item.name);
|
|
ip.refreshResults(250);
|
|
}
|
|
ip.ajax.filterItems = ip.ajax.filterItems || { url: sbc.url.base + 'Param/Param/Ajax/GetFilterItems', formId: $frm.filter.prop('id'), callbackFn: fnHandleFilterItems };
|
|
sbc.doAjaxRequest(ip.ajax.filterItems, 50);
|
|
}
|
|
|
|
//--------------------------------------------------------------
|
|
$(function ()
|
|
{
|
|
$pnl.items = $('.sbc-panel.Params');
|
|
$pnl.header = $('.sbc-panel-header', $pnl.items);
|
|
$pnl.bgr = $('.sbc-burger', $pnl.header);
|
|
$pnl.mnu = $('ul.sbc-dropdown-menu', $pnl.bgr);
|
|
$pnl.filter = $('.sbc-panel-filter', $pnl.items);
|
|
$pnl.msgs = $('.sbc-panel-messages', $pnl.items);
|
|
$pnl.table = $('.sbc-panel-item-table', $pnl.items);
|
|
$pnl.tmpls = $('.sbc-panel-item-template', $pnl.items);
|
|
|
|
$frm.filter = $('#frmParamFilter');
|
|
$('.FilterSearch', $frm.filter).on('input', function () { ip.refreshResults(250); });
|
|
$('.FilterItemType', $frm.filter).on('input', ip.onFilterItemTypeChange);
|
|
$($frm.filter).on('dblclick', '.FilterItem', ip.selectFilterItem).on('click', '.sbc-action.FilterItemSearch', ip.selectFilterItem);
|
|
|
|
$frm.itemId = $('#frmParamId');
|
|
var opt = sbc.dlgOpts();
|
|
opt.buttons[sbc.lbl.save] = function () { ip.storeItem(); };
|
|
opt.buttons[sbc.lbl.cancel] = function () { $(this).dialog('close'); };
|
|
$dlg.item = $('#dlgParam').dialog(opt);
|
|
$frm.item = $('#frmParam');
|
|
$($frm.item).on('input', '.sbc-input, .sbc-textarea', function () { sbc.clearControlError(this); }).on('change', '.sbc-select', function () { sbc.clearControlError(this); });
|
|
$($frm.item).on('input', '.sbc-input.ParamValue', ip.setValueChanged);
|
|
|
|
setTimeout(ip.refreshResults, 250);
|
|
});
|
|
|
|
//--------------------------------------------------------------
|
|
}(window, window.document));
|
|
|
|
/* The End */ |