jenkins-bot has submitted this change and it was merged. Change subject: Toolbar edit group widget optimization ......................................................................
Toolbar edit group widget optimization The change applies some logic and performance improvement to the toolbar editgroup widget. The edit group buttons' characteristics are moved to the options object. Except for the edit button which is displayed initially, the other buttons will be created not before they are needed for interaction. Additionally, the event handlers on each button have been merged to a single event handler applied to the edit group node itself. Change-Id: I80a5f17c24b20c230062630193b3d3360de77697 --- M lib/resources/Resources.php M lib/resources/jquery.wikibase/jquery.wikibase.claimlistview.js M lib/resources/jquery.wikibase/toolbar/edittoolbar.js M lib/resources/jquery.wikibase/toolbar/toolbareditgroup.js M lib/resources/wikibase.ui.AliasesEditTool.js M lib/resources/wikibase.ui.PropertyEditTool.EditableAliases.js M lib/resources/wikibase.ui.PropertyEditTool.EditableValue.js M lib/tests/qunit/jquery.wikibase/toolbar/toolbareditgroup.tests.js M lib/tests/qunit/wikibase.ui.PropertyEditTool.EditableValue.tests.js M repo/resources/wikibase.ui.entityViewInit.js 10 files changed, 357 insertions(+), 172 deletions(-) Approvals: Tobias Gritschacher: Looks good to me, approved WikidataJenkins: Verified jenkins-bot: Verified diff --git a/lib/resources/Resources.php b/lib/resources/Resources.php index 79a83f1..0a0069f 100644 --- a/lib/resources/Resources.php +++ b/lib/resources/Resources.php @@ -463,10 +463,6 @@ 'wikibase.AbstractedRepoApi', ), 'messages' => array( - 'wikibase-cancel', - 'wikibase-edit', - 'wikibase-save', - 'wikibase-add', 'wikibase-save-inprogress', 'wikibase-remove-inprogress', 'wikibase-label-edit-placeholder', @@ -485,7 +481,6 @@ 'wikibase-sitelinks-sitename-columnheading-special', 'wikibase-sitelinks-siteid-columnheading', 'wikibase-sitelinks-link-columnheading', - 'wikibase-remove', 'wikibase-propertyedittool-full', 'wikibase-propertyedittool-counter-pending-tooltip', 'wikibase-propertyedittool-counter-entrieslabel', @@ -876,6 +871,12 @@ 'jquery.wikibase.toolbar', 'jquery.wikibase.wbtooltip', ), + 'messages' => array( + 'wikibase-cancel', + 'wikibase-edit', + 'wikibase-save', + 'wikibase-remove', + ), ), 'jquery.wikibase.wbtooltip' => $moduleTemplate + array( diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.claimlistview.js b/lib/resources/jquery.wikibase/jquery.wikibase.claimlistview.js index f202d0e..f7a2693 100644 --- a/lib/resources/jquery.wikibase/jquery.wikibase.claimlistview.js +++ b/lib/resources/jquery.wikibase/jquery.wikibase.claimlistview.js @@ -437,7 +437,8 @@ function( event ) { var $target = $( event.target ), statementview = $target.data( 'statementview' ), - $btnSave = $target.data( 'edittoolbar' ).toolbar.editGroup.$btnSave, + toolbar = $target.data( 'edittoolbar' ).toolbar, + $btnSave = toolbar.editGroup.getButton( 'save' ), btnSave = $btnSave.data( 'toolbarbutton' ); /** diff --git a/lib/resources/jquery.wikibase/toolbar/edittoolbar.js b/lib/resources/jquery.wikibase/toolbar/edittoolbar.js index 69d355f..17d0502 100644 --- a/lib/resources/jquery.wikibase/toolbar/edittoolbar.js +++ b/lib/resources/jquery.wikibase/toolbar/edittoolbar.js @@ -188,7 +188,7 @@ editGroup.toNonEditMode(); self.enable(); self.toggleActionMessage( function() { - editGroup.$btnEdit.focus(); + editGroup.getButton( 'edit' ).focus(); } ); } ) .on( prefix + 'afterstartediting ' + prefix + 'change', function( event ) { @@ -199,9 +199,9 @@ && self._interactionWidget.isInitialValue(); if ( self._interactionWidget.isValid() && !isEmpty && !isInitial ) { - editGroup.$btnSave.data( 'toolbarbutton' ).enable(); + editGroup.enableButton( 'save' ); } else { - editGroup.$btnSave.data( 'toolbarbutton' ).disable(); + editGroup.disableButton( 'save' ); } } ) .on( prefix + 'toggleerror', function( event, error ) { @@ -209,10 +209,10 @@ var $anchor; if ( error.action === 'save' ) { - $anchor = editGroup.$btnSave; + $anchor = editGroup.getButton( 'save' ); } if ( error.action === 'remove' ) { - $anchor = editGroup.$btnRemove; + $anchor = editGroup.getButton( 'remove '); } self.enable(); diff --git a/lib/resources/jquery.wikibase/toolbar/toolbareditgroup.js b/lib/resources/jquery.wikibase/toolbar/toolbareditgroup.js index 6176988..9bbcedd 100644 --- a/lib/resources/jquery.wikibase/toolbar/toolbareditgroup.js +++ b/lib/resources/jquery.wikibase/toolbar/toolbareditgroup.js @@ -1,8 +1,10 @@ /** - * * @licence GNU GPL v2+ * @author H. Snater < mediaw...@snater.com > * @author Daniel Werner + * + * @option {Object} buttonCharacteristics The characteristics of the edit group's buttons. + * Default: Object containing basic information to initialize the buttons. * * @option {boolean} displayRemoveButton Whether to display a "remove" button. * Default: false @@ -45,32 +47,39 @@ * @type {Object} */ options: { + buttonCharacteristics: { + edit: { + label: mw.msg( 'wikibase-edit' ), + href: 'javascript:void(0);', + cssClassExt: 'editbutton', + disabled: false + }, + save: { + label: mw.msg( 'wikibase-save' ), + href: 'javascript:void(0);', + cssClassExt: 'savebutton', + disabled: false + }, + remove: { + label: mw.msg( 'wikibase-remove' ), + href: 'javascript:void(0);', + cssClassExt: 'removebutton', + disabled: false + }, + cancel: { + label: mw.msg( 'wikibase-cancel' ), + href: 'javascript:void(0);', + cssClassExt: 'cancelbutton', + disabled: false + } + }, displayRemoveButton: false }, /** - * Edit button. - * @type {jQuery} + * @type {Object} */ - $btnEdit: null, - - /** - * Cancel button. - * @type {jQuery} - */ - $btnCancel: null, - - /** - * Save button. - * @type {jQuery} - */ - $btnSave: null, - - /** - * Remove button. - * @type {jQuery} - */ - $btnRemove: null, + _buttons: null, /** * Node holding the tooltips image with the tooltip itself attached. @@ -87,8 +96,23 @@ /** * @see jQuery.Widget._create + * + * @throws {Error} if any button characteristic is not defined. */ _create: function() { + var buttonCharacteristics = this.options.buttonCharacteristics; + + if( + !buttonCharacteristics.edit + || !buttonCharacteristics.save + || !buttonCharacteristics.remove + || !buttonCharacteristics.cancel + ) { + throw new Error( 'Incomplete button characteristics' ); + } + + this._buttons = {}; + PARENT.prototype._create.call( this ); this._initToolbar(); }, @@ -113,22 +137,16 @@ this.$tooltipAnchor = null; } - $.each( - [this.$btnEdit, this.$btnCancel, this.$btnSave, this.$btnRemove ], - function( i, $btn ) { - if ( $btn !== null ) { - if( $btn.data( 'toolbarbutton' ) ) { - $btn.data( 'toolbarbutton' ).destroy(); - } - $btn.remove(); + $.each( this._buttons, function( name, $button ) { + if ( $button !== null ) { + if( $button.data( 'toolbarbutton' ) ) { + $button.data( 'toolbarbutton' ).destroy(); } + $button.remove(); } - ); + } ); - this.$btnEdit = null; - this.$btnCancel = null; - this.$btnSave = null; - this.$btnRemove = null; + this._buttons = {}; }, /** @@ -154,40 +172,7 @@ // Tooltip anchor has no disabled/enabled behavior. .toolbarlabel( { stateChangeable: false } ); - // Now, create the buttons we need for basic editing: - this.$btnEdit = mw.template( - 'wikibase-toolbarbutton', - mw.msg( 'wikibase-edit' ), - 'javascript:void(0);' - ) - .toolbarbutton(); - this.$btnEdit.addClass( this.widgetBaseClass + '-editbutton' ); - - this.$btnCancel = mw.template( - 'wikibase-toolbarbutton', - mw.msg( 'wikibase-cancel' ), - 'javascript:void(0);' - ) - .toolbarbutton(); - this.$btnCancel.addClass( this.widgetBaseClass + '-cancelbutton' ); - - this.$btnSave = mw.template( - 'wikibase-toolbarbutton', - mw.msg( 'wikibase-save' ), - 'javascript:void(0);' - ) - .toolbarbutton(); - this.$btnSave.addClass( this.widgetBaseClass + '-savebutton' ); - - this.$btnRemove = mw.template( - 'wikibase-toolbarbutton', - mw.msg( 'wikibase-remove' ), - 'javascript:void(0);' - ) - .toolbarbutton(); - this.$btnRemove.addClass( this.widgetBaseClass + '-removebutton' ); - - this._attachEventHandlers(); + this._attachEventHandler(); this.toNonEditMode(); // Initialize the toolbar. }, @@ -196,34 +181,35 @@ * Attaches event handlers regarding the toolbar edit group. * @since 0.4 */ - _attachEventHandlers: function() { + _attachEventHandler: function() { var self = this; - this.$btnEdit.on( 'toolbarbuttonaction.' + this.widgetName, function( event ) { - self._trigger( 'edit', null, [ function() { self.toEditMode(); } ] ); - } ); - - this.$btnCancel.on( 'toolbarbuttonaction.' + this.widgetName, function( event ) { - self._trigger( 'cancel', null, [ function() { - if( self.element.data( 'toolbareditgroup' ) ) { - self.toNonEditMode(); - } - } ] ); - } ); - - this.$btnSave.on( 'toolbarbuttonaction.' + this.widgetName, function( event ) { - self._trigger( 'save', null, [ function() { - if( self.element.data( 'toolbareditgroup' ) && !self.isDisabled() ) { - // Only toggle EditGroup as long as it still exists. - // If edit group is disabled, the interaction target appears to be invalid and - // edit mode is supposed to persist. - self.toNonEditMode(); - } - } ] ); - } ); - - this.$btnRemove.on( 'toolbarbuttonaction.' + this.widgetName, function( event ) { - self._trigger( 'remove' ); + this.element.on( 'toolbarbuttonaction.' + this.widgetName, function( event ) { + switch( event.target ) { + case self._buttons.edit.get( 0 ): + self._trigger( 'edit', null, [ function() { self.toEditMode(); } ] ); + break; + case self._buttons.save.get( 0 ): + self._trigger( 'save', null, [ function() { + if( self.element.data( 'toolbareditgroup' ) && !self.isDisabled() ) { + // Only toggle EditGroup as long as it still exists. + // If edit group is disabled, the interaction target appears to be invalid and + // edit mode is supposed to persist. + self.toNonEditMode(); + } + } ] ); + break; + case self.options.displayRemoveButton && self._buttons.remove.get( 0 ): + self._trigger( 'remove' ); + break; + case self._buttons.cancel.get( 0 ): + self._trigger( 'cancel', null, [ function() { + if( self.element.data( 'toolbareditgroup' ) ) { + self.toNonEditMode(); + } + } ] ); + break; + } } ); }, @@ -234,12 +220,18 @@ * @since 0.4 */ toEditMode: function() { - this.innerGroup.removeElement( this.$btnEdit ); - this.innerGroup.addElement( this.$btnSave ); - if ( this.options.displayRemoveButton ) { - this.innerGroup.addElement( this.$btnRemove ); + if( this._buttons.edit ) { + this.innerGroup.removeElement( this._buttons.edit ); } - this.innerGroup.addElement( this.$btnCancel ); + + this.innerGroup.addElement( this.getButton( 'save' ) ); + + if ( this.options.displayRemoveButton ) { + this.innerGroup.addElement( this.getButton( 'remove' ) ); + } + + this.innerGroup.addElement( this.getButton( 'cancel' ) ); + this.addElement( this.$tooltipAnchor, 1 ); // Insert tooltip after interaction links. this.element @@ -257,18 +249,90 @@ toNonEditMode: function() { // Remove buttons for editing and display buttons for switching back to edit mode: this.removeElement( this.$tooltipAnchor ); - this.innerGroup.removeElement( this.$btnSave ); - if ( this.options.displayRemoveButton ) { - this.innerGroup.removeElement( this.$btnRemove ); + + if( this._buttons.save ) { + this.innerGroup.removeElement( this._buttons.save ); } - this.innerGroup.removeElement( this.$btnCancel ); - this.innerGroup.addElement( this.$btnEdit ); + + if ( this.options.displayRemoveButton && this._buttons.remove ) { + this.innerGroup.removeElement( this._buttons.remove ); + } + + if( this._buttons.cancel ) { + this.innerGroup.removeElement( this._buttons.cancel ); + } + + this.innerGroup.addElement( this.getButton( 'edit' ) ); this.element .removeClass( this.widgetBaseClass + '-ineditmode' ) .addClass( this.widgetBaseClass + '-innoneditmode' ); this.draw(); + }, + + /** + * Returns whether a specific button has been generated yet. + * @since 0.5 + * + * @param {string} buttonName "edit"|"save"|"remove"|"cancel" + * @return {boolean} + */ + hasButton: function( buttonName ) { + return !!this._buttons[buttonName]; + }, + + /** + * Returns a button by its name creating the button if it has not yet been created. + * @since 0.5 + * + * @param {string} buttonName "edit"|"save"|"remove"|"cancel" + * @return {jQuery} + */ + getButton: function( buttonName ) { + if( !this._buttons[buttonName] ) { + var buttonCharacteristics = this.options.buttonCharacteristics[buttonName]; + + this._buttons[buttonName] = mw.template( + 'wikibase-toolbarbutton', + buttonCharacteristics.label, + buttonCharacteristics.href + ) + .toolbarbutton() + .addClass( this.widgetBaseClass + '-' + buttonCharacteristics.cssClassExt ); + + if( buttonCharacteristics.disabled ) { + this._buttons[buttonName].data( 'toolbarbutton' ).disable(); + } + } + + return this._buttons[buttonName]; + }, + + /** + * Disables a particular edit group button. + * @since 0.5 + * + * @param {string} buttonName "edit"|"save"|"remove"|"cancel" + */ + disableButton: function( buttonName ) { + this.options.buttonCharacteristics[buttonName].disabled = true; + if( this._buttons[buttonName] ) { + this._buttons[buttonName].data( 'toolbarbutton' ).disable(); + } + }, + + /** + * Enables a particular edit group button. + * @since 0.5 + * + * @param {string} buttonName "edit"|"save"|"remove"|"cancel" + */ + enableButton: function( buttonName ) { + this.options.buttonCharacteristics[buttonName].disabled = false; + if( this._buttons[buttonName] ) { + this._buttons[buttonName].data( 'toolbarbutton' ).enable(); + } }, /** @@ -317,13 +381,13 @@ } ) ); // Clone buttons: - clone.$btnEdit = this.$btnEdit.data( 'toolbarbutton' ).clone(); - clone.$btnCancel = this.$btnCancel.data( 'toolbarbutton' ).clone(); - clone.$btnSave = this.$btnSave.data( 'toolbarbutton' ).clone(); - clone.$btnRemove = this.$btnRemove.data( 'toolbarbutton' ).clone(); + clone._buttons.edit = this._buttons.edit ? this._buttons.edit.data( 'toolbarbutton' ).clone() : null; + clone._buttons.cancel = this._buttons.cancel ? this._buttons.cancel.data( 'toolbarbutton' ).clone() : null; + clone._buttons.save = this._buttons.save ? this._buttons.save.data( 'toolbarbutton' ).clone() : null; + clone._buttons.remove = this._buttons.remove ? this._buttons.remove.data( 'toolbarbutton' ).clone() : null; // Re-attach event handlers: - clone._attachEventHandlers(); + clone._attachEventHandler(); // Re-init the toolbar contents' visibility: clone.toNonEditMode(); diff --git a/lib/resources/wikibase.ui.AliasesEditTool.js b/lib/resources/wikibase.ui.AliasesEditTool.js index 9296ab2..d4c740a 100644 --- a/lib/resources/wikibase.ui.AliasesEditTool.js +++ b/lib/resources/wikibase.ui.AliasesEditTool.js @@ -116,7 +116,7 @@ toolbar = PARENT.prototype._buildSingleValueToolbar.call( this, options ); // determine whether to show or hide the add button when cancelling edit mode - $( toolbar.$editGroup.data( 'toolbareditgroup' ).$btnCancel ).on( + $( toolbar.$editGroup.data( 'toolbareditgroup' ).getButton( 'cancel' ) ).on( 'toolbarbuttonaction', function( event ) { if ( self.getValues()[0].getValue()[0].length === 0 ) { // no aliases at all diff --git a/lib/resources/wikibase.ui.PropertyEditTool.EditableAliases.js b/lib/resources/wikibase.ui.PropertyEditTool.EditableAliases.js index d0fed1b..aa427ff 100644 --- a/lib/resources/wikibase.ui.PropertyEditTool.EditableAliases.js +++ b/lib/resources/wikibase.ui.PropertyEditTool.EditableAliases.js @@ -55,8 +55,7 @@ PARENT.prototype._interfaceHandler_onInputRegistered.call( this, relatedInterface ); // Always enable cancel button since it is alright to have an empty value: - var editGroup = this._toolbar.$editGroup.data( 'toolbareditgroup' ); - editGroup.$btnCancel.data( 'toolbarbutton' ).enable(); + this._toolbar.$editGroup.data( 'toolbareditgroup' ).enableButton( 'cancel' ); } }, @@ -126,7 +125,7 @@ // "remove" action is implied. But since there ist no "remove" button to attach an error // tooltip to, the "save" button shall be used even when a "remove" action has been // triggered. - $anchor = this._toolbar.$editGroup.data( 'toolbareditgroup' ).$btnSave; + $anchor = this._toolbar.$editGroup.data( 'toolbareditgroup' ).getButton( 'save' ); PARENT.prototype.showError.call( this, error, $anchor ); }, diff --git a/lib/resources/wikibase.ui.PropertyEditTool.EditableValue.js b/lib/resources/wikibase.ui.PropertyEditTool.EditableValue.js index ae621bf..fd153d0 100644 --- a/lib/resources/wikibase.ui.PropertyEditTool.EditableValue.js +++ b/lib/resources/wikibase.ui.PropertyEditTool.EditableValue.js @@ -284,7 +284,8 @@ var scrollTop = $( 'body' ).scrollTop(); // enable editing from the beginning if there is no value yet or pending value... - this._toolbar.$editGroup.data( 'toolbareditgroup' ).$btnEdit.trigger( 'click' ); + this._toolbar.$editGroup.data( 'toolbareditgroup' ) + .getButton( 'edit' ).trigger( 'click' ); this.removeFocus(); // ...but don't set focus there for now $( 'body' ).scrollTop( scrollTop ); } @@ -534,8 +535,8 @@ } ); var editGroup = this._toolbar.$editGroup.data( 'toolbareditgroup' ); - if( editGroup.$btnSave.data( 'wbtooltip' ) ) { - editGroup.$btnSave.data( 'wbtooltip' ).degrade(); + if( editGroup.hasButton( 'save' ) && editGroup.getButton( 'save' ).data( 'wbtooltip' ) ) { + editGroup.getButton( 'save' ).data( 'wbtooltip' ).degrade(); } editGroup.$tooltipAnchor.data( 'wbtooltip' ).hide(); @@ -646,8 +647,8 @@ var editGroup = self._toolbar.$editGroup.data( 'toolbareditgroup' ), action = ( apiAction === self.API_ACTION.SAVE ) ? 'save' : 'remove', $anchor = ( apiAction === self.API_ACTION.REMOVE ) - ? editGroup.$btnRemove - : editGroup.$btnSave, + ? editGroup.getButton( 'remove' ) + : editGroup.getButton( 'save' ), error = wb.RepoApiError.newFromApiResponse( textStatus, response, action ); self.showError( error, $anchor ); } ); @@ -947,8 +948,8 @@ !this.isPending() && this.isEmpty() && this.isNew(); var editGroup = this._toolbar.$editGroup.data( 'toolbareditgroup' ); - editGroup.$btnSave.data( 'toolbarbutton' )[ disableSave ? 'disable' : 'enable' ](); - editGroup.$btnCancel.data( 'toolbarbutton' )[ disableCancel ? 'disable' : 'enable' ](); + editGroup[ disableSave ? 'disableButton' : 'enableButton' ]( 'save' ); + editGroup[ disableCancel ? 'disableButton' : 'enableButton' ]( 'cancel' ); /** * Propagate stopping of edit mode (enabling other actions) when all editable value actions @@ -978,7 +979,8 @@ */ _interfaceHandler_onKeyUp: function( relatedInterface, event ) { if( event.which === $.ui.keyCode.ESCAPE ) { - this._toolbar.$editGroup.data( 'toolbareditgroup' ).$btnCancel.trigger( 'click' ); + this._toolbar.$editGroup.data( 'toolbareditgroup' ) + .getButton( 'cancel' ).trigger( 'click' ); } }, @@ -1004,10 +1006,10 @@ } // Value not modified yet, cancel button not available but enter should also stop // edit mode - simulating click on cancel link: - editGroup.$btnCancel.trigger( 'click' ); + editGroup.getButton( 'cancel' ).trigger( 'click' ); } else { // Try to save the value: - editGroup.$btnSave.trigger( 'click' ); + editGroup.getButton( 'save' ).trigger( 'click' ); } } }, diff --git a/lib/tests/qunit/jquery.wikibase/toolbar/toolbareditgroup.tests.js b/lib/tests/qunit/jquery.wikibase/toolbar/toolbareditgroup.tests.js index 5c03abe..ea8d0dd 100644 --- a/lib/tests/qunit/jquery.wikibase/toolbar/toolbareditgroup.tests.js +++ b/lib/tests/qunit/jquery.wikibase/toolbar/toolbareditgroup.tests.js @@ -1,6 +1,4 @@ /** - * @since 0.1 - * * @licence GNU GPL v2+ * @author H. Snater < mediaw...@snater.com > */ @@ -10,18 +8,18 @@ QUnit.module( 'jQuery.wikibase.toolbareditgroup', QUnit.newWbEnvironment() ); - QUnit.test( 'init check', function( assert ) { + QUnit.test( 'Create & Destroy', function( assert ) { var $editGroup = $( '<span/>' ).toolbareditgroup(), editGroup = $editGroup.data( 'toolbareditgroup' ); assert.ok( editGroup.innerGroup instanceof jQuery.wikibase.toolbar, - 'Initiated inner group.' + 'Created inner group.' ); assert.ok( editGroup.$tooltipAnchor instanceof $, - 'Initiated tooltip.' + 'Created tooltip.' ); assert.equal( @@ -31,23 +29,24 @@ ); assert.ok( - editGroup.$btnEdit.data( 'toolbarbutton' ) instanceof $.wikibase.toolbarbutton, - 'Initiated edit button.' + editGroup.hasButton( 'edit' ) && editGroup.getButton( 'edit' ).data( 'toolbarbutton' ) + instanceof $.wikibase.toolbarbutton, + 'Created edit button.' ); assert.ok( - editGroup.$btnCancel.data( 'toolbarbutton' ) instanceof $.wikibase.toolbarbutton, - 'Initiated cancel button.' + !editGroup.hasButton( 'btnCancel' ), + 'Cancel button not yet initialized.' ); assert.ok( - editGroup.$btnSave.data( 'toolbarbutton' ) instanceof $.wikibase.toolbarbutton, - 'Initiated save button.' + !editGroup.hasButton( 'btnSave' ), + 'Save button not yet initialized.' ); assert.ok( - editGroup.$btnRemove.data( 'toolbarbutton' ) instanceof $.wikibase.toolbarbutton, - 'Initiated remove button.' + !editGroup.hasButton( 'remove' ), + 'Remove button not yet initialized.' ); assert.equal( @@ -63,7 +62,7 @@ ); assert.equal( - editGroup.innerGroup.hasElement( editGroup.$btnEdit ), + editGroup.innerGroup.hasElement( editGroup.getButton( 'edit' ) ), true, 'Edit button is in inner group.' ); @@ -82,28 +81,147 @@ 'Destroyed tooltip.' ); - assert.equal( - editGroup.$btnEdit, - null, + assert.ok( + !editGroup.hasButton( 'edit' ), 'Destroyed edit button.' ); - assert.equal( - editGroup.$btnCancel, - null, - 'Destroyed cancel button.' + assert.ok( + !editGroup.hasButton( 'cancel' ), + 'Cancel button does not exist.' ); - assert.equal( - editGroup.$btnSave, - null, - 'Destroyed save button.' + assert.ok( + !editGroup.hasButton( 'save' ), + 'Save button does not exist.' ); - assert.equal( - editGroup.$btnRemove, - null, - 'Destroyed remove button.' + assert.ok( + !editGroup.hasButton( 'remove' ), + 'Remove button does not exist.' + ); + + } ); + + QUnit.test( 'toEditMode() & toNonEditMode()', function( assert ) { + var $editGroup = $( '<span/>' ).toolbareditgroup( { + displayRemoveButton: false + } ), + editGroup = $editGroup.data( 'toolbareditgroup' ); + + editGroup.toEditMode(); + + assert.ok( + editGroup.hasButton( 'edit' ), + 'Created edit button.' + ); + + assert.ok( + editGroup.hasButton( 'cancel' ), + 'Created cancel button.' + ); + + assert.ok( + editGroup.hasButton( 'save' ), + 'Created save button.' + ); + + assert.ok( + !editGroup.hasButton( 'remove' ), + 'Remove button is not initialized.' + ); + + assert.ok( + !editGroup.innerGroup.hasElement( editGroup.getButton( 'edit' ) ), + 'Edit button is not visible.' + ); + + assert.ok( + editGroup.innerGroup.hasElement( editGroup.getButton( 'save' ) ), + 'Save button is visible.' + ); + + assert.ok( + editGroup.innerGroup.hasElement( editGroup.getButton( 'cancel' ) ), + 'Cancel button is visible.' + ); + + assert.ok( + editGroup.hasElement( editGroup.$tooltipAnchor ), + 'Tooltip anchor is visible.' + ); + + editGroup.toNonEditMode(); + + assert.ok( + editGroup.innerGroup.hasElement( editGroup.getButton( 'edit' ) ), + 'Edit button is visible.' + ); + + assert.ok( + !editGroup.innerGroup.hasElement( editGroup.getButton( 'save' ) ), + 'Save button is not visible.' + ); + + assert.ok( + !editGroup.innerGroup.hasElement( editGroup.getButton( 'cancel' ) ), + 'Cancel button is not visible.' + ); + + assert.ok( + !editGroup.hasElement( editGroup.$tooltipAnchor ), + 'Tooltip anchor is not visible.' + ); + + } ); + + QUnit.test( 'Remove button handling on toEditMode() & toNonEditMode()', function( assert ) { + var $editGroup = $( '<span/>' ).toolbareditgroup( { + displayRemoveButton: true + } ), + editGroup = $editGroup.data( 'toolbareditgroup' ); + + editGroup.toEditMode(); + + assert.ok( + editGroup.hasButton( 'remove' ), + 'Created remove button.' + ); + + assert.ok( + editGroup.innerGroup.hasElement( editGroup.getButton( 'remove' ) ), + 'Remove button is visible.' + ); + + editGroup.toNonEditMode(); + + assert.ok( + !editGroup.innerGroup.hasElement( editGroup.getButton( 'remove' ) ), + 'Remove button is not visible.' + ); + + } ); + + QUnit.test( 'getButton()', function( assert ) { + var $editGroup = $( '<span/>' ).toolbareditgroup(), + editGroup = $editGroup.data( 'toolbareditgroup' ); + + assert.ok( + !editGroup.hasButton( 'save' ), + '"Save" button does not exist.' + ); + + var $btnSave = editGroup.getButton( 'save' ); + + assert.ok( + $btnSave instanceof jQuery && $btnSave.data( 'toolbarbutton' ) !== undefined, + 'Created "save" button.' + ); + + assert.strictEqual( + editGroup.getButton( 'save' ), + $btnSave, + 'Not re-instantiating button when re-retrieving it.' ); } ); diff --git a/lib/tests/qunit/wikibase.ui.PropertyEditTool.EditableValue.tests.js b/lib/tests/qunit/wikibase.ui.PropertyEditTool.EditableValue.tests.js index 9a8ebfc..06bca86 100644 --- a/lib/tests/qunit/wikibase.ui.PropertyEditTool.EditableValue.tests.js +++ b/lib/tests/qunit/wikibase.ui.PropertyEditTool.EditableValue.tests.js @@ -228,17 +228,17 @@ ); assert.ok( - editGroup.$btnSave.data( 'toolbarbutton' ).isDisabled(), + editGroup.getButton( 'save' ).data( 'toolbarbutton' ).isDisabled(), 'Save button is disabled.' ); assert.ok( - editGroup.$btnCancel.data( 'toolbarbutton' ).isDisabled(), + editGroup.getButton( 'cancel' ).data( 'toolbarbutton' ).isDisabled(), 'Cancel button is disabled.' ); assert.ok( - editGroup.$btnRemove.data( 'toolbarbutton' ).isEnabled(), + editGroup.getButton( 'remove' ).data( 'toolbarbutton' ).isEnabled(), 'Remove button is enabled.' ); @@ -481,7 +481,7 @@ ); assert.ok( - editGroup.$btnSave.data( 'wbtooltip' ) instanceof $.wikibase.wbtooltip, + editGroup.getButton( 'save' ).data( 'wbtooltip' ) instanceof $.wikibase.wbtooltip, 'attached tooltip to save button' ); @@ -514,7 +514,7 @@ ev.remove(); assert.ok( - editGroup.$btnRemove.data( 'wbtooltip' ) instanceof $.wikibase.wbtooltip, + editGroup.getButton( 'save' ).data( 'wbtooltip' ) instanceof $.wikibase.wbtooltip, 'attached tooltip to remove button after trying to remove with API action' ); @@ -523,7 +523,7 @@ ev.setValue( DATAVALUES.VALID[0] ); ev.stopEditing( true ); assert.equal( - editGroup.$btnSave.data( 'wbtooltip' ).options.content.message, + editGroup.getButton( 'save' ).data( 'wbtooltip' ).options.content.message, mw.msg( 'wikibase-error-save-generic' ), "when getting unknown error-code from API, generic message should be shown" ); @@ -533,7 +533,7 @@ ev.setValue( DATAVALUES.VALID[0] ); ev.stopEditing( true ); assert.equal( - editGroup.$btnSave.data( 'wbtooltip' ).options.content.message, + editGroup.getButton( 'save' ).data( 'wbtooltip' ).options.content.message, mw.msg( 'wikibase-error-ui-client-error' ), "when getting an registered error-code from API, the corresponding message should be shown" ); diff --git a/repo/resources/wikibase.ui.entityViewInit.js b/repo/resources/wikibase.ui.entityViewInit.js index 3ffbb4f..693fe42 100644 --- a/repo/resources/wikibase.ui.entityViewInit.js +++ b/repo/resources/wikibase.ui.entityViewInit.js @@ -247,7 +247,7 @@ content: $message, permanent: true, gravity: gravity, - $anchor: toolbar.$btnSave + $anchor: toolbar.getButton( 'save' ) } ); $hideMessage.on( 'click', function( event ) { -- To view, visit https://gerrit.wikimedia.org/r/101198 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I80a5f17c24b20c230062630193b3d3360de77697 Gerrit-PatchSet: 9 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Henning Snater <henning.sna...@wikimedia.de> Gerrit-Reviewer: Adrian Lang <adrian.l...@wikimedia.de> Gerrit-Reviewer: Tobias Gritschacher <tobias.gritschac...@wikimedia.de> Gerrit-Reviewer: WikidataJenkins <wikidata-servi...@wikimedia.de> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits