diff --git a/api/js/etemplate/et2_core_baseWidget.js b/api/js/etemplate/et2_core_baseWidget.js index 985c3f70cd..5d7d17dfca 100644 --- a/api/js/etemplate/et2_core_baseWidget.js +++ b/api/js/etemplate/et2_core_baseWidget.js @@ -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; diff --git a/api/js/etemplate/et2_core_baseWidget.ts b/api/js/etemplate/et2_core_baseWidget.ts index 4077159733..156a2a2a7b 100644 --- a/api/js/etemplate/et2_core_baseWidget.ts +++ b/api/js/etemplate/et2_core_baseWidget.ts @@ -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 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 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, []);