MediaWiki:Gadget-FDVEDiagramLink.js: Difference between revisions

Recognise {{Diagram|page=}} as well as the bare parser function, so converted embeds keep double-click-to-edit
Restore "Change which diagram is shown" for template-wrapped embeds, where the page name is now an editable parameter
 
Line 73: Line 73:
*/
*/
function diagramPageFromModel( model ) {
function diagramPageFromModel( model ) {
var info = diagramInfoFromModel( model );
return info ? info.page : null;
}
/**
* @param {ve.dm.MWTransclusionNode} model
* @return {Object|null} { page: string, isTemplate: boolean }, or null.
*  isTemplate distinguishes {{Diagram|page=}} - whose page name is an
*  editable parameter - from a bare {{#display_diagram:}}, whose page name
*  is part of the parser-function target and cannot be edited in VE.
*/
function diagramInfoFromModel( model ) {
var mwData, parts, i, part, target, params, wt, colon;
var mwData, parts, i, part, target, params, wt, colon;


Line 102: Line 114:
wt = ( wt === undefined || wt === null ) ? '' : String( wt ).trim();
wt = ( wt === undefined || wt === null ) ? '' : String( wt ).trim();
if ( wt ) {
if ( wt ) {
return wt;
return { page: wt, isTemplate: true };
}
}
continue;
continue;
Line 119: Line 131:
wt = wt.slice( colon + 1 ).trim();
wt = wt.slice( colon + 1 ).trim();
if ( wt ) {
if ( wt ) {
return wt;
return { page: wt, isTemplate: false };
}
}
}
}
Line 201: Line 213:


FDDiagramContextItem.prototype.renderBody = function () {
FDDiagramContextItem.prototype.renderBody = function () {
var page = diagramPageFromModel( this.model ),
var item = this,
info = diagramInfoFromModel( this.model ),
url = this.getDiagramEditUrl(),
url = this.getDiagramEditUrl(),
$edit;
$edit, $change;


if ( !page || !url ) {
if ( !info || !url ) {
// Should be unreachable - isCompatibleWith already required a page.
// Should be unreachable - isCompatibleWith already required a page.
FDDiagramContextItem.super.prototype.renderBody.call( this );
FDDiagramContextItem.super.prototype.renderBody.call( this );
Line 211: Line 224:
}
}


// A plain anchor is fine here: the context popup is outside the
// Plain anchors are fine here: the context popup lives OUTSIDE the
// contenteditable surface, so VE does not suppress the click.
// contenteditable surface, so VE does not suppress the click the way it
// does inside the node itself.
$edit = $( '<a>' )
$edit = $( '<a>' )
.addClass( 'fd-ve-diagramContextItem-edit' )
.addClass( 'fd-ve-diagramContextItem-edit' )
.attr( { target: '_blank', rel: 'noopener' } )
.attr( { target: '_blank', rel: 'noopener' } )
.text( 'Edit “' + page.replace( /_/g, ' ' ) + '” ↗' );
.text( 'Edit “' + info.page.replace( /_/g, ' ' ) + '” ↗' );
ve.setAttributeSafe( $edit[ 0 ], 'href', url, '#' );
ve.setAttributeSafe( $edit[ 0 ], 'href', url, '#' );


// Deliberately the only control here. There was also a "Change which
// diagram is shown" link that fell through to the stock transclusion
// dialog, but that dialog is empty for this node: #display_diagram is a
// parser function, so the diagram page is part of the target string
// rather than a parameter, and VE sources its parameter list from
// TemplateData, which a parser function has none of. It looked
// actionable and was not. To point an article at a different diagram,
// edit the page source - it is one obvious line of wikitext.
this.$body.empty().append( $( '<div>' ).append( $edit ) );
this.$body.empty().append( $( '<div>' ).append( $edit ) );
// Offered ONLY for {{Diagram|page=}}, where the page name is a real
// template parameter that VE's transclusion dialog can edit. For a bare
// {{#display_diagram:}} the same dialog opens empty - the page name is part
// of the parser-function target, and VE has no setter for a target - so
// showing this there would be a control that looks actionable and is not.
//
// This item replaces the stock transclusion context item (ModeledFactory
// keeps only the most specific class in an inheritance chain), so without
// this link there is no route at all to the parameter dialog.
if ( info.isTemplate ) {
$change = $( '<a>' )
.addClass( 'fd-ve-diagramContextItem-change' )
.attr( 'href', '#' )
.text( 'Change which diagram is shown' )
.on( 'click', function ( e ) {
e.preventDefault();
ve.ui.MWTransclusionContextItem.prototype.onEditButtonClick.call( item );
} );
this.$body.append( $( '<div>' ).append( $change ) );
}
};
};