Convert et2_widget_url to TS

This commit is contained in:
Hadi Nategh 2020-02-06 12:10:57 +01:00
parent 06341552cf
commit edf95a4bdf
2 changed files with 942 additions and 474 deletions

View File

@ -1,3 +1,4 @@
"use strict";
/** /**
* EGroupware eTemplate2 - JS URL object * EGroupware eTemplate2 - JS URL object
* *
@ -9,485 +10,443 @@
* @copyright Nathan Gray 2011 * @copyright Nathan Gray 2011
* @version $Id$ * @version $Id$
*/ */
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
/*egw:uses /*egw:uses
et2_textbox; et2_textbox;
et2_valueWidget; et2_valueWidget;
/api/js/jquery/jquery.base64.js; /api/js/jquery/jquery.base64.js;
*/ */
var et2_core_widget_1 = require("./et2_core_widget");
var et2_widget_textbox_1 = require("./et2_widget_textbox");
var et2_core_inheritance_1 = require("./et2_core_inheritance");
var et2_core_valueWidget_1 = require("./et2_core_valueWidget");
/** /**
* Class which implements the "url" XET-Tag, which covers URLs, email & phone * Class which implements the "url" XET-Tag, which covers URLs, email & phone
* *
* @augments et2_textbox * @augments et2_textbox
*/ */
var et2_url = (function(){ "use strict"; return et2_textbox.extend( var et2_url = /** @class */ (function (_super_1) {
{ __extends(et2_url, _super_1);
attributes: { function et2_url() {
"multiline": { var _this = _super_1 !== null && _super_1.apply(this, arguments) || this;
"ignore": true _this._button = null;
}, _this.value = "";
"allow_path": { return _this;
type: "boolean", }
name: "Allow path", /**
description: "Allow a path instead of a URL, path must start with /", * @memberOf et2_url
default: false */
}, et2_url.prototype.createInputWidget = function () {
"trailing_slash": { this.input = jQuery(document.createElement("input"))
type: "boolean", .blur(this, this.validate)
name: "Trailing slash", .blur(this, function (e) { e.data.set_value(e.data.getValue()); });
description: "Require (or forbid) that the path ends with a /", this._button = null;
default: et2_no_init if (this.size) {
} this.set_size(this.size);
}, }
this.setDOMNode(this.input[0]);
/** };
* Regexes for validating email addresses incl. email in angle-brackets eg. et2_url.prototype.destroy = function () {
* + "Ralf Becker <rb@stylite.de>" if (this.input) {
* + "Ralf Becker (Stylite AG) <rb@stylite.de>" this.input.unbind();
* + "<rb@stylite.de>" or "rb@stylite.de" }
* + '"Becker, Ralf" <rb@stylite.de>' this._button = null;
* + "'Becker, Ralf' <rb@stylite.de>" _super_1.prototype.destroy.call(this);
* but NOT: };
* - "Becker, Ralf <rb@stylite.de>" (contains comma outside " or ' enclosed block) /**
* - "Becker < Ralf <rb@stylite.de>" (contains < ----------- " ---------------) * Override parent to update href of 'button'
* *
* About umlaut or IDN domains: we currently only allow German umlauts in domain part! * @param _value value to set
* We forbid all non-ascii chars in local part, as Horde does not yet support SMTPUTF8 extension (rfc6531) */
* and we get a "SMTP server does not support internationalized header data" error otherwise. et2_url.prototype.set_value = function (_value) {
* this.update_button(_value);
* Using \042 instead of " to NOT stall minifyer! _super_1.prototype.set_value.call(this, _value);
* };
* Similar, but not identical, preg is in Etemplate\Widget\Url PHP class! et2_url.prototype.update_button = function (_value) {
* We can not use "(?<![.\s])", used to check that name-part does not end in if (this.value == _value)
* a dot or white-space. The expression is valid in recent Chrome, but fails return;
* eg. in Safari 11.0 or node.js 4.8.3 and therefore grunt uglify! if (_value) {
* Server-side will fail in that case because it uses the full regexp. // Create button if it doesn't exist yet
*/ if (this._button == null) {
EMAIL_PREG: new RegExp(/^(([^\042',<][^,<]+|\042[^\042]+\042|\'[^\']+\'|"(?:[^"\\]|\\.)*")\s?<)?[^\x00-\x20()\xe2\x80\x8b<>@,;:\042\[\]\x80-\xff]+@([a-z0-9ÄÖÜäöüß](|[a-z0-9ÄÖÜäöüß_-]*[a-z0-9ÄÖÜäöüß])\.)+[a-z]{2,}>?$/i), this._button = jQuery(document.createElement("a")).addClass("et2_url");
/** this.getSurroundings().insertDOMNode(this._button[0]);
* @memberOf et2_url this.getSurroundings().update();
*/ }
createInputWidget: function() { this._button.removeClass("url phone email").removeAttr("href");
this.input = jQuery(document.createElement("input")) _value = this.get_link(this.getType(), _value);
.blur(this,this.validate) switch (this.getType()) {
.blur(this,function(e){e.data.set_value(e.data.getValue());}); case "url":
// Silently use http if no protocol
this._button = null; this._button.attr("href", _value).attr("target", "_blank").addClass("url");
break;
if(this.size) { case "url-phone":
this.set_size(this.size); if (_value) {
} if (typeof _value == 'function') {
this._button.click(this, _value).addClass("phone").show();
this.setDOMNode(this.input[0]); }
}, else {
this._button.attr("href", _value).addClass("phone").show();
destroy: function() { }
if(this.input) { }
this.input.unbind(); else if (_value === false) {
} // Can't make a good handler, hide button
this._button = null; this._button.hide();
this._super.apply(this); }
}, break;
case "url-email":
/** if (typeof _value == 'function') {
* Override parent to update href of 'button' this._button.click(this, _value).addClass("email");
* }
* @param _value value to set else {
*/ this._button.attr("href", _value).addClass("email");
set_value: function(_value) { }
this.update_button(_value); break;
this._super.apply(this, arguments); }
}, }
else {
update_button: function(_value) { if (this._button)
if(this.value == _value) return; this._button.hide();
if(_value) if (this._button && this.getSurroundings && this.getSurroundings().removeDOMNode) {
{ this.getSurroundings().removeDOMNode(this._button[0]);
// Create button if it doesn't exist yet }
if(this._button == null) this._button = null;
{ }
this._button = jQuery(document.createElement("a")).addClass("et2_url"); };
this.getSurroundings().insertDOMNode(this._button[0]); et2_url.prototype.get_link = function (type, value) {
this.getSurroundings().update(); if (!value)
} return false;
this._button.removeClass("url phone email").removeAttr("href"); switch (type) {
_value = this.get_link(this._type, _value); case "url":
switch(this._type) // Silently use http if no protocol
{ if (value.indexOf("://") == -1)
case "url": value = "http://" + value;
// Silently use http if no protocol break;
this._button.attr("href", _value).attr("target", "_blank").addClass("url"); case "url-phone":
break; // Clean number
case "url-phone": value = value.replace('&#9829;', '').replace('(0)', '');
if(_value) { value = value.replace(/[abc]/gi, 2).replace(/[def]/gi, 3).replace(/[ghi]/gi, 4).replace(/[jkl]/gi, 5).replace(/[mno]/gi, 6);
if(typeof _value == 'function') value = value.replace(/[pqrs]/gi, 7).replace(/[tuv]/gi, 8).replace(/[wxyz]/gi, 9);
{ // remove everything but numbers and plus, as telephon software might not like it
this._button.click(this, _value).addClass("phone").show(); value = value.replace(/[^0-9+]/g, '');
} // movile Webkit (iPhone, Android) have precedence over server configuration!
else if (navigator.userAgent.indexOf('AppleWebKit') !== -1 &&
{ (navigator.userAgent.indexOf("iPhone") !== -1 || navigator.userAgent.indexOf("Android") !== -1) &&
this._button.attr("href", _value).addClass("phone").show(); value.indexOf("tel:") == -1) {
} value = "tel:" + value;
} else if (_value === false) { }
// Can't make a good handler, hide button else if (this.egw().config("call_link")) {
this._button.hide(); var link = this.egw().config("call_link")
} // tel: links use no URL encoding according to rfc3966 section-5.1.4
break; .replace("%1", this.egw().config("call_link").substr(0, 4) == 'tel:' ?
case "url-email": value : encodeURIComponent(value))
if(typeof _value == 'function') .replace("%u", this.egw().user('account_lid'))
{ .replace("%t", this.egw().user('account_phone'));
this._button.click(this, _value).addClass("email"); var popup = this.egw().config("call_popup");
} value = function () { egw.open_link(link, '_phonecall', popup); };
else }
{ else {
this._button.attr("href", _value).addClass("email"); // Can't make a good handler
} return false;
break; }
} break;
} case "url-email":
else if (value.indexOf("mailto:") == -1) {
{ value = "mailto:" + value;
if(this._button) this._button.hide(); }
if(this._button && this.getSurroundings && this.getSurroundings().removeDOMNode) if ((this.egw().user('apps').mail || this.egw().user('apps').felamimail) &&
{ this.egw().preference('force_mailto', 'addressbook') != '1') {
this.getSurroundings().removeDOMNode(this._button[0]); return function () { egw.open_link(value); };
} }
this._button = null; break;
} }
}, return value;
};
get_link: function(type, value) { et2_url.prototype.validate = function (e) {
if(!value) return false; e.data.hideMessage();
switch(type) if (e.data._super) {
{ e.data._super.apply(this, arguments);
case "url": }
// Silently use http if no protocol // Check value, and correct if possible
if(value.indexOf("://") == -1) value = "http://"+value; var value = jQuery.trim(e.data.getValue());
break; if (value == "")
case "url-phone": return;
// Clean number switch (e.data._type) {
value = value.replace('&#9829;','').replace('(0)',''); case "url":
value = value.replace(/[abc]/gi,2).replace(/[def]/gi,3).replace(/[ghi]/gi,4).replace(/[jkl]/gi,5).replace(/[mno]/gi,6); if (value.indexOf("://") == -1 && !e.data.options.allow_path) {
value = value.replace(/[pqrs]/gi,7).replace(/[tuv]/gi,8).replace(/[wxyz]/gi,9); e.data.set_value("http://" + value);
// remove everything but numbers and plus, as telephon software might not like it e.data.showMessage(e.data.egw().lang("Protocol is required"), "hint", true);
value = value.replace(/[^0-9+]/g, ''); }
else if (e.data.options.allow_path && value[0] !== '/') {
// movile Webkit (iPhone, Android) have precedence over server configuration! e.data.showMessage(e.data.egw().lang("Path must start with '/'"), "hint", true);
if (navigator.userAgent.indexOf('AppleWebKit') !== -1 && }
(navigator.userAgent.indexOf("iPhone") !== -1 || navigator.userAgent.indexOf("Android") !== -1) && // Adjust trailing slash - required or forbidden
value.indexOf("tel:") == -1) if (e.data.options.trailing_slash === true && value[value.length - 1] !== '/') {
{ e.data.set_value(value + '/');
value = "tel:"+value; }
} else if (e.data.options.trailing_slash === false && value[value.length - 1] === '/') {
else if (this.egw().config("call_link")) e.data.set_value(value.substr(0, value.length - 1));
{ }
var link = this.egw().config("call_link") break;
// tel: links use no URL encoding according to rfc3966 section-5.1.4 case "url-email":
.replace("%1", this.egw().config("call_link").substr(0, 4) == 'tel:' ? if (!e.data.EMAIL_PREG.test(value) ||
value : encodeURIComponent(value)) // If they use Text <email>, make sure the <> match
.replace("%u",this.egw().user('account_lid')) (value.indexOf("<") > 0 && value.indexOf(">") != value.length - 1) ||
.replace("%t",this.egw().user('account_phone')); (value.indexOf(">") > 0 && value.indexOf("<") < 0)) {
var popup = this.egw().config("call_popup"); e.data.showMessage("Invalid email", "validation_error", true);
value = function() { egw.open_link(link, '_phonecall', popup); }; }
} }
else { };
// Can't make a good handler et2_url.prototype.attachToDOM = function () {
return false; var res = _super_1.prototype.attachToDOM.call(this);
} if (this.input[0].parentNode)
break; jQuery(this.input[0].parentNode).addClass('et2_url_span');
case "url-email": return res;
if(value.indexOf("mailto:") == -1) };
{ et2_url._attributes = {
value = "mailto:"+value; "multiline": {
} "ignore": true
if((this.egw().user('apps').mail || this.egw().user('apps').felamimail) && },
this.egw().preference('force_mailto','addressbook') != '1' ) "allow_path": {
{ type: "boolean",
return function() {egw.open_link(value);}; name: "Allow path",
} description: "Allow a path instead of a URL, path must start with /",
break; default: false
} },
"trailing_slash": {
return value; type: "boolean",
}, name: "Trailing slash",
description: "Require (or forbid) that the path ends with a /",
validate: function(e) { default: et2_no_init
e.data.hideMessage(); }
};
if(e.data._super) { /**
e.data._super.apply(this, arguments); * Regexes for validating email addresses incl. email in angle-brackets eg.
} * + "Ralf Becker <rb@stylite.de>"
* + "Ralf Becker (Stylite AG) <rb@stylite.de>"
// Check value, and correct if possible * + "<rb@stylite.de>" or "rb@stylite.de"
var value = jQuery.trim(e.data.getValue()); * + '"Becker, Ralf" <rb@stylite.de>'
if(value == "") return; * + "'Becker, Ralf' <rb@stylite.de>"
switch(e.data._type) { * but NOT:
case "url": * - "Becker, Ralf <rb@stylite.de>" (contains comma outside " or ' enclosed block)
if(value.indexOf("://") == -1 && !e.data.options.allow_path) { * - "Becker < Ralf <rb@stylite.de>" (contains < ----------- " ---------------)
e.data.set_value("http://"+value); *
e.data.showMessage(e.data.egw().lang("Protocol is required"), "hint", true); * About umlaut or IDN domains: we currently only allow German umlauts in domain part!
} * We forbid all non-ascii chars in local part, as Horde does not yet support SMTPUTF8 extension (rfc6531)
else if (e.data.options.allow_path && value[0] !== '/') * and we get a "SMTP server does not support internationalized header data" error otherwise.
{ *
e.data.showMessage(e.data.egw().lang("Path must start with '/'"), "hint", true); * Using \042 instead of " to NOT stall minifyer!
} *
* Similar, but not identical, preg is in Etemplate\Widget\Url PHP class!
// Adjust trailing slash - required or forbidden * We can not use "(?<![.\s])", used to check that name-part does not end in
if(e.data.options.trailing_slash === true && value[value.length-1] !== '/' ) * a dot or white-space. The expression is valid in recent Chrome, but fails
{ * eg. in Safari 11.0 or node.js 4.8.3 and therefore grunt uglify!
e.data.set_value(value+'/'); * Server-side will fail in that case because it uses the full regexp.
} */
else if (e.data.options.trailing_slash === false && value[value.length-1] === '/' ) et2_url.EMAIL_PREG = new RegExp(/^(([^\042',<][^,<]+|\042[^\042]+\042|\'[^\']+\'|"(?:[^"\\]|\\.)*")\s?<)?[^\x00-\x20()\xe2\x80\x8b<>@,;:\042\[\]\x80-\xff]+@([a-z0-9ÄÖÜäöüß](|[a-z0-9ÄÖÜäöüß_-]*[a-z0-9ÄÖÜäöüß])\.)+[a-z]{2,}>?$/i);
{ return et2_url;
e.data.set_value(value.substr(0,value.length-1)); }(et2_widget_textbox_1.et2_textbox));
} et2_core_widget_1.et2_register_widget(et2_url, ["url", "url-email", "url-phone"]);
break;
case "url-email":
if(!e.data.EMAIL_PREG.test(value) ||
// If they use Text <email>, make sure the <> match
(value.indexOf("<") > 0 && value.indexOf(">") != value.length-1) ||
(value.indexOf(">") > 0 && value.indexOf("<") < 0)
)
{
e.data.showMessage("Invalid email","validation_error",true);
}
}
},
attachToDOM: function()
{
this._super.apply(this, arguments);
if (this.input[0].parentNode) jQuery(this.input[0].parentNode).addClass('et2_url_span');
}
});}).call(this);
et2_register_widget(et2_url, ["url", "url-email", "url-phone"]);
/** /**
* et2_url_ro is the readonly implementation of the url, email & phone. * et2_url_ro is the readonly implementation of the url, email & phone.
* It renders things as links, when possible * It renders things as links, when possible
* *
* @augments et2_valueWidget * @augments et2_valueWidget
*/ */
var et2_url_ro = (function(){ "use strict"; return et2_valueWidget.extend([et2_IDetachedDOM], var et2_url_ro = /** @class */ (function (_super_1) {
{ __extends(et2_url_ro, _super_1);
attributes: { /**
"contact_plus": { * Constructor
"name": "Add contact button", *
"type": "boolean", * @memberOf et2_url_ro
"default": false, */
"description": "Allow to add email as contact to addressbook" function et2_url_ro(_parent, _attrs, _child) {
}, var _this =
"full_email": { // Call the inherited constructor
"name": "Show full email address", _super_1.call(this, _parent, _attrs, et2_core_inheritance_1.ClassWithAttributes.extendAttributes(et2_url_ro._attributes, _child || {})) || this;
"type": "boolean", _this.value = "";
"default": true, _this.span = null;
"description": "Allow to show full email address if ture otherwise show only name" _this.value = "";
} _this.span = jQuery(document.createElement("a"))
}, .addClass("et2_textbox readonly");
// Do not a tag if no call_link is set and not in mobile, empty a tag may conflict
/** // with some browser telephony addons (eg. telify in FF)
* Constructor if (!egw.config('call_link') && _this.getType() == 'url-phone' && !egwIsMobile()) {
* _this.span = jQuery(document.createElement("span"))
* @memberOf et2_url_ro .addClass("et2_textbox readonly");
*/ }
init: function() { if (_this.getType() == 'url-email') {
this._super.apply(this, arguments); _this.span.addClass('et2_email');
}
this.value = ""; _this.setDOMNode(_this.span[0]);
this.span = jQuery(document.createElement("a")) return _this;
.addClass("et2_textbox readonly"); }
// Do not a tag if no call_link is set and not in mobile, empty a tag may conflict et2_url_ro.prototype.set_value = function (_value) {
// with some browser telephony addons (eg. telify in FF) this.value = _value;
if (!egw.config('call_link') && this._type == 'url-phone' && !egwIsMobile()){ var link = et2_url.prototype.get_link(this.getType(), _value);
this.span = jQuery(document.createElement("span")) if (!link) {
.addClass("et2_textbox readonly"); this.span.text(_value);
} this.span.removeAttr("href");
if(this._type == 'url-email') return;
{ }
this.span.addClass('et2_email'); this.span.text(_value);
} switch (this.getType()) {
this.setDOMNode(this.span[0]); case "url":
}, this.span.attr("href", link).attr("target", "_blank");
break;
set_value: function(_value) { case "url-phone":
this.value = _value; if (typeof link == 'function') {
this.span.off('click.et2_url');
var link = et2_url.prototype.get_link(this._type, _value); this.span.on('click.et2_url', link);
this.span.attr("href", "#");
if(!link) }
{ else if (link) {
this.span.text(_value); this.span.attr("href", link);
this.span.removeAttr("href"); }
return; break;
} case "url-email":
this.span.text(_value); if (typeof link == 'function') {
switch(this._type) { this.span.off('click.et2_url');
case "url": this.span.on('click.et2_url', link);
this.span.attr("href", link).attr("target", "_blank"); this.span.removeAttr("href");
break; }
case "url-phone": else {
if(typeof link == 'function') this.span.attr("href", link);
{ if (!this.span.attr("target")) {
this.span.off('click.et2_url'); this.span.attr("target", "_blank");
this.span.on('click.et2_url', link); }
this.span.attr("href", "#"); }
} // wrap email address if there's a name
else if (link) if (this.span.text() && this.span.text().split("<") && this.options.full_email) {
{ var val = this.span.text().split("<");
this.span.attr("href", link); val = val[0] != "" ? val[0] : val[2];
} // need to preserve the original value somehow
break; // as it's been used for add contact plus feature
case "url-email": this.span.attr('title', _value);
if(typeof link == 'function') this.span.text(val.replace(/"/g, ''));
{ this.span.append("<span class='noemail'>" +
this.span.off('click.et2_url'); _value.replace(val, '')
this.span.on('click.et2_url', link); .replace(/</g, '&lt;')
this.span.removeAttr("href"); .replace(/>/g, '&gt;')
} + "</span>");
else }
{ // Add contact_plus button
this.span.attr("href", link); if (this.options.contact_plus) {
if(!this.span.attr("target")) // If user doesn't have access to addressbook, stop
{ if (!egw.app('addressbook'))
this.span.attr("target", "_blank"); return;
} // Bind onmouseenter event on <a> tag in order to add contact plus
} // Need to keep span & value so it works inside nextmatch
// wrap email address if there's a name this.span.on('mouseenter', jQuery.proxy(function (event) {
if (this.span.text() && this.span.text().split("<") && this.options.full_email) event.stopImmediatePropagation();
{ this.widget._add_contact_tooltip.call(this, et2_url_ro.email_cache[this.value]);
var val = this.span.text().split("<"); if (typeof et2_url_ro.email_cache[this.value] === 'undefined') {
val = val[0] != ""? val[0]: val[2]; // Ask server if we know this email
this.widget.egw().jsonq('EGroupware\\Api\\Etemplate\\Widget\\Url::ajax_contact', this.value, this.widget._add_contact_tooltip, this);
// need to preserve the original value somehow }
// as it's been used for add contact plus feature }, { widget: this, span: this.span, value: this.value }));
this.span.attr('title',_value); }
break;
this.span.text(val.replace(/"/g,'')); }
this.span.append("<span class='noemail'>"+ };
_value.replace(val,'') /**
.replace(/</g, '&lt;') * Add a button to add the email address as a contact
.replace(/>/g, '&gt;') *
+"</span>"); * @param {boolean} email_exists True, or else nothing happens
*/
} et2_url_ro.prototype._add_contact_tooltip = function (email_exists) {
var value = this.value || this.widget.value || null;
// Add contact_plus button et2_url_ro.email_cache[value] = email_exists;
if (this.options.contact_plus) // Close all the others
{ jQuery('.et2_email').each(function () {
// If user doesn't have access to addressbook, stop if (jQuery(this).tooltip('instance')) {
if(!egw.app('addressbook')) return; jQuery(this).tooltip('close');
}
// Bind onmouseenter event on <a> tag in order to add contact plus });
// Need to keep span & value so it works inside nextmatch if (email_exists || !this.span.is(':hover'))
this.span.on ('mouseenter', jQuery.proxy(function (event) { return;
event.stopImmediatePropagation(); this.span.tooltip({
this.widget._add_contact_tooltip.call(this, et2_url_ro.email_cache[this.value]); items: 'a.et2_email',
if(typeof et2_url_ro.email_cache[this.value] === 'undefined') position: { my: "right top", at: "left top", collision: "flipfit" },
{ tooltipClass: "et2_email_popup",
// Ask server if we know this email content: function () {
this.widget.egw().jsonq('EGroupware\\Api\\Etemplate\\Widget\\Url::ajax_contact', // Here we could do all sorts of things
this.value, this.widget._add_contact_tooltip, this var extra = {
); 'presets[email]': jQuery(this).attr('title') ? jQuery(this).attr('title') : jQuery(this).text()
} };
},{widget: this, span: this.span, value: this.value})); return jQuery('<a href="#" class= "et2_url_email_contactPlus" title="' + egw.lang('Add a new contact') + '"><img src="'
} + (typeof email_exists === 'undefined' ? egw.image("loading") : egw.image("new")) + '"/></a>')
break; .on('click', function () {
} egw.open('', 'addressbook', 'add', extra);
}, });
},
/** close: function (event, ui) {
* Add a button to add the email address as a contact ui.tooltip.hover(function () {
* jQuery(this).stop(true).fadeTo(400, 1);
* @param {boolean} email_exists True, or else nothing happens //.fadeIn("slow"); // doesn't work because of stop()
*/ }, function () {
_add_contact_tooltip: function(email_exists) jQuery(this).fadeOut("400", function () { jQuery(this).remove(); });
{ });
var value = this.value || this.widget.value || null; }
et2_url_ro.email_cache[value] = email_exists; })
.tooltip("open");
// Close all the others jQuery('.egwGridView_scrollarea').one('scroll', jQuery.proxy(function () {
jQuery('.et2_email').each(function() { if (this.tooltip("instance")) {
if(jQuery(this).tooltip('instance')) { this.tooltip('destroy');
jQuery(this).tooltip('close'); }
} }, this.span));
}); };
/**
if(email_exists || !this.span.is(':hover')) return; * Code for implementing et2_IDetachedDOM
*
this.span.tooltip({ * @param {array} _attrs array to add further attributes to
items: 'a.et2_email', */
position: {my:"right top", at:"left top", collision:"flipfit"}, et2_url_ro.prototype.getDetachedAttributes = function (_attrs) {
tooltipClass: "et2_email_popup", _attrs.push("value", "class", "statustext");
content: function() };
{ et2_url_ro.prototype.getDetachedNodes = function () {
// Here we could do all sorts of things return [this.span[0]];
var extra = { };
'presets[email]': jQuery(this).attr('title') ? jQuery(this).attr('title') : jQuery(this).text() et2_url_ro.prototype.setDetachedAttributes = function (_nodes, _values) {
}; // Update the properties
this.span = jQuery(_nodes[0]);
return jQuery('<a href="#" class= "et2_url_email_contactPlus" title="'+egw.lang('Add a new contact')+'"><img src="' if (typeof _values["value"] != "undefined") {
+ (typeof email_exists === 'undefined' ? egw.image("loading") : egw.image("new")) +'"/></a>') this.set_value(_values["value"]);
.on('click', function() { }
egw.open('','addressbook','add',extra); if (typeof _values["class"] != "undefined") {
}); _nodes[0].setAttribute("class", _values["class"]);
}, }
close: function( event, ui ) // Set to original status text if not set for this row
{ this.span.attr('title', _values.statustext ? _values.statustext : this.options.statustext);
ui.tooltip.hover( };
function () { et2_url_ro._attributes = {
jQuery(this).stop(true).fadeTo(400, 1); "contact_plus": {
//.fadeIn("slow"); // doesn't work because of stop() "name": "Add contact button",
}, "type": "boolean",
function () { "default": false,
jQuery(this).fadeOut("400", function(){ jQuery(this).remove();}); "description": "Allow to add email as contact to addressbook"
} },
); "full_email": {
} "name": "Show full email address",
}) "type": "boolean",
.tooltip("open"); "default": true,
"description": "Allow to show full email address if ture otherwise show only name"
jQuery('.egwGridView_scrollarea').one('scroll', jQuery.proxy(function() { }
if(this.tooltip("instance")) };
{ et2_url_ro.email_cache = [];
this.tooltip('destroy'); return et2_url_ro;
} }(et2_core_valueWidget_1.et2_valueWidget));
}, this.span)); et2_core_widget_1.et2_register_widget(et2_url_ro, ["url_ro", "url-email_ro", "url-phone_ro"]);
//# sourceMappingURL=et2_widget_url.js.map
},
/**
* Code for implementing et2_IDetachedDOM
*
* @param {array} _attrs array to add further attributes to
*/
getDetachedAttributes: function(_attrs)
{
_attrs.push("value", "class", "statustext");
},
getDetachedNodes: function()
{
return [this.span[0]];
},
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")
{
_nodes[0].setAttribute("class", _values["class"]);
}
// Set to original status text if not set for this row
this.span.attr('title',_values.statustext ? _values.statustext : this.options.statustext);
}
});}).call(this);
et2_url_ro.email_cache = [];
et2_register_widget(et2_url_ro, ["url_ro", "url-email_ro", "url-phone_ro"]);

View File

@ -0,0 +1,509 @@
/**
* EGroupware eTemplate2 - JS URL object
*
* @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$
*/
/*egw:uses
et2_textbox;
et2_valueWidget;
/api/js/jquery/jquery.base64.js;
*/
import {et2_register_widget, WidgetConfig} from "./et2_core_widget";
import {et2_textbox} from "./et2_widget_textbox";
import {ClassWithAttributes} from "./et2_core_inheritance";
import {et2_valueWidget} from "./et2_core_valueWidget";
/**
* Class which implements the "url" XET-Tag, which covers URLs, email & phone
*
* @augments et2_textbox
*/
class et2_url extends et2_textbox
{
static readonly _attributes : any = {
"multiline": {
"ignore": true
},
"allow_path": {
type: "boolean",
name: "Allow path",
description: "Allow a path instead of a URL, path must start with /",
default: false
},
"trailing_slash": {
type: "boolean",
name: "Trailing slash",
description: "Require (or forbid) that the path ends with a /",
default: et2_no_init
}
};
/**
* 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!
* We forbid all non-ascii chars in local part, as Horde does not yet support SMTPUTF8 extension (rfc6531)
* and we get a "SMTP server does not support internationalized header data" error otherwise.
*
* Using \042 instead of " to NOT stall minifyer!
*
* Similar, but not identical, preg is in Etemplate\Widget\Url PHP class!
* We can not use "(?<![.\s])", used to check that name-part does not end in
* a dot or white-space. The expression is valid in recent Chrome, but fails
* eg. in Safari 11.0 or node.js 4.8.3 and therefore grunt uglify!
* Server-side will fail in that case because it uses the full regexp.
*/
static EMAIL_PREG : RegExp = new RegExp(/^(([^\042',<][^,<]+|\042[^\042]+\042|\'[^\']+\'|"(?:[^"\\]|\\.)*")\s?<)?[^\x00-\x20()\xe2\x80\x8b<>@,;:\042\[\]\x80-\xff]+@([a-z0-9ÄÖÜäöüß](|[a-z0-9ÄÖÜäöüß_-]*[a-z0-9ÄÖÜäöüß])\.)+[a-z]{2,}>?$/i);
private _button : JQuery = null;
value: string = "";
/**
* @memberOf et2_url
*/
createInputWidget() {
this.input = jQuery(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() {
if(this.input) {
this.input.unbind();
}
this._button = null;
super.destroy();
}
/**
* Override parent to update href of 'button'
*
* @param _value value to set
*/
set_value(_value: any) {
this.update_button(_value);
super.set_value(_value);
}
update_button(_value) {
if(this.value == _value) return;
if(_value)
{
// Create button if it doesn't exist yet
if(this._button == null)
{
this._button = jQuery(document.createElement("a")).addClass("et2_url");
this.getSurroundings().insertDOMNode(this._button[0]);
this.getSurroundings().update();
}
this._button.removeClass("url phone email").removeAttr("href");
_value = this.get_link(this.getType(), _value);
switch(this.getType())
{
case "url":
// Silently use http if no protocol
this._button.attr("href", _value).attr("target", "_blank").addClass("url");
break;
case "url-phone":
if(_value) {
if(typeof _value == 'function')
{
this._button.click(this, _value).addClass("phone").show();
}
else
{
this._button.attr("href", _value).addClass("phone").show();
}
} else if (_value === false) {
// Can't make a good handler, hide button
this._button.hide();
}
break;
case "url-email":
if(typeof _value == 'function')
{
this._button.click(this, _value).addClass("email");
}
else
{
this._button.attr("href", _value).addClass("email");
}
break;
}
}
else
{
if(this._button) this._button.hide();
if(this._button && this.getSurroundings && this.getSurroundings().removeDOMNode)
{
this.getSurroundings().removeDOMNode(this._button[0]);
}
this._button = null;
}
}
get_link(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('&#9829;','').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);
// remove everything but numbers and plus, as telephon software might not like it
value = value.replace(/[^0-9+]/g, '');
// 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)
{
value = "tel:"+value;
}
else if (this.egw().config("call_link"))
{
var link = this.egw().config("call_link")
// tel: links use no URL encoding according to rfc3966 section-5.1.4
.replace("%1", this.egw().config("call_link").substr(0, 4) == 'tel:' ?
value : 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); };
}
else {
// Can't make a good handler
return false;
}
break;
case "url-email":
if(value.indexOf("mailto:") == -1)
{
value = "mailto:"+value;
}
if((this.egw().user('apps').mail || this.egw().user('apps').felamimail) &&
this.egw().preference('force_mailto','addressbook') != '1' )
{
return function() {egw.open_link(value);};
}
break;
}
return value;
}
validate(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.options.allow_path) {
e.data.set_value("http://"+value);
e.data.showMessage(e.data.egw().lang("Protocol is required"), "hint", true);
}
else if (e.data.options.allow_path && value[0] !== '/')
{
e.data.showMessage(e.data.egw().lang("Path must start with '/'"), "hint", true);
}
// Adjust trailing slash - required or forbidden
if(e.data.options.trailing_slash === true && value[value.length-1] !== '/' )
{
e.data.set_value(value+'/');
}
else if (e.data.options.trailing_slash === false && value[value.length-1] === '/' )
{
e.data.set_value(value.substr(0,value.length-1));
}
break;
case "url-email":
if(!e.data.EMAIL_PREG.test(value) ||
// If they use Text <email>, make sure the <> match
(value.indexOf("<") > 0 && value.indexOf(">") != value.length-1) ||
(value.indexOf(">") > 0 && value.indexOf("<") < 0)
)
{
e.data.showMessage("Invalid email","validation_error",true);
}
}
}
attachToDOM() : boolean
{
let res = super.attachToDOM();
if (this.input[0].parentNode) jQuery(this.input[0].parentNode).addClass('et2_url_span');
return res;
}
}
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
*
* @augments et2_valueWidget
*/
class et2_url_ro extends et2_valueWidget
{
static readonly _attributes : any = {
"contact_plus": {
"name": "Add contact button",
"type": "boolean",
"default": false,
"description": "Allow to add email as contact to addressbook"
},
"full_email": {
"name": "Show full email address",
"type": "boolean",
"default": true,
"description": "Allow to show full email address if ture otherwise show only name"
}
};
value : string = "";
span : JQuery = null;
static email_cache : string[] = [];
/**
* Constructor
*
* @memberOf et2_url_ro
*/
constructor(_parent, _attrs? : WidgetConfig, _child? : object)
{
// Call the inherited constructor
super(_parent, _attrs, ClassWithAttributes.extendAttributes(et2_url_ro._attributes, _child || {}));
this.value = "";
this.span = jQuery(document.createElement("a"))
.addClass("et2_textbox readonly");
// Do not a tag if no call_link is set and not in mobile, empty a tag may conflict
// with some browser telephony addons (eg. telify in FF)
if (!egw.config('call_link') && this.getType() == 'url-phone' && !egwIsMobile()) {
this.span = jQuery(document.createElement("span"))
.addClass("et2_textbox readonly");
}
if(this.getType() == 'url-email')
{
this.span.addClass('et2_email');
}
this.setDOMNode(this.span[0]);
}
set_value(_value)
{
this.value = _value;
let link = et2_url.prototype.get_link(this.getType(), _value);
if(!link)
{
this.span.text(_value);
this.span.removeAttr("href");
return;
}
this.span.text(_value);
switch(this.getType())
{
case "url":
this.span.attr("href", link).attr("target", "_blank");
break;
case "url-phone":
if(typeof link == 'function')
{
this.span.off('click.et2_url');
this.span.on('click.et2_url', link);
this.span.attr("href", "#");
}
else if (link)
{
this.span.attr("href", link);
}
break;
case "url-email":
if(typeof link == 'function')
{
this.span.off('click.et2_url');
this.span.on('click.et2_url', link);
this.span.removeAttr("href");
}
else
{
this.span.attr("href", link);
if(!this.span.attr("target"))
{
this.span.attr("target", "_blank");
}
}
// wrap email address if there's a name
if (this.span.text() && this.span.text().split("<") && this.options.full_email)
{
let val : any = this.span.text().split("<");
val = val[0] != ""? val[0]: val[2];
// need to preserve the original value somehow
// as it's been used for add contact plus feature
this.span.attr('title',_value);
this.span.text(val.replace(/"/g,''));
this.span.append("<span class='noemail'>"+
_value.replace(val,'')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
+"</span>");
}
// Add contact_plus button
if (this.options.contact_plus)
{
// If user doesn't have access to addressbook, stop
if(!egw.app('addressbook')) return;
// Bind onmouseenter event on <a> tag in order to add contact plus
// Need to keep span & value so it works inside nextmatch
this.span.on ('mouseenter', jQuery.proxy(function (event) {
event.stopImmediatePropagation();
this.widget._add_contact_tooltip.call(this, et2_url_ro.email_cache[this.value]);
if(typeof et2_url_ro.email_cache[this.value] === 'undefined')
{
// Ask server if we know this email
this.widget.egw().jsonq('EGroupware\\Api\\Etemplate\\Widget\\Url::ajax_contact',
this.value, this.widget._add_contact_tooltip, this
);
}
},{widget: this, span: this.span, value: this.value}));
}
break;
}
}
/**
* Add a button to add the email address as a contact
*
* @param {boolean} email_exists True, or else nothing happens
*/
private _add_contact_tooltip(email_exists)
{
var value = this.value || this.widget.value || null;
et2_url_ro.email_cache[value] = email_exists;
// Close all the others
jQuery('.et2_email').each(function() {
if(jQuery(this).tooltip('instance')) {
jQuery(this).tooltip('close');
}
});
if(email_exists || !this.span.is(':hover')) return;
this.span.tooltip({
items: 'a.et2_email',
position: {my:"right top", at:"left top", collision:"flipfit"},
tooltipClass: "et2_email_popup",
content()
{
// Here we could do all sorts of things
var extra = {
'presets[email]': jQuery(this).attr('title') ? jQuery(this).attr('title') : jQuery(this).text()
};
return jQuery('<a href="#" class= "et2_url_email_contactPlus" title="'+egw.lang('Add a new contact')+'"><img src="'
+ (typeof email_exists === 'undefined' ? egw.image("loading") : egw.image("new")) +'"/></a>')
.on('click', function() {
egw.open('','addressbook','add',extra);
});
},
close( event, ui )
{
ui.tooltip.hover(
function () {
jQuery(this).stop(true).fadeTo(400, 1);
//.fadeIn("slow"); // doesn't work because of stop()
},
function () {
jQuery(this).fadeOut("400", function(){ jQuery(this).remove();});
}
);
}
})
.tooltip("open");
jQuery('.egwGridView_scrollarea').one('scroll', jQuery.proxy(function() {
if(this.tooltip("instance"))
{
this.tooltip('destroy');
}
}, this.span));
}
/**
* Code for implementing et2_IDetachedDOM
*
* @param {array} _attrs array to add further attributes to
*/
getDetachedAttributes(_attrs)
{
_attrs.push("value", "class", "statustext");
}
getDetachedNodes()
{
return [this.span[0]];
}
setDetachedAttributes(_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")
{
_nodes[0].setAttribute("class", _values["class"]);
}
// Set to original status text if not set for this row
this.span.attr('title',_values.statustext ? _values.statustext : this.options.statustext);
}
}
et2_register_widget(et2_url_ro, ["url_ro", "url-email_ro", "url-phone_ro"]);