var Utility = {};
//-----------------------------------------------------------------------
// client
//-----------------------------------------------------------------------
Utility.navigate = function(url, args, wnd)
{
(wnd || window).navigate(Utility.getUrl(url, args));
}
Utility.openTab = function(url, args, title, icon, hint, id)
{
Esc.getTopWindow().MainFrame.tabStrip.add(title, Utility.getUrl(url, args), icon, hint, id);
}
Utility.closeTab = function()
{
Esc.getTopWindow().MainFrame.tabStrip.closeCurrentTab();
}
Utility.isInTab = function()
{
var top = Esc.getTopWindow();
return top != window && top.MainFrame != null && top.MainFrame.tabStrip != null;
}
Utility.openWindow = function(url, args, name, features)
{
window.open(Utility.getUrl(url, args), name, features || "");
}
Utility.close = function()
{
if (Utility.isInDialog()) {
Utility.closeDialog();
}
else if (Utility.isInTab()) {
Utility.closeTab();
}
else {
window.close();
}
}
Utility.openDialog = function(url, args, style, callback, dialogArgs)
{
Esc.openDialog(Utility.getUrl(url, args), style, callback, dialogArgs);
}
Utility.closeDialog = function(result)
{
var dialog = Esc.getCurrentDialog();
if (typeof(result) != "undefined") {
dialog == null ? (window.returnValue = result) : (dialog.result = result);
}
dialog == null ? window.close() : dialog.hide();
}
Utility.setDialogSize = function(width, height)
{
var dialog = Esc.getCurrentDialog();
dialog.setSize(width, height);
}
Utility.getDialogResult = function()
{
var dialog = Esc.getCurrentDialog();
return dialog == null ? window.returnValue : dialog.result;
}
Utility.setDialogResult = function(result)
{
var dialog = Esc.getCurrentDialog();
dialog == null ? (window.returnValue = result) : (dialog.result = result);
}
Utility.setDialogResultAttribute = function(key, value)
{
var dialog = Esc.getCurrentDialog();
var result = (dialog == null ? window.returnValue : dialog.result) || {};
result[key] = value;
dialog == null ? (window.returnValue = result) : (dialog.result = result);
}
Utility.getDialogArguments = function()
{
var dialog = Esc.getCurrentDialog();
return dialog == null ? undefined : dialog.arguments;
}
Utility.isInDialog = function()
{
return window.dialogWidth != null || Esc.getCurrentDialog() != null;
}
Utility.isDialogTop = function()
{
if (window == window.top && window.dialogWidth != null) {
return true;
}
dialog = Esc.getCurrentDialog();
return dialog != null && window == dialog.getContentWindow();
}
Utility.download = function(url, args)
{
if (!/\.jdn$/.test(url)) {
url += ".jdn";
}
url = Utility.setUrlParam(url, "pageCode", clientData.pageCode);
url = Utility.setUrlParam(url, "rand", Math.random());
Utility.openWindow(url, args);
}
Utility.getUrl = function(url, args)
{
if (args == null) {
return url;
}
var result = Utility.setUrlParam(url, "args", JSON.stringify(args));
if (args != null && result.length > 1000) {
var key = Utility.invoke("Ecp.Misc.putUrlArguments", args).key;
result = Utility.setUrlParam(url, "argsKey", key);
}
return result;
}
Utility.showError = function(error)
{
var url = error.page == null ? "Ecp.ActionError.jdp" : error.page.url;
var width = error.page == null ? "400" : error.page.dialogWidth;
var height = error.page == null ? "125" : error.page.dialogHeight;
/*
var features = "dialogWidth:" + width + "px;dialogHeight:" + height + "px;status:no;resizable:yes;help:no";
var args = {
message : error.message,
stackTrace : error.stackTrace,
data : error.page && error.page.data
};
Utility._openRealDialog(url, args, features);
*/
var style = "width:" + width + "px;height:" + height + "px";
var args = {
message : error.message,
stackTrace : error.stackTrace,
data : error.page && error.page.data
};
Utility.openDialog(url, args, style);
}
Utility.prompt = function(title, information, defaultValue, allowEmpty, callback)
{
var args = {information:information};
if (!Esc.isEmpty(title)) {
args.title = title;
}
if (!Esc.isEmpty(defaultValue)) {
args.defaultValue = defaultValue;
}
if (allowEmpty) {
args.allowEmpty = true;
}
Utility.openDialog("Ecp.Misc.Prompt.jdp", args, "width:350px;height:150px", callback);
}
Utility.isUuid = function(s)
{
return /^\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$/.test(s);
}
Utility.formatText = function(code)
{
var text = Esc.i18n(code);
for (var i = 1; i < arguments.length; ++i) {
text = text.replace("${" + (i - 1) + "}", arguments[i]);
}
return text;
}
//-----------------------------------------------------------------------
// AJAX
//-----------------------------------------------------------------------
Utility.invoke = function(url, args, async, callback, options)
{
var ajax = new Utility.Ajax(url, async);
ajax.progressBarArgs = options && options.progressBar;
ajax.onsuccess = callback;
if (options && options.ignoreError) {
ajax.onerror = null;
}
return ajax.submit(args);
}
Utility.Ajax = function(url, async)
{
this._url = /\.jdt/.test(url) ? url : url + ".jdt";
this._async = async == true;
this._text = null;
this._json = null;
this._success = true;
this.onerror = this.showError;
this.onsuccess = null;
}
Utility.Ajax.prototype.setUrl = function(url)
{
this._url = url;
}
Utility.Ajax.prototype.setAsync = function(async)
{
this._async = async != false;
}
Utility.Ajax.prototype.success = function()
{
return this._success;
}
Utility.Ajax.prototype.getResult = function()
{
return this._json;
}
Utility.Ajax.prototype.getErrorMessage = function()
{
if (this._success || this._json == null || typeof(this._json) != "object") {
return null;
}
return this._json.message;
}
Utility.Ajax.prototype.showError = function()
{
if (Esc.isEmpty(this._text)) {
alert(Utility.serverNotAccessableMessage);
}
else if (this._json == null) {
alert("Invalid JSON format:\n\n" + this._text);
}
else {
Utility.showError(this._json);
}
}
Utility.Ajax.prototype.submit = function(data)
{
var http = new ActiveXObject("Microsoft.XMLHTTP");
var json = {
args: (data == null ? {} : data),
pageCode: (typeof(clientData) == "undefined" ? "" : clientData.pageCode)
};
http.open("POST", this._url, this._async);
if (this._async) {
var ajax = this;
http.onreadystatechange = function() {
if (http.readyState == 4) {
if (ajax._progressBar != null) {
ajax._progressBar.hide();
ajax._progressBar = null;
}
if (http.status != 200) {
if (this.onerror != null) {
alert(Utility.serverNotAccessableMessage);
}
}
else {
ajax._parse(http.responseText);
}
}
};
var pbArgs = this.progressBarArgs;
if (pbArgs != null && pbArgs.text != null) {
this._progressBar = Esc.showProgressBar(pbArgs.text, pbArgs.target, pbArgs.showMask);
}
http.send(JSON.stringify(json));
}
else {
try {
http.send(JSON.stringify(json));
}
catch (e) {
if (this.onerror != null) {
alert(Utility.serverNotAccessableMessage);
}
return undefined;
}
if (http.status != 200) {
if (this.onerror != null) {
alert(Utility.serverNotAccessableMessage);
}
return undefined;
}
this._parse(http.responseText);
return this._success ? this._json : undefined;
}
}
Utility.Ajax.prototype._parse = function(text)
{
try {
this._text = text;
this._json = JSON.parse(text);
this._success = !Esc.isObject(this._json) || !this._json["<isError>"];
}
catch (e) {
this._success = false;
}
if (!this._success && this.onerror != null) {
this.onerror(this._json);
}
if (this._success && this.onsuccess != null) {
this._json = this.onsuccess(this._json);
}
}
//-----------------------------------------------------------------------
// UrlBuilder
//-----------------------------------------------------------------------
Utility.getUrlParam = function(url, key)
{
var url = url || window.location.href;
var reg = new RegExp("(\\?|&)" + key + "=([^&]*)");
return reg.test(url) ? RegExp.$2 : null;
}
Utility.setUrlParam = function(url, key, value)
{
var reg = new RegExp("(\\?|&)" + key + "=[^&]*");
if (reg.test(url))
{
url = url.replace(reg, "$1" + key + "=" + encodeURIComponent(value));
}
else
{
url += (url.indexOf("?") == -1 ? "?" : "&") + key + "=" + encodeURIComponent(value);
}
return url;
}
Utility.removeUrlParam = function(url, key, ignoreCase)
{
var flag = ignoreCase ? "" : "i";
var reg1 = new RegExp("\\?" + key + "=[^&]*$", flag);
var reg2 = new RegExp("\\?" + key + "=[^&]*&", flag);
var reg3 = new RegExp("&" + key + "=[^&]*", flag);
return url.replace(reg1, "").replace(reg2, "?").replace(reg3, "");
}
Utility.UrlBuilder = function(url)
{
this.url = url;
}
Utility.UrlBuilder.prototype.set = function(key, value)
{
this.url = Utility.setUrlParam(this.url, key, value);
return this;
}
Utility.UrlBuilder.prototype.remove = function(key)
{
this.url = Utility.removeUrlParam(this.url, key);
return this;
}
Utility.UrlBuilder.prototype.toString = function()
{
return this.url;
}
//-----------------------------------------------------------------------
// cache
//-----------------------------------------------------------------------
Utility.ClientCache = function(){};
Utility.ClientCache.prototype.getCacheItem = function(key)
{
var keyParts = key.split(".");
var hash = this._getCacheItemHash(keyParts);
return hash && hash[keyParts[keyParts.length - 1] + "__"];
}
Utility.ClientCache.prototype.setCacheItem = function(key, value)
{
var keyParts = key.split(".");
var hash = this._getCacheItemHash(keyParts, true);
hash[keyParts[keyParts.length - 1] + "__"] = value;
}
Utility.ClientCache.prototype.deleteCacheItem = function(key)
{
var keyParts = key.split(".");
var hash = this._getCacheItemHash(keyParts);
if (hash != null) {
delete hash[keyParts[keyParts.length - 1] + "__"];
}
}
Utility.ClientCache.prototype.getCacheItems = function(keys, prefix)
{
var ret = [];
for (var i = 0; i < keys.length; ++i) {
var key = prefix == null ? keys[i] : prefix + keys[i];
ret.push(this.getCacheItem(key));
}
return ret;
}
Utility.ClientCache.prototype.setCacheItems = function(keys, values, prefix)
{
for (var i = 0; i < keys.length; ++i) {
var key = prefix == null ? keys[i] : prefix + keys[i];
this.setCacheItem(key, values[i]);
}
}
Utility.ClientCache.prototype.clearCache = function()
{
Esc.getTopWindow()._cache = {};
}
Utility.ClientCache.prototype._getCacheItemHash = function(keyParts, createIfNotExists)
{
var hash = Esc.getTopWindow()._cache;
if (hash == null) {
hash = {};
Esc.getTopWindow()._cache = hash;
}
for (var i = 0; i < keyParts.length - 1; ++i) {
var keyPart = keyParts[i];
if (!(keyPart in hash)) {
if (createIfNotExists) {
hash[keyPart] = {};
}
else {
return null;
}
}
hash = hash[keyPart];
}
return hash;
}
//-----------------------------------------------------------------------
// i18n
//-----------------------------------------------------------------------
Utility.I18nLoader = function() {};
Utility.I18nLoader.prototype.load = function(keys)
{
return Utility.invoke("Ecp.I18n.getText", {keys:keys}, false, function(ret) {
if (ret.text != null) {
for (var i = 0; i < ret.text.length; ++i) {
if (ret.text[i] != null) {
ret.text[i] = ret.text[i].replace(/\\n/g, "\n");
}
}
}
return ret.text;
});
}
//-----------------------------------------------------------------------
// Other
//-----------------------------------------------------------------------
Utility.log = function(level, message)
{
Utility.invoke("Ecp.Misc.writeLog", {message:message, level:level}, true, null, {ignoreError:true});
}
Utility.randomUuid = function()
{
var dg = new Date(1582, 10, 15, 0, 0, 0, 0);
var dc = new Date();
var t = dc.getTime() - dg.getTime();
var tl = Utility._getUuidBits(t, 0, 31);
var tm = Utility._getUuidBits(t, 32, 47);
var thv = Utility._getUuidBits(t, 48, 59) + '1';
var csar = Utility._getUuidBits(Utility.random(0x01000), 0, 7);
var csl = Utility._getUuidBits(Utility.random(0x01000), 0, 7);
var n = Utility._getUuidBits(Utility.random(0x10000), 0, 15)
+ Utility._getUuidBits(Utility.random(0x10000), 0, 15)
+ Utility._getUuidBits(Utility.random(0x10000), 0, 15);
return [tl, tm, thv, csar + csl, n].join("-");
}
Utility.random = function(upperBound)
{
return Math.floor(Math.random() * upperBound);
}
Utility._getUuidBits = function(value, start, end)
{
var hex = value.toString(16).toLowerCase() + "0000000000000000";
return hex.substring(start >> 2, (end >> 2) + 1);
}
//-----------------------------------------------------------------------
// private
//-----------------------------------------------------------------------
Utility._openRealDialog = function(url, args, features)
{
if (Esc.IEVERSION <= 6 && /dialogHeight:(\d+)/.test(features))
{
var temp = "dialogHeight:" + (parseInt(RegExp.$1) + 52);
features = features.replace(/dialogHeight:(\d+)/, temp);
}
return window.showModalDialog(url, args, features);
}
Utility._init = function()
{
Esc.setCacheManager(new Utility.ClientCache());
Esc.setI18nLoader(new Utility.I18nLoader());
document.attachEvent("onclick", function(){
if (event.ctrlKey && clientData && clientData.isDevelopMode) {
if (window.messagePopup == null) {
window.messagePopup = window.createPopup();
var body = window.messagePopup.document.body;
body.innerHTML = "<div style='width:100%;height:100%;border:1px solid black;overflow:hidden'>"
+ "<textarea style='border:none;width:100%;height:100%;background-color:#FFFFDD'></textarea>"
+ "</div>";
}
var popup = window.messagePopup;
var textarea = popup.document.body.firstChild.firstChild;
var width = window.screen.availWidth - 100;
var height = Math.round((window.screen.availHeight - 100) * 0.8);
textarea.value = window.location.href + "\n\n" + event.srcElement.outerHTML;
popup.show(50, 50, width, height);
}
});
Utility.serverNotAccessableMessage = Esc.i18n("Public.ServerNotAccessable");
}
Utility._init();