2011-08-04 21:08:50 +02:00
|
|
|
/**
|
2013-04-13 21:00:13 +02:00
|
|
|
* EGroupware eTemplate2 - JS Widget base class
|
2011-08-04 21:08:50 +02:00
|
|
|
*
|
|
|
|
* @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-10 16:36:31 +02:00
|
|
|
/**
|
|
|
|
* IE Fix for array.indexOf
|
|
|
|
*/
|
|
|
|
if (typeof Array.prototype.indexOf == "undefined")
|
|
|
|
{
|
|
|
|
Array.prototype.indexOf = function(_elem) {
|
|
|
|
for (var i = 0; i < this.length; i++)
|
|
|
|
{
|
|
|
|
if (this[i] === _elem)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-08-05 16:53:54 +02:00
|
|
|
/**
|
2011-08-10 16:36:31 +02:00
|
|
|
* Array with all types supported by the et2_checkType function.
|
2011-08-05 16:53:54 +02:00
|
|
|
*/
|
2015-03-11 16:06:06 +01:00
|
|
|
var et2_validTypes = ["boolean", "string", "rawstring", "html", "float", "integer", "any", "js", "dimension"];
|
2011-08-10 16:36:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Object whith default values for the above types. Do not specify array or
|
|
|
|
* objects inside the et2_typeDefaults object, as this instance will be shared
|
|
|
|
* between all users of it.
|
|
|
|
*/
|
|
|
|
var et2_typeDefaults = {
|
|
|
|
"boolean": false,
|
|
|
|
"string": "",
|
2015-03-11 13:12:52 +01:00
|
|
|
"rawstring": "", // no html-entity decoding
|
2014-02-14 11:14:28 +01:00
|
|
|
"html": "",
|
2011-08-15 11:24:32 +02:00
|
|
|
"js": null,
|
2011-08-10 16:36:31 +02:00
|
|
|
"float": 0.0,
|
|
|
|
"integer": 0,
|
2011-08-19 18:00:44 +02:00
|
|
|
"any": null,
|
|
|
|
"dimension": "auto"
|
2011-08-10 16:36:31 +02:00
|
|
|
};
|
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
function et2_evalBool(_val)
|
|
|
|
{
|
2011-08-22 17:58:47 +02:00
|
|
|
if (typeof _val == "string")
|
|
|
|
{
|
2014-06-09 21:27:02 +02:00
|
|
|
if (_val == "false" || _val == "0")
|
2011-08-22 17:58:47 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-16 14:31:18 +02:00
|
|
|
return _val ? true : false;
|
|
|
|
}
|
|
|
|
|
2011-09-10 15:16:40 +02:00
|
|
|
/**
|
|
|
|
* Concat et2 name together, eg. et2_concat("namespace","test[something]") == "namespace[test][something]"
|
|
|
|
* @param variable number of arguments to contact
|
|
|
|
* @returns string
|
|
|
|
*/
|
|
|
|
function et2_form_name(_cname,_name)
|
|
|
|
{
|
|
|
|
var parts = [];
|
|
|
|
for(var i=0; i < arguments.length; ++i)
|
|
|
|
{
|
|
|
|
var name = arguments[i];
|
|
|
|
if (typeof name == 'string' && name.length > 0) // et2_namespace("","test") === "test" === et2_namespace(null,"test")
|
|
|
|
{
|
|
|
|
parts = parts.concat(name.replace(/]/g,'').split('['));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var name = parts.shift();
|
|
|
|
return parts.length ? name + '['+parts.join('][')+']' : name;
|
|
|
|
}
|
|
|
|
|
2011-08-10 16:36:31 +02:00
|
|
|
/**
|
|
|
|
* Checks whether the given value is of the given type. Strings are converted
|
|
|
|
* into the corresponding type. The (converted) value is returned. All supported
|
|
|
|
* types are listed in the et2_validTypes array.
|
2014-01-27 22:59:29 +01:00
|
|
|
*
|
2013-10-09 16:35:03 +02:00
|
|
|
* @param mixed _val value
|
|
|
|
* @param string _type a valid type eg. "string" or "js"
|
|
|
|
* @param string _attr attribute name
|
|
|
|
* @param object _widget
|
2011-08-10 16:36:31 +02:00
|
|
|
*/
|
2013-10-09 16:35:03 +02:00
|
|
|
function et2_checkType(_val, _type, _attr, _widget)
|
2011-08-05 16:53:54 +02:00
|
|
|
{
|
2011-08-16 15:45:41 +02:00
|
|
|
if (typeof _attr == "undefined")
|
|
|
|
{
|
|
|
|
_attr = null;
|
|
|
|
}
|
|
|
|
|
2011-08-10 16:36:31 +02:00
|
|
|
function _err() {
|
2011-08-16 15:45:41 +02:00
|
|
|
var res = et2_typeDefaults[_type];
|
|
|
|
|
2013-02-14 16:56:19 +01:00
|
|
|
if(typeof _val != "undefined" && _val)
|
|
|
|
{
|
2014-01-27 22:59:29 +01:00
|
|
|
egw.debug("warn", "Widget %o: '" + _val + "' was not of specified _type '" +
|
2013-02-14 16:56:19 +01:00
|
|
|
_type + (_attr != null ? "' for attribute '" + _attr + "' " : "") +
|
2013-10-10 16:52:48 +02:00
|
|
|
"and is now '" + res + "'",_widget);
|
2013-02-14 16:56:19 +01:00
|
|
|
}
|
2011-08-16 15:45:41 +02:00
|
|
|
return res;
|
2011-08-10 16:36:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the type is "any" simply return the value again
|
|
|
|
if (_type == "any")
|
|
|
|
{
|
|
|
|
return _val;
|
|
|
|
}
|
|
|
|
|
2014-01-27 22:59:29 +01:00
|
|
|
// we dont check default-value any further, that also fixes type="js" does NOT accept null,
|
|
|
|
// which happens on expanded values
|
|
|
|
if (_val === et2_typeDefaults[_type])
|
|
|
|
{
|
|
|
|
return _val;
|
|
|
|
}
|
|
|
|
|
2011-08-10 16:36:31 +02:00
|
|
|
// If the type is boolean, check whether the given value is exactly true or
|
|
|
|
// false. Otherwise check whether the value is the string "true" or "false".
|
|
|
|
if (_type == "boolean")
|
|
|
|
{
|
|
|
|
if (_val === true || _val === false)
|
2011-08-05 16:53:54 +02:00
|
|
|
{
|
2011-08-10 16:36:31 +02:00
|
|
|
return _val;
|
2011-08-05 16:53:54 +02:00
|
|
|
}
|
2011-08-10 16:36:31 +02:00
|
|
|
|
2011-08-16 21:01:45 +02:00
|
|
|
if (typeof _val == "string")
|
2011-08-10 16:36:31 +02:00
|
|
|
{
|
2011-08-16 21:01:45 +02:00
|
|
|
var lcv = _val.toLowerCase();
|
|
|
|
if (lcv === "true" || lcv === "false" || lcv === "")
|
|
|
|
{
|
|
|
|
return _val === "true";
|
|
|
|
}
|
2012-07-26 00:37:56 +02:00
|
|
|
if(lcv === "0" || lcv === "1")
|
|
|
|
{
|
|
|
|
return _val === "1";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (typeof _val == "number")
|
|
|
|
{
|
|
|
|
return _val != 0;
|
2011-08-10 16:36:31 +02:00
|
|
|
}
|
|
|
|
|
2011-08-16 15:45:41 +02:00
|
|
|
return _err();
|
2011-08-10 16:36:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether the given value is of the type "string"
|
2015-03-11 13:12:52 +01:00
|
|
|
if (_type == "string" || _type == "html" || _type == "rawstring")
|
2011-08-10 16:36:31 +02:00
|
|
|
{
|
2014-06-12 10:25:31 +02:00
|
|
|
if (typeof _val == "number") // as php is a bit vague here, silently convert to a string
|
|
|
|
{
|
|
|
|
return _val.toString();
|
|
|
|
}
|
|
|
|
|
2011-08-10 16:36:31 +02:00
|
|
|
if (typeof _val == "string")
|
|
|
|
{
|
2015-03-11 13:12:52 +01:00
|
|
|
return _type == "string" ? html_entity_decode(_val) : _val;
|
2011-08-10 16:36:31 +02:00
|
|
|
}
|
|
|
|
|
2012-07-24 01:54:16 +02:00
|
|
|
// Handle some less common possibilities
|
|
|
|
// Maybe a split on an empty string
|
|
|
|
if(typeof _val == "object" && jQuery.isEmptyObject(_val)) return "";
|
|
|
|
|
2011-08-16 15:45:41 +02:00
|
|
|
return _err();
|
2011-08-10 16:36:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether the value is already a number, otherwise try to convert it
|
|
|
|
// to one.
|
|
|
|
if (_type == "float")
|
|
|
|
{
|
|
|
|
if (typeof _val == "number")
|
|
|
|
{
|
|
|
|
return _val;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isNaN(_val))
|
|
|
|
{
|
|
|
|
return parseFloat(_val);
|
|
|
|
}
|
|
|
|
|
2011-08-16 15:45:41 +02:00
|
|
|
return _err();
|
2011-08-10 16:36:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether the value is an integer by comparing the result of
|
|
|
|
// parseInt(_val) to the value itself.
|
|
|
|
if (_type == "integer")
|
|
|
|
{
|
|
|
|
if (parseInt(_val) == _val)
|
|
|
|
{
|
|
|
|
return parseInt(_val);
|
|
|
|
}
|
|
|
|
|
2011-08-16 15:45:41 +02:00
|
|
|
return _err();
|
2011-08-10 16:36:31 +02:00
|
|
|
}
|
|
|
|
|
2011-08-19 18:00:44 +02:00
|
|
|
// Parse the given dimension value
|
|
|
|
if (_type == "dimension")
|
|
|
|
{
|
|
|
|
// Case 1: The value is "auto"
|
|
|
|
if (_val == "auto")
|
|
|
|
{
|
|
|
|
return _val;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case 2: The value is simply a number, attach "px"
|
|
|
|
if (!isNaN(_val))
|
|
|
|
{
|
|
|
|
return parseFloat(_val) + "px";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case 3: The value is already a valid css pixel value or a percentage
|
2014-01-27 22:59:29 +01:00
|
|
|
if (typeof _val == "string" &&
|
2011-08-19 18:00:44 +02:00
|
|
|
((_val.indexOf("px") == _val.length - 2 && !isNaN(_val.split("px")[0])) ||
|
|
|
|
(_val.indexOf("%") == _val.length - 1 && !isNaN(_val.split("%")[0]))))
|
|
|
|
{
|
|
|
|
return _val;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _err();
|
|
|
|
}
|
|
|
|
|
2013-04-16 20:44:43 +02:00
|
|
|
// Javascript
|
|
|
|
if (_type == "js")
|
|
|
|
{
|
2014-12-11 19:02:08 +01:00
|
|
|
if (typeof _val == "function" || typeof _val == "undefined")
|
2013-04-16 20:44:43 +02:00
|
|
|
{
|
|
|
|
return _val;
|
|
|
|
}
|
2014-12-11 17:59:46 +01:00
|
|
|
if (_val) _val = _val.replace(/window\.close\(\)/g, 'egw(window).close()');
|
2013-04-16 20:44:43 +02:00
|
|
|
|
|
|
|
// Check to see if it's a string in app.appname.function format, and wrap it in
|
|
|
|
// a closure to make sure context is preserved
|
2016-03-17 19:12:36 +01:00
|
|
|
if(typeof _val == "string" && _val.substr(0,4) == "app." && app)
|
2013-04-16 20:44:43 +02:00
|
|
|
{
|
2013-07-19 12:55:07 +02:00
|
|
|
var parts = _val.split('.');
|
2013-04-16 20:44:43 +02:00
|
|
|
var func = parts.pop();
|
|
|
|
var parent = window;
|
|
|
|
for(var i=0; i < parts.length && typeof parent[parts[i]] != 'undefined'; ++i)
|
|
|
|
{
|
|
|
|
parent = parent[parts[i]];
|
|
|
|
}
|
|
|
|
if (typeof parent[func] == 'function')
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2013-07-19 14:37:47 +02:00
|
|
|
return jQuery.proxy(parent[func],parent);
|
2013-04-16 20:44:43 +02:00
|
|
|
}
|
|
|
|
catch (e)
|
|
|
|
{
|
|
|
|
req.egw.debug('error', 'Function', _val);
|
|
|
|
return _err();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-27 22:59:29 +01:00
|
|
|
|
2014-12-11 18:02:52 +01:00
|
|
|
if (!_val || typeof _val == "string")
|
2013-10-09 16:35:03 +02:00
|
|
|
{
|
|
|
|
return _val; // get compiled later in widgets own initAttributes, as widget is not yet initialised
|
|
|
|
}
|
2013-04-16 20:44:43 +02:00
|
|
|
}
|
|
|
|
|
2011-08-10 16:36:31 +02:00
|
|
|
// We should never come here
|
2013-10-10 15:17:07 +02:00
|
|
|
throw("Invalid type identifier '" + _attr + "': '" + _type+"'");
|
2011-08-10 16:36:31 +02:00
|
|
|
}
|
|
|
|
|
2011-08-10 18:23:02 +02:00
|
|
|
/**
|
|
|
|
* If et2_no_init is set as default value, the initAttributes function will not
|
|
|
|
* try to initialize the attribute with the default value.
|
|
|
|
*/
|
|
|
|
var et2_no_init = new Object();
|
|
|
|
|
2011-08-10 16:36:31 +02:00
|
|
|
/**
|
|
|
|
* Validates the given attribute with the given id. The validation checks for
|
|
|
|
* the existance of a human name, a description, a type and a default value.
|
|
|
|
* If the human name defaults to the given id, the description defaults to an
|
|
|
|
* empty string, the type defaults to any and the default to the corresponding
|
|
|
|
* type default.
|
|
|
|
*/
|
|
|
|
function et2_validateAttrib(_id, _attrib)
|
|
|
|
{
|
|
|
|
// Default ignore to false.
|
|
|
|
if (typeof _attrib["ignore"] == "undefined")
|
|
|
|
{
|
2013-04-13 21:00:13 +02:00
|
|
|
_attrib["ignore"] = false;
|
2011-08-10 16:36:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Break if "ignore" is set to true.
|
|
|
|
if (_attrib.ignore)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof _attrib["name"] == "undefined")
|
|
|
|
{
|
|
|
|
_attrib["name"] = _id;
|
2012-03-05 14:07:38 +01:00
|
|
|
egw.debug("log", "Human name ('name'-Field) for attribute '" +
|
2011-08-10 16:36:31 +02:00
|
|
|
_id + "' has not been supplied, set to '" + _id + "'");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof _attrib["description"] == "undefined")
|
|
|
|
{
|
|
|
|
_attrib["description"] = "";
|
2012-03-05 14:07:38 +01:00
|
|
|
egw.debug("log", "Description for attribute '" +
|
2011-08-10 16:36:31 +02:00
|
|
|
_id + "' has not been supplied");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof _attrib["type"] == "undefined")
|
|
|
|
{
|
|
|
|
_attrib["type"] = "any";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (et2_validTypes.indexOf(_attrib["type"]) < 0)
|
|
|
|
{
|
2014-01-27 22:59:29 +01:00
|
|
|
egw.debug("error", "Invalid type '" + _attrib["type"] + "' for attribute '" + _id +
|
2012-03-21 22:27:54 +01:00
|
|
|
"' supplied. Valid types are ", et2_validTypes);
|
2011-08-10 16:36:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the defaults
|
|
|
|
if (typeof _attrib["default"] == "undefined")
|
|
|
|
{
|
|
|
|
_attrib["default"] = et2_typeDefaults[_attrib["type"]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Equivalent to the PHP array_values function
|
|
|
|
*/
|
|
|
|
function et2_arrayValues(_arr)
|
|
|
|
{
|
|
|
|
var result = [];
|
|
|
|
for (var key in _arr)
|
|
|
|
{
|
|
|
|
if (parseInt(key) == key)
|
|
|
|
{
|
|
|
|
result.push(_arr[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-09-08 20:36:09 +02:00
|
|
|
/**
|
|
|
|
* Equivalent to the PHP array_keys function
|
|
|
|
*/
|
|
|
|
function et2_arrayKeys(_arr)
|
|
|
|
{
|
|
|
|
var result = [];
|
|
|
|
for (var key in _arr)
|
|
|
|
{
|
|
|
|
result.push(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function et2_arrayIntKeys(_arr)
|
|
|
|
{
|
|
|
|
var result = [];
|
|
|
|
for (var key in _arr)
|
|
|
|
{
|
|
|
|
result.push(parseInt(key));
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-10 16:36:31 +02:00
|
|
|
/**
|
|
|
|
* Equivalent to the PHP substr function, partly take from phpjs, licensed under
|
|
|
|
* the GPL.
|
|
|
|
*/
|
|
|
|
function et2_substr (str, start, len) {
|
|
|
|
var end = str.length;
|
|
|
|
|
|
|
|
if (start < 0)
|
|
|
|
{
|
|
|
|
start += end;
|
|
|
|
}
|
|
|
|
end = typeof len === 'undefined' ? end : (len < 0 ? len + end : len + start);
|
|
|
|
|
|
|
|
return start >= str.length || start < 0 || start > end ? "" : str.slice(start, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Split a $delimiter-separated options string, which can contain parts with
|
|
|
|
* delimiters enclosed in $enclosure. Ported from class.boetemplate.inc.php
|
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
* - et2_csvSplit('"1,2,3",2,3') === array('1,2,3','2','3')
|
|
|
|
* - et2_csvSplit('1,2,3',2) === array('1','2,3')
|
|
|
|
* - et2_csvSplit('"1,2,3",2,3',2) === array('1,2,3','2,3')
|
|
|
|
* - et2_csvSplit('"a""b,c",d') === array('a"b,c','d') // to escape enclosures double them!
|
|
|
|
*
|
|
|
|
* @param string _str
|
|
|
|
* @param int _num=null in how many parts to split maximal, parts over this
|
|
|
|
* number end up (unseparated) in the last part
|
|
|
|
* @param string _delimiter=','
|
|
|
|
* @param string _enclosure='"'
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function et2_csvSplit(_str, _num, _delimiter, _enclosure)
|
|
|
|
{
|
|
|
|
// Default the parameters
|
2013-03-20 17:18:53 +01:00
|
|
|
if (typeof _str == "undefined" || _str == null)
|
2011-10-17 18:08:34 +02:00
|
|
|
{
|
|
|
|
_str = "";
|
|
|
|
}
|
2011-08-10 16:36:31 +02:00
|
|
|
if (typeof _num == "undefined")
|
|
|
|
{
|
2011-08-15 20:54:13 +02:00
|
|
|
_num = null;
|
2011-08-10 16:36:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof _delimiter == "undefined")
|
|
|
|
{
|
|
|
|
_delimiter = ",";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof _enclosure == "undefined")
|
|
|
|
{
|
|
|
|
_enclosure = '"';
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the _enclosure string does not occur in the string, simply use the
|
|
|
|
// split function
|
|
|
|
if (_str.indexOf(_enclosure) == -1)
|
|
|
|
{
|
|
|
|
return _num === null ? _str.split(_delimiter) :
|
|
|
|
_str.split(_delimiter, _num);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split the string at the delimiter and join it again, when a enclosure is
|
|
|
|
// found at the beginning/end of a part
|
2014-01-27 22:59:29 +01:00
|
|
|
var parts = _str.split(_delimiter);
|
2011-08-10 16:36:31 +02:00
|
|
|
for (var n = 0; typeof parts[n] != "undefined"; n++)
|
|
|
|
{
|
|
|
|
var part = parts[n];
|
|
|
|
|
|
|
|
if (part.charAt(0) === _enclosure)
|
|
|
|
{
|
|
|
|
var m = n;
|
|
|
|
while (typeof parts[m + 1] != "undefined" && parts[n].substr(-1) !== _enclosure)
|
|
|
|
{
|
|
|
|
parts[n] += _delimiter + parts[++m];
|
|
|
|
delete(parts[m]);
|
|
|
|
}
|
|
|
|
parts[n] = et2_substr(parts[n].replace(
|
|
|
|
new RegExp(_enclosure + _enclosure, 'g'), _enclosure), 1 , -1);
|
|
|
|
n = m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rebuild the array index
|
|
|
|
parts = et2_arrayValues(parts);
|
|
|
|
|
|
|
|
// Limit the parts to the given number
|
|
|
|
if (_num !== null && _num > 0 && _num < parts.length && parts.length > 0)
|
|
|
|
{
|
|
|
|
parts[_num - 1] = parts.slice(_num - 1, parts.length).join(_delimiter);
|
|
|
|
parts = parts.slice(0, _num);
|
|
|
|
}
|
|
|
|
|
|
|
|
return parts;
|
2011-08-05 16:53:54 +02:00
|
|
|
}
|
|
|
|
|
2011-08-22 12:50:55 +02:00
|
|
|
/**
|
|
|
|
* Parses the given string and returns an array marking parts which are URLs
|
|
|
|
*/
|
|
|
|
function et2_activateLinks(_content)
|
|
|
|
{
|
|
|
|
var _match = false;
|
|
|
|
var arr = [];
|
|
|
|
|
|
|
|
function _splitPush(_matches, _proc)
|
|
|
|
{
|
|
|
|
if (_matches)
|
|
|
|
{
|
|
|
|
// We had a match
|
|
|
|
_match = true;
|
|
|
|
|
|
|
|
// Replace "undefined" with ""
|
|
|
|
for (var i = 1; i < _matches.length; i++)
|
|
|
|
{
|
|
|
|
if (typeof _matches[i] == "undefined")
|
|
|
|
{
|
|
|
|
_matches[i] = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-12 19:28:47 +01:00
|
|
|
// Split the content string at the given position(s)
|
|
|
|
// but we only handle the first occurence
|
|
|
|
var splitted = _content.split(_matches[0]);
|
2011-08-22 12:50:55 +02:00
|
|
|
|
|
|
|
// Push the not-matched part
|
2014-06-03 09:22:34 +02:00
|
|
|
var left = splitted.shift();
|
|
|
|
if (left)
|
2011-08-22 12:50:55 +02:00
|
|
|
{
|
|
|
|
// activate the links of the left string
|
2014-06-03 09:22:34 +02:00
|
|
|
arr = arr.concat(et2_activateLinks(left));
|
2011-08-22 12:50:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call the callback function which converts the matches into an object
|
|
|
|
// and appends it to the string
|
|
|
|
_proc(_matches);
|
|
|
|
|
|
|
|
// Set the new working string to the right part
|
2014-03-12 19:28:47 +01:00
|
|
|
_content = splitted.join(_matches[0]);
|
2011-08-22 12:50:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-06 11:42:37 +01:00
|
|
|
var mail_regExp = /(mailto:)?([a-z0-9._-]+)@([a-z0-9_-]+)\.([a-z0-9._-]+)/i;
|
2011-08-22 12:50:55 +02:00
|
|
|
|
|
|
|
// First match things beginning with http:// (or other protocols)
|
|
|
|
var protocol = '(http:\\/\\/|(ftp:\\/\\/|https:\\/\\/))'; // only http:// gets removed, other protocolls are shown
|
|
|
|
var domain = '([\\w-]+\\.[\\w-.]+)';
|
|
|
|
var subdir = '([\\w\\-\\.,@?^=%&;:\\/~\\+#]*[\\w\\-\\@?^=%&\\/~\\+#])?';
|
|
|
|
var http_regExp = new RegExp(protocol + domain + subdir, 'i');
|
|
|
|
|
|
|
|
// Now match things beginning with www.
|
|
|
|
var domain = 'www(\\.[\\w-.]+)';
|
|
|
|
var subdir = '([\\w\\-\\.,@?^=%&:\\/~\\+#]*[\\w\\-\\@?^=%&\\/~\\+#])?';
|
|
|
|
var www_regExp = new RegExp(domain + subdir, 'i');
|
|
|
|
|
|
|
|
do {
|
|
|
|
_match = false;
|
|
|
|
|
|
|
|
// Abort if the remaining length of _content is smaller than 20 for
|
|
|
|
// performance reasons
|
|
|
|
if (!_content)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No need make emailaddress spam-save, as it gets dynamically created
|
|
|
|
_splitPush(_content.match(mail_regExp), function(_matches) {
|
|
|
|
arr.push({
|
2014-03-06 11:42:37 +01:00
|
|
|
"href": (_matches[1]?'':'mailto:')+_matches[0],
|
|
|
|
"text": _matches[2] + "@" + _matches[3] + "." + _matches[4]
|
2011-08-22 12:50:55 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Create hrefs for links starting with "http://"
|
|
|
|
_splitPush(_content.match(http_regExp), function(_matches) {
|
|
|
|
arr.push({
|
|
|
|
"href": _matches[0],
|
|
|
|
"text": _matches[2] + _matches[3] + _matches[4]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Create hrefs for links starting with "www."
|
|
|
|
_splitPush(_content.match(www_regExp), function(_matches) {
|
|
|
|
arr.push({
|
|
|
|
"href": "http://" + _matches[0],
|
|
|
|
"text": _matches[0]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} while (_match)
|
|
|
|
|
|
|
|
arr.push(_content);
|
|
|
|
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inserts the structure generated by et2_activateLinks into the given DOM-Node
|
|
|
|
*/
|
|
|
|
function et2_insertLinkText(_text, _node, _target)
|
|
|
|
{
|
2014-01-27 22:59:29 +01:00
|
|
|
if(!_node)
|
2011-10-12 18:34:52 +02:00
|
|
|
{
|
2012-03-05 14:07:38 +01:00
|
|
|
egw.debug("warn", "et2_insertLinkText called without node", _text, _node, _target);
|
2011-10-12 18:34:52 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-06 18:50:38 +02:00
|
|
|
// Clear the node
|
|
|
|
for (var i = _node.childNodes.length - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
_node.removeChild(_node.childNodes[i]);
|
|
|
|
}
|
|
|
|
|
2011-08-22 12:50:55 +02:00
|
|
|
for (var i = 0; i < _text.length; i++)
|
|
|
|
{
|
|
|
|
var s = _text[i];
|
|
|
|
|
2011-09-15 19:51:21 +02:00
|
|
|
if (typeof s == "string" || typeof s == "number")
|
2011-08-22 12:50:55 +02:00
|
|
|
{
|
2012-03-12 14:41:10 +01:00
|
|
|
// Include line breaks
|
2012-03-16 00:15:01 +01:00
|
|
|
var lines = s.split ? s.split('\n') : [s];
|
2012-03-12 14:41:10 +01:00
|
|
|
|
2012-03-12 14:52:32 +01:00
|
|
|
// Insert the lines
|
|
|
|
for (var j = 0; j < lines.length; j++)
|
2012-03-12 14:41:10 +01:00
|
|
|
{
|
2012-03-12 14:52:32 +01:00
|
|
|
_node.appendChild(document.createTextNode(lines[j]));
|
2012-03-12 14:41:10 +01:00
|
|
|
|
2012-03-12 14:52:32 +01:00
|
|
|
if (j < lines.length - 1)
|
2012-03-12 14:41:10 +01:00
|
|
|
{
|
|
|
|
_node.appendChild(document.createElement("br"));
|
|
|
|
}
|
|
|
|
}
|
2011-08-22 12:50:55 +02:00
|
|
|
}
|
2013-02-05 13:32:57 +01:00
|
|
|
else if(s.text) // no need to generate a link, if there is no content in it
|
2011-08-22 12:50:55 +02:00
|
|
|
{
|
2011-10-12 18:34:52 +02:00
|
|
|
if(!s.href)
|
|
|
|
{
|
2012-03-05 14:07:38 +01:00
|
|
|
egw.debug("warn", "et2_activateLinks gave bad data", s, _node, _target);
|
2011-10-12 18:34:52 +02:00
|
|
|
s.href = "";
|
|
|
|
}
|
2011-08-22 12:50:55 +02:00
|
|
|
var a = $j(document.createElement("a"))
|
|
|
|
.attr("href", s.href)
|
2013-02-05 13:32:57 +01:00
|
|
|
.text(s.text);
|
2011-08-22 12:50:55 +02:00
|
|
|
|
2014-03-06 11:42:37 +01:00
|
|
|
if (typeof _target != "undefined" && _target && _target != "_self" && s.href.substr(0, 7) != "mailto:")
|
2011-08-22 12:50:55 +02:00
|
|
|
{
|
|
|
|
a.attr("target", _target);
|
|
|
|
}
|
2014-03-06 11:42:37 +01:00
|
|
|
// open mailto links depending on preferences in mail app
|
|
|
|
if (s.href.substr(0, 7) == "mailto:" &&
|
|
|
|
(egw.user('apps').mail || egw.user('apps').felamimail) &&
|
|
|
|
egw.preference('force_mailto','addressbook') != '1')
|
|
|
|
{
|
|
|
|
a.click(function(event){
|
|
|
|
egw.open_link(this.href);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
2011-08-22 12:50:55 +02:00
|
|
|
|
|
|
|
a.appendTo(_node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-15 14:34:00 +02:00
|
|
|
/**
|
|
|
|
* Creates a copy of the given object (non recursive)
|
|
|
|
*/
|
|
|
|
function et2_cloneObject(_obj)
|
|
|
|
{
|
|
|
|
var result = {};
|
|
|
|
|
|
|
|
for (var key in _obj)
|
|
|
|
{
|
|
|
|
result[key] = _obj[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2011-08-05 16:53:54 +02:00
|
|
|
|
2011-08-23 16:59:49 +02:00
|
|
|
/**
|
|
|
|
* Returns true if the given array of nodes or their children contains the given
|
|
|
|
* child node.
|
|
|
|
*/
|
|
|
|
function et2_hasChild(_nodes, _child)
|
|
|
|
{
|
|
|
|
for (var i = 0; i < _nodes.length; i++)
|
|
|
|
{
|
|
|
|
if (_nodes[i] == _child)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (_nodes[i].childNodes)
|
|
|
|
{
|
|
|
|
var res = et2_hasChild(_nodes[i].childNodes, _child);
|
|
|
|
|
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-08-29 17:04:10 +02:00
|
|
|
/**
|
|
|
|
* Functions to work with ranges and range intersection (used in the dataview)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Common functions used in most view classes
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an "range" object with the given top position and height
|
|
|
|
*/
|
|
|
|
function et2_range(_top, _height)
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
"top": _top,
|
|
|
|
"bottom": _top + _height
|
2013-04-13 21:00:13 +02:00
|
|
|
};
|
2011-08-29 17:04:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an "area" object with the given top- and bottom position
|
|
|
|
*/
|
|
|
|
function et2_bounds(_top, _bottom)
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
"top": _top,
|
|
|
|
"bottom": _bottom
|
2013-04-13 21:00:13 +02:00
|
|
|
};
|
2011-08-29 17:04:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether two range objects intersect each other
|
|
|
|
*/
|
|
|
|
function et2_rangeIntersect(_ar1, _ar2)
|
|
|
|
{
|
|
|
|
return ! (_ar1.bottom < _ar2.top || _ar1.top > _ar2.bottom);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether two ranges intersect (result = 0) or their relative position
|
|
|
|
* to each other (used to do a binary search inside a list of sorted range objects).
|
|
|
|
*/
|
|
|
|
function et2_rangeIntersectDir(_ar1, _ar2)
|
|
|
|
{
|
|
|
|
if (_ar1.bottom < _ar2.top)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (_ar1.top > _ar2.bottom)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-23 13:20:57 +01:00
|
|
|
/**
|
|
|
|
* Returns whether two ranges are equal.
|
|
|
|
*/
|
|
|
|
function et2_rangeEqual(_ar1, _ar2)
|
|
|
|
{
|
|
|
|
return _ar1.top === _ar2.top && _ar1.bottom === _ar2.bottom;
|
|
|
|
}
|
|
|
|
|
2012-03-26 17:28:02 +02:00
|
|
|
/**
|
|
|
|
* Substracts _ar2 from _ar1, returns an array of new ranges.
|
|
|
|
*/
|
|
|
|
function et2_rangeSubstract(_ar1, _ar2)
|
|
|
|
{
|
|
|
|
// Per default return the complete _ar1 range
|
|
|
|
var res = [_ar1];
|
|
|
|
|
|
|
|
// Check whether there is an intersection between the given ranges
|
|
|
|
if (et2_rangeIntersect(_ar1, _ar2))
|
|
|
|
{
|
|
|
|
res = [et2_bounds(_ar1.top, _ar2.top),
|
|
|
|
et2_bounds(_ar2.bottom, _ar1.bottom)];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all zero-length ranges from the result
|
|
|
|
for (var i = res.length - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
if (res[i].bottom - res[i].top <= 0)
|
|
|
|
{
|
|
|
|
res.splice(i, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-02-13 18:29:02 +01:00
|
|
|
/**
|
|
|
|
* Decode html entities so they can be added via .text(_str), eg. html_entity_decode('&') === '&'
|
|
|
|
*
|
|
|
|
* @param {string} _str
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function html_entity_decode(_str)
|
|
|
|
{
|
|
|
|
return _str && _str.indexOf('&') != -1 ? jQuery('<span>'+_str+'</span>').text() : _str;
|
2014-11-27 18:29:44 +01:00
|
|
|
}
|