2021-06-24 16:21:31 +00:00
|
|
|
var
|
2021-07-22 20:20:16 +00:00
|
|
|
// Data::getLocalData()
|
|
|
|
parserData = require( './parser/data.json' ),
|
2021-06-24 16:21:31 +00:00
|
|
|
utils = require( './utils.js' );
|
|
|
|
|
|
|
|
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
|
|
|
|
var featuresEnabled = mw.config.get( 'wgDiscussionToolsFeaturesEnabled' ) || {};
|
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-06-24 19:10:40 +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
|
|
|
|
var href = $( e.currentTarget ).attr( 'href' );
|
|
|
|
var uri;
|
|
|
|
try {
|
|
|
|
uri = new mw.Uri( href, { arrayParams: true } );
|
|
|
|
} catch ( err ) {
|
|
|
|
// T106244: URL encoded values using fallback 8-bit encoding (invalid UTF-8) cause mediawiki.Uri to crash
|
|
|
|
return;
|
|
|
|
}
|
2021-07-22 20:20:16 +00:00
|
|
|
|
|
|
|
var title = utils.getTitleFromUrl( href );
|
|
|
|
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
|
|
|
|
( uri.query.action === 'edit' || uri.query.veaction === 'editsource' ) && uri.query.section === 'new'
|
|
|
|
) {
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( uri.query.editintro || uri.query.preload || uri.query.preloadparams || uri.query.preloadtitle ) {
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
if ( e.type === 'click' && ( e.which !== OO.ui.MouseButtons.LEFT || e.shiftKey || e.altKey || e.ctrlKey || e.metaKey ) ) {
|
|
|
|
// 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-07-20 20:32:25 +00:00
|
|
|
if ( this.$addSectionLink ) {
|
|
|
|
this.$addSectionLink.off( 'click keypress', this.onAddSectionLinkClickHandler );
|
|
|
|
this.$body.off( 'click keypress', 'a:not( [data-mw-comment] )', this.onAnyLinkClickHandler );
|
|
|
|
}
|
2021-06-24 16:21:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = ReplyLinksController;
|