/**
* File Name: fumctions.php
* Date Created: 12/08
*
*/
/*
* Message box to display errors for a given Ext object
* protecting actual Javascript errors from throwing
*
* @param obj The Ext object to attempt to extract errors from
* @param type string value of the Ext type
*/
function displayErrors(obj, type) {
try {
switch (type) {
case 'store':
generateErrorWarning(obj.reader.jsonData.errors);
break;
case 'action':
if (typeof(obj.responseText) != 'undefined') {
var responseText = Ext.decode(obj.responseText);
if (typeof(responseText.errors) != 'undefined') {
generateErrorWarning(responseText.errors);
}
else if (typeof(responseText.session) != 'undefined') {
// Prompt for user data and process the result using a callback:
var prompt = Ext.ComponentMgr.get('loginWindow');
if (prompt == null) {
prompt = new EnertechXchange.CMS.Login({});
}
prompt.show();
}
}
else if (typeof(obj.result) != 'undefined') {
if (typeof(obj.result.errors) != 'undefined') {
generateErrorWarning(obj.result.errors);
}
}
break;
default:
throw 'Type passed to displayErrors is not known.';
}
}
catch(e) {
generateErrorWarning(e);
}
}
/*
* Display the error to the user
*
*/
function generateErrorWarning(oError) {
if (false) {
var error = "";
if (typeof(oError) == 'string') {
error = oError;
}
if ((typeof(oError) === 'object') || (typeof(oError) === 'array')) {
for (var i in oError) {
error += oError[i] + "
";
}
}
else {
// handle other types
}
Ext.create('widget.uxNotification', {
title: 'Errors!',
position: 't',
autoCloseDelay: 3000,
iconCls: 'iconError',
html: error.substring(0, error.length-2)
}).show();
}
else {
Ext.create('widget.uxNotification', {
title: 'Errors!',
position: 't',
autoCloseDelay: 3000,
iconCls: 'iconError',
html: 'An error occurred while processing the requested task.'
}).show();
}
}
/**
* Function to compare passed value to minimum and maximum values and
* set it accordingly
*
* @param {integer} value
* @param {integer} minimum
* @param {integer} maximum
* @return {integer}
*/
function setCorrectDimesionValue(value, minimum, maximum) {
var result = 0;
if (value > minimum && value < maximum) {
result = value;
} else if (value <= minimum) {
result = minimum;
} else if (value >= maximum) {
result = maximum;
}
return result;
}
/**
* Function to popup image in a window
* @param integer windowId
* @param string imageUrl
* @param string imageName
*/
function openImageWindow(windowId, imageUrl, imageName, imageWidth, imageHeight) {
/**
* compare image dimensions with default width and height and configure window accordingly
*/
var windowItemWidth = setCorrectDimesionValue(imageWidth, 450, 1000);
var windowItemHeight = setCorrectDimesionValue(imageHeight, 450, 600);
/**
* If not exists -> create closable/expandable window popup with image item inside
* If exists -> show
*/
if (!Ext.getCmp(windowId)) {
win = new Ext.Window({
closable: true,
closeAction: 'close',
width: windowItemWidth,
height: windowItemHeight,
constrain: true,
id: windowId,
layout: 'fit',
maximizable: true,
maximized: false,
plain: true,
title: imageName,
items: [{
autoScroll: true,
height: windowItemHeight,
html: '
',
width: windowItemWidth
}]
});
win.show();
} else {
Ext.getCmp(windowId).show();
}
}
/**
* Function to popup a preview
* @param integer windowId
* @param string imageUrl
* @param string imageName
*/
function openPreviewWindow(windowId, content, contentTitle) {
/**
* If not exists -> create closable/expandable window popup with image item inside
* If exists -> show
*/
if (!Ext.getCmp(windowId)) {
win = new Ext.Window({
closable: true,
closeAction: 'close',
height: 620,
constrain: true,
id: windowId,
layout: 'fit',
maximizable: true,
maximized: false,
plain: true,
title: contentTitle,
width: 800,
items: [{
id: 'previewItem',
autoScroll: true,
height: 620,
html : content,
width : 800
}]
});
win.show();
} else {
/*var win = Ext.getCmp(windowId);
var windowItem = Ext.getCmp(windowId).items.item('previewItem').el.dom;
var wrapDiv = Ext.DomQuery.selectNode("div", el);
*/
/**
* check if html is different and set content to newly passed value
*/
/*
if (wrap.innerHTML != content) {
wrap.innerHTML = content;
}
Ext.getCmp(windowId).doLayout();*/
win.show();
}
}
/*
* hash table simulation for javascript
*/
function javascriptHash(){
this.length = 0;
this.items = new Array();
for (var i = 0; i < arguments.length; i += 2) {
if (typeof(arguments[i + 1]) != 'undefined') {
this.items[arguments[i]] = arguments[i + 1];
this.length++;
}
}
this.removeItem = function(in_key)
{
var tmp_previous;
if (typeof(this.items[in_key]) != 'undefined') {
this.length--;
var tmp_previous = this.items[in_key];
delete this.items[in_key];
}
return tmp_previous;
}
this.getItem = function(in_key) {
return this.items[in_key];
}
this.setItem = function(in_key, in_value)
{
var tmp_previous;
if (typeof(in_value) != 'undefined') {
if (typeof(this.items[in_key]) == 'undefined') {
this.length++;
}
else {
tmp_previous = this.items[in_key];
}
this.items[in_key] = in_value;
}
return tmp_previous;
}
this.hasItem = function(in_key)
{
return typeof(this.items[in_key]) != 'undefined';
}
}
/**
* Unflag selected record
* passing record id and grid id to refresh
* after response is received
*
* @param {integer} recordId
* @param {integer} gridId
*/
function unflagRecord(recordId, gridId) {
Ext.Msg.show({
scope: this,
title: 'Unflag record',
msg: 'Are you sure you want to unflag this record?',
buttons: Ext.Msg.YESNO,
icon: Ext.MessageBox.WARNING,
fn: function(response) {
if (response !== 'yes') {
return;
}
Ext.Ajax.request({
loadMask: true,
method: 'post',
scope: this,
url: 'ajax/flag_ajax.php',
waitMsg: 'Loading ...',
failure: function(form,action){
displayErrors(action, 'action');
},
params: {
'a': 'd',
'id': recordId
},
success: function(action, response) {
/**
* reload grid
*/
Ext.getCmp(gridId).store.reload();
}
});
}
});
}
/**
* Popup a form to enter reason for flagging a record
* @param {string} windowIdParam
* @param {string} table
* @param {string} windowTitle
* @param {integer} recordId
* @param {integer} gridId
*/
function showFlagPopup(windowIdParam, table, windowTitle, recordId, gridId) {
/**
* create popup
*/
var flagForm = new EnertechXchange.CMS.NotesPopupFormControl({
textareaLabel: 'Reason',
textareaName: 'reason_flagged',
textareaWidth: 247 + (247/2),
textareaHeight: 100,
clearBtnText: 'Clear',
clearBtnIconCls: 'iconReset',
saveBtnText: 'Save',
saveBtnIconCls: 'iconSave',
saveWithAjax: true,
textareaInvalidText: 'Please enter reason for flagging this record',
windowId: windowIdParam
});
if (!Ext.getCmp(windowIdParam)) {
/**
* create save button handler for save button of the popup
*/
flagForm.saveBtn.on('click', function(){
if (flagForm.textarea.getValue() != '') {
Ext.Ajax.request({
loadMask: true,
method: 'post',
scope: this,
url:'ajax/flag_ajax.php',
waitMsg: 'Loading ...',
failure: function(form,action){
displayErrors(action, 'action');
},
params: {
'a': 'save',
'id': recordId,
'rf': flagForm.textarea.getValue(),
'tn': table
},
success: function(action, response) {
/**
* reload grid here
*/
//Ext.Msg.alert('Success!', 'Record has been flagged successfully.');
Ext.getCmp(gridId).store.reload();
win.destroy();
}
});
} else { flagForm.textarea.markInvalid('Please enter reason for flagging this record');}
});
/**
* create popup window
*/
win = new Ext.Window({
closable: true,
closeAction: 'close',
height: 250,
constrain: true,
id: windowIdParam,
layout: 'fit',
maximizable: true,
maximized: false,
plain: true,
title: windowTitle,
width: 435,
items: [ flagForm ]
});
win.show();
} else {
win.show();
}
}
/**
* function to check if value exists in array
*/
function isValueInArray(myArray, myValue) {
var result = false;
var arrayLength = myArray.length;
for (var i = 0; i < arrayLength; i++) {
if (myArray[i] == myValue) {
result = true;
break;
} else { continue; }
}
return result;
}
/**
* Add new help content tab
* inside the Help Popup Tab Panel
* @param {string} passedUrl
* @param {integer} panelId
* @param {string} passedTitle
* @param {string} idLayout
* @param {string} passedIconCls
*/
function addHelp(passedUrl, panelId, passedTitle, idLayout, passedIconCls) {
var helpPanel = Ext.getCmp(panelId);
if (!Ext.getCmp(idLayout)) {
helpPanel.add(new Ext.Panel({
autoScroll: true,
iconCls: passedIconCls,
id: idLayout,
autoLoad: passedUrl,
border: false,
frame: false,
title: passedTitle,
closable: true
}));
helpPanel.doLayout();
helpPanel.setActiveTab(helpPanel.items.item(helpPanel.items.length-1));
} else {
Ext.getCmp(idLayout).show();
helpPanel.activate(helpPanel.getItem(idLayout));
}
}
/**
* Reload side panel help content
* @param {object} tab
*/
function reloadTabHelpContent(tab) {
if (tab.id != 'welcomeTab' && tab.id != 'mapPanel') {
/**
* check if active tab is the first tab -> if so, show grid help, otherwise show form help
*
* also check if content loaded is the import items form or reports form ->
* if so, do not need to reload help, it will be replaced in the activate event
* handler of both forms
*
*/
if (tab.items.item(0).getXType() != 'importItemsForm' && tab.items.item(0).getXType() != 'reportForm' && tab.items.item(0).getXType() != 'vumapReportForm' && tab.items.item(0).getXType() != 'vumapSubscriptions') {
if (tab.getActiveTab().id != tab.items.item(0).id) {
showHelp(tab.items.item(0).helpFormFile);
} else {
showHelp(tab.items.item(0).helpGridFile);
}
}
} else {
/**
* if only welcome tab is opened -> clear help panel
*/
if (tab.ownerCt.items.length == 1) {
var viewport = tab.ownerCt.ownerCt.ownerCt;
var helpPanel = viewport.items.item(1);
helpPanel.load({
url: 'help/Enertech_Xchange_User_Manual_October_2023.pdf'
});
}
}
}
/**
* Load file into side panel
* @param {string} passedUrl
*/
function showHelp(passedUrl) {
var helpPanel = mainPanel.items.item(0).items.item(1);
helpPanel.load({url: passedUrl});
}
/**
* Clear side help panel
*/
function clearHelpPanel() {
var helpPanel = mainPanel.items.item(0).items.item(1);
var helpText = helpPanel.items.item(0);
if (typeof(helpText) != 'undefined') {
helpPanel.remove(Ext.getCmp(helpText.getId()));
}
}
/**
* Show popup help
*/
function showHelpWindow() {
if (!Ext.getCmp('genericHelp')) {
win = new Ext.Window({
closable: true,
closeAction: 'close',
constrain: true,
id: 'genericHelp',
iconCls: 'iconHelp',
layout: 'fit',
maximizable: true,
maximized: false,
plain: true,
layout: 'fit',
title: 'Application Help',
width: '70%',
height: 500,
items: [ { html: '