2011-08-10 19:44:22 +02:00
|
|
|
/**
|
|
|
|
* eGroupWare eTemplate2 - JS content array manager
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage api
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Andreas Stöckel
|
|
|
|
* @copyright Stylite 2011
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
2011-08-24 12:18:07 +02:00
|
|
|
"use strict";
|
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
/*egw:uses
|
2011-09-07 18:59:23 +02:00
|
|
|
et2_core_common;
|
2011-08-24 12:18:07 +02:00
|
|
|
et2_core_inheritance;
|
2011-09-07 18:59:23 +02:00
|
|
|
et2_core_phpExpressionCompiler;
|
2011-08-16 14:31:18 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
var et2_arrayMgr = Class.extend({
|
2011-08-10 19:44:22 +02:00
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
splitIds: true,
|
2011-08-10 19:44:22 +02:00
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
init: function(_data, _parentMgr) {
|
|
|
|
if (typeof _parentMgr == "undefined")
|
|
|
|
{
|
|
|
|
_parentMgr = null;
|
|
|
|
}
|
2011-08-12 17:26:08 +02:00
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
// Copy the parent manager which is needed to access relative data when
|
|
|
|
// being in a relative perspective of the manager
|
|
|
|
this.parentMgr = _parentMgr;
|
2011-08-12 17:26:08 +02:00
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
// Hold a reference to the data
|
|
|
|
if (typeof _data == "undefined" || !_data)
|
|
|
|
{
|
|
|
|
et2_debug("error", "Invalid data passed to content array manager!");
|
|
|
|
_data = {};
|
|
|
|
}
|
2011-08-12 17:26:08 +02:00
|
|
|
|
2011-10-07 01:08:15 +02:00
|
|
|
// Expand sub-arrays that have been shmushed together, so further perspectives work
|
|
|
|
if (this.splitIds)
|
|
|
|
{
|
|
|
|
for(var key in _data) {
|
|
|
|
var indexes = key.replace(/]/g,'').split('[');
|
|
|
|
if(indexes.length > 1)
|
|
|
|
{
|
|
|
|
var value = _data[key];
|
|
|
|
var target = _data;
|
|
|
|
for(var i = 0; i < indexes.length; i++) {
|
|
|
|
if(typeof target[indexes[i]] == "undefined") {
|
|
|
|
target[indexes[i]] = i == indexes.length-1 ? value : {};
|
|
|
|
}
|
|
|
|
target = target[indexes[i]];
|
|
|
|
}
|
|
|
|
delete _data[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
this.data = _data;
|
2011-08-12 17:26:08 +02:00
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
// Holds information about the current perspective
|
|
|
|
this.perspectiveData = {
|
|
|
|
"owner": null,
|
|
|
|
"key": null,
|
2011-09-06 18:50:38 +02:00
|
|
|
"row": null
|
2011-08-16 14:31:18 +02:00
|
|
|
}
|
|
|
|
},
|
2011-08-10 19:44:22 +02:00
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
/**
|
|
|
|
* Returns the root content array manager object
|
|
|
|
*/
|
|
|
|
getRoot : function() {
|
|
|
|
if (this.parentMgr != null)
|
|
|
|
{
|
|
|
|
return this.parentMgr.getRoot();
|
|
|
|
}
|
2011-08-10 19:44:22 +02:00
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
return this;
|
|
|
|
},
|
2011-08-10 19:44:22 +02:00
|
|
|
|
2011-09-08 20:36:09 +02:00
|
|
|
/* getValueForID : function(_id) {
|
2011-08-16 14:31:18 +02:00
|
|
|
if (typeof this.data[_id] != "undefined")
|
|
|
|
{
|
|
|
|
return this.data[_id];
|
|
|
|
}
|
2011-08-12 19:02:21 +02:00
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
return null;
|
2011-09-08 20:36:09 +02:00
|
|
|
},*/
|
2011-08-16 14:31:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the path to this content array manager perspective as an array
|
|
|
|
* containing the key values
|
|
|
|
*
|
|
|
|
* @param _path is used internally, do not supply it manually.
|
|
|
|
*/
|
|
|
|
getPath : function(_path) {
|
|
|
|
if (typeof _path == "undefined")
|
|
|
|
{
|
|
|
|
_path = [];
|
|
|
|
}
|
2011-08-12 19:02:21 +02:00
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
if (this.perspectiveData.key != null)
|
|
|
|
{
|
|
|
|
_path.push(this.perspectiveData.key);
|
|
|
|
}
|
2011-08-12 19:02:21 +02:00
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
if (this.parentMgr != null)
|
|
|
|
{
|
|
|
|
this.parentMgr.getPath(_path);
|
|
|
|
}
|
2011-08-12 19:02:21 +02:00
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
return _path;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get array entry is the equivalent to the boetemplate get_array function.
|
|
|
|
* It returns a reference to the (sub) array with the given key. This also works
|
|
|
|
* for keys using the ETemplate referencing scheme like a[b][c]
|
|
|
|
*
|
|
|
|
* @param _key is the string index, may contain sub-indices like a[b]
|
|
|
|
* @param _referenceInto if true none-existing sub-arrays/-indices get created
|
|
|
|
* to be returned as reference, else false is returned. Defaults to false
|
|
|
|
* @param _skipEmpty returns false if _key is not present in this content array.
|
|
|
|
* Defaults to false.
|
|
|
|
*/
|
|
|
|
getEntry : function(_key, _referenceInto, _skipEmpty) {
|
|
|
|
if (typeof _referenceInto == "undefined")
|
|
|
|
{
|
|
|
|
_referenceInto = false;
|
|
|
|
}
|
2011-08-12 17:26:08 +02:00
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
if (typeof _skipEmpty == "undefined")
|
|
|
|
{
|
|
|
|
_skipEmpty = false;
|
|
|
|
}
|
2011-08-12 17:26:08 +02:00
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
// Parse the given key by removing the "]"-chars and splitting at "["
|
|
|
|
var indexes = [_key];
|
2011-08-12 17:26:08 +02:00
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
if (this.splitIds)
|
2011-08-12 17:26:08 +02:00
|
|
|
{
|
2011-08-16 14:31:18 +02:00
|
|
|
indexes = _key.replace(/]/g,'').split('[');
|
2011-08-12 17:26:08 +02:00
|
|
|
}
|
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
var entry = this.data;
|
|
|
|
for (var i = 0; i < indexes.length; i++)
|
2011-08-12 17:26:08 +02:00
|
|
|
{
|
2011-08-16 14:31:18 +02:00
|
|
|
// Abort if the current entry is not an object (associative array) and
|
|
|
|
// we should descend further into it.
|
|
|
|
var isObject = entry instanceof Object;
|
|
|
|
if (!isObject && !_referenceInto)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether the entry actually exists
|
|
|
|
var idx = indexes[i];
|
|
|
|
if (_skipEmpty && (!isObject || typeof entry[idx] == "undefined"))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry = entry[idx];
|
2011-08-12 17:26:08 +02:00
|
|
|
}
|
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
return entry;
|
|
|
|
},
|
|
|
|
|
2011-09-07 18:59:23 +02:00
|
|
|
compiledExpressions: {},
|
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
/**
|
|
|
|
* Equivaltent to the boetemplate::expand_name function.
|
|
|
|
*
|
|
|
|
* Expands variables inside the given identifier to their values inside the
|
|
|
|
* content array.
|
|
|
|
*/
|
|
|
|
expandName : function(_ident) {
|
|
|
|
// Check whether the identifier refers to an index in the content array
|
|
|
|
var is_index_in_content = _ident.charAt(0) == '@';
|
|
|
|
|
|
|
|
// Check whether "$" occurs in the given identifier
|
|
|
|
var pos_var = _ident.indexOf('$');
|
2011-09-07 18:59:23 +02:00
|
|
|
if (pos_var >= 0 && this.perspectiveData.row != null)
|
2011-08-16 14:31:18 +02:00
|
|
|
{
|
2011-09-07 18:59:23 +02:00
|
|
|
// Get the content array for the current row
|
|
|
|
var row = this.perspectiveData.row;
|
|
|
|
var cont = this.data;
|
|
|
|
var row_cont = cont[row];
|
|
|
|
|
|
|
|
// Check whether the expression has already been compiled - if not,
|
|
|
|
// try to compile it first. If an error occurs, the identifier
|
|
|
|
// function is set to null
|
|
|
|
var proto = this.constructor.prototype;
|
|
|
|
if (typeof proto.compiledExpressions[_ident] == "undefined")
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
proto.compiledExpressions[_ident] = et2_compilePHPExpression(
|
|
|
|
_ident, ["row", "cont", "row_cont"]);
|
|
|
|
}
|
|
|
|
catch(e)
|
|
|
|
{
|
|
|
|
proto.compiledExpressions[_ident] = null;
|
|
|
|
et2_debug("error", "Error while compiling PHP->JS ", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute the previously compiled expression, if it is not "null"
|
|
|
|
// because compilation failed. The parameters have to be in the same
|
|
|
|
// order as defined during compilation.
|
|
|
|
if (proto.compiledExpressions[_ident])
|
2011-09-06 18:50:38 +02:00
|
|
|
{
|
2011-09-07 18:59:23 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
_ident = proto.compiledExpressions[_ident](row, cont, row_cont);
|
|
|
|
}
|
|
|
|
catch(e)
|
|
|
|
{
|
|
|
|
et2_debug("error", e);
|
|
|
|
}
|
2011-09-06 18:50:38 +02:00
|
|
|
}
|
2011-08-16 14:31:18 +02:00
|
|
|
}
|
2011-08-12 17:26:08 +02:00
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
if (is_index_in_content)
|
|
|
|
{
|
|
|
|
// If an additional "@" is specified, this means that we have to return
|
|
|
|
// the entry from the root element
|
|
|
|
if (_ident.charAt(1) == '@')
|
|
|
|
{
|
|
|
|
_ident = this.getRoot().getEntry(_ident.substr(2));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_ident = this.getEntry(_ident.substr(1));
|
|
|
|
}
|
|
|
|
}
|
2011-08-12 17:26:08 +02:00
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
return _ident;
|
|
|
|
},
|
2011-08-12 17:26:08 +02:00
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
parseBoolExpression: function(_expression) {
|
|
|
|
// If the first char of the expression is a '!' this means, that the value
|
|
|
|
// is to be negated.
|
|
|
|
if (_expression.charAt(0) == '!')
|
2011-08-12 17:26:08 +02:00
|
|
|
{
|
2011-08-16 14:31:18 +02:00
|
|
|
return !this.parseBoolExpression(_expression.substr(1));
|
2011-08-12 17:26:08 +02:00
|
|
|
}
|
2011-08-16 14:31:18 +02:00
|
|
|
|
|
|
|
// Split the expression at a possible "="
|
|
|
|
var parts = _expression.split('=');
|
|
|
|
|
|
|
|
// Expand the first value
|
|
|
|
var val = this.expandName(parts[0]);
|
|
|
|
|
|
|
|
// If a second expression existed, test that one
|
|
|
|
if (typeof parts[1] != "undefined")
|
2011-08-12 18:29:24 +02:00
|
|
|
{
|
2011-08-16 14:31:18 +02:00
|
|
|
// Expand the second value
|
|
|
|
var checkVal = this.expandName(parts[1]);
|
|
|
|
|
|
|
|
// Values starting with / are treated as regular expression. It is
|
|
|
|
// checked whether the first value matches the regular expression
|
|
|
|
if (checkVal.charAt(0) == '/')
|
|
|
|
{
|
|
|
|
return (new RegExp(checkVal.substr(1, checkVal.length - 2)))
|
2011-08-30 23:36:55 +02:00
|
|
|
.test(val) ? true : false;
|
2011-08-16 14:31:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise check for simple equality
|
|
|
|
return val == checkVal;
|
2011-08-12 18:29:24 +02:00
|
|
|
}
|
2011-08-12 17:26:08 +02:00
|
|
|
|
2011-08-22 17:58:47 +02:00
|
|
|
return et2_evalBool(val);
|
2011-08-16 14:31:18 +02:00
|
|
|
},
|
2011-08-12 17:26:08 +02:00
|
|
|
|
2011-09-05 16:35:28 +02:00
|
|
|
openPerspective: function(_owner, _root, _row)
|
2011-08-12 17:26:08 +02:00
|
|
|
{
|
2011-08-16 14:31:18 +02:00
|
|
|
// Get the root node
|
|
|
|
var root = typeof _root == "string" ? this.data[_root] :
|
|
|
|
(_root == null ? this.data : _root);
|
2011-08-12 17:26:08 +02:00
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
// Create a new content array manager with the given root
|
|
|
|
var mgr = new et2_arrayMgr(root, this);
|
2011-08-12 17:26:08 +02:00
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
// Set the owner
|
|
|
|
mgr.perspectiveData.owner = _owner;
|
2011-08-12 17:26:08 +02:00
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
// Set the root key
|
|
|
|
if (typeof _root == "string")
|
|
|
|
{
|
|
|
|
mgr.perspectiveData.key = _root;
|
|
|
|
}
|
2011-08-12 17:26:08 +02:00
|
|
|
|
2011-09-05 16:35:28 +02:00
|
|
|
// Set _row parameter
|
|
|
|
if (typeof _row != "undefined")
|
2011-08-12 17:26:08 +02:00
|
|
|
{
|
2011-08-16 14:31:18 +02:00
|
|
|
mgr.perspectiveData.row = _row;
|
2011-08-12 17:26:08 +02:00
|
|
|
}
|
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
return mgr;
|
2011-08-12 17:26:08 +02:00
|
|
|
}
|
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
});
|
2011-08-12 17:26:08 +02:00
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
var et2_readonlysArrayMgr = et2_arrayMgr.extend({
|
2011-08-12 17:26:08 +02:00
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
splitIds: false,
|
2011-08-12 17:26:08 +02:00
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
isReadOnly: function(_id, _attr, _parent) {
|
|
|
|
var entry = null;
|
2011-08-12 18:29:24 +02:00
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
if (_id != null)
|
|
|
|
{
|
|
|
|
entry = this.getEntry(_id);
|
|
|
|
}
|
2011-08-12 17:26:08 +02:00
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
// Let the array entry override the read only attribute entry
|
|
|
|
if (typeof entry != "undefined" && !(entry instanceof Object))
|
|
|
|
{
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the attribute is set, return that
|
|
|
|
if (typeof _attr != "undefined" && _attr !== null)
|
|
|
|
{
|
|
|
|
return et2_evalBool(_attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise take into accounf whether the parent is readonly
|
|
|
|
if (typeof _parent != "undefined" && _parent)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise return the default value
|
|
|
|
return (typeof this.getEntry("__ALL__") != "undefined");
|
2011-08-12 17:26:08 +02:00
|
|
|
}
|
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
});
|
|
|
|
|
2011-09-05 16:35:28 +02:00
|
|
|
/**
|
2011-09-07 18:59:23 +02:00
|
|
|
* Creates a new set of array managers
|
|
|
|
*
|
|
|
|
* @param _owner is the owner object of the array managers - this object (a widget)
|
|
|
|
* will free the array manager
|
|
|
|
* @param _mgrs is the original set of array managers, the array managers are
|
|
|
|
* inside an associative array as recived from et2_widget::getArrayMgrs()
|
|
|
|
* @param _data is an associative array of new data which will be merged into the
|
|
|
|
* existing array managers.
|
|
|
|
* @param _row is the row for which the array managers will be opened.
|
2011-09-05 16:35:28 +02:00
|
|
|
*/
|
|
|
|
function et2_arrayMgrs_expand(_owner, _mgrs, _data, _row)
|
|
|
|
{
|
|
|
|
// Create a copy of the given _mgrs associative array
|
|
|
|
var result = {};
|
|
|
|
|
|
|
|
// Merge the given data associative array into the existing array managers
|
2011-09-07 18:59:23 +02:00
|
|
|
for (var key in _mgrs)
|
2011-09-05 16:35:28 +02:00
|
|
|
{
|
2011-09-07 18:59:23 +02:00
|
|
|
result[key] = _mgrs[key];
|
|
|
|
|
|
|
|
if (typeof _data[key] != "undefined")
|
2011-09-05 16:35:28 +02:00
|
|
|
{
|
|
|
|
// Open a perspective for the given data row
|
2011-09-06 18:50:38 +02:00
|
|
|
var rowData = {};
|
|
|
|
rowData[_row] = _data[key];
|
|
|
|
|
2011-09-07 18:59:23 +02:00
|
|
|
result[key] = _mgrs[key].openPerspective(_owner, rowData, _row);
|
2011-09-05 16:35:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the resulting managers object
|
|
|
|
return result;
|
|
|
|
}
|
2011-08-10 19:44:22 +02:00
|
|
|
|