2021-09-20 22:58:02 +02:00
|
|
|
/**
|
|
|
|
* EGroupware eTemplate2 - JS Placeholder widgets
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage api
|
|
|
|
* @link https://www.egroupware.org
|
|
|
|
* @author Nathan Gray
|
|
|
|
* @copyright Nathan Gray 2021
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*egw:uses
|
|
|
|
et2_core_inputWidget;
|
|
|
|
et2_core_valueWidget;
|
|
|
|
et2_widget_description;
|
|
|
|
*/
|
|
|
|
|
2022-03-23 18:01:36 +01:00
|
|
|
import {et2_register_widget, WidgetConfig} from "./et2_core_widget";
|
2021-09-20 22:58:02 +02:00
|
|
|
import {ClassWithAttributes} from "./et2_core_inheritance";
|
|
|
|
import {et2_inputWidget} from "./et2_core_inputWidget";
|
2022-03-23 18:01:36 +01:00
|
|
|
import {Et2Dialog} from "./Et2Dialog/Et2Dialog";
|
2022-08-04 00:05:50 +02:00
|
|
|
import {Et2LinkEntry} from "./Et2Link/Et2LinkEntry";
|
|
|
|
import {Et2Select} from "./Et2Select/Et2Select";
|
|
|
|
import {Et2Description} from "./Et2Description/Et2Description";
|
|
|
|
import {Et2Button} from "./Et2Button/Et2Button";
|
2021-09-20 22:58:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a dialog to choose a placeholder
|
|
|
|
*/
|
|
|
|
export class et2_placeholder_select extends et2_inputWidget
|
|
|
|
{
|
|
|
|
static readonly _attributes : any = {
|
|
|
|
insert_callback: {
|
|
|
|
"name": "Insert callback",
|
|
|
|
"description": "Method called with the selected placeholder text",
|
|
|
|
"type": "js"
|
2021-09-21 22:06:24 +02:00
|
|
|
},
|
|
|
|
dialog_title: {
|
|
|
|
"name": "Dialog title",
|
|
|
|
"type": "string",
|
|
|
|
"default": "Insert Placeholder"
|
2021-09-20 22:58:02 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static placeholders : Object | null = null;
|
|
|
|
|
|
|
|
button : JQuery;
|
|
|
|
submit_callback : any;
|
2022-03-23 18:01:36 +01:00
|
|
|
dialog : Et2Dialog;
|
2021-09-20 22:58:02 +02:00
|
|
|
protected value : any;
|
|
|
|
|
2021-09-21 18:19:24 +02:00
|
|
|
protected LIST_URL = 'EGroupware\\Api\\Etemplate\\Widget\\Placeholder::ajax_get_placeholders';
|
|
|
|
protected TEMPLATE = '/api/templates/default/insert_merge_placeholder.xet?1';
|
|
|
|
|
2021-09-20 22:58:02 +02:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param _parent
|
|
|
|
* @param _attrs
|
|
|
|
* @memberOf et2_vfsSelect
|
|
|
|
*/
|
|
|
|
constructor(_parent, _attrs? : WidgetConfig, _child? : object)
|
|
|
|
{
|
|
|
|
// Call the inherited constructor
|
|
|
|
super(_parent, _attrs, ClassWithAttributes.extendAttributes(et2_placeholder_select._attributes, _child || {}));
|
|
|
|
|
|
|
|
// Allow no child widgets
|
|
|
|
this.supportedWidgetClasses = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
_content(_content, _callback)
|
|
|
|
{
|
|
|
|
let self = this;
|
2022-03-30 23:11:15 +02:00
|
|
|
if(this.dialog)
|
2021-09-20 22:58:02 +02:00
|
|
|
{
|
2022-03-30 23:11:15 +02:00
|
|
|
this.dialog.close();
|
2021-09-20 22:58:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var callback = _callback || this._buildDialog;
|
|
|
|
if(et2_placeholder_select.placeholders === null)
|
|
|
|
{
|
|
|
|
this.egw().loading_prompt('placeholder_select', true, '', 'body');
|
|
|
|
this.egw().json(
|
2021-09-21 18:19:24 +02:00
|
|
|
this.LIST_URL,
|
2021-09-20 22:58:02 +02:00
|
|
|
[],
|
|
|
|
function(_content)
|
|
|
|
{
|
2021-09-27 22:46:41 +02:00
|
|
|
if(typeof _content === 'object' && _content.message)
|
|
|
|
{
|
|
|
|
// Something went wrong
|
|
|
|
this.egw().message(_content.message, 'error');
|
|
|
|
return;
|
|
|
|
}
|
2021-09-20 22:58:02 +02:00
|
|
|
this.egw().loading_prompt('placeholder_select', false);
|
|
|
|
et2_placeholder_select.placeholders = _content;
|
|
|
|
callback.apply(self, arguments);
|
|
|
|
}.bind(this)
|
|
|
|
).sendRequest(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._buildDialog(et2_placeholder_select.placeholders);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-09-21 18:19:24 +02:00
|
|
|
* Builds placeholder selection dialog
|
2021-09-20 22:58:02 +02:00
|
|
|
*
|
|
|
|
* @param {object} _data content
|
|
|
|
*/
|
2021-09-21 22:06:24 +02:00
|
|
|
protected _buildDialog(_data)
|
2021-09-20 22:58:02 +02:00
|
|
|
{
|
|
|
|
let buttons = [
|
|
|
|
{
|
2022-03-23 18:01:36 +01:00
|
|
|
label: this.egw().lang("Insert"),
|
2021-09-20 22:58:02 +02:00
|
|
|
id: "submit",
|
2021-09-21 00:28:20 +02:00
|
|
|
image: "export"
|
2021-09-20 22:58:02 +02:00
|
|
|
}
|
|
|
|
];
|
|
|
|
let extra_buttons_action = {};
|
|
|
|
|
|
|
|
if(this.options.extra_buttons && this.options.method)
|
|
|
|
{
|
|
|
|
for(let i = 0; i < this.options.extra_buttons.length; i++)
|
|
|
|
{
|
|
|
|
delete (this.options.extra_buttons[i]['click']);
|
|
|
|
buttons.push(this.options.extra_buttons[i]);
|
|
|
|
extra_buttons_action[this.options.extra_buttons[i]['id']] = this.options.extra_buttons[i]['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-03-23 18:01:36 +01:00
|
|
|
buttons.push({label: this.egw().lang("Cancel"), id: "cancel", image: "cancel"});
|
2021-09-20 22:58:02 +02:00
|
|
|
|
|
|
|
let data = {
|
2021-09-21 00:28:20 +02:00
|
|
|
content: {app: '', group: '', entry: {}},
|
|
|
|
sel_options: {app: [], group: []},
|
2021-10-04 22:45:57 +02:00
|
|
|
modifications: {
|
|
|
|
outer_box: {
|
|
|
|
entry: {
|
|
|
|
application_list: []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-20 22:58:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Object.keys(_data).map((key) =>
|
|
|
|
{
|
|
|
|
data.sel_options.app.push(
|
|
|
|
{
|
|
|
|
value: key,
|
|
|
|
label: this.egw().lang(key)
|
|
|
|
});
|
|
|
|
});
|
|
|
|
data.sel_options.group = this._get_group_options(Object.keys(_data)[0]);
|
|
|
|
data.content.app = data.sel_options.app[0].value;
|
2021-10-04 22:45:57 +02:00
|
|
|
data.content.group = data.sel_options.group[0]?.value;
|
2021-09-27 22:46:41 +02:00
|
|
|
data.content.entry = {app: data.content.app};
|
2021-09-21 00:28:20 +02:00
|
|
|
data.modifications.outer_box.entry.application_list = Object.keys(_data);
|
2021-10-01 21:34:27 +02:00
|
|
|
// Remove non-app placeholders (user & general)
|
|
|
|
let non_apps = ['user', 'general'];
|
|
|
|
for(let i = 0; i < non_apps.length; i++)
|
|
|
|
{
|
|
|
|
let index = data.modifications.outer_box.entry.application_list.indexOf(non_apps[i]);
|
|
|
|
data.modifications.outer_box.entry.application_list.splice(index, 1);
|
|
|
|
}
|
2021-09-20 22:58:02 +02:00
|
|
|
|
|
|
|
// callback for dialog
|
2021-09-21 00:28:20 +02:00
|
|
|
this.submit_callback = function(submit_button_id, submit_value)
|
2021-09-20 22:58:02 +02:00
|
|
|
{
|
|
|
|
if((submit_button_id == 'submit' || (extra_buttons_action && extra_buttons_action[submit_button_id])) && submit_value)
|
|
|
|
{
|
2021-09-21 22:06:24 +02:00
|
|
|
this._do_insert_callback(submit_value);
|
2021-09-20 22:58:02 +02:00
|
|
|
return true;
|
|
|
|
}
|
2022-10-05 18:50:54 +02:00
|
|
|
else if(submit_button_id == 'cancel')
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Keep dialog open
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-20 22:58:02 +02:00
|
|
|
}.bind(this);
|
|
|
|
|
2022-03-23 18:01:36 +01:00
|
|
|
this.dialog = new Et2Dialog(this.egw());
|
|
|
|
this.dialog.transformAttributes({
|
|
|
|
callback: this.submit_callback,
|
|
|
|
title: this.options.dialog_title || "Insert Placeholder",
|
|
|
|
buttons: buttons,
|
|
|
|
value: data,
|
|
|
|
template: this.egw().webserverUrl + this.TEMPLATE,
|
2022-12-02 17:39:25 +01:00
|
|
|
resizable: true,
|
|
|
|
width: ''
|
2022-03-23 18:01:36 +01:00
|
|
|
});
|
|
|
|
document.body.appendChild(<HTMLElement><unknown>this.dialog);
|
2022-03-30 23:11:15 +02:00
|
|
|
this.dialog.addEventListener('open', this._on_template_load.bind(this));
|
2021-09-21 18:19:24 +02:00
|
|
|
}
|
2021-09-20 22:58:02 +02:00
|
|
|
|
2021-09-21 18:19:24 +02:00
|
|
|
doLoadingFinished()
|
|
|
|
{
|
|
|
|
this._content.call(this, null);
|
|
|
|
return true;
|
|
|
|
}
|
2021-09-20 22:58:02 +02:00
|
|
|
|
2021-09-21 18:19:24 +02:00
|
|
|
/**
|
|
|
|
* Post-load of the dialog
|
|
|
|
* Bind internal events, set some things that are difficult to do in the template
|
|
|
|
*/
|
|
|
|
_on_template_load()
|
|
|
|
{
|
2022-08-04 00:05:50 +02:00
|
|
|
let app = <Et2Select><unknown>this.dialog.template.widgetContainer.getDOMWidgetById("app");
|
|
|
|
let group = <Et2Select><unknown>this.dialog.template.widgetContainer.getDOMWidgetById("group");
|
|
|
|
let placeholder_list = <Et2Select><unknown>this.dialog.template.widgetContainer.getDOMWidgetById("placeholder_list");
|
|
|
|
let preview = <Et2Description><unknown>this.dialog.template.widgetContainer.getDOMWidgetById("preview_placeholder");
|
|
|
|
let entry = <Et2LinkEntry><unknown>this.dialog.template.widgetContainer.getDOMWidgetById("entry");
|
2021-09-20 22:58:02 +02:00
|
|
|
|
2021-09-21 18:19:24 +02:00
|
|
|
placeholder_list.set_select_options(this._get_placeholders(app.get_value(), group.get_value()));
|
2021-09-20 22:58:02 +02:00
|
|
|
|
2021-09-21 18:19:24 +02:00
|
|
|
// Bind some handlers
|
|
|
|
app.onchange = (node, widget) =>
|
|
|
|
{
|
2021-10-01 21:34:27 +02:00
|
|
|
preview.set_value("");
|
2021-10-07 21:45:49 +02:00
|
|
|
if(['user', 'filemanager'].indexOf(widget.get_value()) >= 0)
|
2021-09-27 22:46:41 +02:00
|
|
|
{
|
2021-10-07 21:45:49 +02:00
|
|
|
// These ones don't let you select an entry for preview (they don't work)
|
2021-10-01 21:34:27 +02:00
|
|
|
entry.set_disabled(true);
|
|
|
|
entry.set_value({app: 'user', id: '', query: ''});
|
2021-09-27 22:46:41 +02:00
|
|
|
}
|
|
|
|
else if(widget.get_value() == 'general')
|
|
|
|
{
|
|
|
|
// Don't change entry app, leave it
|
2021-10-01 21:34:27 +02:00
|
|
|
entry.set_disabled(false);
|
2021-09-27 22:46:41 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-02-11 21:02:21 +01:00
|
|
|
// Load app translations
|
|
|
|
this.egw().langRequireApp(this.egw().window, widget.get_value());
|
2021-10-01 21:34:27 +02:00
|
|
|
entry.set_disabled(false);
|
2021-09-27 22:46:41 +02:00
|
|
|
entry.set_value({app: widget.get_value(), id: '', query: ''});
|
|
|
|
}
|
2021-10-01 21:34:27 +02:00
|
|
|
let groups = this._get_group_options(widget.get_value());
|
|
|
|
group.set_select_options(groups);
|
|
|
|
group.set_value(groups[0].value);
|
|
|
|
group.onchange();
|
2021-09-21 18:19:24 +02:00
|
|
|
}
|
|
|
|
group.onchange = (select_node, select_widget) =>
|
|
|
|
{
|
2021-09-27 22:46:41 +02:00
|
|
|
let options = this._get_placeholders(app.get_value(), group.get_value())
|
|
|
|
placeholder_list.set_select_options(options);
|
2021-09-21 18:19:24 +02:00
|
|
|
preview.set_value("");
|
2022-12-02 17:39:25 +01:00
|
|
|
placeholder_list.updateComplete.then(() => placeholder_list.set_value(options[0].value));
|
2021-09-21 18:19:24 +02:00
|
|
|
}
|
|
|
|
placeholder_list.onchange = this._on_placeholder_select.bind(this);
|
|
|
|
entry.onchange = this._on_placeholder_select.bind(this);
|
2022-08-04 00:05:50 +02:00
|
|
|
(<Et2Button><unknown>this.dialog.template.widgetContainer.getDOMWidgetById("insert_placeholder")).onclick = () =>
|
2021-09-21 18:19:24 +02:00
|
|
|
{
|
|
|
|
this.options.insert_callback(this.dialog.template.widgetContainer.getDOMWidgetById("preview_placeholder").getDOMNode().textContent);
|
|
|
|
};
|
2022-08-04 00:05:50 +02:00
|
|
|
(<Et2Button><unknown>this.dialog.template.widgetContainer.getDOMWidgetById("insert_content")).onclick = () =>
|
2021-09-21 18:19:24 +02:00
|
|
|
{
|
|
|
|
this.options.insert_callback(this.dialog.template.widgetContainer.getDOMWidgetById("preview_content").getDOMNode().textContent);
|
|
|
|
};
|
2021-09-20 22:58:02 +02:00
|
|
|
|
2021-09-27 22:46:41 +02:00
|
|
|
app.set_value(app.get_value());
|
2021-09-20 22:58:02 +02:00
|
|
|
}
|
|
|
|
|
2021-09-21 18:19:24 +02:00
|
|
|
/**
|
|
|
|
* User has selected a placeholder
|
|
|
|
* Update the UI, and if they have an entry selected do the replacement and show that.
|
|
|
|
*/
|
|
|
|
_on_placeholder_select()
|
2021-09-20 22:58:02 +02:00
|
|
|
{
|
2022-08-04 00:05:50 +02:00
|
|
|
let app = <Et2LinkEntry><unknown>this.dialog.template.widgetContainer.getDOMWidgetById("app");
|
|
|
|
let entry = <Et2LinkEntry><unknown>this.dialog.template.widgetContainer.getDOMWidgetById("entry");
|
|
|
|
let placeholder_list = <Et2Select><unknown>this.dialog.template.widgetContainer.getDOMWidgetById("placeholder_list");
|
|
|
|
let preview = <Et2Description><unknown>this.dialog.template.widgetContainer.getDOMWidgetById("preview_placeholder");
|
|
|
|
let preview_content = <Et2Description><unknown>this.dialog.template.widgetContainer.getDOMWidgetById("preview_content");
|
2021-09-21 00:28:20 +02:00
|
|
|
|
|
|
|
// Show the selected placeholder
|
2021-09-21 22:06:24 +02:00
|
|
|
this.set_value(placeholder_list.get_value());
|
2021-09-20 22:58:02 +02:00
|
|
|
preview.set_value(placeholder_list.get_value());
|
2022-12-02 17:39:25 +01:00
|
|
|
preview.getDOMNode().parentNode.style.visibility = placeholder_list.get_value()?.trim() ? null : 'hidden';
|
2021-09-21 00:28:20 +02:00
|
|
|
|
|
|
|
if(placeholder_list.get_value() && entry.get_value())
|
2021-09-20 22:58:02 +02:00
|
|
|
{
|
|
|
|
// Show the selected placeholder replaced with value from the selected entry
|
|
|
|
this.egw().json(
|
|
|
|
'EGroupware\\Api\\Etemplate\\Widget\\Placeholder::ajax_fill_placeholders',
|
2021-09-27 22:46:41 +02:00
|
|
|
[placeholder_list.get_value(), entry.get_value()],
|
2021-09-20 22:58:02 +02:00
|
|
|
function(_content)
|
|
|
|
{
|
2021-09-27 22:46:41 +02:00
|
|
|
if(!_content)
|
|
|
|
{
|
|
|
|
_content = '';
|
|
|
|
}
|
2021-09-20 22:58:02 +02:00
|
|
|
preview_content.set_value(_content);
|
|
|
|
preview_content.getDOMNode().parentNode.style.visibility = _content.trim() ? null : 'hidden';
|
|
|
|
}.bind(this)
|
|
|
|
).sendRequest(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-09-21 00:28:20 +02:00
|
|
|
// No value, hide the row
|
2021-09-20 22:58:02 +02:00
|
|
|
preview_content.getDOMNode().parentNode.style.visibility = 'hidden';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-21 18:19:24 +02:00
|
|
|
/**
|
|
|
|
* Get the list of placeholder groups under the selected application
|
|
|
|
* @param appname
|
|
|
|
* @returns {value:string, label:string}[]
|
|
|
|
*/
|
|
|
|
_get_group_options(appname : string)
|
2021-09-20 22:58:02 +02:00
|
|
|
{
|
|
|
|
let options = [];
|
|
|
|
Object.keys(et2_placeholder_select.placeholders[appname]).map((key) =>
|
|
|
|
{
|
2021-10-04 22:45:57 +02:00
|
|
|
// @ts-ignore
|
|
|
|
if(Object.keys(et2_placeholder_select.placeholders[appname][key]).filter((key) => isNaN(key)).length > 0)
|
|
|
|
{
|
|
|
|
// Handle groups of groups
|
|
|
|
if(typeof et2_placeholder_select.placeholders[appname][key].label !== "undefined")
|
2021-09-20 22:58:02 +02:00
|
|
|
{
|
2021-10-04 22:45:57 +02:00
|
|
|
options[key] = et2_placeholder_select.placeholders[appname][key];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
options[this.egw().lang(key)] = [];
|
|
|
|
for(let sub of Object.keys(et2_placeholder_select.placeholders[appname][key]))
|
|
|
|
{
|
|
|
|
if(!et2_placeholder_select.placeholders[appname][key][sub])
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2021-10-07 22:51:16 +02:00
|
|
|
options[this.egw().lang(key)].push({
|
2021-10-04 22:45:57 +02:00
|
|
|
value: key + '-' + sub,
|
|
|
|
label: this.egw().lang(sub)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
options.push({
|
2021-09-20 22:58:02 +02:00
|
|
|
value: key,
|
|
|
|
label: this.egw().lang(key)
|
|
|
|
});
|
2021-10-04 22:45:57 +02:00
|
|
|
}
|
2021-09-20 22:58:02 +02:00
|
|
|
});
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2021-09-21 18:19:24 +02:00
|
|
|
/**
|
|
|
|
* Get a list of placeholders under the given application + group
|
|
|
|
*
|
|
|
|
* @param appname
|
|
|
|
* @param group
|
|
|
|
* @returns {value:string, label:string}[]
|
|
|
|
*/
|
|
|
|
_get_placeholders(appname : string, group : string)
|
2021-09-20 22:58:02 +02:00
|
|
|
{
|
2021-10-04 22:45:57 +02:00
|
|
|
let _group = group.split('-', 2);
|
|
|
|
let ph = et2_placeholder_select.placeholders[appname];
|
|
|
|
for(let i = 0; typeof ph !== "undefined" && i < _group.length; i++)
|
2021-09-20 22:58:02 +02:00
|
|
|
{
|
2021-10-04 22:45:57 +02:00
|
|
|
ph = ph[_group[i]];
|
|
|
|
}
|
|
|
|
return ph || [];
|
2021-09-20 22:58:02 +02:00
|
|
|
}
|
|
|
|
|
2021-09-21 22:06:24 +02:00
|
|
|
/**
|
|
|
|
* Get the correct insert text call the insert callback with it
|
|
|
|
*
|
|
|
|
* @param dialog_values
|
|
|
|
*/
|
|
|
|
_do_insert_callback(dialog_values : Object)
|
|
|
|
{
|
|
|
|
this.options.insert_callback(this.get_value());
|
|
|
|
}
|
|
|
|
|
2021-09-20 22:58:02 +02:00
|
|
|
set_value(value)
|
|
|
|
{
|
|
|
|
this.value = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
getValue()
|
|
|
|
{
|
|
|
|
return this.value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
et2_register_widget(et2_placeholder_select, ["placeholder-select"]);
|
|
|
|
|
2021-09-21 22:06:24 +02:00
|
|
|
/**
|
|
|
|
* Display a dialog to choose from a set list of placeholder snippets
|
|
|
|
*/
|
|
|
|
export class et2_placeholder_snippet_select extends et2_placeholder_select
|
|
|
|
{
|
|
|
|
static readonly _attributes : any = {
|
|
|
|
dialog_title: {
|
|
|
|
"default": "Insert address"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
static placeholders = {
|
|
|
|
"addressbook": {
|
|
|
|
"addresses": {
|
2021-09-28 18:16:57 +02:00
|
|
|
"{{org_name}}\n{{n_fn}}\n{{adr_one_street}}{{NELF adr_one_street2}}\n{{adr_one_formatted}}": "Business address",
|
2021-09-21 22:06:24 +02:00
|
|
|
"{{n_fn}}\n{{adr_two_street}}{{NELF adr_two_street2}}\n{{adr_two_formatted}}": "Home address",
|
2021-09-28 18:16:57 +02:00
|
|
|
"{{n_fn}}\n{{email}}\n{{tel_work}}": "Name, email, phone"
|
2021-09-21 22:06:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
button : JQuery;
|
|
|
|
submit_callback : any;
|
2022-03-30 21:24:52 +02:00
|
|
|
dialog : Et2Dialog;
|
2021-09-21 22:06:24 +02:00
|
|
|
protected value : any;
|
|
|
|
|
|
|
|
protected LIST_URL = 'EGroupware\\Api\\Etemplate\\Widget\\Placeholder::ajax_get_placeholders';
|
|
|
|
protected TEMPLATE = '/api/templates/default/placeholder_snippet.xet?1';
|
|
|
|
|
2022-02-11 21:02:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param _parent
|
|
|
|
* @param _attrs
|
|
|
|
* @memberOf et2_vfsSelect
|
|
|
|
*/
|
|
|
|
constructor(_parent, _attrs? : WidgetConfig, _child? : object)
|
|
|
|
{
|
|
|
|
// Call the inherited constructor
|
|
|
|
super(_parent, _attrs, ClassWithAttributes.extendAttributes(et2_placeholder_select._attributes, _child || {}));
|
|
|
|
|
|
|
|
// Load app translations
|
|
|
|
this.egw().langRequireApp(this.egw().window, "addressbook");
|
|
|
|
}
|
|
|
|
|
2021-09-21 22:06:24 +02:00
|
|
|
/**
|
|
|
|
* Post-load of the dialog
|
|
|
|
* Bind internal events, set some things that are difficult to do in the template
|
|
|
|
*/
|
|
|
|
_on_template_load()
|
|
|
|
{
|
2022-08-04 00:05:50 +02:00
|
|
|
let app = <Et2Select><unknown>this.dialog.template.widgetContainer.getDOMWidgetById("app");
|
|
|
|
let placeholder_list = <Et2Select><unknown>this.dialog.template.widgetContainer.getDOMWidgetById("placeholder_list");
|
|
|
|
let preview = <Et2Description><unknown>this.dialog.template.widgetContainer.getDOMWidgetById("preview_content");
|
|
|
|
let entry = <Et2LinkEntry><unknown>this.dialog.template.widgetContainer.getDOMWidgetById("entry");
|
2021-09-21 22:06:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
placeholder_list.set_select_options(this._get_placeholders("addressbook", "addresses"));
|
|
|
|
|
|
|
|
// Further setup / styling that can't be done in etemplate
|
2022-08-04 00:05:50 +02:00
|
|
|
app.setAttribute("readonly", true);
|
2021-09-21 22:06:24 +02:00
|
|
|
|
|
|
|
// Bind some handlers
|
|
|
|
app.onchange = (node, widget) =>
|
|
|
|
{
|
|
|
|
entry.set_value({app: widget.get_value()});
|
2022-10-05 18:50:54 +02:00
|
|
|
placeholder_list.set_select_options(this._get_placeholders(app.value, "addresses"));
|
2021-09-21 22:06:24 +02:00
|
|
|
}
|
|
|
|
placeholder_list.onchange = this._on_placeholder_select.bind(this);
|
|
|
|
entry.onchange = this._on_placeholder_select.bind(this);
|
|
|
|
|
2022-10-05 18:50:54 +02:00
|
|
|
app.set_value(app.value);
|
2021-09-21 22:06:24 +02:00
|
|
|
this._on_placeholder_select();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* User has selected a placeholder
|
|
|
|
* Update the UI, and if they have an entry selected do the replacement and show that.
|
|
|
|
*/
|
|
|
|
_on_placeholder_select()
|
|
|
|
{
|
2022-10-05 18:50:54 +02:00
|
|
|
let app = <Et2Select><unknown>this.dialog.template.widgetContainer.getDOMWidgetById("app");
|
2022-08-04 00:05:50 +02:00
|
|
|
let entry = <Et2LinkEntry><unknown>this.dialog.template.widgetContainer.getDOMWidgetById("entry");
|
|
|
|
let placeholder_list = <Et2Select><unknown>this.dialog.template.widgetContainer.getDOMWidgetById("placeholder_list");
|
|
|
|
let preview_content = <Et2Description><unknown>this.dialog.template.widgetContainer.getDOMWidgetById("preview_content");
|
2022-10-05 18:50:54 +02:00
|
|
|
let placeholder = "";
|
|
|
|
if(app && app.value)
|
|
|
|
{
|
|
|
|
placeholder = Object.keys(et2_placeholder_snippet_select.placeholders[<string>app.value]["addresses"])[<string>placeholder_list.value];
|
|
|
|
}
|
2021-09-21 22:06:24 +02:00
|
|
|
|
2022-10-05 18:50:54 +02:00
|
|
|
if(placeholder && entry.get_value())
|
2021-09-21 22:06:24 +02:00
|
|
|
{
|
|
|
|
// Show the selected placeholder replaced with value from the selected entry
|
|
|
|
this.egw().json(
|
|
|
|
'EGroupware\\Api\\Etemplate\\Widget\\Placeholder::ajax_fill_placeholders',
|
2022-10-05 18:50:54 +02:00
|
|
|
[placeholder, {app: "addressbook", id: entry.get_value()}],
|
2021-09-21 22:06:24 +02:00
|
|
|
function(_content)
|
|
|
|
{
|
2021-09-27 22:46:41 +02:00
|
|
|
if(!_content)
|
|
|
|
{
|
|
|
|
_content = '';
|
|
|
|
}
|
2021-09-21 22:06:24 +02:00
|
|
|
this.set_value(_content);
|
|
|
|
preview_content.set_value(_content);
|
|
|
|
preview_content.getDOMNode().parentNode.style.visibility = _content.trim() ? null : 'hidden';
|
|
|
|
}.bind(this)
|
|
|
|
).sendRequest(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// No value, hide the row
|
|
|
|
preview_content.getDOMNode().parentNode.style.visibility = 'hidden';
|
|
|
|
}
|
|
|
|
if(!entry.get_value())
|
|
|
|
{
|
2022-08-04 00:05:50 +02:00
|
|
|
entry._searchNode.focus();
|
2021-09-21 22:06:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the list of placeholder groups under the selected application
|
|
|
|
* @param appname
|
|
|
|
* @returns {value:string, label:string}[]
|
|
|
|
*/
|
|
|
|
_get_group_options(appname : string)
|
|
|
|
{
|
|
|
|
let options = [];
|
|
|
|
Object.keys(et2_placeholder_select.placeholders[appname]).map((key) =>
|
|
|
|
{
|
|
|
|
options.push(
|
|
|
|
{
|
|
|
|
value: key,
|
|
|
|
label: this.egw().lang(key)
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of placeholders under the given application + group
|
|
|
|
*
|
|
|
|
* @param appname
|
|
|
|
* @param group
|
|
|
|
* @returns {value:string, label:string}[]
|
|
|
|
*/
|
|
|
|
_get_placeholders(appname : string, group : string)
|
|
|
|
{
|
|
|
|
let options = [];
|
2022-10-05 18:50:54 +02:00
|
|
|
Object.keys(et2_placeholder_snippet_select.placeholders[appname][group]).map((key, index) =>
|
2021-09-21 22:06:24 +02:00
|
|
|
{
|
|
|
|
options.push(
|
|
|
|
{
|
2022-10-05 18:50:54 +02:00
|
|
|
value: index,
|
2021-09-28 18:16:57 +02:00
|
|
|
label: this.egw().lang(et2_placeholder_snippet_select.placeholders[appname][group][key])
|
2021-09-21 22:06:24 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the correct insert text call the insert callback with it
|
|
|
|
*
|
|
|
|
* @param dialog_values
|
|
|
|
*/
|
|
|
|
_do_insert_callback(dialog_values : Object)
|
|
|
|
{
|
|
|
|
this.options.insert_callback(this.get_value());
|
|
|
|
}
|
|
|
|
|
|
|
|
set_value(value)
|
|
|
|
{
|
|
|
|
this.value = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
getValue()
|
|
|
|
{
|
|
|
|
return this.value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
et2_register_widget(et2_placeholder_snippet_select, ["placeholder-snippet"]);
|