Skip to content

Commit

Permalink
Use all Mithril lifecycle stubs (#2847)
Browse files Browse the repository at this point in the history
  • Loading branch information
askvortsov1 authored May 9, 2021
1 parent ceb5677 commit d4e3254
Show file tree
Hide file tree
Showing 17 changed files with 77 additions and 23 deletions.
4 changes: 3 additions & 1 deletion js/src/admin/components/AdminNav.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ export default class AdminNav extends Component {
this.scrollToActive();
}

onupdate() {
onupdate(vnode) {
super.onupdate(vnode);

this.scrollToActive();
}

Expand Down
15 changes: 15 additions & 0 deletions js/src/common/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ export default abstract class Component<T extends ComponentAttrs = ComponentAttr
this.setAttrs(vnode.attrs);
}

/**
* @inheritdoc
*/
onupdate(vnode: Mithril.VnodeDOM<T, this>) {}

/**
* @inheritdoc
*/
onbeforeremove(vnode: Mithril.VnodeDOM<T, this>) {}

/**
* @inheritdoc
*/
onremove(vnode: Mithril.VnodeDOM<T, this>) {}

/**
* Returns a jQuery object for this component's element. If you pass in a
* selector string, this method will return a jQuery object, using the current
Expand Down
4 changes: 3 additions & 1 deletion js/src/common/components/ConfirmDocumentUnload.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export default class ConfirmDocumentUnload extends Component {
$(window).on('beforeunload', this.boundHandler);
}

onremove() {
onremove(vnode) {
super.onremove(vnode);

$(window).off('beforeunload', this.boundHandler);
}

Expand Down
4 changes: 3 additions & 1 deletion js/src/common/components/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ export default class Modal extends Component {
this.attrs.animateShow(() => this.onready());
}

onbeforeremove() {
onbeforeremove(vnode) {
super.onbeforeremove(vnode);

// If the global modal state currently contains a modal,
// we've just opened up a new one, and accordingly,
// we don't need to show a hide animation.
Expand Down
4 changes: 3 additions & 1 deletion js/src/common/components/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ export default class Page extends Component {
}
}

onremove() {
onremove(vnode) {
super.onremove(vnode);

if (this.bodyClass) {
$('#app').removeClass(this.bodyClass);
}
Expand Down
4 changes: 3 additions & 1 deletion js/src/common/components/TextEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ export default class TextEditor extends Component {
this.attrs.composer.editor = this.buildEditor(this.$('.TextEditor-editorContainer')[0]);
}

onupdate() {
onupdate(vnode) {
super.onupdate(vnode);

const newDisabled = !!this.attrs.disabled;

if (this.disabled !== newDisabled) {
Expand Down
4 changes: 3 additions & 1 deletion js/src/forum/components/AffixedSidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export default class AffixedSidebar extends Component {
$(window).on('resize', this.boundOnresize).resize();
}

onremove() {
onremove(vnode) {
super.onremove(vnode);

$(window).off('resize', this.boundOnresize);
}

Expand Down
8 changes: 6 additions & 2 deletions js/src/forum/components/Composer.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ export default class Composer extends Component {
);
}

onupdate() {
onupdate(vnode) {
super.onupdate(vnode);

if (this.state.position === this.prevPosition) {
// Set the height of the Composer element and its contents on each redraw,
// so that they do not lose it if their DOM elements are recreated.
Expand Down Expand Up @@ -95,7 +97,9 @@ export default class Composer extends Component {
.on('mouseup', (this.handlers.onmouseup = this.onmouseup.bind(this)));
}

onremove() {
onremove(vnode) {
super.onremove(vnode);

$(window).off('resize', this.handlers.onresize);

$(document).off('mousemove', this.handlers.onmousemove).off('mouseup', this.handlers.onmouseup);
Expand Down
4 changes: 3 additions & 1 deletion js/src/forum/components/ComposerPostPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ export default class ComposerPostPreview extends Component {
this.updateInterval = setInterval(updatePreview, 50);
}

onremove() {
onremove(vnode) {
super.onremove(vnode);

clearInterval(this.updateInterval);
}
}
5 changes: 3 additions & 2 deletions js/src/forum/components/DiscussionPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ export default class DiscussionPage extends Page {
this.bodyClass = 'App--discussion';
}

onremove() {
super.onremove();
onremove(vnode) {
super.onremove(vnode);

// If we are indeed navigating away from this discussion, then disable the
// discussion list pane. Also, if we're composing a reply to this
// discussion, minimize the composer – unless it's empty, in which case
Expand Down
8 changes: 5 additions & 3 deletions js/src/forum/components/IndexPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,16 @@ export default class IndexPage extends Page {
}
}

onbeforeremove() {
onbeforeremove(vnode) {
super.onbeforeremove(vnode);

// Save the scroll position so we can restore it when we return to the
// discussion list.
app.cache.scrollTop = $(window).scrollTop();
}

onremove() {
super.onremove();
onremove(vnode) {
super.onremove(vnode);

$('#app').css('min-height', '');
}
Expand Down
4 changes: 3 additions & 1 deletion js/src/forum/components/NotificationList.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ export default class NotificationList extends Component {
this.$scrollParent.on('scroll', this.boundScrollHandler);
}

onremove() {
onremove(vnode) {
super.onremove(vnode);

this.$scrollParent.off('scroll', this.boundScrollHandler);
}

Expand Down
4 changes: 3 additions & 1 deletion js/src/forum/components/Post.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ export default class Post extends Component {
return this.subtree.needsRebuild();
}

onupdate() {
onupdate(vnode) {
super.onupdate(vnode);

const $actions = this.$('.Post-actions');
const $controls = this.$('.Post-controls');

Expand Down
4 changes: 3 additions & 1 deletion js/src/forum/components/PostEdited.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export default class PostEdited extends Component {
this.rebuildTooltip();
}

onupdate() {
onupdate(vnode) {
super.onupdate(vnode);

this.rebuildTooltip();
}

Expand Down
8 changes: 6 additions & 2 deletions js/src/forum/components/PostStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ export default class PostStream extends Component {
return <div className="PostStream">{items}</div>;
}

onupdate() {
onupdate(vnode) {
super.onupdate(vnode);

this.triggerScroll();
}

Expand All @@ -120,7 +122,9 @@ export default class PostStream extends Component {
setTimeout(() => this.scrollListener.start());
}

onremove() {
onremove(vnode) {
super.onremove(vnode);

this.scrollListener.stop();
clearTimeout(this.calculatePositionTimeout);
}
Expand Down
8 changes: 6 additions & 2 deletions js/src/forum/components/PostStreamScrubber.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ export default class PostStreamScrubber extends Component {
);
}

onupdate() {
onupdate(vnode) {
super.onupdate(vnode);

if (this.stream.forceUpdateScrubber) {
this.stream.forceUpdateScrubber = false;
this.stream.loadPromise.then(() => this.updateScrubberValues({ animate: true, forceHeightChange: true }));
Expand Down Expand Up @@ -142,7 +144,9 @@ export default class PostStreamScrubber extends Component {
this.stream.loadPromise.then(() => this.updateScrubberValues({ animate: false, forceHeightChange: true }));
}

onremove() {
onremove(vnode) {
super.onremove(vnode);

this.scrollListener.stop();
$(window).off('resize', this.handlers.onresize);

Expand Down
8 changes: 6 additions & 2 deletions js/src/forum/components/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ export default class Search extends Component {
this.element.querySelector('.Search-results').style['max-height'] = `${maxHeight}px`;
}

onupdate() {
onupdate(vnode) {
super.onupdate(vnode);

// Highlight the item that is currently selected.
this.setIndex(this.getCurrentNumericIndex());

Expand Down Expand Up @@ -200,7 +202,9 @@ export default class Search extends Component {
window.addEventListener('resize', this.updateMaxHeightHandler);
}

onremove() {
onremove(vnode) {
super.onremove(vnode);

window.removeEventListener('resize', this.updateMaxHeightHandler);
}

Expand Down

0 comments on commit d4e3254

Please sign in to comment.