merged master
[lhc/web/wiklou.git] / resources / mediawiki.action / mediawiki.action.view.rightClickEdit.js
1 /*
2 * JavaScript to enable right click edit functionality.
3 * When the user right-clicks in a heading, it will open the
4 * edit screen.
5 */
6 jQuery( function ( $ ) {
7 // Select all h1-h6 elements that contain editsection links
8 // Don't use the ":has:(.editsection a)" selector because it performs very bad.
9 // http://jsperf.com/jq-1-7-2-vs-jq-1-8-1-performance-of-mw-has/2
10 $( document ).on( 'contextmenu', 'h1, h2, h3, h4, h5, h6', function ( e ) {
11 var $edit, href;
12
13 $edit = $( this ).find( '.editsection a' );
14 if ( !$edit.length ) {
15 return;
16 }
17
18 // Get href of the editsection link
19 href = $edit.prop( 'href' );
20
21 // Headings can contain rich text.
22 // Make sure to not block contextmenu events on (other) anchor tags
23 // inside the heading (e.g. to do things like copy URL, open in new tab, ..).
24 // e.target can be the heading, but it can also be anything inside the heading.
25 if ( href && e.target.nodeName.toLowerCase() !== 'a' ) {
26 window.location = href;
27 e.preventDefault();
28 }
29 } );
30 } );