/**
* TrimPath Junction. Release 1.0.19.
* Copyright (C) 2004, 2005 Metaha.
*
* TrimPath Junction is licensed under the GNU General Public License
* and the Apache License, Version 2.0, as follows:
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var TrimPath;
(function(safeEval) {
if (TrimPath == null)
TrimPath = new Object();
var MANY_ZEROS = "000000000000000000";
var junctionUtil = TrimPath.junctionUtil = {
upperFirst : function(str) {
return str.charAt(0).toUpperCase() + str.substring(1);
},
lowerFirst : function(str) {
return str.charAt(0).toLowerCase() + str.substring(1);
},
leftZeroPad : function(val, minLength) {
if (typeof(val) != "string")
val = String(val);
return (MANY_ZEROS.substring(0, minLength - val.length)) + val;
},
toSQLDateString : function(date) {
if (date == null)
date = new Date();
return junctionUtil.leftZeroPad(date.getUTCFullYear(), 4) + '/' +
junctionUtil.leftZeroPad(date.getUTCMonth() + 1, 2) + '/' +
junctionUtil.leftZeroPad(date.getUTCDate(), 2) + ' ' +
junctionUtil.leftZeroPad(date.getUTCHours(), 2) + ':' +
junctionUtil.leftZeroPad(date.getUTCMinutes(), 2) + ':' +
junctionUtil.leftZeroPad(date.getUTCSeconds(), 2) + ' UTC';
},
prepSQLParams : function(sqlParams) {
if (sqlParams != null) {
for (var i = 0; i < sqlParams.length; i++) {
if (sqlParams[i] instanceof Date)
sqlParams[i] = junctionUtil.toSQLDateString(sqlParams[i]);
}
}
return sqlParams;
},
setMapTreeValue : function(mapTree, path, value) { // Example path is 'order[customer][name]'.
var keys = path.replace(/\]/g, '').split('[');
for (var k = 0; k < keys.length; k++) {
var key = keys[k];
if (k < keys.length - 1) {
if (mapTree[key] == null)
mapTree[key] = {};
mapTree = mapTree[key];
} else {
mapTree[key] = value;
}
}
},
copyProps : function(src, dst) {
for (var k in src) {
if (typeof(src[k]) != 'function') {
dst[k] = src[k];
// By convention, fields with "Id" suffix are integers.
if (k.slice(-2) == "Id")
dst[k] = junctionUtil.nanToNull(parseInt(dst[k]));
}
}
return dst;
},
nanToNull : function(val) {
return isNaN(val) ? null : val;
},
findRecordIndex : function(records, id) { // Returns the index of the record with the given id.
if (records != null) {
for (var i = 0; i < records.length; i++) {
if (records[i].id == id)
return i;
}
}
return -1;
},
beforeFilter : function(funcFilter, scopeObj) { // The remaining varargs are action function names in the scope object.
var installBeforeFilter = function(actionName) {
var funcOrig = scopeObj[actionName];
scopeObj[actionName] = function(req, res) {
if (funcFilter(req, res) == false)
return;
return funcOrig(req, res);
}
}
for (var i = 2; i < arguments.length; i++)
installBeforeFilter(arguments[i]);
},
createMemoryDataProvider : function(data) {
var recordChanged = function(records, id, op) {
if (records.changes == null)
records.changes = {};
if (records.changes[id] == null)
records.changes[id] = [];
records.changes[id].push(op);
}
var memoryDataProvider = {
query : function(stmt) {
return stmt.filter(data); // See TrimQuery API.
},
save : function(tableName, obj) {
var records = data[tableName];
if (records == null)
records = data[tableName] = [];
var index = junctionUtil.findRecordIndex(records, obj.id);
if (index >= 0) {
junctionUtil.copyProps(obj, records[index]);
recordChanged(records, obj.id, "update");
return true;
}
if (obj.id == null || obj.id == 0) // We use a negative id value to denote client-only existence.
obj.id = memoryDataProvider.memoryLastId = memoryDataProvider.memoryLastId - 1;
records.push(junctionUtil.copyProps(obj, {}));
recordChanged(records, obj.id, "insert");
return true;
},
destroy : function(tableName, id) {
var records = data[tableName];
var index = junctionUtil.findRecordIndex(records, id);
if (index >= 0) {
records.splice(index, 1);
recordChanged(records, id, "delete");
return true;
}
return false;
},
// Attributes that part of memory data provider only...
memoryLastId : 0, // Shared by all tables and grows negative.
memoryData : data // Keyed by table name.
};
return memoryDataProvider;
},
toArray : function(obj, length) {
length = length || obj.length;
var result = [];
for (var i = 0; i < length; i++)
result.push(obj[i]);
return result;
},
// The following util functions only work in a DOM environment.
toggleDisplay : function(id, doc) {
var doc = doc || document;
var el = doc.getElementById(id);
if (el != null)
el.style.display = (el.style.display != "block" ? "block" : "none");
return el;
},
viewSource : function(viewSourceId, doc) {
var doc = doc || document;
var viewSourceId = viewSourceId || "junctionViewSource";
var viewSourceEl = junctionUtil.toggleDisplay(viewSourceId, doc);
if (viewSourceEl != null) {
if (viewSourceEl.style.display == "block") {
var srcs, result = "
";
srcs = junctionUtil.toArray(doc.getElementById("junction_js_yard").childNodes);
srcs = srcs.concat(junctionUtil.toArray(doc.getElementById("junction_jst_yard").childNodes));
for (var i = 0; i < srcs.length; i++) {
var src = srcs[i];
if (src.id != null && src.id.length > 0) {
result += '