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$
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2011-08-15 14:34:00 +02:00
|
|
|
function et2_arrayMgr(_data, _parentMgr)
|
2011-08-10 19:44:22 +02:00
|
|
|
{
|
|
|
|
if (typeof _parentMgr == "undefined")
|
|
|
|
{
|
|
|
|
_parentMgr = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the parent manager which is needed to access relative data when
|
|
|
|
// being in a relative perspective of the manager
|
|
|
|
this.parentMgr = _parentMgr;
|
|
|
|
|
|
|
|
// Hold a reference to the data
|
2011-08-12 17:26:08 +02:00
|
|
|
if (typeof _data == "undefined" || !_data)
|
|
|
|
{
|
|
|
|
et2_debug("error", "Invalid data passed to content array manager!");
|
|
|
|
_data = {};
|
|
|
|
}
|
|
|
|
|
2011-08-10 19:44:22 +02:00
|
|
|
this.data = _data;
|
2011-08-12 17:26:08 +02:00
|
|
|
|
|
|
|
// Holds information about the current perspective
|
|
|
|
this.perspectiveData = {
|
2011-08-12 18:29:24 +02:00
|
|
|
"owner": null,
|
2011-08-12 17:26:08 +02:00
|
|
|
"key": null,
|
|
|
|
"col": 0,
|
|
|
|
"row": 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the root content array manager object
|
|
|
|
*/
|
2011-08-15 14:34:00 +02:00
|
|
|
et2_arrayMgr.prototype.getRoot = function()
|
2011-08-12 17:26:08 +02:00
|
|
|
{
|
|
|
|
if (this.parentMgr != null)
|
|
|
|
{
|
|
|
|
return this.parentMgr.getRoot();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
2011-08-10 19:44:22 +02:00
|
|
|
}
|
|
|
|
|
2011-08-15 14:34:00 +02:00
|
|
|
et2_arrayMgr.prototype.getValueForID = function(_id)
|
2011-08-10 19:44:22 +02:00
|
|
|
{
|
|
|
|
if (typeof this.data[_id] != "undefined")
|
|
|
|
{
|
|
|
|
return this.data[_id];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2011-08-12 19:02:21 +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.
|
|
|
|
*/
|
2011-08-15 14:34:00 +02:00
|
|
|
et2_arrayMgr.prototype.getPath = function(_path)
|
2011-08-12 19:02:21 +02:00
|
|
|
{
|
|
|
|
if (typeof _path == "undefined")
|
|
|
|
{
|
|
|
|
_path = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.perspectiveData.key != null)
|
|
|
|
{
|
|
|
|
_path.push(this.perspectiveData.key);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.parentMgr != null)
|
|
|
|
{
|
|
|
|
this.parentMgr.getPath(_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _path;
|
|
|
|
}
|
|
|
|
|
2011-08-12 17:26:08 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2011-08-15 14:34:00 +02:00
|
|
|
et2_arrayMgr.prototype.getEntry = function(_key, _referenceInto,
|
2011-08-12 17:26:08 +02:00
|
|
|
_skipEmpty)
|
|
|
|
{
|
|
|
|
if (typeof _referenceInto == "undefined")
|
|
|
|
{
|
|
|
|
_referenceInto = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof _skipEmpty == "undefined")
|
|
|
|
{
|
|
|
|
_skipEmpty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the given key by removing the "]"-chars and splitting at "["
|
|
|
|
var indexes = _key.replace(/]/g,'').split('[');
|
|
|
|
|
|
|
|
var entry = this.data;
|
|
|
|
for (var i = 0; i < indexes.length; i++)
|
|
|
|
{
|
|
|
|
// 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 false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether the entry actually exists
|
|
|
|
var idx = indexes[i];
|
|
|
|
if (_skipEmpty && (!isObject || typeof entry[idx] == "undefined"))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry = entry[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Equivaltent to the boetemplate::expand_name function.
|
|
|
|
*
|
|
|
|
* Expands variables inside the given identifier to their values inside the
|
|
|
|
* content array.
|
|
|
|
*/
|
2011-08-15 14:34:00 +02:00
|
|
|
et2_arrayMgr.prototype.expandName = function(_ident)
|
2011-08-12 17:26:08 +02:00
|
|
|
{
|
|
|
|
// 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('$');
|
|
|
|
if (pos_var >= 0)
|
|
|
|
{
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
2011-08-12 18:29:24 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
_ident = this.getEntry(_ident.substr(1));
|
|
|
|
}
|
2011-08-12 17:26:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return _ident;
|
|
|
|
}
|
|
|
|
|
2011-08-15 14:34:00 +02:00
|
|
|
et2_arrayMgr.prototype.parseBoolExpression = function(_expression)
|
2011-08-10 19:44:22 +02:00
|
|
|
{
|
2011-08-12 17:26:08 +02:00
|
|
|
// If the first char of the expression is a '!' this means, that the value
|
|
|
|
// is to be negated.
|
|
|
|
if (_expression.charAt(0) == '!')
|
|
|
|
{
|
|
|
|
return !this.parseBoolExpression(_expression.substr(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
{
|
|
|
|
// 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)))
|
|
|
|
.match(val) ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise check for simple equality
|
|
|
|
return val == checkVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
return val != '' && (typeof val != "string" || val.toLowerCase() != "false");
|
|
|
|
}
|
|
|
|
|
2011-08-15 14:34:00 +02:00
|
|
|
et2_arrayMgr.prototype.openPerspective = function(_owner, _root, _col, _row)
|
2011-08-12 17:26:08 +02:00
|
|
|
{
|
|
|
|
// Get the root node
|
|
|
|
var root = typeof _root == "string" ? this.data[_root] :
|
|
|
|
(_root == null ? this.data : _root);
|
|
|
|
|
|
|
|
// Create a new content array manager with the given root
|
2011-08-15 14:34:00 +02:00
|
|
|
var mgr = new et2_arrayMgr(root, this);
|
2011-08-12 17:26:08 +02:00
|
|
|
|
2011-08-12 18:29:24 +02:00
|
|
|
// Set the owner
|
|
|
|
mgr.perspectiveData.owner = _owner;
|
|
|
|
|
2011-08-12 17:26:08 +02:00
|
|
|
// Set the root key
|
|
|
|
if (typeof _root == "string")
|
|
|
|
{
|
2011-08-12 19:02:21 +02:00
|
|
|
mgr.perspectiveData.key = _root;
|
2011-08-12 17:26:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the _col and _row parameter
|
|
|
|
if (typeof _col != "undefined" && typeof _row != "undefined")
|
|
|
|
{
|
|
|
|
mgr.perspectiveData.col = _col;
|
|
|
|
mgr.perspectiveData.row = _row;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mgr;
|
2011-08-10 19:44:22 +02:00
|
|
|
}
|
|
|
|
|