206 lines
7.5 KiB
JavaScript
206 lines
7.5 KiB
JavaScript
/*
|
|
* Commodity page scripts.
|
|
*/
|
|
|
|
(function (win, doc, undef)
|
|
{
|
|
"use strict";
|
|
let co = sbm.Commodities = { data: {}, ajax: {} };
|
|
let $pnl = co.$pnl = {}, $frm = co.$frm = {}, $dlg = co.$dlg = {};
|
|
|
|
//--------------------------------------------------------------
|
|
co.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);
|
|
}
|
|
co.ajax.refreshResults = co.ajax.refreshResults || { url: sbm.url.base + 'Commodity/Ajax/List', formId: $frm.filter.prop('id'), callbackFn: co.doRefreshResults };
|
|
sbm.doAjaxRequest(co.ajax.refreshResults, delay || 50);
|
|
sbm.awaitingRefreshResponse = true;
|
|
}
|
|
co.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, '#frmCommodityFilter', co.refreshResults);
|
|
if (!response.success) return;
|
|
|
|
// Reference data.
|
|
if (response.defaultStatusCode) sbm.data.defaultStatusCode = response.defaultStatusCode;
|
|
if (response.statuses) sbm.data.statuses = response.statuses;
|
|
|
|
// Menu.
|
|
$pnl.bgr.invisible();
|
|
$pnl.mnu.empty();
|
|
$pnl.mnu.append(co.getFilterMenuOption(sbm.lbl.refresh, function () { co.refreshResults(); }, 'sbm-refresh'));
|
|
$pnl.mnu.append(co.getFilterMenuOption(sbm.lbl.create, function () { co.createItem(); }, 'sbm-add'));
|
|
$pnl.bgr.toggleVisibility(!!$('.sbm-dropdown-item', $pnl.mnu).length);
|
|
|
|
// Items.
|
|
var items = response.commodities || [];
|
|
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(co.populateItem(co.getItemTemplate(), items[itemIdx]));
|
|
}
|
|
co.populateItem = function ($item, item)
|
|
{
|
|
if (!item) return;
|
|
$pnl.msgs.hide().empty();
|
|
$item.prop('id', co.getItemId(item));
|
|
$('.CommodityType', $item).text(item.typeDesc);
|
|
$('.CommodityCode', $item).text(item.code);
|
|
$('.CommodityDesc', $item).text(item.desc);
|
|
$('.CommoditySalesPrice', $item).text(item.salesPriceFmt);
|
|
$('.CommoditySalesUnits', $item).text(item.salesUnit);
|
|
$('.CommodityNotes', $item).text(item.notes);
|
|
$('.CommodityStatusDesc', $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 () { co.updateItem(item) };
|
|
$mnu.append(co.getItemMenuOption(sbm.lbl.edit, fnUpd, 'sbm-edit'));
|
|
$('.CommodityCode,.CommodityDesc', $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;
|
|
}
|
|
co.getItemId = function (item)
|
|
{
|
|
return 'Cmd_' + (item || {}).id;
|
|
}
|
|
|
|
//--------------------------------------------------------------
|
|
co.getFilterDropdownTemplate = function ()
|
|
{
|
|
return $('.sbm-dropdown-menu.CommodityList li.sbm-dropdown-item', $pnl.tmpls).clone();
|
|
}
|
|
co.getItemTemplate = function ()
|
|
{
|
|
return $('table tr.sbm-row-item.Commodity', $pnl.tmpls).clone();
|
|
}
|
|
co.getItemDropdownTemplate = function ()
|
|
{
|
|
return $('.sbm-dropdown-menu.Commodity li.sbm-dropdown-item', $pnl.tmpls).clone();
|
|
}
|
|
|
|
//--------------------------------------------------------------
|
|
co.getFilterMenuOption = function (lbl, fn, cls)
|
|
{
|
|
return co.getMenuOption(co.getFilterDropdownTemplate(), lbl, fn, cls);
|
|
}
|
|
co.getItemMenuOption = function (lbl, fn, cls)
|
|
{
|
|
return co.getMenuOption(co.getItemDropdownTemplate(), lbl, fn, cls);
|
|
}
|
|
co.getMenuOption = function ($opt, lbl, fn, cls)
|
|
{
|
|
return $opt.find('div.sbm-label').text(lbl).end().on('click', fn).addClass(cls);
|
|
}
|
|
|
|
//--------------------------------------------------------------
|
|
co.createItem = function ()
|
|
{
|
|
co.clearItem();
|
|
let frm = $frm.item;
|
|
$('.CommodityStatus', frm).val(sbm.data.defaultStatusCode);
|
|
sbm.clearControlErrors();
|
|
$dlg.item.dialog('option', 'title', sbm.dlg.title.createCommodity).prop('readonly', false);
|
|
co.openItemDlg();
|
|
}
|
|
co.clearItem = function ()
|
|
{
|
|
sbm.clearControlErrors($frm.item);
|
|
$('.sbm-input,.sbm-textarea,.sbm-select', $frm.item).val('');
|
|
sbm.populateSelectCodeDesc('#cbCommodityStatus', sbm.data.statuses);
|
|
}
|
|
co.openItemDlg = function ()
|
|
{
|
|
sbm.openDialog($dlg.item, 600, 640);
|
|
}
|
|
|
|
//--------------------------------------------------------------
|
|
co.updateItem = function (item)
|
|
{
|
|
$('.CommodityId', $frm.itemId).val(item.id);
|
|
co.ajax.updateItem = co.ajax.updateItem || { url: sbm.url.base + 'Commodity/Ajax/GetItem', formId: 'frmCommodityId', callbackFn: co.onUpdateItem };
|
|
sbm.doAjaxRequest(co.ajax.updateItem, 50);
|
|
}
|
|
co.onUpdateItem = function (response)
|
|
{
|
|
//sbm.debug('onUpdateItem', response);
|
|
co.clearItem();
|
|
if (!response.success) return;
|
|
let item = response.commodity;
|
|
let $ctrls = $('.sbm-input,.sbm-textarea,.sbm-select', $frm.item);
|
|
$ctrls.filter('.CommodityId').val(item.id);
|
|
$ctrls.filter('.CommodityExists').val(1);
|
|
$ctrls.filter('.CommodityType').val(item.typeCode);
|
|
$ctrls.filter('.CommodityCode').val(item.code);
|
|
$ctrls.filter('.CommodityDesc').val(item.desc);
|
|
$ctrls.filter('.CommodityNotes').val(item.notes);
|
|
sbm.selectOrAdd($ctrls.filter('.CommodityStatus'), item.statusCode, item.statusDesc);
|
|
$dlg.item.dialog('option', 'title', sbm.dlg.title.updateCommodity);
|
|
co.openItemDlg();
|
|
}
|
|
|
|
//--------------------------------------------------------------
|
|
co.storeItem = function ()
|
|
{
|
|
co.ajax.storeItem = co.ajax.storeItem || { url: sbm.url.base + 'Commodity/Ajax/StoreItem', formId: 'frmCommodity', callbackFn: co.onStoreItem };
|
|
sbm.doAjaxRequest(co.ajax.storeItem, 50);
|
|
}
|
|
co.onStoreItem = function (response)
|
|
{
|
|
//sbm.debug('onStoreItem', response);
|
|
sbm.refreshControlValues(response.controlValues);
|
|
if (!response.success) return;
|
|
$dlg.item.dialog('close');
|
|
|
|
var item = response.commodity;
|
|
var $item = $('#' + co.getItemId(item), $pnl.table);
|
|
if (!$item.length)
|
|
{
|
|
$item = co.getItemTemplate();
|
|
$('table tbody', $pnl.table).prepend($item).show();
|
|
}
|
|
co.populateItem($item, item);
|
|
}
|
|
|
|
//--------------------------------------------------------------
|
|
$(function ()
|
|
{
|
|
$pnl.items = $('.sbm-panel.Commodities');
|
|
$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 = $('#frmCommodityFilter');
|
|
$frm.filter.on('input', '.FilterSearch', function () { co.refreshResults(250); }).on('change', '.FilterCommodityType,.FilterStatusCat', function () { co.refreshResults(50); });
|
|
|
|
$frm.itemId = $('#frmCommodityId');
|
|
$frm.item = $('#frmCommodity');
|
|
$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 () { co.storeItem(); };
|
|
opt.buttons[sbm.lbl.cancel] = function () { $(this).dialog('close'); };
|
|
$dlg.item = $('#dlgCommodity').dialog(opt);
|
|
|
|
setTimeout(co.refreshResults, 250);
|
|
});
|
|
|
|
}(window, window.document));
|
|
|
|
/* The End */ |