2022-06-10 18:11:34 +02:00
|
|
|
/**
|
|
|
|
* EGroupware eTemplate2 - Email-selection WebComponent
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package api
|
|
|
|
* @link https://www.egroupware.org
|
|
|
|
* @author Nathan Gray
|
|
|
|
*/
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
import {Et2Select} from "../Et2Select";
|
|
|
|
import {css, html, nothing, PropertyValues} from "lit";
|
|
|
|
import {IsEmail} from "../../Validators/IsEmail";
|
2022-06-21 16:21:23 +02:00
|
|
|
import interact from "@interactjs/interact";
|
2023-04-11 15:01:40 +02:00
|
|
|
import {Validator} from "@lion/form-core";
|
2023-09-13 19:55:33 +02:00
|
|
|
import {classMap} from "lit/directives/class-map.js";
|
2022-06-10 18:11:34 +02:00
|
|
|
|
2022-09-28 19:14:02 +02:00
|
|
|
/**
|
|
|
|
* Select email address(es)
|
|
|
|
*
|
|
|
|
* Allows free entries of valid email addresses, but also searches contacts.
|
|
|
|
*
|
|
|
|
* You should set multiple="true" for most cases for better UI
|
|
|
|
* @see Et2SelectEmail
|
|
|
|
*/
|
2022-06-10 18:11:34 +02:00
|
|
|
export class Et2SelectEmail extends Et2Select
|
|
|
|
{
|
|
|
|
static get styles()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
...super.styles,
|
|
|
|
css`
|
|
|
|
:host {
|
|
|
|
display: block;
|
|
|
|
flex: 1 1 auto;
|
|
|
|
min-width: 200px;
|
|
|
|
}
|
|
|
|
::slotted(sl-icon[slot="suffix"]) {
|
|
|
|
display: none;
|
|
|
|
}
|
2022-06-16 00:43:39 +02:00
|
|
|
|
|
|
|
/* Hide selected options from the dropdown */
|
|
|
|
::slotted([checked])
|
|
|
|
{
|
|
|
|
display: none;
|
|
|
|
}
|
2022-06-10 18:11:34 +02:00
|
|
|
`
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-06-21 16:21:23 +02:00
|
|
|
static get properties()
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
...super.properties,
|
|
|
|
|
|
|
|
/**
|
2022-07-19 21:18:51 +02:00
|
|
|
* Allow drag and drop tags between two or more Et2SelectEmail widgets
|
2022-06-21 16:21:23 +02:00
|
|
|
*/
|
2022-07-19 21:18:51 +02:00
|
|
|
allowDragAndDrop: {type: Boolean},
|
|
|
|
|
2023-04-11 15:01:40 +02:00
|
|
|
/**
|
|
|
|
* Allow placeholders like {{email}}, beside real email-addresses
|
|
|
|
*/
|
|
|
|
allowPlaceholder: {type: Boolean},
|
|
|
|
|
2022-09-12 14:50:52 +02:00
|
|
|
/**
|
|
|
|
* Include mailing lists: returns them with their integer list_id
|
|
|
|
*/
|
|
|
|
includeLists: {type: Boolean},
|
|
|
|
|
2023-04-11 18:19:17 +02:00
|
|
|
/**
|
|
|
|
* If the email is a contact, we normally show the contact name instead of the email.
|
|
|
|
* Set to true to turn this off and always show just the email
|
|
|
|
* Mutually exclusive with fullEmail!
|
|
|
|
*/
|
|
|
|
onlyEmail: {type: Boolean},
|
|
|
|
|
2022-07-19 21:18:51 +02:00
|
|
|
/**
|
|
|
|
* Show the full, original value email address under all circumstances, rather than the contact name for known contacts
|
|
|
|
*/
|
2022-07-21 17:57:50 +02:00
|
|
|
fullEmail: {type: Boolean}
|
2022-06-21 16:21:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-10 18:11:34 +02:00
|
|
|
constructor(...args : any[])
|
|
|
|
{
|
|
|
|
super(...args);
|
2023-04-17 17:28:27 +02:00
|
|
|
|
2023-04-18 16:51:21 +02:00
|
|
|
// Additional option for select email, per ticket #79694
|
|
|
|
this._close_on_select = this.egw().preference("select_multiple_close") != "open";
|
2023-04-17 17:28:27 +02:00
|
|
|
|
2022-06-10 18:11:34 +02:00
|
|
|
this.search = true;
|
|
|
|
this.searchUrl = "EGroupware\\Api\\Etemplate\\Widget\\Taglist::ajax_email";
|
|
|
|
this.allowFreeEntries = true;
|
2023-04-11 15:01:40 +02:00
|
|
|
this.allowPlaceholder = false;
|
2022-06-10 18:11:34 +02:00
|
|
|
this.editModeEnabled = true;
|
2022-06-21 16:21:23 +02:00
|
|
|
this.allowDragAndDrop = false;
|
2022-09-12 14:50:52 +02:00
|
|
|
this.includeLists = false;
|
2022-09-22 16:58:52 +02:00
|
|
|
this.multiple = false;
|
2022-07-21 17:57:50 +02:00
|
|
|
this.fullEmail = false;
|
2023-04-11 18:19:17 +02:00
|
|
|
this.onlyEmail = false;
|
2023-04-11 15:01:40 +02:00
|
|
|
this.defaultValidators.push(new IsEmail(this.allowPlaceholder));
|
|
|
|
}
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
|
2023-04-11 15:01:40 +02:00
|
|
|
/** @param {import('@lion/core').PropertyValues } changedProperties */
|
|
|
|
willUpdate(changedProperties : PropertyValues)
|
|
|
|
{
|
|
|
|
super.willUpdate(changedProperties);
|
|
|
|
|
|
|
|
if(changedProperties.has('allowPlaceholder'))
|
|
|
|
{
|
|
|
|
this.defaultValidators = (<Array<Validator>>this.defaultValidators).filter(v => !(v instanceof IsEmail));
|
|
|
|
this.defaultValidators.push(new IsEmail(this.allowPlaceholder));
|
|
|
|
}
|
2022-06-10 18:11:34 +02:00
|
|
|
}
|
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
updated(changedProperties : Map<string, any>)
|
2022-06-10 18:11:34 +02:00
|
|
|
{
|
2023-11-23 21:44:45 +01:00
|
|
|
super.updated(changedProperties);
|
2023-11-23 23:28:32 +01:00
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
// Make tags draggable
|
|
|
|
if(!this.readonly && this.allowFreeEntries && this.allowDragAndDrop)
|
2023-11-23 23:28:32 +01:00
|
|
|
{
|
|
|
|
this._makeDraggable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make the tags draggable
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected _makeDraggable()
|
|
|
|
{
|
|
|
|
this.select.updateComplete.then(() =>
|
2023-09-13 19:55:33 +02:00
|
|
|
{
|
|
|
|
let dragTranslate = {x: 0, y: 0};
|
2023-11-23 23:28:32 +01:00
|
|
|
const tags = Array.from(this.select.shadowRoot.querySelectorAll(".select__tags et2-email-tag"));
|
2023-09-13 19:55:33 +02:00
|
|
|
let draggable = interact(tags).draggable({
|
|
|
|
startAxis: 'xy',
|
|
|
|
listeners: {
|
|
|
|
start: function(e)
|
|
|
|
{
|
|
|
|
let dragPosition = {x: e.page.x, y: e.page.y};
|
|
|
|
dragTranslate = {x: 0, y: 0};
|
|
|
|
e.target.setAttribute('style', `width:${e.target.clientWidth}px !important`);
|
|
|
|
e.target.style.position = 'fixed';
|
|
|
|
e.target.style.zIndex = 10;
|
|
|
|
e.target.style.transform =
|
|
|
|
`translate(${dragPosition.x}px, ${dragPosition.y}px)`;
|
|
|
|
},
|
|
|
|
move: function(e)
|
|
|
|
{
|
|
|
|
dragTranslate.x += e.delta.x;
|
|
|
|
dragTranslate.y += e.delta.y;
|
|
|
|
e.target.style.transform =
|
|
|
|
`translate(${dragTranslate.x}px, ${dragTranslate.y}px)`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// set parent_node with widget context in order to make it accessible after drop
|
|
|
|
draggable.parent_node = this;
|
2023-11-23 23:28:32 +01:00
|
|
|
});
|
2022-06-10 18:11:34 +02:00
|
|
|
}
|
|
|
|
|
2022-06-22 22:36:38 +02:00
|
|
|
protected _bindListeners()
|
|
|
|
{
|
|
|
|
super._bindListeners();
|
|
|
|
if(!this.multiple)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
interact(this).dropzone({
|
|
|
|
accept: `.et2-select-draggable`,
|
|
|
|
ondrop: function(e)
|
|
|
|
{
|
2023-02-10 22:12:33 +01:00
|
|
|
// Add in as free entry
|
2022-06-22 22:36:38 +02:00
|
|
|
e.target.createFreeEntry(e.draggable.target.value);
|
2023-02-10 22:12:33 +01:00
|
|
|
e.target.classList.remove('et2_dropZone');
|
2022-06-22 22:36:38 +02:00
|
|
|
|
|
|
|
// remove the dragged value from its origin source
|
|
|
|
e.draggable.parent_node.value = e.draggable.parent_node.value.filter(_item => {return e.draggable.target.value !== _item;})
|
|
|
|
},
|
|
|
|
ondragenter: function(e)
|
|
|
|
{
|
|
|
|
e.target.classList.add('et2_dropZone');
|
|
|
|
},
|
|
|
|
ondragleave: function(e)
|
|
|
|
{
|
|
|
|
e.target.classList.remove('et2_dropZone');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2023-09-13 19:55:33 +02:00
|
|
|
|
2023-03-27 16:51:20 +02:00
|
|
|
/**
|
|
|
|
* Handle keypresses inside the search input
|
|
|
|
* Overridden from parent to also skip the hidden selected options, which other selects do not do
|
|
|
|
*
|
|
|
|
* @param {KeyboardEvent} event
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected _handleSearchKeyDown(event : KeyboardEvent)
|
|
|
|
{
|
|
|
|
// Pass off some keys to select
|
|
|
|
if(['ArrowDown', 'ArrowUp', 'Home', 'End'].includes(event.key))
|
|
|
|
{
|
|
|
|
// Strip out hidden non-matching selected so key navigation works
|
|
|
|
this.menuItems = this.menuItems.filter(i => !i.checked);
|
|
|
|
}
|
|
|
|
return super._handleSearchKeyDown(event);
|
|
|
|
}
|
|
|
|
|
2022-06-10 18:11:34 +02:00
|
|
|
/**
|
|
|
|
* Actually query the server.
|
|
|
|
*
|
|
|
|
* Overridden to change request to match server
|
|
|
|
*
|
|
|
|
* @param {string} search
|
|
|
|
* @param {object} options
|
|
|
|
* @returns {any}
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected remoteQuery(search : string, options : object)
|
|
|
|
{
|
2023-11-15 23:25:53 +01:00
|
|
|
return this.egw().request(this.searchUrl, [search, {includeLists: this.includeLists}]).then((results) =>
|
2022-06-10 18:11:34 +02:00
|
|
|
{
|
2023-11-15 23:25:53 +01:00
|
|
|
// If results have a total included, pull it out.
|
|
|
|
// It will cause errors if left in the results
|
|
|
|
if(typeof results.total !== "undefined")
|
|
|
|
{
|
|
|
|
this._total_result_count += results.total;
|
|
|
|
delete results.total;
|
|
|
|
// Make it an array, since it was probably an object, and cleanSelectOptions() treats objects differently
|
|
|
|
results = Object.values(results);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._total_result_count += results.length;
|
|
|
|
}
|
|
|
|
this._total_result_count -= this.processRemoteResults(results);
|
2022-06-10 18:11:34 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-24 20:10:10 +02:00
|
|
|
/**
|
|
|
|
* Use a custom tag for when multiple=true
|
|
|
|
*
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2023-09-13 19:55:33 +02:00
|
|
|
_tagTemplate(option, index)
|
2022-06-21 16:21:23 +02:00
|
|
|
{
|
2023-09-14 22:30:20 +02:00
|
|
|
const readonly = (this.readonly || option && typeof (option.disabled) != "undefined" && option.disabled);
|
|
|
|
const isEditable = this.editModeEnabled && !readonly;
|
2022-12-12 22:07:08 +01:00
|
|
|
|
2023-09-13 19:55:33 +02:00
|
|
|
return html`
|
|
|
|
<et2-email-tag
|
|
|
|
class=${classMap({
|
|
|
|
...option.classList,
|
|
|
|
"et2-select-draggable": !this.readonly && this.allowFreeEntries && this.allowDragAndDrop
|
|
|
|
})}
|
2023-09-25 14:35:18 +02:00
|
|
|
.fullEmail=${this.fullEmail}
|
|
|
|
.onlyEmail=${this.onlyEmail}
|
2023-09-14 22:30:20 +02:00
|
|
|
?removable=${!readonly}
|
|
|
|
?readonly=${readonly}
|
|
|
|
?editable=${isEditable}
|
|
|
|
.value=${option.value.replaceAll("___", " ")}
|
2023-09-13 19:55:33 +02:00
|
|
|
>
|
|
|
|
${option.getTextLabel().trim()}
|
|
|
|
</et2-email-tag>
|
|
|
|
`;
|
2022-06-21 16:21:23 +02:00
|
|
|
}
|
2022-09-19 17:23:23 +02:00
|
|
|
|
2023-01-27 21:26:50 +01:00
|
|
|
/**
|
|
|
|
* Override icon for the select option to use lavatar, same as Et2SelectAccount
|
|
|
|
*
|
|
|
|
* @param option
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected _iconTemplate(option)
|
|
|
|
{
|
|
|
|
// lavatar uses a size property, not a CSS variable
|
|
|
|
let style = getComputedStyle(this);
|
|
|
|
|
|
|
|
return html`
|
|
|
|
<et2-lavatar slot="prefix" part="icon" .size=${style.getPropertyValue("--icon-width")}
|
|
|
|
lname=${option.lname || nothing}
|
|
|
|
fname=${option.fname || nothing}
|
|
|
|
image=${option.icon || nothing}
|
|
|
|
>
|
|
|
|
</et2-lavatar>`;
|
|
|
|
}
|
|
|
|
|
2022-10-06 13:14:08 +02:00
|
|
|
/**
|
|
|
|
* Overwritten to NOT split RFC822 addresses containing a comma in quoted name part
|
|
|
|
*
|
|
|
|
* E.g. '"Becker, Ralf" <rb@egroupware.org>'
|
|
|
|
*
|
|
|
|
* @param val
|
|
|
|
*/
|
|
|
|
set_value(val : string | string[] | number | number[])
|
|
|
|
{
|
|
|
|
if(typeof val === 'string' && val.indexOf(',') !== -1)
|
|
|
|
{
|
|
|
|
val = val.split(',');
|
|
|
|
for(let n=0; n < val.length-1; n++)
|
|
|
|
{
|
|
|
|
while (val[n].indexOf('@') === -1 && n < val.length-1)
|
|
|
|
{
|
|
|
|
val[n] += ',' + val[n+1];
|
|
|
|
val.splice(n+1, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
super.set_value(val);
|
|
|
|
}
|
2023-01-31 17:47:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sometimes users paste multiple comma separated values at once. Split them then handle normally.
|
|
|
|
* Overridden here to handle email addresses that may have commas using the regex from the validator.
|
|
|
|
*
|
|
|
|
* @param {ClipboardEvent} event
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected _handlePaste(event : ClipboardEvent)
|
|
|
|
{
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
let paste = event.clipboardData.getData('text');
|
|
|
|
if(!paste)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const selection = window.getSelection();
|
|
|
|
if(selection.rangeCount)
|
|
|
|
{
|
|
|
|
selection.deleteFromDocument();
|
|
|
|
}
|
|
|
|
|
2023-10-26 18:09:44 +02:00
|
|
|
let preg = this.allowPlaceholder ? IsEmail.EMAIL_PLACEHOLDER_PREG : IsEmail.EMAIL_PREG;
|
2023-01-31 17:47:46 +01:00
|
|
|
// Trim line start / end anchors off validation regex, make global
|
2023-10-26 18:09:44 +02:00
|
|
|
let regex = new RegExp(preg.toString().substring(2, preg.toString().length - 3), 'g');
|
2023-01-31 17:47:46 +01:00
|
|
|
let values = paste.match(regex);
|
|
|
|
values.forEach(v =>
|
|
|
|
{
|
|
|
|
this.createFreeEntry(v.trim());
|
|
|
|
});
|
|
|
|
this.dropdown.hide();
|
|
|
|
}
|
2022-06-10 18:11:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// @ts-ignore TypeScript is not recognizing that this widget is a LitElement
|
|
|
|
customElements.define("et2-select-email", Et2SelectEmail);
|