2021-06-24 16:21:31 +00:00
|
|
|
var
|
2022-02-19 04:03:09 +00:00
|
|
|
// LanguageData::getLocalData()
|
2021-07-22 20:20:16 +00:00
|
|
|
parserData = require( './parser/data.json' ),
|
2021-06-24 16:21:31 +00:00
|
|
|
utils = require( './utils.js' );
|
2021-11-05 19:20:42 +00:00
|
|
|
var featuresEnabled = mw.config.get( 'wgDiscussionToolsFeaturesEnabled' ) || {};
|
2021-06-24 16:21:31 +00:00
|
|
|
|
|
|
|
function ReplyLinksController( $pageContainer ) {
|
|
|
|
// Mixin constructors
|
|
|
|
OO.EventEmitter.call( this );
|
|
|
|
|
|
|
|
this.$pageContainer = $pageContainer;
|
2021-06-24 19:10:40 +00:00
|
|
|
this.$body = $( document.body );
|
2021-06-24 16:21:31 +00:00
|
|
|
this.onReplyLinkClickHandler = this.onReplyLinkClick.bind( this );
|
|
|
|
this.onAddSectionLinkClickHandler = this.onAddSectionLinkClick.bind( this );
|
2021-06-24 19:10:40 +00:00
|
|
|
this.onAnyLinkClickHandler = this.onAnyLinkClick.bind( this );
|
2021-06-24 16:21:31 +00:00
|
|
|
|
|
|
|
// Reply links
|
|
|
|
this.$replyLinks = $pageContainer.find( 'a.ext-discussiontools-init-replylink-reply[data-mw-comment]' );
|
|
|
|
this.$replyLinks.on( 'click keypress', this.onReplyLinkClickHandler );
|
|
|
|
|
|
|
|
// "Add topic" link in the skin interface
|
2021-10-13 19:59:47 +00:00
|
|
|
if ( featuresEnabled.newtopictool ) {
|
2021-06-24 16:21:31 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
|
|
|
var $addSectionTab = $( '#ca-addsection' );
|
2021-07-29 06:12:10 +00:00
|
|
|
if ( $addSectionTab.length ) {
|
2021-06-24 16:21:31 +00:00
|
|
|
this.$addSectionLink = $addSectionTab.find( 'a' );
|
|
|
|
this.$addSectionLink.on( 'click keypress', this.onAddSectionLinkClickHandler );
|
|
|
|
}
|
2021-04-29 14:24:49 +00:00
|
|
|
// Handle events on all links that potentially open the new section interface,
|
|
|
|
// including links in the page content (from templates) or from gadgets.
|
|
|
|
this.$body.on( 'click keypress', 'a:not( [data-mw-comment] )', this.onAnyLinkClickHandler );
|
2021-06-24 16:21:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OO.initClass( ReplyLinksController );
|
|
|
|
OO.mixinClass( ReplyLinksController, OO.EventEmitter );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event link-click
|
|
|
|
* @param {string} id
|
|
|
|
* $@param {jQuery} $link
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
ReplyLinksController.prototype.onReplyLinkClick = function ( e ) {
|
|
|
|
if ( !this.isActivationEvent( e ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
e.preventDefault();
|
|
|
|
|
2021-09-30 20:53:50 +00:00
|
|
|
// Browser plugins (such as Google Translate) may add extra tags inside
|
|
|
|
// the link, so find the containing link tag with the data we need.
|
|
|
|
var $link = $( e.target ).closest( 'a[data-mw-comment]' );
|
|
|
|
if ( !$link.length ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.emit( 'link-click', $link.data( 'mw-comment' ).id, $link );
|
2021-06-24 16:21:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ReplyLinksController.prototype.onAddSectionLinkClick = function ( e ) {
|
|
|
|
if ( !this.isActivationEvent( e ) ) {
|
|
|
|
return;
|
|
|
|
}
|
2021-06-30 16:00:34 +00:00
|
|
|
// Disable VisualEditor's new section editor (in wikitext mode / NWE), to allow our own.
|
|
|
|
// We do this on first click, because we don't control the order in which our code and NWE code
|
|
|
|
// runs, so its event handlers may not be registered yet.
|
|
|
|
$( e.target ).closest( '#ca-addsection' ).off( '.ve-target' );
|
|
|
|
|
2021-06-24 19:10:40 +00:00
|
|
|
// onAnyLinkClick() will also handle clicks on this element, so we don't emit() here to avoid
|
|
|
|
// doing it twice.
|
|
|
|
};
|
|
|
|
|
|
|
|
ReplyLinksController.prototype.onAnyLinkClick = function ( e ) {
|
|
|
|
// Check query parameters to see if this is really a new topic link
|
2022-03-29 21:25:21 +00:00
|
|
|
var href = e.currentTarget.href;
|
|
|
|
if ( !href ) {
|
2021-06-24 19:10:40 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-03-29 21:25:21 +00:00
|
|
|
var url = new URL( href );
|
2021-07-22 20:20:16 +00:00
|
|
|
|
2022-02-21 22:07:38 +00:00
|
|
|
var title = mw.Title.newFromText( utils.getTitleFromUrl( href ) || '' );
|
2021-07-22 20:20:16 +00:00
|
|
|
if ( !title ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recognize links to add a new topic:
|
|
|
|
if (
|
|
|
|
// Special:NewSection/...
|
|
|
|
title.getNamespaceId() === mw.config.get( 'wgNamespaceIds' ).special &&
|
|
|
|
title.getMainText().split( '/' )[ 0 ] === parserData.specialNewSectionName
|
|
|
|
) {
|
|
|
|
// Get the real title from the subpage parameter
|
|
|
|
var param = title.getMainText().slice( parserData.specialNewSectionName.length + 1 );
|
|
|
|
title = mw.Title.newFromText( param );
|
|
|
|
if ( !title ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (
|
|
|
|
// ?title=...&action=edit§ion=new
|
|
|
|
// ?title=...&veaction=editsource§ion=new
|
2022-03-29 21:25:21 +00:00
|
|
|
( url.searchParams.get( 'action' ) === 'edit' || url.searchParams.get( 'veaction' ) === 'editsource' ) &&
|
|
|
|
url.searchParams.get( 'section' ) === 'new' &&
|
|
|
|
url.searchParams.get( 'dtenable' ) !== '0'
|
2021-07-22 20:20:16 +00:00
|
|
|
) {
|
|
|
|
// Do nothing
|
|
|
|
|
|
|
|
} else {
|
2021-06-24 19:10:40 +00:00
|
|
|
// Not a link to add a new topic
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-22 20:20:16 +00:00
|
|
|
if ( title.getPrefixedDb() !== mw.config.get( 'wgRelevantPageName' ) ) {
|
2021-06-24 19:10:40 +00:00
|
|
|
// Link to add a section on another page, not supported yet (T282205)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-29 21:25:21 +00:00
|
|
|
if (
|
|
|
|
url.searchParams.get( 'editintro' ) || url.searchParams.get( 'preload' ) ||
|
|
|
|
url.searchParams.getAll( 'preloadparams[]' ).length || url.searchParams.get( 'preloadtitle' )
|
|
|
|
) {
|
2021-06-24 19:10:40 +00:00
|
|
|
// Adding a new topic with preloaded text is not supported yet (T269310)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !this.isActivationEvent( e ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
this.emit( 'link-click', utils.NEW_TOPIC_COMMENT_ID, $( e.currentTarget ) );
|
2021-06-24 16:21:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ReplyLinksController.prototype.isActivationEvent = function ( e ) {
|
|
|
|
if ( e.type === 'keypress' && e.which !== OO.ui.Keys.ENTER && e.which !== OO.ui.Keys.SPACE ) {
|
|
|
|
// Only handle keypresses on the "Enter" or "Space" keys
|
|
|
|
return false;
|
|
|
|
}
|
2022-02-06 23:38:53 +00:00
|
|
|
if ( e.type === 'click' && !utils.isUnmodifiedLeftClick( e ) ) {
|
2021-06-24 16:21:31 +00:00
|
|
|
// Only handle unmodified left clicks
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
ReplyLinksController.prototype.focusLink = function ( $link ) {
|
|
|
|
if ( $link.is( this.$replyLinks ) ) {
|
|
|
|
$link.trigger( 'focus' );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ReplyLinksController.prototype.setActiveLink = function ( $link ) {
|
|
|
|
this.$activeLink = $link;
|
|
|
|
|
|
|
|
if ( this.$activeLink.is( this.$replyLinks ) ) {
|
|
|
|
this.$activeLink.closest( '.ext-discussiontools-init-replylink-buttons' )
|
|
|
|
.addClass( 'ext-discussiontools-init-replylink-active' );
|
2021-07-28 10:36:58 +00:00
|
|
|
} else if ( this.$addSectionLink && this.$activeLink.is( this.$addSectionLink ) ) {
|
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
|
|
|
$( '#ca-addsection' ).addClass( 'selected' );
|
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
|
|
|
$( '#ca-view' ).removeClass( 'selected' );
|
2021-06-24 16:21:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.$pageContainer.addClass( 'ext-discussiontools-init-replylink-open' );
|
|
|
|
this.$replyLinks.attr( {
|
|
|
|
tabindex: '-1'
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Suppress page takeover behavior for VE editing so that our unload
|
|
|
|
// handler can warn of data loss.
|
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
|
|
|
$( '#ca-edit, #ca-ve-edit, .mw-editsection a, #ca-addsection' ).off( '.ve-target' );
|
|
|
|
};
|
|
|
|
|
|
|
|
ReplyLinksController.prototype.clearActiveLink = function () {
|
|
|
|
if ( this.$activeLink.is( this.$replyLinks ) ) {
|
|
|
|
this.$activeLink.closest( '.ext-discussiontools-init-replylink-buttons' )
|
|
|
|
.removeClass( 'ext-discussiontools-init-replylink-active' );
|
2021-07-28 10:36:58 +00:00
|
|
|
} else if ( this.$addSectionLink && this.$activeLink.is( this.$addSectionLink ) ) {
|
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
|
|
|
$( '#ca-addsection' ).removeClass( 'selected' );
|
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
|
|
|
$( '#ca-view' ).addClass( 'selected' );
|
2021-06-24 16:21:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.$pageContainer.removeClass( 'ext-discussiontools-init-replylink-open' );
|
|
|
|
this.$replyLinks.attr( {
|
|
|
|
tabindex: '0'
|
|
|
|
} );
|
|
|
|
|
|
|
|
// We deliberately mangled edit links earlier so VE can't steal our page;
|
|
|
|
// have it redo setup to fix those.
|
|
|
|
if ( mw.libs.ve && mw.libs.ve.setupEditLinks ) {
|
|
|
|
mw.libs.ve.setupEditLinks();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$activeLink = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
ReplyLinksController.prototype.teardown = function () {
|
|
|
|
if ( this.$activeLink ) {
|
|
|
|
this.clearActiveLink();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$replyLinks.off( 'click keypress', this.onReplyLinkClickHandler );
|
2021-11-05 19:20:42 +00:00
|
|
|
if ( featuresEnabled.newtopictool ) {
|
|
|
|
if ( this.$addSectionLink ) {
|
|
|
|
this.$addSectionLink.off( 'click keypress', this.onAddSectionLinkClickHandler );
|
|
|
|
}
|
2021-07-20 20:32:25 +00:00
|
|
|
this.$body.off( 'click keypress', 'a:not( [data-mw-comment] )', this.onAnyLinkClickHandler );
|
|
|
|
}
|
2021-06-24 16:21:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = ReplyLinksController;
|