189 lines
7.5 KiB
JavaScript
189 lines
7.5 KiB
JavaScript
/*
|
|
* Commodity 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 + 'Commodity/Ajax/List', formId: 'frmCommodityFilter', callbackFn: sbm.doRefreshResults };
|
|
sbm.doAjaxRequest(sbm.ajax.refreshResults, delay || 50);
|
|
sbm.awaitingRefreshResponse = true;
|
|
}
|
|
sbm.getFilterDropdownTemplate = function ()
|
|
{
|
|
return $('.sbm-panel-item-template .sbm-dropdown-menu.CommodityFilter li.sbm-dropdown-item', $pnl.filter).clone();
|
|
}
|
|
sbm.getItemTemplate = function ()
|
|
{
|
|
return $('.sbm-panel-item-template .sbm-panel-item.Commodity', $pnl.results).clone();
|
|
}
|
|
sbm.getItemDropdownTemplate = function ()
|
|
{
|
|
return $('.sbm-panel-item-template .sbm-dropdown-menu.Commodity li.sbm-dropdown-item', $pnl.results).clone();
|
|
}
|
|
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, '#frmCommodityFilter', sbm.refreshResults);
|
|
if (!response.success) return;
|
|
// Reference data - Statuses.
|
|
if (response.defaultStatusCode) sbm.data.defaultStatusCode = response.defaultStatusCode;
|
|
if (response.statuses)
|
|
sbm.populateSelectCodeDesc('#cbCommodityStatus', 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.commodities;
|
|
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);
|
|
$('.CommodityCode', $item).text(item.code);
|
|
$('.CommodityDesc', $item).text(item.desc);
|
|
$('.CommodityType', $item).text(item.typeDesc);
|
|
$('.CommodityAssociationCount', $item).on('click', function () { sbm.onClickAssociationCount(item.id); }).toggle(item.associationCount).find('.AssociationCount').text(item.associationCount);
|
|
$('.CommodityAddress', $item).text(item.addressSummary);
|
|
$('.CommodityNotes', $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.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.commodity;
|
|
$('.CommodityId,.CommodityExists,input[type="text"],textarea,select', frm).val('');
|
|
$('.CommodityStatus', frm).val(sbm.data.defaultStatusCode);
|
|
sbm.clearControlErrors();
|
|
sbm.$dlg.commodity.dialog('option', 'title', sbm.dlg.title.createCommodity);
|
|
sbm.openDialog(sbm.$dlg.commodity, 600, 600);
|
|
}
|
|
|
|
//--------------------------------------------------------------
|
|
sbm.updateItem = function (id)
|
|
{
|
|
$('.CommodityId', $frm.commodityId).val(id);
|
|
sbm.ajax.updateItem = sbm.ajax.updateItem || { url: sbm.url.base + 'Commodity/Ajax/GetItem', formId: 'frmCommodityId', callbackFn: sbm.onUpdateItem };
|
|
sbm.doAjaxRequest(sbm.ajax.updateItem, 50);
|
|
}
|
|
sbm.onUpdateItem = function (response)
|
|
{
|
|
sbm.debug('onUpdateItem', response);
|
|
let frm = $frm.commodity;
|
|
$('.CommodityId,input[type="text"],textarea,select', frm).val('');
|
|
if (!response.success) return;
|
|
let item = response.commodity;
|
|
$('.CommodityId', frm).val(item.id);
|
|
$('.CommodityExists', frm).val(1);
|
|
$('.CommodityType', frm).val(item.typeCode);
|
|
$('.CommodityCode', frm).val(item.code);
|
|
$('.CommodityDesc', frm).val(item.desc);
|
|
$('.CommodityNotes', frm).val(item.notes);
|
|
$('.CommodityStatus', frm).val(item.statusCode);
|
|
sbm.clearControlErrors();
|
|
sbm.$dlg.commodity.dialog('option', 'title', sbm.dlg.title.updateCommodity);
|
|
sbm.openDialog(sbm.$dlg.commodity, 600, 600);
|
|
}
|
|
|
|
//--------------------------------------------------------------
|
|
sbm.storeItem = function ()
|
|
{
|
|
sbm.ajax.storeItem = sbm.ajax.storeItem || { url: sbm.url.base + 'Commodity/Ajax/StoreItem', formId: 'frmCommodity', 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.commodity.dialog('close');
|
|
|
|
var item = response.commodity;
|
|
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.commodities = $('.sbm-panel.Commodities');
|
|
$pnl.filter = $('.sbm-panel-filter', $pnl.commodities);
|
|
$pnl.results = $('.sbm-panel-results', $pnl.commodities);
|
|
|
|
$('.sbm-input, .sbm-textarea').on('input', function () { sbm.clearControlError(this); });
|
|
$('.sbm-select').on('change', function () { sbm.clearControlError(this); });
|
|
|
|
$frm.filter = $('#frmCommodityFilter');
|
|
$('.FilterCommodity', $frm.filter).on('input', function () { sbm.refreshResults(250); });
|
|
$('.FilterCommodityType,.FilterCommodityStatusCategory', $frm.filter).on('change', function () { sbm.refreshResults(50); });
|
|
$frm.commodityId = $('#frmCommodityId');
|
|
$frm.commodity = $('#frmCommodity');
|
|
|
|
var opt = sbm.dlgOpts();
|
|
opt.buttons[sbm.dlg.btn.ok] = function () { sbm.storeItem(); };
|
|
opt.buttons[sbm.dlg.btn.cancel] = function () { $(this).dialog('close'); };
|
|
sbm.$dlg.commodity = $('#dlgCommodity').dialog(opt);
|
|
|
|
setTimeout(sbm.refreshResults, 250);
|
|
});
|
|
|
|
}(window, window.document));
|
|
|
|
/* The End */ |