Files
BusinessMgr-2019/SaBusinessMgrCoreWebPortal/wwwroot/js/sbm-association.js
T

189 lines
7.6 KiB
JavaScript

/*
* Association page scripts.
*/
(function (win, doc, undef)
{
"use strict";
sbm.itemIdPrefix = 'Itm_';
let $pnl = sbm.$pnl;
let $frm = sbm.$frm;
let $dlg = sbm.$dlg;
//--------------------------------------------------------------
sbm.refreshResults = function (delay)
{
if (!sbm.awaitingRefreshResponse)
{
$('.sbm-burger', $pnl.filter).hide();
$('.sbm-panel-item-list', $pnl.results).fadeOut(200).empty();
$('.sbm-panel-messages', $pnl.results).hide().empty().append($('<div class="sbm-message">' + sbm.msg.dataQuerying + '</div>')).fadeIn(200);
}
sbm.ajax.refreshResults = sbm.ajax.refreshResults || { url: sbm.url.base + 'Association/Ajax/List', formId: 'frmAssociationFilter', callbackFn: sbm.doRefreshResults };
sbm.doAjaxRequest(sbm.ajax.refreshResults, delay || 50);
sbm.awaitingRefreshResponse = true;
}
sbm.doRefreshResults = function (response)
{
//sbm.debug('doRefreshResults', response);
delete sbm.awaitingRefreshResponse;
sbm.refreshControlValues(response.controlValues);
var $msgs = $('.sbm-panel-messages', $pnl.results).hide().empty();
var $items = $('.sbm-panel-item-list', $pnl.results).hide().empty();
sbm.refreshPaging(response, '#frmAssociationFilter', sbm.refreshResults);
if (!response.success) return;
// Reference data - Statuses.
if (response.defaultStatusCode) sbm.data.defaultStatusCode = response.defaultStatusCode;
if (response.statuses)
sbm.populateSelectCodeDesc('#cbAssociationStatus', response.statuses);
// Menu.
var $bgr = $('.sbm-burger', $pnl.filter).hide();
var $mnu = $('ul.sbm-dropdown-menu', $bgr).empty();
$mnu.append(sbm.getFilterMenuOption(sbm.lbl.refresh, function () { sbm.refreshResults(); }));
$mnu.append(sbm.getFilterMenuOption(sbm.lbl.create, function () { sbm.createItem(); }));
$bgr.toggle(!!$('.sbm-dropdown-item', $mnu).length);
// Items.
var items = response.associations;
if (!items.length)
return $msgs.append($('<div class="sbm-message">' + sbm.msg.dataNoneFound + '</div>')).fadeIn(500);
for (let itemIdx = 0; itemIdx < items.length; itemIdx++)
$items.append(sbm.populateItem(sbm.getItemTemplate(), items[itemIdx]));
$items.fadeIn(50);
}
sbm.populateItem = function ($item, item)
{
if (!item) return;
$item.prop('id', sbm.itemIdPrefix + item.id);
$('.AssociationCode', $item).text(item.code);
$('.AssociationTargetName', $item).text(item.targetName);
$('.AssociationType', $item).text(item.typeDesc);
//$('.AssociationTransactionCount', $item).on('click', function () { sbm.onClickTransactionCount(item.id); }).toggle(item.associationCount).find('.TransactionCount').text(item.associationCount);
$('.AssociationAddress', $item).text(item.addressSummary);
$('.AssociationNotes', $item).text(item.notes);
$('.StatusDesc', $item).text(item.statusDesc).addClass('sbm-status-' + item.statusCode.toLowerCase());
$('.sbm-caption .sbm-action').toggleClass('sbm-action', !item.deleted);
let $mnu = $('ul.sbm-dropdown-menu', $item);
if (!item.deleted)
{
$mnu.append(sbm.getItemMenuOption(sbm.lbl.edit, function () { sbm.updateItem(item.id) }));
$('.sbm-header .sbm-action.sbm-item-update', $item).off('click').on('click', function () { sbm.updateItem(item.id) });
$item.off('dblclick').on('dblclick', function () { sbm.updateItem(item.id) });
}
$('.sbm-burger', $item).toggle(!!$('.sbm-dropdown-item', $mnu).length);
$('.sbm-panel-messages', $pnl.results).hide().empty();
return $item;
}
//--------------------------------------------------------------
sbm.getFilterDropdownTemplate = function ()
{
return $('.sbm-panel-item-template .sbm-dropdown-menu.AssociationFilter li.sbm-dropdown-item', $pnl.filter).clone();
}
sbm.getItemTemplate = function ()
{
return $('.sbm-panel-item-template .sbm-panel-item.Association', $pnl.results).clone();
}
sbm.getItemDropdownTemplate = function ()
{
return $('.sbm-panel-item-template .sbm-dropdown-menu.Association li.sbm-dropdown-item', $pnl.results).clone();
}
sbm.getFilterMenuOption = function (lbl, fn)
{
return sbm.getMenuOption(sbm.getFilterDropdownTemplate(), lbl, fn);
}
sbm.getItemMenuOption = function (lbl, fn)
{
return sbm.getMenuOption(sbm.getItemDropdownTemplate(), lbl, fn);
}
sbm.getMenuOption = function ($opt, lbl, fn)
{
return $opt.find('div.sbm-label').text(lbl).end().on('click', fn);
}
//--------------------------------------------------------------
sbm.createItem = function ()
{
let frm = $frm.association;
$('.AssociationId,.AssociationExists,input[type="text"],textarea,select', frm).val('');
$('.AssociationStatus', frm).val(sbm.data.defaultStatusCode);
sbm.clearControlErrors();
sbm.$dlg.association.dialog('option', 'title', sbm.dlg.title.createAssociation);
sbm.openDialog(sbm.$dlg.association, 600, 600);
}
//--------------------------------------------------------------
sbm.updateItem = function (id)
{
$('.AssociationId', $frm.associationId).val(id);
sbm.ajax.updateItem = sbm.ajax.updateItem || { url: sbm.url.base + 'Association/Ajax/GetItem', formId: 'frmAssociationId', callbackFn: sbm.onUpdateItem };
sbm.doAjaxRequest(sbm.ajax.updateItem, 50);
}
sbm.onUpdateItem = function (response)
{
sbm.debug('onUpdateItem', response);
let frm = $frm.association;
$('.AssociationId,input[type="text"],textarea,select', frm).val('');
if (!response.success) return;
let item = response.association;
$('.AssociationId', frm).val(item.id);
$('.AssociationExists', frm).val(1);
$('.AssociationType', frm).val(item.typeCode);
$('.AssociationCode', frm).val(item.code);
$('.AssociationDesc', frm).val(item.desc);
$('.AssociationNotes', frm).val(item.notes);
$('.AssociationStatus', frm).val(item.statusCode);
sbm.clearControlErrors();
sbm.$dlg.association.dialog('option', 'title', sbm.dlg.title.updateAssociation);
sbm.openDialog(sbm.$dlg.association, 600, 600);
}
//--------------------------------------------------------------
sbm.storeItem = function ()
{
sbm.ajax.storeItem = sbm.ajax.storeItem || { url: sbm.url.base + 'Association/Ajax/StoreItem', formId: 'frmAssociation', callbackFn: sbm.onStoreItem };
sbm.doAjaxRequest(sbm.ajax.storeItem, 50);
}
sbm.onStoreItem = function (response)
{
//sbm.debug('onStoreItem', response);
sbm.refreshControlValues(response.controlValues);
if (!response.success) return;
sbm.$dlg.association.dialog('close');
var item = response.association;
var $item = $('#' + sbm.itemIdPrefix + item.id);
if (!$item.length)
{
$item = sbm.getItemTemplate();
$('.sbm-panel-item-list', $pnl.results).prepend($item).show();
}
$item.replaceWith(sbm.populateItem(sbm.getItemTemplate(), item));
}
//--------------------------------------------------------------
$(function ()
{
$pnl.associations = $('.sbm-panel.Associations');
$pnl.filter = $('.sbm-panel-filter', $pnl.associations);
$pnl.results = $('.sbm-panel-results', $pnl.associations);
$('.sbm-input, .sbm-textarea').on('input', function () { sbm.clearControlError(this); });
$('.sbm-select').on('change', function () { sbm.clearControlError(this); });
$frm.filter = $('#frmAssociationFilter');
$('.FilterAssociation', $frm.filter).on('input', function () { sbm.refreshResults(250); });
$('.FilterAssociationType,.FilterAssociationStatusCategory', $frm.filter).on('change', function () { sbm.refreshResults(50); });
$frm.associationId = $('#frmAssociationId');
$frm.association = $('#frmAssociation');
var opt = sbm.dlgOpts();
opt.buttons[sbm.lbl.ok] = function () { sbm.storeItem(); };
opt.buttons[sbm.lbl.cancel] = function () { $(this).dialog('close'); };
sbm.$dlg.association = $('#dlgAssociation').dialog(opt);
setTimeout(sbm.refreshResults, 250);
});
}(window, window.document));
/* The End */