258 lines
10 KiB
JavaScript
258 lines
10 KiB
JavaScript
/*
|
|
* Contact page scripts.
|
|
*/
|
|
|
|
(function (win, doc, undef)
|
|
{
|
|
"use strict";
|
|
let co = sbm.Contacts = { 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 + 'Contact/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, '#frmContactFilter', co.refreshResults);
|
|
if (!response.success) return;
|
|
|
|
// Reference data.
|
|
co.config = response.config;
|
|
co.configInd = response.configInd;
|
|
co.configOrg = response.configOrg;
|
|
co.configGrp = response.configGrp;
|
|
co.data.organisationTypes = response.organisationTypes;
|
|
co.data.titles = response.titles;
|
|
co.data.salutations = response.salutations;
|
|
co.data.statuses = response.statuses;
|
|
co.data.defaultStatusCode = response.defaultStatusCode;
|
|
|
|
// 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.createOrg, function () { co.createItem(co.configOrg, sbm.dlg.title.createContact); }, 'sbm-add'));
|
|
$pnl.mnu.append(co.getFilterMenuOption(sbm.lbl.createInd, function () { co.createItem(co.configInd, sbm.dlg.title.createContact); }, 'sbm-add'));
|
|
$pnl.mnu.append(co.getFilterMenuOption(sbm.lbl.createGrp, function () { co.createItem(co.configGrp, sbm.dlg.title.createContact); }, 'sbm-add'));
|
|
$pnl.bgr.toggleVisibility(!!$('.sbm-dropdown-item', $pnl.mnu).length);
|
|
|
|
// Items.
|
|
var items = response.contacts || [];
|
|
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));
|
|
$('.ContactName', $item).text(item.displayName);
|
|
$('.ContactType', $item).text(item.typeDesc);
|
|
if (item.associationCount)
|
|
$('.ContactAssociationCount', $item).text(item.associationCount).on('click', function () { co.onClickAssociationCount(item); }).addClass('sbm-action-text');
|
|
$('.ContactAddress', $item).text(item.addressSummary);
|
|
$('.ContactContact', $item).text(item.contact);
|
|
$('.ContactPhone', $item).text(item.phone);
|
|
$('.ContactNotes', $item).text(item.notes);
|
|
$('.ContactStatusDesc', $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'));
|
|
$('.ContactName', $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 'Con_' + (item || {}).id;
|
|
}
|
|
|
|
//--------------------------------------------------------------
|
|
co.getFilterDropdownTemplate = function ()
|
|
{
|
|
return $('.sbm-dropdown-menu.ContactList li.sbm-dropdown-item', $pnl.tmpls).clone();
|
|
}
|
|
co.getItemTemplate = function ()
|
|
{
|
|
return $('table tr.sbm-row-item.Contact', $pnl.tmpls).clone();
|
|
}
|
|
co.getItemDropdownTemplate = function ()
|
|
{
|
|
return $('.sbm-dropdown-menu.Contact 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 (cfg, title)
|
|
{
|
|
co.clearItem(cfg);
|
|
let frm = $frm.item;
|
|
$('.ContactStatus', frm).val(co.data.defaultStatusCode).prop('readonly', false);
|
|
co.configureItemControls(cfg);
|
|
co.openItemDlg(title);
|
|
}
|
|
co.clearItem = function (cfg)
|
|
{
|
|
//sbm.debug('clearItem', cfg);
|
|
let frm = $frm.item;
|
|
sbm.clearItemForm(frm);
|
|
if (cfg.isIndividual) $('.ContactIsIndividual', frm).val(1);
|
|
if (cfg.isGroup) $('.ContactIsGroup', frm).val(1);
|
|
sbm.populateSelectCodeDesc('#cbContactType', co.data.organisationTypes);
|
|
sbm.populateSelectCodeDesc('#cbContactTitle', co.data.titles);
|
|
sbm.selectOrAdd('#cbContactTitle', "", "")
|
|
sbm.populateSelectCodeDesc('#cbContactSalutation', co.data.salutations);
|
|
sbm.populateSelectCodeDesc('#cbContactStatus', co.data.statuses);
|
|
}
|
|
sbm.clearItemForm = function ($frm)
|
|
{
|
|
sbm.clearControlErrors($frm);
|
|
$('.sbm-input,.sbm-textarea,.sbm-select,.sbm-checkbox', $frm).val('').prop('readonly', false).prop('disabled', false).removeAttr('checked');
|
|
}
|
|
co.configureItemControls = function (cfg)
|
|
{
|
|
sbm.configureItemControls($frm.item, cfg);
|
|
}
|
|
sbm.configureItemControls = function ($frm, cfg)
|
|
{
|
|
//sbm.debug('configureItemControls', cfg);
|
|
let fld = cfg.field || {};
|
|
let $ctrl = $('.sbm-form-control', $frm.item).show();
|
|
(fld.hide || []).forEach(function (v, i, a) { $ctrl.filter('.' + cfg.field.prefix + v).hide().find('.sbm-input,.sbm-textarea,.sbm-select,.sbm-checkbox').prop('disabled', true); });
|
|
(fld.readonly || []).forEach(function (v, i, a) { $ctrl.filter('.' + cfg.field.prefix + v).find('.sbm-input,.sbm.textarea,.sbm-select,.sbm-checkbox').prop('readonly', true).prop('disabled', true); });
|
|
}
|
|
co.openItemDlg = function (title)
|
|
{
|
|
if (title)$dlg.item.dialog('option', 'title', title);
|
|
sbm.openDialog($dlg.item, 640, 600);
|
|
}
|
|
|
|
//--------------------------------------------------------------
|
|
co.updateItem = function (item)
|
|
{
|
|
$('.ContactId', $frm.itemId).val(item.id);
|
|
co.ajax.updateItem = co.ajax.updateItem || { url: sbm.url.base + 'Contact/Ajax/GetItem', formId: 'frmContactId', callbackFn: co.onUpdateItem };
|
|
sbm.doAjaxRequest(co.ajax.updateItem, 50);
|
|
}
|
|
co.onUpdateItem = function (response)
|
|
{
|
|
//sbm.debug('onUpdateItem', response);
|
|
co.clearItem(response.config);
|
|
if (!response.success) return;
|
|
let item = response.contact;
|
|
let $ctrls = $('.sbm-input,.sbm-textarea,.sbm-select,.sbm-checkbox', $frm.item);
|
|
$ctrls.filter('.ContactId').val(item.id);
|
|
$ctrls.filter('.ContactExists').val(1);
|
|
$ctrls.filter('.ContactType').val(item.typeCode);
|
|
$ctrls.filter('.ContactProspect').prop('checked', !!item.prospect);
|
|
$ctrls.filter('.ContactNotes').val(item.notes);
|
|
$ctrls.filter('.ContactStatus').val(item.statusCode);
|
|
sbm.populateFieldValues(response.config, $frm.item, item.field);
|
|
co.configureItemControls(response.config);
|
|
co.openItemDlg(sbm.dlg.title.updateContact);
|
|
}
|
|
sbm.populateFieldValues = function (cfg, $frm, fld)
|
|
{
|
|
let val = (fld || {}).value || [];
|
|
let $ctrl = $('.sbm-form-control', $frm.item).show();
|
|
for (var name in val) { $ctrl.filter('.' + cfg.field.prefix + name).find('.sbm-input,.sbm-textarea,.sbm-select').val(val[name]).end().find('.sbm-checkbox').prop('checked',!!val[name]); };
|
|
}
|
|
|
|
//--------------------------------------------------------------
|
|
co.storeItem = function ()
|
|
{
|
|
co.ajax.storeItem = co.ajax.storeItem || { url: sbm.url.base + 'Contact/Ajax/StoreItem', formId: 'frmContact', 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.contact;
|
|
var $item = $('#' + co.getItemId(item), $pnl.table);
|
|
if (!$item.length)
|
|
{
|
|
$item = co.getItemTemplate();
|
|
$('table tbody', $pnl.table).prepend($item).show();
|
|
}
|
|
co.populateItem($item, item);
|
|
}
|
|
|
|
//--------------------------------------------------------------
|
|
co.onClickAssociationCount = function (item)
|
|
{
|
|
if (!item.id) return;
|
|
sbm.doPleaseWait();
|
|
let frm = $frm.itemId;
|
|
$('.ContactId', frm).val(item.id);
|
|
frm.prop('action', sbm.url.base + 'Association').submit();
|
|
}
|
|
|
|
//--------------------------------------------------------------
|
|
$(function ()
|
|
{
|
|
$pnl.items = $('.sbm-panel.Contacts');
|
|
$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 = $('#frmContactFilter', $pnl.items);
|
|
$frm.filter.on('input', '.FilterSearch', function () { co.refreshResults(250); }).on('change', '.FilterContactType', function () { co.refreshResults(50); });
|
|
|
|
$frm.itemId = $('#frmContactId');
|
|
$frm.item = $('#frmContact');
|
|
$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 = $('#dlgContact').dialog(opt);
|
|
|
|
setTimeout(co.refreshResults, 250);
|
|
});
|
|
|
|
//--------------------------------------------------------------
|
|
}(window, window.document));
|
|
|
|
/* The End */ |