Skip to content

Commit

Permalink
Merge pull request #486 from foretagsplatsen/32129139/Sort_methods_of…
Browse files Browse the repository at this point in the history
…_Widget2

32129139: Sort methods of Widget2
  • Loading branch information
DamienCassou authored Apr 10, 2024
2 parents c5f79cf + e6ff346 commit 4fde543
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 128 deletions.
220 changes: 104 additions & 116 deletions src/Widget2.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ export default class Widget2 {
this._setParameters = this._router.setParameters;
}

//
// Public
//

/**
* Returns a unique id for the widget
*
Expand Down Expand Up @@ -214,6 +210,110 @@ export default class Widget2 {
this.onAttach.trigger();
}

/**
* Main entry point for rendering. For convenience "renderOn" will wrap the content
* rendered by "renderContentOn" in a root element (renderRootOn) that will be matched
* by asJQuery.
*
* Usually concrete widgets override "renderContentOn" to render it content. Widgets
* can override "renderOn" but must then make sure that it can be matched by "asJQuery".
*
* One way to do that is to make sure to have only one root element and setting the ID of
* that element to the ID of the widget.
*
* @example
*
* renderOn(html) {
* html.ul({id: that.getId()}
* html.li("BMW"),
* html.li("Toyota")
* );
* };
*
*
* @param html
*/
renderOn(html) {
// Renders widget by wrapping `renderContentOn()` in a root element.
this._renderRootOn(html).render(this.renderContentOn.bind(this));
}

registerChild(widget) {
this._children.push(widget);
}

/**
* Overridden in concrete widgets to render widget to canvas/DOM.
*
* @example
*
* renderContentOn(html) {
* html.ul(
* html.li("BMW"),
* html.li("Toyota")
* );
* };
*
* @param {htmlCanvas} html
*/
renderContentOn(_html) {
throw new Error("Subclass responsibility");
}

/**
* Re-renders the widget and replace it in the DOM
*/
update() {
if (this._inUpdateTransaction || !this.isRendered()) {
return;
}

this.willDetach();
this._willUpdate();

this._withAttachHooks(() => {
// clear content of root
this.asJQuery().empty();

// re-render content on root
let html = htmlCanvas(this.asJQuery());

this._withChildrenRegistration(() => {
this.renderContentOn(html);
});
});
}

withinTransaction(fn, onDone) {
if (this._inUpdateTransaction) {
fn();
} else {
try {
this._inUpdateTransaction = true;
fn();
} finally {
this._inUpdateTransaction = false;
if (onDone) {
onDone();
}
}
}
}

/**
* Evaluate `fn`, ensuring that an update will be
* performed after evaluating the function. Nested calls
* to `withUpdate` or `update` will result in updating the
* widget only once.
*/
withUpdate(fn) {
this.withinTransaction(fn, this.update.bind(this));
}

withNoUpdate(fn) {
this.withinTransaction(fn);
}

/**
* Evaluate `fn`, calling `willAttach` before and `didAttach` after
* the evaluation.
Expand Down Expand Up @@ -248,10 +348,6 @@ export default class Widget2 {
names.forEach((name) => this._createEvent(name));
}

//
// Protected
//

/**
* Exposes the internal ID generator. Generates new unique IDs to
* be used for sub-widgets, etc.
Expand All @@ -267,10 +363,6 @@ export default class Widget2 {
*/
_dispose() {}

//
// Render
//

/**
* Private rendering function. This is the function
* internally called each time the widget is rendered, in
Expand All @@ -283,34 +375,6 @@ export default class Widget2 {
});
}

/**
* Main entry point for rendering. For convenience "renderOn" will wrap the content
* rendered by "renderContentOn" in a root element (renderRootOn) that will be matched
* by asJQuery.
*
* Usually concrete widgets override "renderContentOn" to render it content. Widgets
* can override "renderOn" but must then make sure that it can be matched by "asJQuery".
*
* One way to do that is to make sure to have only one root element and setting the ID of
* that element to the ID of the widget.
*
* @example
*
* renderOn(html) {
* html.ul({id: that.getId()}
* html.li("BMW"),
* html.li("Toyota")
* );
* };
*
*
* @param html
*/
renderOn(html) {
// Renders widget by wrapping `renderContentOn()` in a root element.
this._renderRootOn(html).render(this.renderContentOn.bind(this));
}

_withChildrenRegistration(fn) {
let parent = getCurrentWidget();

Expand All @@ -324,10 +388,6 @@ export default class Widget2 {
}, this);
}

registerChild(widget) {
this._children.push(widget);
}

/**
* Renders a wrapper element (by default a "widgetjs-widget" tag) and
* sets the element ID to the ID of the widget so that it can be found by
Expand All @@ -340,24 +400,6 @@ export default class Widget2 {
return html.tag("widgetjs-widget").id(this._id);
}

/**
* Overridden in concrete widgets to render widget to canvas/DOM.
*
* @example
*
* renderContentOn(html) {
* html.ul(
* html.li("BMW"),
* html.li("Toyota")
* );
* };
*
* @param {htmlCanvas} html
*/
renderContentOn(_html) {
throw new Error("Subclass responsibility");
}

/**
* Hook evaluated before the widget is attached (or reattached due
* to an update of rendering) to the DOM.
Expand All @@ -379,58 +421,4 @@ export default class Widget2 {
* Hook evaluated before widget update.
*/
_willUpdate() {}

/**
* Re-renders the widget and replace it in the DOM
*/
update() {
if (this._inUpdateTransaction || !this.isRendered()) {
return;
}

this.willDetach();
this._willUpdate();

this._withAttachHooks(() => {
// clear content of root
this.asJQuery().empty();

// re-render content on root
let html = htmlCanvas(this.asJQuery());

this._withChildrenRegistration(() => {
this.renderContentOn(html);
});
});
}

withinTransaction(fn, onDone) {
if (this._inUpdateTransaction) {
fn();
} else {
try {
this._inUpdateTransaction = true;
fn();
} finally {
this._inUpdateTransaction = false;
if (onDone) {
onDone();
}
}
}
}

/**
* Evaluate `fn`, ensuring that an update will be
* performed after evaluating the function. Nested calls
* to `withUpdate` or `update` will result in updating the
* widget only once.
*/
withUpdate(fn) {
this.withinTransaction(fn, this.update.bind(this));
}

withNoUpdate(fn) {
this.withinTransaction(fn);
}
}
12 changes: 0 additions & 12 deletions src/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@ const widget = object.subclass((that, my) => {
that.onAttach = my.events.createEvent();
that.onDetach = my.events.createEvent();

//
// Public
//

/**
* Returns a unique id for the widget
*
Expand Down Expand Up @@ -259,10 +255,6 @@ const widget = object.subclass((that, my) => {
that.unregister = my.events.unregister;
that.trigger = my.events.trigger;

//
// Protected
//

/**
* Exposes the internal ID generator. Generates new unique IDs to be used
* for sub-widgets, etc.
Expand Down Expand Up @@ -296,10 +288,6 @@ const widget = object.subclass((that, my) => {
my.getParameter = my.router.getParameter;
my.setParameters = my.router.setParameters;

//
// Render
//

/**
* Private rendering function. This is the function
* internally called each time the widget is rendered, in
Expand Down

0 comments on commit 4fde543

Please sign in to comment.