MarkTraceur has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/100610


Change subject: Move clearInterface things to mw.LightboxInterface
......................................................................

Move clearInterface things to mw.LightboxInterface

Also fixes a bug where we didn't clear our arrow key listeners, and VE
would sometimes see lightbox loads accidentally.

+tests for .empty()

Bug: 58107
(cherry-picked from commit 1411110b3d585d3b8c4cf62e4e3f891155ac3279)

Change-Id: I58fbd2e5e3cfbf1f2dfe8ab9f6b2900152215ddf
---
M resources/ext.multimediaViewer/ext.multimediaViewer.js
M resources/ext.multimediaViewer/ext.multimediaViewer.lightboxinterface.js
M tests/qunit/lightboxinterface.test.js
3 files changed, 101 insertions(+), 29 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MultimediaViewer 
refs/changes/10/100610/1

diff --git a/resources/ext.multimediaViewer/ext.multimediaViewer.js 
b/resources/ext.multimediaViewer/ext.multimediaViewer.js
index 382c4d3..72e46d0 100755
--- a/resources/ext.multimediaViewer/ext.multimediaViewer.js
+++ b/resources/ext.multimediaViewer/ext.multimediaViewer.js
@@ -166,32 +166,6 @@
                                this.$imageMetadata.show();
                        }
                } );
-
-               lightboxHooks.register( 'clearInterface', function () {
-                       this.$license.empty().addClass( 'empty' );
-
-                       this.$imageDesc.empty();
-                       this.$imageDescDiv.addClass( 'empty' );
-                       this.$title.empty();
-                       this.$credit.empty().addClass( 'empty' );
-
-                       this.$username.empty();
-                       this.$usernameLi.addClass( 'empty' );
-
-                       this.$repo.empty();
-                       this.$repoLi.addClass( 'empty' );
-
-                       this.$datetime.empty();
-                       this.$datetimeLi.addClass( 'empty' );
-
-                       this.$useFile.data( 'title', null );
-                       this.$useFile.data( 'link', null );
-                       this.$useFile.data( 'src', null );
-
-                       this.$useFileLi.addClass( 'empty' );
-
-                       this.$imageDiv.addClass( 'empty' );
-               } );
        }
 
        MMVP = MultimediaViewer.prototype;
diff --git 
a/resources/ext.multimediaViewer/ext.multimediaViewer.lightboxinterface.js 
b/resources/ext.multimediaViewer/ext.multimediaViewer.lightboxinterface.js
index 3acae01..d9ea352 100644
--- a/resources/ext.multimediaViewer/ext.multimediaViewer.lightboxinterface.js
+++ b/resources/ext.multimediaViewer/ext.multimediaViewer.lightboxinterface.js
@@ -28,6 +28,41 @@
 
        LIP = LightboxInterface.prototype;
 
+       LIP.empty = function () {
+               if ( this.handleKeyDown ) {
+                       // Clear events on document
+                       $( document ).off( 'keydown', this.handleKeyDown );
+                       this.handleKeyDown = undefined;
+               }
+
+               this.$license.empty().addClass( 'empty' );
+
+               this.$imageDesc.empty();
+               this.$imageDescDiv.addClass( 'empty' );
+               this.$title.empty();
+               this.$credit.empty().addClass( 'empty' );
+
+               this.$username.empty();
+               this.$usernameLi.addClass( 'empty' );
+
+               this.$repo.empty();
+               this.$repoLi.addClass( 'empty' );
+
+               this.$datetime.empty();
+               this.$datetimeLi.addClass( 'empty' );
+
+               this.$useFile.data( 'title', null );
+               this.$useFile.data( 'link', null );
+               this.$useFile.data( 'src', null );
+               this.$useFile.data( 'isLocal', null );
+
+               this.$useFileLi.addClass( 'empty' );
+
+               this.$imageDiv.addClass( 'empty' );
+
+               MLBInterface.prototype.empty.call( this );
+       };
+
        LIP.load = function ( image ) {
                var hashFragment = '#mediaviewer/' + 
mw.mediaViewer.currentImageFilename + '/' + 
mw.mediaViewer.lightbox.currentIndex;
 
@@ -344,7 +379,7 @@
        };
 
        LIP.initializeNavigation = function () {
-               function handleKeyDown( e ) {
+               this.handleKeyDown = this.handleKeyDown || function ( e ) {
                        var isRtl = $( document.body ).hasClass( 'rtl' );
 
                        switch ( e.keyCode ) {
@@ -365,7 +400,7 @@
                                        }
                                        break;
                        }
-               }
+               };
 
                this.$nextButton = $( '<div>' )
                        .addClass( 'mw-mlb-next-image disabled' )
@@ -383,7 +418,7 @@
                        } )
                        .appendTo( this.$main );
 
-               $( document ).off( 'keydown', handleKeyDown ).on( 'keydown', 
handleKeyDown );
+               $( document ).off( 'keydown', this.handleKeyDown ).on( 
'keydown', this.handleKeyDown );
        };
 
        window.LightboxInterface = LightboxInterface;
diff --git a/tests/qunit/lightboxinterface.test.js 
b/tests/qunit/lightboxinterface.test.js
index 70c0ac8..c1a36e3 100644
--- a/tests/qunit/lightboxinterface.test.js
+++ b/tests/qunit/lightboxinterface.test.js
@@ -1,4 +1,25 @@
 ( function ( mw, $ ) {
+       var thingsShouldBeEmptied = [
+                       '$license',
+                       '$imageDesc',
+                       '$title',
+                       '$credit',
+                       '$username',
+                       '$repo',
+                       '$datetime'
+               ],
+
+               thingsShouldHaveEmptyClass = [
+                       '$license',
+                       '$imageDescDiv',
+                       '$credit',
+                       '$usernameLi',
+                       '$repoLi',
+                       '$datetimeLi',
+                       '$useFileLi',
+                       '$imageDiv'
+               ];
+
        QUnit.module( 'ext.multimediaViewer.lightboxInterface', 
QUnit.newMwEnvironment() );
 
        QUnit.test( 'Sanity test, object creation and ui construction', 9, 
function ( assert ) {
@@ -26,4 +47,46 @@
                // UI areas not attached to the document anymore.
                checkIfUIAreasAttachedToDocument(0);
        } );
+
+       QUnit.asyncTest( 'Check we are saving the resize listener', 2, function 
( assert ) {
+               var img = new 
window.LightboxImage('http://en.wikipedia.org/w/skins/vector/images/search-ltr.png'),
+                       lightbox = new window.LightboxInterface.BaseClass();
+
+               // resizeListener not saved yet
+               assert.strictEqual( this.resizeListener, undefined, 'Listener 
is not saved yet' );
+
+               // Save original loadCallback
+               lightbox.originalLoadCallback = lightbox.loadCallback;
+
+               // Mock loadCallback
+               lightbox.loadCallback = function ( image, ele ) {
+                       // Call original loadCallback
+                       this.originalLoadCallback( image, ele );
+
+                       // resizeListener should have been saved
+                       assert.notStrictEqual( this.resizeListener, undefined, 
'Saved listener !' );
+                       QUnit.start();
+               };
+
+               lightbox.load(img);
+       } );
+
+       QUnit.test( 'The interface is emptied properly when necessary', 
thingsShouldBeEmptied.length + thingsShouldHaveEmptyClass.length + 2, function 
( assert ) {
+               var i,
+                       lightbox = new window.LightboxInterface();
+
+               assert.notStrictEqual( lightbox.handleKeyDown, undefined, 'The 
keydown handler is present before empty() gets called' );
+
+               lightbox.empty();
+
+               for ( i = 0; i < thingsShouldBeEmptied.length; i++ ) {
+                       assert.strictEqual( 
lightbox[thingsShouldBeEmptied[i]].text(), '', 'We successfully emptied the ' + 
thingsShouldBeEmptied[i] + ' element' );
+               }
+
+               for ( i = 0; i < thingsShouldHaveEmptyClass.length; i++ ) {
+                       assert.strictEqual( 
lightbox[thingsShouldHaveEmptyClass[i]].hasClass( 'empty' ), true, 'We 
successfully applied the empty class to the ' + thingsShouldHaveEmptyClass[i] + 
' element' );
+               }
+
+               assert.strictEqual( lightbox.handleKeyDown, undefined, 'The 
keydown handler got removed' );
+       } );
 }( mediaWiki, jQuery ) );

-- 
To view, visit https://gerrit.wikimedia.org/r/100610
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I58fbd2e5e3cfbf1f2dfe8ab9f6b2900152215ddf
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MultimediaViewer
Gerrit-Branch: master
Gerrit-Owner: MarkTraceur <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to