Skip to content

Commit

Permalink
Use css class instead of jQuery .show()/hide()/toggle()
Browse files Browse the repository at this point in the history
To decrease computation time, avoid using jQuery's .show() and
.toggle() by using our own hide css class.

Bug: T87420
Change-Id: Ibf7c99aa4aad1ccca67ae385612d0e45edcd3157
  • Loading branch information
mooeypoo committed Feb 2, 2015
1 parent 51d361c commit c762da4
Show file tree
Hide file tree
Showing 16 changed files with 68 additions and 36 deletions.
4 changes: 2 additions & 2 deletions build/typos.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
[ "visiblit(ies|y)", "visibilit$1" ],
[ "movablilties", "movabilities" ],
[ "inpsect(ors?|ion)", "inspect$1" ],
[ "teh", "the" ],
[ "\bteh\b", "the" ],
[ "intialization", "initialization" ],
[ "durring", "during" ],
[ "occured", "occurred" ]
]
}
}
2 changes: 1 addition & 1 deletion src/Widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ OO.ui.Widget.prototype.toggle = function ( show ) {

if ( show !== this.isVisible() ) {
this.visible = show;
this.$element.toggle( show );
this.$element.toggleClass( 'oo-ui-element-hidden', !this.visible );
this.emit( 'toggle', show );
}

Expand Down
12 changes: 7 additions & 5 deletions src/Window.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,12 @@ OO.ui.Window.prototype.toggle = function ( show ) {
if ( this.isolated && !this.isLoaded() ) {
// Hide the window using visibility instead of display until loading is complete
// Can't use display: none; because that prevents the iframe from loading in Firefox
this.$element.css( 'visibility', show ? 'visible' : 'hidden' );
this.$element
.css( 'visibility', show ? 'visible' : 'hidden' );
} else {
this.$element.toggle( show ).css( 'visibility', '' );
this.$element
.toggleClass( 'oo-ui-element-hidden', !this.visible )
.css( 'visibility', '' );
}
this.emit( 'toggle', show );
}
Expand Down Expand Up @@ -646,8 +649,7 @@ OO.ui.Window.prototype.setup = function ( data ) {
var win = this,
deferred = $.Deferred();

this.$element.show();
this.visible = true;
this.toggle( true );
this.getSetupProcess( data ).execute().done( function () {
// Force redraw by asking the browser to measure the elements' widths
win.$element.addClass( 'oo-ui-window-setup' ).width();
Expand Down Expand Up @@ -730,7 +732,7 @@ OO.ui.Window.prototype.teardown = function ( data ) {
// Force redraw by asking the browser to measure the elements' widths
win.$element.removeClass( 'oo-ui-window-load oo-ui-window-setup' ).width();
win.$content.removeClass( 'oo-ui-window-content-setup' ).width();
win.$element.hide();
win.$element.addClass( 'oo-ui-element-hidden' );
win.visible = false;
deferred.resolve();
} );
Expand Down
4 changes: 2 additions & 2 deletions src/dialogs/ProcessDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,14 @@ OO.ui.ProcessDialog.prototype.showErrors = function ( errors ) {
}
this.retryButton.toggle( recoverable );
this.$errorsTitle.after( this.$errorItems );
this.$errors.show().scrollTop( 0 );
this.$errors.removeClass( 'oo-ui-widget-hidden' ).scrollTop( 0 );
};

/**
* Hide errors.
*/
OO.ui.ProcessDialog.prototype.hideErrors = function () {
this.$errors.hide();
this.$errors.addClass( 'oo-ui-widget-hidden' );
this.$errorItems.remove();
this.$errorItems = null;
};
10 changes: 5 additions & 5 deletions src/elements/DraggableGroupElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ OO.ui.DraggableGroupElement.prototype.onDragLeave = function () {
// This means the item was dragged outside the widget
this.$placeholder
.css( 'left', 0 )
.hide();
.addClass( 'oo-ui-element-hidden' );
};

/**
Expand Down Expand Up @@ -196,20 +196,20 @@ OO.ui.DraggableGroupElement.prototype.onDragOver = function ( e ) {
if ( this.sideInsertion ) {
this.$placeholder
.css( cssOutput )
.show();
.removeClass( 'oo-ui-element-hidden' );
} else {
this.$placeholder
.css( {
left: 0,
top: 0
} )
.hide();
.addClass( 'oo-ui-element-hidden' );
}
} else {
// This means the item was dragged outside the widget
this.$placeholder
.css( 'left', 0 )
.hide();
.addClass( 'oo-ui-element-hidden' );
}
// Prevent default
e.preventDefault();
Expand All @@ -229,7 +229,7 @@ OO.ui.DraggableGroupElement.prototype.setDragItem = function ( item ) {
OO.ui.DraggableGroupElement.prototype.unsetDragItem = function () {
this.dragItem = null;
this.itemDragOver = null;
this.$placeholder.hide();
this.$placeholder.addClass( 'oo-ui-element-hidden' );
this.sideInsertion = '';
};

Expand Down
39 changes: 29 additions & 10 deletions src/layouts/StackLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ OO.ui.StackLayout.prototype.unsetCurrentItem = function () {
* @chainable
*/
OO.ui.StackLayout.prototype.addItems = function ( items, index ) {
// Update the visibility
this.updateHiddenState( items, this.currentItem );

// Mixin method
OO.ui.GroupElement.prototype.addItems.call( this, items, index );

Expand Down Expand Up @@ -146,18 +149,10 @@ OO.ui.StackLayout.prototype.clearItems = function () {
* @fires set
*/
OO.ui.StackLayout.prototype.setItem = function ( item ) {
var i, len;

if ( item !== this.currentItem ) {
if ( !this.continuous ) {
for ( i = 0, len = this.items.length; i < len; i++ ) {
this.items[ i ].$element.css( 'display', '' );
}
}
this.updateHiddenState( this.items, item );

if ( $.inArray( item, this.items ) !== -1 ) {
if ( !this.continuous ) {
item.$element.css( 'display', 'block' );
}
this.currentItem = item;
this.emit( 'set', item );
} else {
Expand All @@ -167,3 +162,27 @@ OO.ui.StackLayout.prototype.setItem = function ( item ) {

return this;
};

/**
* Update the visibility of all items in case of non-continuous view.
*
* Ensure all items are hidden except for the selected one.
* This method does nothing when the stack is continuous.
*
* @param {OO.ui.Layout[]} items Item list iterate over
* @param {OO.ui.Layout} [selectedItem] Selected item to show
*/
OO.ui.StackLayout.prototype.updateHiddenState = function ( items, selectedItem ) {
var i, len;

if ( !this.continuous ) {
for ( i = 0, len = items.length; i < len; i++ ) {
if ( !selectedItem || selectedItem !== items[ i ] ) {
items[ i ].$element.addClass( 'oo-ui-element-hidden' );
}
}
if ( selectedItem ) {
selectedItem.$element.removeClass( 'oo-ui-element-hidden' );
}
}
};
9 changes: 9 additions & 0 deletions src/styles/Element.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@import 'common';

.oo-ui-element {
&-hidden {
display: none !important;
}

.theme-oo-ui-element();
}
1 change: 1 addition & 0 deletions src/styles/core.less
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
direction: ltr;
}

@import 'Element.less';
@import 'elements/ButtonElement.less';
@import 'elements/ClippableElement.less';
@import 'elements/FlaggedElement.less';
Expand Down
4 changes: 0 additions & 4 deletions src/styles/layouts/StackLayout.less
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
@import '../common';

.oo-ui-stackLayout {
> .oo-ui-panelLayout {
display: none;
}

&-continuous > .oo-ui-panelLayout {
display: block;
position: relative;
Expand Down
1 change: 1 addition & 0 deletions src/styles/theme.less
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

.theme-oo-ui-layout () {}
.theme-oo-ui-element () {}
.theme-oo-ui-widget () {}
.theme-oo-ui-window () {}
.theme-oo-ui-dialog () {}
Expand Down
2 changes: 2 additions & 0 deletions src/themes/apex/elements.less
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
@import 'common';

.theme-oo-ui-element () {}

.theme-oo-ui-buttonElement () {
> .oo-ui-buttonElement-button {
color: #333;
Expand Down
2 changes: 2 additions & 0 deletions src/themes/blank/elements.less
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
@import 'common';

.theme-oo-ui-element () {}

.theme-oo-ui-buttonElement () {}

.theme-oo-ui-clippableElement () {}
Expand Down
2 changes: 2 additions & 0 deletions src/themes/mediawiki/elements.less
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
@import 'common';

.theme-oo-ui-element () {}

.theme-oo-ui-buttonElement () {
> .oo-ui-buttonElement-button {
font-weight: bold;
Expand Down
5 changes: 2 additions & 3 deletions src/widgets/MenuSelectWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ OO.ui.MenuSelectWidget = function OoUiMenuSelectWidget( config ) {

// Initialization
this.$element
.hide()
.attr( 'role', 'menu' )
.addClass( 'oo-ui-menuSelectWidget' );
.addClass( 'oo-ui-menuSelectWidget oo-ui-element-hidden' )
.attr( 'role', 'menu' );
};

/* Setup */
Expand Down
5 changes: 2 additions & 3 deletions src/widgets/PopupWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,13 @@ OO.ui.PopupWidget = function OoUiPopupWidget( config ) {
.addClass( 'oo-ui-popupWidget-head' )
.append( this.$label, this.closeButton.$element );
if ( !config.head ) {
this.$head.hide();
this.$head.addClass( 'oo-ui-element-hidden' );
}
this.$popup
.addClass( 'oo-ui-popupWidget-popup' )
.append( this.$head, this.$body );
this.$element
.hide()
.addClass( 'oo-ui-popupWidget' )
.addClass( 'oo-ui-popupWidget oo-ui-element-hidden' )
.append( this.$popup, this.$anchor );
// Move content, which was added to #$element by OO.ui.Widget, to the body
if ( config.$content instanceof jQuery ) {
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/TextInputWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ OO.ui.TextInputWidget = function OoUiTextInputWidget( config ) {
this.$clone = this.$input
.clone()
.insertAfter( this.$input )
.hide();
.addClass( 'oo-ui-element-hidden' );
}

this.setValidation( config.validate );
Expand Down

0 comments on commit c762da4

Please sign in to comment.