mw.widgets.TitleOptionWidget: Prevent page navigation by clicking labels
authorBartosz Dziewoński <matma.rex@gmail.com>
Tue, 29 Sep 2015 15:09:04 +0000 (17:09 +0200)
committerBartosz Dziewoński <matma.rex@gmail.com>
Tue, 29 Sep 2015 15:09:04 +0000 (17:09 +0200)
However, allow opening the link in a new tab.

The same code is used in other components, e.g. jquery.suggestions.

Follow-up to ba358947bfb81b58d64489063bd4be816d1a46da.

Change-Id: Ie6840a20c1cc1a10fb1fb74717982dbe7e331d36

resources/src/mediawiki.widgets/mw.widgets.TitleOptionWidget.js

index 138715a..9b0b270 100644 (file)
                this.$label.attr( 'href', config.url );
                this.$element.addClass( 'mw-widget-titleOptionWidget' );
 
+               // Allow opening the link in new tab, but not regular navigation.
+               this.$label.on( 'click', function ( e ) {
+                       // Do not interfere with non-left clicks or if modifier keys are pressed (e.g. ctrl-click).
+                       if ( !( e.which !== 1 || e.altKey || e.ctrlKey || e.shiftKey || e.metaKey ) ) {
+                               e.preventDefault();
+                       }
+               } );
+
                // Highlight matching parts of link suggestion
                this.$label.autoEllipsis( { hasSpan: false, tooltip: true, matchText: config.query } );