Files
BusinessMgr-2019/SaBusinessMgrCoreWebPortal/wwwroot/js/sbm-assoc.js
T
2024-11-19 09:47:00 +00:00

211 lines
7.8 KiB
JavaScript

/*
* Association page scripts.
*/
(function (win, doc, undef)
{
"use strict";
let as = sbm.Associations = { data: {}, ajax: {} };
let $pnl = as.$pnl = {}, $frm = as.$frm = {}, $dlg = as.$dlg = {};
//--------------------------------------------------------------
as.refreshResults = function (delay)
{
if (!sbm.awaitingRefreshResponse)
{
$pnl.bgr.invisible();
$pnl.table.find('tbody tr').remove();
$pnl.msgs.hide().empty().append($('<div class="sbm-message">' + sbm.msg.dataQuerying + '</div>')).fadeIn(200);
}
as.ajax.refreshResults = as.ajax.refreshResults || { url: sbm.url.base + 'Association/Ajax/List', formId: $frm.filter.prop('id'), callbackFn: as.doRefreshResults };
sbm.doAjaxRequest(as.ajax.refreshResults, delay || 50);
sbm.awaitingRefreshResponse = true;
}
as.doRefreshResults = function (response)
{
sbm.debug('doRefreshResults', response);
delete sbm.awaitingRefreshResponse;
$pnl.msgs.hide().empty();
let $table = $('table', $pnl.table).find('tbody tr').remove().end();
sbm.refreshPaging(response, '#frmAssociationFilter', as.refreshResults);
if (!response.success) return;
// Reference data.
as.data.associationTypes = response.associationTypes;
as.data.statuses = response.statuses;
as.data.defaultStatusCode = response.defaultStatusCode;
// Menu.
$pnl.bgr.invisible();
$pnl.mnu.empty();
$pnl.mnu.append(as.getFilterMenuOption(sbm.lbl.refresh, function () { as.refreshResults(); }, 'sbm-refresh'));
$pnl.mnu.append(as.getFilterMenuOption(sbm.lbl.create, function () { as.createItem(); }, 'sbm-add'));
$pnl.bgr.toggleVisibility(!!$('.sbm-dropdown-item', $pnl.mnu).length);
// Items.
var items = response.associations || [];
if (!items.length)
return $pnl.msgs.append($('<div class="sbm-message">' + sbm.msg.dataNoneFound + '</div>')).fadeIn(500);
for (let itemIdx = 0; itemIdx < items.length; itemIdx++)
$table.append(as.populateItem(as.getItemTemplate(), items[itemIdx]));
}
as.populateItem = function ($item, item)
{
if (!item) return;
$pnl.msgs.hide().empty();
$item.prop('id', as.getItemId(item));
$('.AssociationCode', $item).text(item.code);
$('.AssociationTargetName', $item).text(item.targetName);
$('.AssociationType', $item).text(item.typeDesc);
$('.AssociationAddress', $item).text(item.addressSummary);
$('.AssociationNotes', $item).text(item.notes);
$('.AssociationStatusDesc', $item).text(item.statusDesc).removeStatusClasses().addClass('sbm-status-' + item.statusCode.toLowerCase());
// Actions.
var $bgr = $('.sbm-burger', $item).hide();
let $mnu = $('ul.sbm-dropdown-menu', $bgr).empty();
if (!item.deleted)
{
let fnUpd = function () { as.updateItem(item) };
$mnu.append(as.getItemMenuOption(sbm.lbl.edit, fnUpd, 'sbm-edit'));
$('.AssociationTargetName', $item).off('click').on('click', fnUpd).addClass('sbm-action-text');
$item.off('dblclick').on('dblclick', fnUpd);
}
$bgr.toggle(!!$('.sbm-dropdown-item', $mnu).length);
return $item;
}
as.getItemId = function (item)
{
return 'Ass_' + (item || {}).id;
}
//--------------------------------------------------------------
as.getFilterDropdownTemplate = function ()
{
return $('.sbm-dropdown-menu.AssociationList li.sbm-dropdown-item', $pnl.tmpls).clone();
}
as.getItemTemplate = function ()
{
return $('table tr.sbm-row-item.Association', $pnl.tmpls).clone();
}
as.getItemDropdownTemplate = function ()
{
return $('.sbm-dropdown-menu.Association li.sbm-dropdown-item', $pnl.tmpls).clone();
}
//--------------------------------------------------------------
as.getFilterMenuOption = function (lbl, fn, cls)
{
return as.getMenuOption(as.getFilterDropdownTemplate(), lbl, fn, cls);
}
as.getItemMenuOption = function (lbl, fn, cls)
{
return as.getMenuOption(as.getItemDropdownTemplate(), lbl, fn, cls);
}
as.getMenuOption = function ($opt, lbl, fn, cls)
{
return $opt.find('div.sbm-label').text(lbl).end().on('click', fn).addClass(cls);
}
//--------------------------------------------------------------
as.createItem = function ()
{
as.clearItem();
let $ctrls = $('.sbm-input,.sbm-textarea,.sbm-select', $frm.item);
$ctrls.filter('.AssociationType').val('').prop('disabled', false);
$ctrls.filter('.AssociationStatus').val(as.data.defaultStatusCode);
$dlg.item.dialog('option', 'title', sbm.dlg.title.createAssociation);
as.openItemDlg();
}
as.clearItem = function ()
{
sbm.clearControlErrors($frm.item);
$('.sbm-input,.sbm-textarea,.sbm-select', $frm.item).val('');
sbm.populateSelectCodeDesc('#cbAssociationType', as.data.associationTypes);
sbm.populateSelectCodeDesc('#cbAssociationStatus', as.data.statuses);
}
as.openItemDlg = function ()
{
sbm.openDialog($dlg.item, 600, 640);
}
//--------------------------------------------------------------
as.updateItem = function (item)
{
$('.AssociationId', $frm.itemId).val(item.id);
as.ajax.updateItem = as.ajax.updateItem || { url: sbm.url.base + 'Association/Ajax/GetItem', formId: 'frmAssociationId', callbackFn: as.onUpdateItem };
sbm.doAjaxRequest(as.ajax.updateItem, 50);
}
as.onUpdateItem = function (response)
{
//sbm.debug('onUpdateItem', response);
as.clearItem();
if (!response.success) return;
let item = response.association;
let $ctrls = $('.sbm-input,.sbm-textarea,.sbm-select', $frm.item);
$ctrls.filter('.AssociationId').val(item.id);
$ctrls.filter('.AssociationExists').val(1);
let $ctrlType = $ctrls.filter('.AssociationType');
sbm.selectOrAdd($ctrlType, item.typeCode, item.typeDesc)
if ($ctrlType.val()) $ctrlType.prop('disabled', true);
$ctrls.filter('.AssociationTargetId').val(item.targetId);
$ctrls.filter('.AssociationTarget').val(item.targetName);
$ctrls.filter('.AssociationCode').val(item.code);
$ctrls.filter('.AssociationDesc').val(item.desc);
$ctrls.filter('.AssociationNotes').val(item.notes);
sbm.selectOrAdd($ctrls.filter('.AssociationStatus'), item.statusCode, item.statusDesc)
$dlg.item.dialog('option', 'title', sbm.dlg.title.updateAssociation);
as.openItemDlg();
}
//--------------------------------------------------------------
as.storeItem = function ()
{
as.ajax.storeItem = as.ajax.storeItem || { url: sbm.url.base + 'Association/Ajax/StoreItem', formId: 'frmAssociation', callbackFn: as.onStoreItem };
sbm.doAjaxRequest(as.ajax.storeItem, 50);
}
as.onStoreItem = function (response)
{
//sbm.debug('onStoreItem', response);
sbm.refreshControlValues(response.controlValues);
if (!response.success) return;
$dlg.item.dialog('close');
var item = response.association;
var $item = $('#' + as.getItemId(item), $pnl.table);
if (!$item.length)
{
$item = as.getItemTemplate();
$('table tbody', $pnl.table).prepend($item).show();
}
as.populateItem($item, item);
}
//--------------------------------------------------------------
$(function ()
{
$pnl.items = $('.sbm-panel.Associations');
$pnl.header = $('.sbm-panel-header', $pnl.items);
$pnl.bgr = $('.sbm-burger', $pnl.header);
$pnl.mnu = $('ul.sbm-dropdown-menu', $pnl.bgr);
$pnl.table = $('.sbm-panel-item-table', $pnl.items);
$pnl.msgs = $('.sbm-panel-messages', $pnl.table);
$pnl.tmpls = $('.sbm-panel-item-template', $pnl.items);
$frm.filter = $('#frmAssociationFilter');
$frm.filter.on('input', '.FilterSearch', function () { as.refreshResults(250); }).on('change', '.FilterAssociationType', function () { as.refreshResults(50); });
$frm.itemId = $('#frmAssociationId');
$frm.item = $('#frmAssociation');
$frm.item.on('input', '.sbm-input, .sbm-textarea', function () { sbm.clearControlError(this); }).on('change', '.sbm-select', function () { sbm.clearControlError(this); });
var opt = sbm.dlgOpts();
opt.buttons[sbm.lbl.ok] = function () { as.storeItem(); };
opt.buttons[sbm.lbl.cancel] = function () { $(this).dialog('close'); };
$dlg.item = $('#dlgAssociation').dialog(opt);
setTimeout(as.refreshResults, 250);
});
}(window, window.document));
/* The End */