2014-05-31 04:47:08 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface MWLinkNodeInspector class.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2014 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-07-14 21:32:49 +00:00
|
|
|
* Inspector for editing unlabeled MediaWiki external links.
|
2014-05-31 04:47:08 +00:00
|
|
|
*
|
|
|
|
* @class
|
2014-07-14 21:32:49 +00:00
|
|
|
* @extends ve.ui.NodeInspector
|
2014-05-31 04:47:08 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
2014-07-14 21:32:49 +00:00
|
|
|
* @param {OO.ui.WindowManager} manager Manager of window
|
2014-05-31 04:47:08 +00:00
|
|
|
* @param {Object} [config] Configuration options
|
|
|
|
*/
|
2014-07-14 21:32:49 +00:00
|
|
|
ve.ui.MWLinkNodeInspector = function VeUiMWLinkNodeInspector( manager, config ) {
|
2014-05-31 04:47:08 +00:00
|
|
|
// Parent constructor
|
2014-07-14 21:32:49 +00:00
|
|
|
ve.ui.NodeInspector.call( this, manager, config );
|
2014-05-31 04:47:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2014-07-14 21:32:49 +00:00
|
|
|
OO.inheritClass( ve.ui.MWLinkNodeInspector, ve.ui.NodeInspector );
|
2014-05-31 04:47:08 +00:00
|
|
|
|
|
|
|
/* Static properties */
|
|
|
|
|
|
|
|
ve.ui.MWLinkNodeInspector.static.name = 'linkNode';
|
|
|
|
|
|
|
|
ve.ui.MWLinkNodeInspector.static.icon = 'link';
|
|
|
|
|
|
|
|
ve.ui.MWLinkNodeInspector.static.title = OO.ui.deferMsg( 'visualeditor-linknodeinspector-title' );
|
|
|
|
|
|
|
|
ve.ui.MWLinkNodeInspector.static.removable = false;
|
|
|
|
|
|
|
|
ve.ui.MWLinkNodeInspector.static.modelClasses = [ ve.dm.MWNumberedExternalLinkNode ];
|
|
|
|
|
2014-07-14 21:32:49 +00:00
|
|
|
ve.ui.MWLinkNodeInspector.static.actions = ve.ui.MWLinkNodeInspector.super.static.actions.concat( [
|
|
|
|
{
|
|
|
|
'action': 'convert',
|
|
|
|
'label': OO.ui.deferMsg( 'visualeditor-linknodeinspector-add-label' )
|
|
|
|
}
|
|
|
|
] );
|
2014-05-31 04:47:08 +00:00
|
|
|
|
2014-07-14 21:32:49 +00:00
|
|
|
/* Methods */
|
2014-05-31 04:47:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWLinkNodeInspector.prototype.initialize = function () {
|
|
|
|
// Parent method
|
|
|
|
ve.ui.MWLinkNodeInspector.super.prototype.initialize.call( this );
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.targetInput = new OO.ui.TextInputWidget( { '$': this.$ } );
|
|
|
|
|
|
|
|
// Initialization
|
2014-07-14 21:32:49 +00:00
|
|
|
this.form.$element.append( this.targetInput.$element );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWLinkNodeInspector.prototype.getActionProcess = function ( action ) {
|
|
|
|
if ( action === 'convert' ) {
|
|
|
|
return new OO.ui.Process( function () {
|
|
|
|
this.close( { 'action': action } );
|
|
|
|
}, this );
|
|
|
|
}
|
|
|
|
return ve.ui.MWLinkNodeInspector.super.prototype.getActionProcess.call( this, action );
|
2014-05-31 04:47:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWLinkNodeInspector.prototype.getSetupProcess = function ( data ) {
|
|
|
|
return ve.ui.MWLinkNodeInspector.super.prototype.getSetupProcess.call( this, data )
|
|
|
|
.next( function () {
|
|
|
|
// Initialization
|
2014-07-14 21:32:49 +00:00
|
|
|
this.targetInput.setValue(
|
|
|
|
this.selectedNode ? this.selectedNode.getAttribute( 'href' ) : ''
|
|
|
|
);
|
2014-05-31 04:47:08 +00:00
|
|
|
}, this );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWLinkNodeInspector.prototype.getReadyProcess = function ( data ) {
|
|
|
|
return ve.ui.MWLinkNodeInspector.super.prototype.getReadyProcess.call( this, data )
|
|
|
|
.next( function () {
|
|
|
|
this.targetInput.focus().select();
|
|
|
|
}, this );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWLinkNodeInspector.prototype.getTeardownProcess = function ( data ) {
|
|
|
|
return ve.ui.MWLinkNodeInspector.super.prototype.getTeardownProcess.call( this, data )
|
|
|
|
.first( function () {
|
|
|
|
var content, annotation, annotations,
|
|
|
|
surfaceModel = this.getFragment().getSurface(),
|
|
|
|
doc = surfaceModel.getDocument(),
|
2014-07-14 21:32:49 +00:00
|
|
|
nodeRange = this.selectedNode.getOuterRange(),
|
2014-05-31 04:47:08 +00:00
|
|
|
value = this.targetInput.getValue(),
|
|
|
|
convert = data.action === 'convert',
|
|
|
|
remove = convert || data.action === 'remove' || !value;
|
|
|
|
|
|
|
|
if ( remove ) {
|
|
|
|
surfaceModel.change(
|
|
|
|
ve.dm.Transaction.newFromRemoval( doc, nodeRange )
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// Default to http:// if the external link doesn't already begin with a supported
|
|
|
|
// protocol - this prevents the link from being converted into literal text upon
|
|
|
|
// save and also fixes a common mistake users may make
|
|
|
|
if ( !ve.init.platform.getExternalLinkUrlProtocolsRegExp().test( value ) ) {
|
|
|
|
value = 'http://' + value;
|
|
|
|
}
|
|
|
|
surfaceModel.change(
|
|
|
|
ve.dm.Transaction.newFromAttributeChanges(
|
|
|
|
doc, nodeRange.start, { 'href': value }
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if ( convert ) {
|
|
|
|
annotation = new ve.dm.MWExternalLinkAnnotation( {
|
|
|
|
'type': 'link/mwExternal',
|
|
|
|
'attributes': {
|
|
|
|
'href': value
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
annotations = doc.data.getAnnotationsFromOffset(
|
2014-07-14 21:32:49 +00:00
|
|
|
this.selectedNode.getOffset()
|
2014-05-31 04:47:08 +00:00
|
|
|
).clone();
|
|
|
|
annotations.push( annotation );
|
|
|
|
content = ve.splitClusters( value );
|
|
|
|
ve.dm.Document.static.addAnnotationsToData( content, annotations );
|
|
|
|
surfaceModel.change(
|
|
|
|
ve.dm.Transaction.newFromInsertion( doc, nodeRange.start, content )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}, this );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.ui.windowFactory.register( ve.ui.MWLinkNodeInspector );
|