Make link annotations more flexible

* When ve.ui.MWLinkAnnotationInspector is being initialized,
internal and external annotation inspectors are hardcoded to
new ve.ui.MWInternalLinkAnnotationWidget and
new ve.ui.MWExternalLinkAnnotationWidget. Make this creation
more flexible by creating these inspectors through a method,
which inheriting classes can override.
* In ve.ui.MWLinkAnnotationInspector.getAnnotationFromFragment,
factor out the creation of link annotations, so overriding
classes have the ability to provide different internal and
external annotations.
* In newFromTitle, static method of MWInternalLinkAnnotation,
creation of `element` isn't flexible for reusability with
slight changes to attributes passed to the constructor. By
factoring out the creation of attributes, inheriting classes
can reuse the existing structure and alter the attributes if
needed.

Bug: T195064
Change-Id: I2037464a7be77783837e9810691c8e372c8197c6
This commit is contained in:
petarpetkovic 2018-06-18 13:09:03 +02:00
parent 53f8f32895
commit 50d556e8d2
2 changed files with 55 additions and 10 deletions

View file

@ -51,13 +51,13 @@ ve.dm.MWInternalLinkAnnotation.static.toDataElement = function ( domElements, co
};
/**
* Build a ve.dm.MWInternalLinkAnnotation from a given mw.Title.
* Build element from a given mw.Title and raw title
*
* @param {mw.Title} title The title to link to.
* @param {string} [rawTitle] String from which the title was created
* @return {ve.dm.MWInternalLinkAnnotation} The annotation.
* @return {Object} The element.
*/
ve.dm.MWInternalLinkAnnotation.static.newFromTitle = function ( title, rawTitle ) {
ve.dm.MWInternalLinkAnnotation.static.dataElementFromTitle = function ( title, rawTitle ) {
var element,
target = title.toText(),
namespaceIds = mw.config.get( 'wgNamespaceIds' );
@ -73,16 +73,31 @@ ve.dm.MWInternalLinkAnnotation.static.newFromTitle = function ( title, rawTitle
}
element = {
type: 'link/mwInternal',
type: this.name,
attributes: {
title: target,
normalizedTitle: ve.dm.MWInternalLinkAnnotation.static.normalizeTitle( title ),
lookupTitle: ve.dm.MWInternalLinkAnnotation.static.getLookupTitle( title )
normalizedTitle: this.normalizeTitle( title ),
lookupTitle: this.getLookupTitle( title )
}
};
if ( rawTitle ) {
element.attributes.origTitle = rawTitle;
}
return element;
};
/**
* Build a ve.dm.MWInternalLinkAnnotation from a given mw.Title.
*
* @param {mw.Title} title The title to link to.
* @param {string} [rawTitle] String from which the title was created
* @return {ve.dm.MWInternalLinkAnnotation} The annotation.
*/
ve.dm.MWInternalLinkAnnotation.static.newFromTitle = function ( title, rawTitle ) {
var element = this.dataElementFromTitle( title, rawTitle );
return new ve.dm.MWInternalLinkAnnotation( element );
};

View file

@ -48,8 +48,8 @@ ve.ui.MWLinkAnnotationInspector.static.actions = ve.ui.MWLinkAnnotationInspector
ve.ui.MWLinkAnnotationInspector.prototype.initialize = function () {
// Properties
this.allowProtocolInInternal = false;
this.internalAnnotationInput = new ve.ui.MWInternalLinkAnnotationWidget();
this.externalAnnotationInput = new ve.ui.MWExternalLinkAnnotationWidget();
this.internalAnnotationInput = this.createInternalAnnotationInput();
this.externalAnnotationInput = this.createExternalAnnotationInput();
this.linkTypeIndex = new OO.ui.IndexLayout( {
expanded: false
@ -97,6 +97,20 @@ ve.ui.MWLinkAnnotationInspector.prototype.initialize = function () {
this.form.$element.append( this.linkTypeIndex.$element );
};
/**
* @return {ve.ui.MWInternalLinkAnnotationWidget}
*/
ve.ui.MWLinkAnnotationInspector.prototype.createInternalAnnotationInput = function () {
return new ve.ui.MWInternalLinkAnnotationWidget();
};
/**
* @return {ve.ui.MWExternalLinkAnnotationWidget}
*/
ve.ui.MWLinkAnnotationInspector.prototype.createExternalAnnotationInput = function () {
return new ve.ui.MWExternalLinkAnnotationWidget();
};
/**
* Check if the current input mode is for external links
*
@ -323,7 +337,7 @@ ve.ui.MWLinkAnnotationInspector.prototype.getAnnotationFromFragment = function (
// Figure out if this is an internal or external link
if ( ve.init.platform.getExternalLinkUrlProtocolsRegExp().test( target ) ) {
// External link
return new ve.dm.MWExternalLinkAnnotation( {
return this.newExternalLinkAnnotation( {
type: 'link/mwExternal',
attributes: {
href: target
@ -331,7 +345,7 @@ ve.ui.MWLinkAnnotationInspector.prototype.getAnnotationFromFragment = function (
} );
} else if ( title ) {
// Internal link
return ve.dm.MWInternalLinkAnnotation.static.newFromTitle( title );
return this.newInternalLinkAnnotationFromTitle( title );
} else {
// Doesn't look like an external link and mw.Title considered it an illegal value,
// for an internal link.
@ -339,6 +353,22 @@ ve.ui.MWLinkAnnotationInspector.prototype.getAnnotationFromFragment = function (
}
};
/**
* @param {mw.Title} title The title to link to.
* @return {ve.dm.MWInternalLinkAnnotation} The annotation.
*/
ve.ui.MWLinkAnnotationInspector.prototype.newInternalLinkAnnotationFromTitle = function ( title ) {
return ve.dm.MWInternalLinkAnnotation.static.newFromTitle( title );
};
/**
* @param {Object} element
* @return {ve.dm.MWExternalLinkAnnotation} The annotation.
*/
ve.ui.MWLinkAnnotationInspector.prototype.newExternalLinkAnnotation = function ( element ) {
return new ve.dm.MWExternalLinkAnnotation( element );
};
/**
* @inheritdoc
*/