Drop separate .render/.initialize logic from View classes

Most callers use it as if it's a `getElement` call anyway.

There is one .initialize method left in the HelpDialog class. That's
part of the upstream OOUI Window interfaces.

Change-Id: I5727c59ad0ad05d712d51d255906ddc44e898668
This commit is contained in:
thiemowmde 2024-02-21 18:40:07 +01:00 committed by Thiemo Kreuz (WMDE)
parent f7b6d277ee
commit 759c081add
5 changed files with 18 additions and 36 deletions

View file

@ -132,6 +132,8 @@ $.extend( PointerLine.prototype, {
/** /**
* Initializes the DOM element with the line-box for drawing the lines * Initializes the DOM element with the line-box for drawing the lines
*
* @private
*/ */
initialize: function () { initialize: function () {
// eslint-disable-next-line mediawiki/class-doc // eslint-disable-next-line mediawiki/class-doc
@ -179,19 +181,14 @@ $.extend( PointerLine.prototype, {
mw.hook( 'revslider.collapse' ).add( this.removeColoredColumnBorders.bind( this ) ); mw.hook( 'revslider.collapse' ).add( this.removeColoredColumnBorders.bind( this ) );
}, },
/**
* @return {jQuery}
*/
render: function () {
this.initialize();
this.setColumnBorderHooks();
return this.getElement();
},
/** /**
* @return {jQuery} * @return {jQuery}
*/ */
getElement: function () { getElement: function () {
if ( !this.$html ) {
this.initialize();
this.setColumnBorderHooks();
}
return this.$html; return this.$html;
} }

View file

@ -27,27 +27,15 @@ $.extend( PointerView.prototype, {
*/ */
$html: null, $html: null,
/**
* Initializes the DOM element
*/
initialize: function () {
// eslint-disable-next-line mediawiki/class-doc
this.$html = $( '<div>' )
.addClass( 'mw-revslider-pointer mw-revslider-pointer-cursor ' + this.name );
},
/** /**
* @return {jQuery} * @return {jQuery}
*/ */
render: function () {
this.initialize();
return this.$html;
},
/**
* @return {jQuery|null} Null if not initialized
*/
getElement: function () { getElement: function () {
if ( !this.$html ) {
// eslint-disable-next-line mediawiki/class-doc
this.$html = $( '<div>' )
.addClass( 'mw-revslider-pointer mw-revslider-pointer-cursor ' + this.name );
}
return this.$html; return this.$html;
}, },
@ -72,10 +60,7 @@ $.extend( PointerView.prototype, {
* @param {number} pos * @param {number} pos
*/ */
setDataPositionAttribute: function ( pos ) { setDataPositionAttribute: function ( pos ) {
if ( !this.$html ) { this.getElement().attr( 'data-pos', pos );
this.initialize();
}
this.$html.attr( 'data-pos', pos );
}, },
/** /**

View file

@ -97,7 +97,7 @@ $.extend( SliderView.prototype, {
this.renderPointerContainer( containerWidth ), this.renderPointerContainer( containerWidth ),
this.forwardArrowButton.$element, this.forwardArrowButton.$element,
$( '<div>' ).css( { clear: 'both' } ), $( '<div>' ).css( { clear: 'both' } ),
this.pointerOlder.getLine().render(), this.pointerNewer.getLine().render(), this.pointerOlder.getLine().getElement(), this.pointerNewer.getLine().getElement(),
HelpButtonView.render() HelpButtonView.render()
); );
@ -193,13 +193,13 @@ $.extend( SliderView.prototype, {
.addClass( 'mw-revslider-pointer-container-older' ) .addClass( 'mw-revslider-pointer-container-older' )
.append( .append(
$( '<div>' ).addClass( 'mw-revslider-slider-line' ), $( '<div>' ).addClass( 'mw-revslider-slider-line' ),
this.pointerOlder.getView().render() this.pointerOlder.getView().getElement()
), ),
$( '<div>' ) $( '<div>' )
.addClass( 'mw-revslider-pointer-container-newer' ) .addClass( 'mw-revslider-pointer-container-newer' )
.append( .append(
$( '<div>' ).addClass( 'mw-revslider-slider-line' ), $( '<div>' ).addClass( 'mw-revslider-slider-line' ),
this.pointerNewer.getView().render() this.pointerNewer.getView().getElement()
) )
]; ];
}, },

View file

@ -4,7 +4,7 @@
QUnit.module( 'ext.RevisionSlider.Pointer' ); QUnit.module( 'ext.RevisionSlider.Pointer' );
QUnit.test( 'Initialize Pointer', function ( assert ) { QUnit.test( 'Initialize Pointer', function ( assert ) {
assert.true( ( new Pointer( 'mw-revslider-pointer' ) ).getView().render().hasClass( 'mw-revslider-pointer' ) ); assert.true( ( new Pointer( 'mw-revslider-pointer' ) ).getView().getElement().hasClass( 'mw-revslider-pointer' ) );
} ); } );
QUnit.test( 'Set and get position', function ( assert ) { QUnit.test( 'Set and get position', function ( assert ) {

View file

@ -4,12 +4,12 @@
QUnit.module( 'ext.RevisionSlider.PointerView' ); QUnit.module( 'ext.RevisionSlider.PointerView' );
QUnit.test( 'Initialize PointerView', function ( assert ) { QUnit.test( 'Initialize PointerView', function ( assert ) {
assert.true( ( new PointerView( null, 'mw-revslider-pointer' ) ).render().hasClass( 'mw-revslider-pointer' ) ); assert.true( ( new PointerView( null, 'mw-revslider-pointer' ) ).getElement().hasClass( 'mw-revslider-pointer' ) );
} ); } );
QUnit.test( 'Is newer pointer', function ( assert ) { QUnit.test( 'Is newer pointer', function ( assert ) {
const pv = new PointerView( null, 'mw-revslider-pointer' ); const pv = new PointerView( null, 'mw-revslider-pointer' );
pv.render(); pv.getElement();
assert.false( pv.isNewerPointer() ); assert.false( pv.isNewerPointer() );
pv.getElement().addClass( 'mw-revslider-pointer-newer' ); pv.getElement().addClass( 'mw-revslider-pointer-newer' );