/*
Application: Sarand OperationsMgr
Javascript: Core
Notes: From a sarand.com original.
*/
//-------------------------------------------------------------
// Data members.
var opn = opn || {};
//-------------------------------------------------------------
// Utility functions.
opn.checkTextAreaMaxLength = function (ctrl, len) {
if (ctrl == null || ctrl.value == null) return;
if (ctrl.value.length > len) ctrl.value = ctrl.value.substring(0, len);
};
opn.isDef = function (obj) {
return obj === null || typeof obj == "undefined" ? false : true;
};
opn.isFn = function (fn) {
return fn != null && typeof fn == typeof Function;
};
opn.isNumeric = function (n) {
return !isNaN(parseFloat(n)) && isFinite(n);
};
opn.zeroPad = function (num, count) {
var val = num + '';
while (val.length < count) val = "0" + val;
return val;
};
opn.setOddDataRowClass = function (rowSel) {
$(rowSel).addClass(function (index, currentClasses) {
return index % 2 == 1 ? "other" : "";
});
};
opn.toInt = function (value, defaultValue) {
return opn.isNumeric(value) ? parseInt(value, 10) : defaultValue || 0;
}
//-------------------------------------------------------------
// Security.
opn.redirectHome = function () {
opn.doPleaseWait();
document.location = opn.urlHome;
}
opn.redirectHomeExp = function () {
opn.doPleaseWait();
document.location = opn.urlHomeExp;
}
opn.doLogout = function () {
var $dlg = $('#dlgOpnLogout');
if (!$dlg.hasClass('ui-dialog-content')) {
var optLogout = opn.dlgOpts();
optLogout.buttons[opn.dlgBtnLogout] = function () { $(this).dialog('close'); opn.onLogout(); };
optLogout.buttons[opn.dlgBtnCancel] = function () { $(this).dialog('close'); };
$dlg.dialog(optLogout);
}
$dlg.dialog('open');
};
opn.onLogout = function () {
opn.doPleaseWait();
$('form#frmOpnLogout').submit();
};
//-------------------------------------------------------------
// Data paging.
opn.doPrevDataPage = function (frm) {
var $frm = $(frm);
var $pageNo = $('#ifDataPageNo', $frm);
$pageNo.val(Math.max(1, opn.toInt($pageNo.val()) - 1));
opn.refreshItemList();
};
opn.doNextDataPage = function (frm) {
var $frm = $(frm);
var $pageNo = $('#ifDataPageNo', $frm);
$pageNo.val(opn.toInt($pageNo.val()) + 1);
opn.refreshItemList();
};
opn.refreshPaging = function (paging, $paging) {
if (!paging) {
$paging.hide().Hide();
return;
}
$('#ifDataPageNo', $paging).val(paging ? paging.pageNo : '');
$('.PageNo', $paging).text(paging ? paging.pageNo : '');
$('#DataPageCount', $paging).val(paging ? paging.pageCount : '');
$('.PageCount', $paging).text(paging ? paging.pageCount : '');
$('#DataItemCount', $paging).val(paging ? paging.itemCount : '');
$('#DataItemTotal', $paging).val(paging ? paging.itemTotal : '');
$('.ItemTotal', $paging).text(paging ? paging.itemTotal : '');
if (paging.paged) $paging.show(); else $paging.hide();
}
//-------------------------------------------------------------
// Dialogs.
opn.openDialog = function ($dlg, width) {
$dlg.dialog({ minWidth: null, maxWidth: null, width: Math.min(width || 400, $(window).width() * .90) }).dialog('open');
return $dlg;
};
opn.dlgOpts = function () {
return { autoOpen: false, modal: true, show: 'clip', hide: 'clip', minWidth: 320, maxWidth: 480, width: 'auto', resizable: false, draggable: false, closeOnEscape: false, dialogClass: "no-close center-buttons", buttons: {} };
};
opn.dlgOptsClose = function () {
var opts = opn.dlgOpts();
opts.buttons[opn.dlgBtnClose] = function () { $(this).dialog('close'); };
return opts;
};
opn.doAlert = function (title, msg, onOk) {
if (!msg) {
msg = title;
title = null;
}
if (!opn.isDef(opn.$dlgAlert)) {
var opt = opn.dlgOpts();
opt.title = title || opn.dlgTitleAlert;
opt.buttons[opn.dlgBtnOk] = function () { $(this).dialog('close'); if (onOk) onOk(); };
opn.$dlgAlert = $('
').dialog(opt);
}
$('#dlgAlertMsg').html(msg);
opn.openDialog(opn.$dlgAlert);
};
opn.doConfirm = function (msg, onOk, onCancel) {
if (!opn.isDef(opn.$dlgConfirm)) {
var opt = opn.dlgOpts();
opt.title = opn.dlgTitleConfirm;
opn.$dlgConfirm = $('').dialog(opt);
}
var buttons = opn.$dlgConfirm.dialog('option', 'buttons');
buttons[opn.dlgBtnYes] = function () { $(this).dialog('close'); if (onOk) onOk(); };
buttons[opn.dlgBtnNo] = function () { $(this).dialog('close'); if (onCancel) onCancel(); };
opn.$dlgConfirm.dialog('option', 'buttons', buttons);
$('#dlgConfirmMsg').html(msg);
opn.openDialog(opn.$dlgConfirm);
};
opn.doProgress = function (msg, delay) {
clearTimeout(opn.toShowProgress);
if (!opn.isDef(opn.$dlgProgress)) {
var opt = opn.dlgOpts();
opt.title = opn.dlgTitleProgress;
opt.show = { effect: 'fade', duration: 100 };
opt.hide = { effect: 'fade', duration: 100 };
opn.$dlgProgress = $('').dialog(opt);
}
$('#dlgProgressMsg').html(msg);
if (delay > 0) opn.toShowProgress = setTimeout(function () { opn.$dlgProgress.dialog('open'); }, delay);
else opn.$dlgProgress.dialog('open');
};
opn.hideProgress = function () {
clearTimeout(opn.toShowProgress);
opn.$dlgProgress.dialog('close');
};
//-------------------------------------------------------------
// Notifications.
opn.showNotification = function (title, msg) {
window.clearTimeout(opn.toNotification);
var $win = $(window);
var $not = $('#opn-notification').hide();
if (!$not.length) {
$not = $('');
$('body').append($not);
}
var $hdr = $('.heading', $not).css({ 'background-color': 'rgba(247,217,0,0.7)' });
$('.title', $not).html(title);
$('.message', $not).html(msg);
var fbr = $('#opn-footer')[0].getBoundingClientRect();
var fn = function () { $hdr.delay(2000).animate({ backgroundColor: '#f0f0f0' }, 1000); };
$not.css({ 'bottom': $win.height() - fbr.top, 'right': $win.width() - fbr.right }).slideDown(500, fn);
opn.toNotification = setTimeout(opn.hideNotification, 6000);
};
opn.hideNotification = function () {
window.clearTimeout(opn.toNotification);
$('#opn-notification').slideUp(500);
};
opn.doPleaseWait = function () {
$('#opn-please-wait').show();
};
opn.hidePleaseWait = function () {
$('#opn-please-wait').hide();
};
//-------------------------------------------------------------
// Ajax.
opn.doAjaxRequest = function (ajax, url, formId, callback, delay) {
window.clearTimeout(ajax.timeout);
if (url) ajax.url = url;
if (formId) ajax.formId = formId;
if (callback) ajax.callback = callback;
if (!ajax.noWaitIcon) opn.doPleaseWait();
ajax.timeout = setTimeout(function () { opn.execAjaxRequest(ajax); }, Math.max(50, parseInt(delay || 0, 10)));
};
opn.execAjaxRequest = function (ajax) {
window.clearTimeout(ajax.timeout);
if (ajax.request) ajax.request.abort();
var cfgAjax =
{
url: ajax.url,
type: 'POST',
cache: false,
enctype: 'multipart/form-data',
data: ajax.formData || $("#" + ajax.formId).serialize(),
dataType: 'json',
success: function (response, status, jqXHR) {
ajax.status = status;
if (response.notification)
opn.showNotification(response.notification.title, response.notification.message);
if (response.message)
opn.doAlert(response.message.title, response.message.text, function (response) { if (ajax.errFn) ajax.errFn(response); }(response));
if (response.sessionExpired)
return opn.redirectHomeExp();
if (response.goHome)
return opn.redirectHome();
if (ajax.callback) ajax.callback(response);
},
error: function (jqXHR, status, errorThrown) {
ajax.status = status;
if (status == "abort") return;
return opn.redirectHome();
},
complete: function (jqXHR, textStatus) {
if (!ajax.noWaitIcon) opn.hidePleaseWait();
ajax.complete = true;
}
};
if (ajax.formData) {
cfgAjax.processData = false,
cfgAjax.contentType = false;
}
//console.log(ajax.formData);
ajax.request = $.ajax(cfgAjax);
};
//-------------------------------------------------------------
// Viewport - Desktop or Mobile.
opn.isMob = function () { return $('body.mob').length > 0; };
opn.onChangeViewport = function (viewport) {
var $body = $('body');
$body.removeClass('mob dsk');
if (viewport.is('xs')) $body.addClass('mob');
else if (viewport.current() != 'unrecognized') $body.addClass('dsk');
};
opn.isAutoHeight = function () { return ($(window).width() < 360 || $(window).height() < 560 || opn.isMob()); }
opn.onWinResizePage = function () { };
opn.onWinResize = function () {
var winH = $(window).height();
var $pageBody = $('#opn-body');
var $hdr = $('#opn-header');
var $ftr = $('#opn-footer');
//console.log(winH);
//console.log($hdr.outerHeight(true));
//console.log($ftr.outerHeight(true));
if (opn.isAutoHeight())
$pageBody.height('auto');
else
$pageBody.innerHeight(Math.min(1200, winH - $hdr.outerHeight() - $ftr.outerHeight(true)));
$ftr.css('visibility', 'visible');
opn.onWinResizePage();
};
opn.objBtm = function (sel) {
var $obj = $(sel);
return Math.floor($obj.offset().top + $obj.outerHeight(true));
};
opn.innerBtm = function (sel) {
var $obj = $(sel);
return Math.floor($obj.offset().top + $obj.outerHeight() - opn.bottomSurround($obj));
};
opn.innerTop = function (sel) {
var $obj = $(sel);
return Math.floor($obj.offset().top + opn.topSurround($obj));
};
opn.topSurround = function (sel) {
var $obj = $(sel);
return parseInt($obj.css('margin-top'), 10) + parseInt($obj.css('border-top-width'), 10) + parseInt($obj.css('padding-top'), 10);
};
opn.bottomSurround = function (sel) {
var $obj = $(sel);
return parseInt($obj.css('margin-bottom'), 10) + parseInt($obj.css('border-bottom-width'), 10) + parseInt($obj.css('padding-bottom'), 10);
};
opn.marginHeight = function (sel) {
var $obj = $(sel);
return parseInt($obj.css('margin-top'), 10) + parseInt($obj.css('margin-bottom'), 10);
};
opn.borderHeight = function (sel) {
var $obj = $(sel);
return parseInt($obj.css('border-top-width'), 10) + parseInt($obj.css('border-bottom-width'), 10);
};
//-------------------------------------------------------------
// On page load.
$(function () {
//console.log('Page loaded');
(function ($, viewport) {
var timerResize = 0;
$(window).bind('resize', function (e) {
viewport.changed(function () { opn.onChangeViewport(viewport); });
clearTimeout(timerResize);
timerResize = setTimeout(opn.onWinResize, 200);
}).trigger('resize');
})($, ResponsiveBootstrapToolkit);
$('textarea[maxlength]').keyup(function () {
var limit = parseInt($(this).attr('maxlength'), 10);
var text = $(this).val();
if (text.length > limit)
$(this).val(text.substr(0, limit));
});
// Text selection in element.
jQuery.fn.selectText = function () {
/*
this.find('input').each(function()
{
if($(this).prev().length == 0 || !$(this).prev().hasClass('p_copy'))
$('').insertBefore($(this));
$(this).prev().html($(this).val());
});
*/
var doc = document;
var element = this[0];
var range;
if (doc.body.createTextRange) {
range = doc.body.createTextRange();
range.moveToElementText(element);
range.select();
}
else if (window.getSelection) {
range = doc.createRange();
range.selectNodeContents(element);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
}
};
});
$(window).load(function () {
//console.log('Window loaded');
$(window).trigger('resize');
});
//-------------------------------------------------------------
// T h e E n d
//-------------------------------------------------------------