Add some convenience functions for faster widget access

These are only available on et2_container, so app.ts can use eg: this.et2.setWidgetById(...)

getDOMWidgetById(): Same as getWidgetById() it returns et2_DOMWidget instead of et2_widget
getValueWidgetById(): Same as getWidgetById(), but returns et2_valueWidget
getValueById(): Find a widget, get its value
setValueById(): Find a widget, set its value
setDisabledById(): Find a widget, set its disabled state.
This commit is contained in:
nathangray 2020-05-15 14:01:02 -06:00
parent 1936849bd7
commit 4ba7bac850
2 changed files with 192 additions and 0 deletions

View File

@ -267,6 +267,9 @@ var et2_baseWidget = /** @class */ (function (_super) {
exports.et2_baseWidget = et2_baseWidget;
/**
* Simple container object
*
* There is no tag to put this in a template. By convention we only make one of these per etemplate,
* and it's the top level object.
*/
var et2_container = /** @class */ (function (_super) {
__extends(et2_container, _super);
@ -297,6 +300,90 @@ var et2_container = /** @class */ (function (_super) {
}
}
};
/**
* Searches for a DOM widget by id in the tree, descending into the child levels.
*
* @param _id is the id you're searching for
*/
et2_container.prototype.getDOMWidgetById = function (_id) {
var widget = this.getWidgetById(_id);
if (widget && widget.instanceOf(et2_core_DOMWidget_1.et2_DOMWidget)) {
return widget;
}
return null;
};
/**
* Searches for a Value widget by id in the tree, descending into the child levels.
*
* @param _id is the id you're searching for
*/
et2_container.prototype.getValueWidgetById = function (_id) {
var widget = this.getWidgetById(_id);
if (widget && widget.instanceOf(et2_valueWidget)) {
return widget;
}
return null;
};
/**
* Set the value for a child widget, specified by the given ID
*
* @param id string The ID you're searching for
* @param value Value for the widget
*
* @return Returns the result of widget's set_value(), though this is usually undefined
*
* @throws Error If the widget cannot be found or it does not have a set_value() function
*/
et2_container.prototype.setValueById = function (id, value) {
var widget = this.getWidgetById(id);
if (!widget)
throw 'Could not find widget ' + id;
// Don't care about what class it is, just that it has the function
// @ts-ignore
if (typeof widget.set_value !== 'function') {
throw 'Widget ' + id + ' does not have a set_value() function';
}
// @ts-ignore
return widget.set_value(value);
};
/**
* Get the current value of a child widget, specified by the given ID
*
* This is the current value of the widget, which may be different from the original value given in content
*
* @param id string The ID you're searching for
* @throws Error If the widget cannot be found or it does not have a set_value() function
*/
et2_container.prototype.getValueById = function (id) {
var widget = this.getWidgetById(id);
if (!widget)
throw 'Could not find widget ' + id;
// Don't care about what class it is, just that it has the function
// @ts-ignore
if (typeof widget.get_value !== 'function') {
throw 'Widget ' + id + ' does not have a get_value() function';
}
// @ts-ignore
return widget.get_value();
};
/**
* Set the value for a child widget, specified by the given ID
*
* @param id string The ID you're searching for
* @throws Error If the widget cannot be found or it does not have a set_value() function
*/
et2_container.prototype.setDisabledById = function (id, value) {
var widget = this.getWidgetById(id);
if (!widget)
throw 'Could not find widget ' + id;
// Don't care about what class it is, just that it has the function
// @ts-ignore
if (typeof widget.set_disabled !== 'function') {
throw 'Widget ' + id + ' does not have a set_disabled() function';
}
// @ts-ignore
return widget.set_disabled(value);
};
return et2_container;
}(et2_baseWidget));
exports.et2_container = et2_container;

View File

@ -327,6 +327,9 @@ export class et2_baseWidget extends et2_DOMWidget implements et2_IAligned
/**
* Simple container object
*
* There is no tag to put this in a template. By convention we only make one of these per etemplate,
* and it's the top level object.
*/
export class et2_container extends et2_baseWidget
{
@ -363,6 +366,108 @@ export class et2_container extends et2_baseWidget
}
}
}
/**
* Searches for a DOM widget by id in the tree, descending into the child levels.
*
* @param _id is the id you're searching for
*/
getDOMWidgetById(_id) : et2_DOMWidget | null
{
let widget = this.getWidgetById(_id);
if(widget && widget.instanceOf(et2_DOMWidget))
{
return <et2_DOMWidget>widget;
}
return null
}
/**
* Searches for a Value widget by id in the tree, descending into the child levels.
*
* @param _id is the id you're searching for
*/
getValueWidgetById(_id) : et2_valueWidget | null
{
let widget = this.getWidgetById(_id);
if(widget && widget.instanceOf(et2_valueWidget))
{
return <et2_valueWidget>widget;
}
return null
}
/**
* Set the value for a child widget, specified by the given ID
*
* @param id string The ID you're searching for
* @param value Value for the widget
*
* @return Returns the result of widget's set_value(), though this is usually undefined
*
* @throws Error If the widget cannot be found or it does not have a set_value() function
*/
setValueById(id: string, value): any
{
let widget = this.getWidgetById(id);
if(!widget) throw 'Could not find widget ' + id;
// Don't care about what class it is, just that it has the function
// @ts-ignore
if(typeof widget.set_value !== 'function')
{
throw 'Widget ' + id + ' does not have a set_value() function';
}
// @ts-ignore
return widget.set_value(value);
}
/**
* Get the current value of a child widget, specified by the given ID
*
* This is the current value of the widget, which may be different from the original value given in content
*
* @param id string The ID you're searching for
* @throws Error If the widget cannot be found or it does not have a set_value() function
*/
getValueById(id: string)
{
let widget = this.getWidgetById(id);
if(!widget) throw 'Could not find widget ' + id;
// Don't care about what class it is, just that it has the function
// @ts-ignore
if(typeof widget.get_value !== 'function')
{
throw 'Widget ' + id + ' does not have a get_value() function';
}
// @ts-ignore
return widget.get_value();
}
/**
* Set the value for a child widget, specified by the given ID
*
* @param id string The ID you're searching for
* @throws Error If the widget cannot be found or it does not have a set_value() function
*/
setDisabledById(id: string, value : boolean)
{
let widget = this.getWidgetById(id);
if(!widget) throw 'Could not find widget ' + id;
// Don't care about what class it is, just that it has the function
// @ts-ignore
if(typeof widget.set_disabled !== 'function')
{
throw 'Widget ' + id + ' does not have a set_disabled() function';
}
// @ts-ignore
return widget.set_disabled(value);
}
}
// Register widget for attributes, but not for any xml tags
et2_register_widget(et2_container, []);