2011-08-26 00:42:07 +02:00
|
|
|
/**
|
2013-04-13 21:00:13 +02:00
|
|
|
* EGroupware eTemplate2 - JS URL object
|
2011-08-26 00:42:07 +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 Nathan Gray
|
|
|
|
* @copyright Nathan Gray 2011
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*egw:uses
|
|
|
|
et2_textbox;
|
|
|
|
et2_valueWidget;
|
2011-08-29 23:15:53 +02:00
|
|
|
phpgwapi.jquery.jquery.base64;
|
2011-08-26 00:42:07 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class which implements the "url" XET-Tag, which covers URLs, email & phone
|
2014-01-10 17:19:05 +01:00
|
|
|
*
|
2013-04-13 21:00:13 +02:00
|
|
|
* @augments et2_textbox
|
2014-01-10 17:19:05 +01:00
|
|
|
*/
|
2013-04-13 21:00:13 +02:00
|
|
|
var et2_url = et2_textbox.extend(
|
|
|
|
{
|
2011-08-26 00:42:07 +02:00
|
|
|
attributes: {
|
|
|
|
"multiline": {
|
|
|
|
"ignore": true
|
|
|
|
}
|
|
|
|
},
|
2014-01-10 17:19:05 +01:00
|
|
|
|
2014-01-16 19:10:06 +01:00
|
|
|
/**
|
|
|
|
* Regexes for validating email addresses incl. email in angle-brackets eg.
|
|
|
|
* + "Ralf Becker <rb@stylite.de>"
|
|
|
|
* + "Ralf Becker (Stylite AG) <rb@stylite.de>"
|
|
|
|
* + "<rb@stylite.de>" or "rb@stylite.de"
|
|
|
|
* + '"Becker, Ralf" <rb@stylite.de>'
|
|
|
|
* + "'Becker, Ralf' <rb@stylite.de>"
|
|
|
|
* but NOT:
|
|
|
|
* - "Becker, Ralf <rb@stylite.de>" (contains comma outside " or ' enclosed block)
|
|
|
|
* - "Becker < Ralf <rb@stylite.de>" (contains < ----------- " ---------------)
|
|
|
|
*
|
|
|
|
* About umlaut or IDN domains: we currently only allow German umlauts in domain part!
|
|
|
|
*
|
|
|
|
* Using \042 instead of " to NOT stall minifyer!
|
|
|
|
*
|
|
|
|
* Same preg is in etemplate_widget_url PHP class!
|
|
|
|
*/
|
|
|
|
EMAIL_PREG: new RegExp(/^(([^\042',<][^,<]+|\042[^\042]+\042|\'[^\']+\'|)\s?<)?[^\x00-\x20()<>@,;:\042\[\]]+@([a-z0-9ÄÖÜäöüß](|[a-z0-9ÄÖÜäöüß_-]*[a-z0-9ÄÖÜäöüß])\.)+[a-z]{2,6}>?$/i),
|
2013-04-13 21:00:13 +02:00
|
|
|
/**
|
|
|
|
* @memberOf et2_url
|
|
|
|
*/
|
2011-08-26 00:42:07 +02:00
|
|
|
createInputWidget: function() {
|
|
|
|
this.input = $j(document.createElement("input"))
|
|
|
|
.blur(this,this.validate)
|
|
|
|
.blur(this,function(e){e.data.set_value(e.data.getValue());});
|
|
|
|
|
|
|
|
this._button = null;
|
|
|
|
|
|
|
|
if(this.size) {
|
|
|
|
this.set_size(this.size);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setDOMNode(this.input[0]);
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function() {
|
|
|
|
if(this.input) {
|
|
|
|
this.input.unbind();
|
|
|
|
}
|
|
|
|
this._button = null;
|
2013-07-20 19:20:55 +02:00
|
|
|
this._super.apply(this);
|
2011-08-26 00:42:07 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override parent to update href of 'button'
|
2014-01-27 10:48:43 +01:00
|
|
|
*
|
|
|
|
* @param _value value to set
|
2011-08-26 00:42:07 +02:00
|
|
|
*/
|
|
|
|
set_value: function(_value) {
|
|
|
|
this.update_button(_value);
|
|
|
|
this._super.apply(this, arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
update_button: function(_value) {
|
|
|
|
if(this.value == _value) return;
|
|
|
|
if(_value)
|
|
|
|
{
|
|
|
|
// Create button if it doesn't exist yet
|
|
|
|
if(this._button == null)
|
|
|
|
{
|
|
|
|
this._button = $j(document.createElement("a")).addClass("et2_url");
|
2013-10-11 16:35:13 +02:00
|
|
|
this.getSurroundings().insertDOMNode(this._button[0]);
|
2011-08-26 00:42:07 +02:00
|
|
|
this.getSurroundings().update();
|
|
|
|
}
|
|
|
|
this._button.removeClass("url phone email").removeAttr("href");
|
2011-08-29 23:15:53 +02:00
|
|
|
_value = this.get_link(this._type, _value);
|
2011-08-26 00:42:07 +02:00
|
|
|
switch(this._type)
|
|
|
|
{
|
|
|
|
case "url":
|
|
|
|
// Silently use http if no protocol
|
|
|
|
this._button.attr("href", _value).attr("target", "_blank").addClass("url");
|
|
|
|
break;
|
|
|
|
case "url-phone":
|
2011-08-29 23:15:53 +02:00
|
|
|
if(_value) {
|
|
|
|
if(typeof _value == 'function')
|
|
|
|
{
|
|
|
|
this._button.click(this, _value).addClass("phone").show();
|
|
|
|
}
|
2014-01-10 17:19:05 +01:00
|
|
|
else
|
2011-08-29 23:15:53 +02:00
|
|
|
{
|
|
|
|
this._button.attr("href", _value).addClass("phone").show();
|
|
|
|
}
|
|
|
|
} else if (_value === false) {
|
2011-08-26 00:42:07 +02:00
|
|
|
// Can't make a good handler, hide button
|
|
|
|
this._button.hide();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "url-email":
|
2011-08-29 23:15:53 +02:00
|
|
|
if(typeof _value == 'function')
|
2011-08-26 00:42:07 +02:00
|
|
|
{
|
2011-08-29 23:15:53 +02:00
|
|
|
this._button.click(this, _value).addClass("email");
|
2011-08-26 00:42:07 +02:00
|
|
|
}
|
2011-08-29 23:15:53 +02:00
|
|
|
else
|
2011-08-26 00:42:07 +02:00
|
|
|
{
|
|
|
|
this._button.attr("href", _value).addClass("email");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-08-26 01:00:44 +02:00
|
|
|
if(this._button) this._button.hide();
|
2011-10-12 18:37:56 +02:00
|
|
|
if(this._button && this.getSurroundings && this.getSurroundings().removeDOMNode)
|
2011-08-26 00:42:07 +02:00
|
|
|
{
|
2011-10-12 18:37:56 +02:00
|
|
|
this.getSurroundings().removeDOMNode(this._button[0]);
|
2011-08-26 00:42:07 +02:00
|
|
|
}
|
|
|
|
this._button = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-08-29 23:15:53 +02:00
|
|
|
get_link: function(type, value) {
|
|
|
|
if(!value) return false;
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case "url":
|
|
|
|
// Silently use http if no protocol
|
|
|
|
if(value.indexOf("://") == -1) value = "http://"+value;
|
|
|
|
break;
|
|
|
|
case "url-phone":
|
|
|
|
// Clean number
|
|
|
|
value = value.replace('♥','').replace('(0)','');
|
|
|
|
value = value.replace(/[abc]/gi,2).replace(/[def]/gi,3).replace(/[ghi]/gi,4).replace(/[jkl]/gi,5).replace(/[mno]/gi,6);
|
|
|
|
value = value.replace(/[pqrs]/gi,7).replace(/[tuv]/gi,8).replace(/[wxyz]/gi,9);
|
|
|
|
|
2011-08-31 14:18:38 +02:00
|
|
|
// movile Webkit (iPhone, Android) have precedence over server configuration!
|
|
|
|
if (navigator.userAgent.indexOf('AppleWebKit') !== -1 &&
|
|
|
|
(navigator.userAgent.indexOf("iPhone") !== -1 || navigator.userAgent.indexOf("Android") !== -1) &&
|
|
|
|
value.indexOf("tel:") == -1)
|
|
|
|
{
|
2014-01-10 17:19:05 +01:00
|
|
|
value = "tel:"+value;
|
|
|
|
}
|
|
|
|
else if (this.egw().config("call_link"))
|
2011-08-31 14:18:38 +02:00
|
|
|
{
|
2014-01-18 10:29:32 +01:00
|
|
|
var link = this.egw().config("call_link")
|
|
|
|
.replace("%1", encodeURIComponent(value))
|
|
|
|
.replace("%u",this.egw().user('account_lid'))
|
|
|
|
.replace("%t",this.egw().user('account_phone'));
|
|
|
|
var popup = this.egw().config("call_popup");
|
|
|
|
value = function() { egw.open_link(link, 'phonecall', popup); };
|
2011-08-29 23:15:53 +02:00
|
|
|
}
|
2011-08-31 14:18:38 +02:00
|
|
|
else {
|
2011-08-29 23:15:53 +02:00
|
|
|
// Can't make a good handler
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "url-email":
|
2013-10-07 11:48:17 +02:00
|
|
|
if(value.indexOf("mailto:") == -1)
|
2011-08-29 23:15:53 +02:00
|
|
|
{
|
2013-10-07 11:48:17 +02:00
|
|
|
value = "mailto:"+value;
|
2011-08-29 23:15:53 +02:00
|
|
|
}
|
2014-01-10 17:19:05 +01:00
|
|
|
if((this.egw().user('apps').mail || this.egw().user('apps').felamimail) &&
|
2013-10-07 11:48:17 +02:00
|
|
|
this.egw().preference('force_mailto','addressbook') == '0' )
|
2011-08-29 23:15:53 +02:00
|
|
|
{
|
2013-10-07 11:48:17 +02:00
|
|
|
return function() {egw.open_link(value);};
|
2011-08-29 23:15:53 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
},
|
|
|
|
|
2011-08-26 00:42:07 +02:00
|
|
|
validate: function(e) {
|
|
|
|
e.data.hideMessage();
|
|
|
|
|
|
|
|
if(e.data._super) {
|
|
|
|
e.data._super.apply(this, arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check value, and correct if possible
|
|
|
|
var value = jQuery.trim(e.data.getValue());
|
|
|
|
if(value == "") return;
|
|
|
|
switch(e.data._type) {
|
|
|
|
case "url":
|
|
|
|
if(value.indexOf("://") == -1) {
|
|
|
|
e.data.set_value("http://"+value);
|
2012-03-23 00:16:14 +01:00
|
|
|
e.data.showMessage(e.data.egw().lang("Protocol is required"), "hint", true);
|
2011-08-26 00:42:07 +02:00
|
|
|
}
|
|
|
|
break;
|
2013-07-30 16:16:47 +02:00
|
|
|
case "url-email":
|
2014-01-10 17:19:05 +01:00
|
|
|
if(!e.data.EMAIL_PREG.test(value) ||
|
2013-10-11 16:35:13 +02:00
|
|
|
// If they use Text <email>, make sure the <> match
|
|
|
|
(value.indexOf("<") > 0 && value.indexOf(">") != value.length-1) ||
|
|
|
|
(value.indexOf(">") > 0 && value.indexOf("<") < 0)
|
|
|
|
)
|
2013-07-30 16:16:47 +02:00
|
|
|
{
|
|
|
|
e.data.showMessage("Invalid email","validation_error",true);
|
|
|
|
}
|
2011-08-26 00:42:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
et2_register_widget(et2_url, ["url", "url-email", "url-phone"]);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* et2_url_ro is the readonly implementation of the url, email & phone.
|
|
|
|
* It renders things as links, when possible
|
2014-01-10 17:19:05 +01:00
|
|
|
*
|
2013-04-13 21:00:13 +02:00
|
|
|
* @augments et2_valueWidget
|
2011-08-26 00:42:07 +02:00
|
|
|
*/
|
2013-04-13 21:00:13 +02:00
|
|
|
var et2_url_ro = et2_valueWidget.extend([et2_IDetachedDOM],
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Constructor
|
2014-01-10 17:19:05 +01:00
|
|
|
*
|
2013-04-13 21:00:13 +02:00
|
|
|
* @memberOf et2_url_ro
|
|
|
|
*/
|
2011-08-26 00:42:07 +02:00
|
|
|
init: function() {
|
|
|
|
this._super.apply(this, arguments);
|
|
|
|
|
|
|
|
this.value = "";
|
|
|
|
this.span = $j(document.createElement("a"))
|
|
|
|
.addClass("et2_textbox readonly");
|
|
|
|
this.setDOMNode(this.span[0]);
|
|
|
|
},
|
|
|
|
|
|
|
|
set_value: function(_value) {
|
|
|
|
this.value = _value;
|
|
|
|
|
2011-08-29 23:15:53 +02:00
|
|
|
var link = et2_url.prototype.get_link(this._type, _value);
|
|
|
|
|
2014-01-10 17:19:05 +01:00
|
|
|
if(!link)
|
2011-08-29 23:15:53 +02:00
|
|
|
{
|
|
|
|
this.span.text(_value);
|
2013-11-27 17:42:37 +01:00
|
|
|
this.span.removeAttr("href");
|
2011-08-29 23:15:53 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-08-26 00:42:07 +02:00
|
|
|
this.span.text(_value);
|
|
|
|
switch(this._type) {
|
|
|
|
case "url":
|
2011-08-29 23:15:53 +02:00
|
|
|
this.span.attr("href", link).attr("target", "_blank");
|
2011-08-26 00:42:07 +02:00
|
|
|
break;
|
|
|
|
case "url-phone":
|
2011-08-29 23:15:53 +02:00
|
|
|
if(typeof link == 'function')
|
|
|
|
{
|
2013-12-02 17:24:24 +01:00
|
|
|
this.span.off('click.et2_url');
|
|
|
|
this.span.on('click.et2_url', link);
|
2013-10-08 12:08:09 +02:00
|
|
|
this.span.attr("href", "#");
|
2011-08-29 23:15:53 +02:00
|
|
|
}
|
2013-02-05 13:50:20 +01:00
|
|
|
else if (link)
|
2011-08-29 23:15:53 +02:00
|
|
|
{
|
|
|
|
this.span.attr("href", link);
|
2011-08-26 00:42:07 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "url-email":
|
2011-08-29 23:15:53 +02:00
|
|
|
if(typeof link == 'function')
|
|
|
|
{
|
2013-12-02 17:24:24 +01:00
|
|
|
this.span.off('click.et2_url');
|
|
|
|
this.span.on('click.et2_url', link);
|
2013-10-07 12:05:51 +02:00
|
|
|
this.span.removeAttr("href");
|
2011-08-29 23:15:53 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.span.attr("href", link);
|
2013-10-07 11:48:17 +02:00
|
|
|
if(!this.span.attr("target"))
|
|
|
|
{
|
|
|
|
this.span.attr("target", "_blank");
|
|
|
|
}
|
2011-08-29 23:15:53 +02:00
|
|
|
}
|
2011-08-26 00:42:07 +02:00
|
|
|
break;
|
|
|
|
}
|
2012-07-24 21:59:55 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Code for implementing et2_IDetachedDOM
|
2014-01-27 10:48:43 +01:00
|
|
|
*
|
|
|
|
* @param {array} _attrs array to add further attributes to
|
2012-07-24 21:59:55 +02:00
|
|
|
*/
|
|
|
|
getDetachedAttributes: function(_attrs)
|
|
|
|
{
|
|
|
|
_attrs.push("value", "class");
|
|
|
|
},
|
|
|
|
|
|
|
|
getDetachedNodes: function()
|
|
|
|
{
|
|
|
|
return [this.span[0]];
|
|
|
|
},
|
2011-08-26 00:42:07 +02:00
|
|
|
|
2012-07-24 21:59:55 +02:00
|
|
|
setDetachedAttributes: function(_nodes, _values)
|
|
|
|
{
|
|
|
|
// Update the properties
|
|
|
|
this.span = jQuery(_nodes[0]);
|
|
|
|
if (typeof _values["value"] != "undefined")
|
|
|
|
{
|
|
|
|
this.set_value(_values["value"]);
|
|
|
|
}
|
|
|
|
if (typeof _values["class"] != "undefined")
|
2013-10-07 12:05:51 +02:00
|
|
|
{
|
|
|
|
_nodes[0].setAttribute("class", _values["class"]);
|
|
|
|
}
|
2012-07-24 21:59:55 +02:00
|
|
|
}
|
2011-08-26 00:42:07 +02:00
|
|
|
});
|
|
|
|
et2_register_widget(et2_url_ro, ["url_ro", "url-email_ro", "url-phone_ro"]);
|
|
|
|
|