2014-05-31 04:47:08 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface MWLinkNodeInspector class.
|
|
|
|
*
|
2015-01-08 23:54:03 +00:00
|
|
|
* @copyright 2011-2015 VisualEditor Team and others; see AUTHORS.txt
|
2014-05-31 04:47:08 +00:00
|
|
|
* @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
|
|
|
|
* @param {Object} [config] Configuration options
|
|
|
|
*/
|
2014-08-21 00:50:54 +00:00
|
|
|
ve.ui.MWLinkNodeInspector = function VeUiMWLinkNodeInspector( config ) {
|
2014-05-31 04:47:08 +00:00
|
|
|
// Parent constructor
|
2014-08-21 00:50:54 +00:00
|
|
|
ve.ui.NodeInspector.call( this, 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( [
|
2014-10-22 00:35:28 +00:00
|
|
|
{
|
|
|
|
action: 'open',
|
|
|
|
label: OO.ui.deferMsg( 'visualeditor-linkinspector-open' )
|
|
|
|
},
|
2014-07-14 21:32:49 +00:00
|
|
|
{
|
2014-08-22 20:50:48 +00:00
|
|
|
action: 'convert',
|
|
|
|
label: OO.ui.deferMsg( 'visualeditor-linknodeinspector-add-label' )
|
2014-07-14 21:32:49 +00:00
|
|
|
}
|
|
|
|
] );
|
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
|
2014-10-22 00:35:28 +00:00
|
|
|
this.targetInput = new OO.ui.TextInputWidget( {
|
|
|
|
$: this.$,
|
|
|
|
validate: ve.init.platform.getExternalLinkUrlProtocolsRegExp()
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Events
|
|
|
|
this.targetInput.connect( this, { change: 'onTargetInputChange' } );
|
2014-05-31 04:47:08 +00:00
|
|
|
|
|
|
|
// Initialization
|
2014-07-14 21:32:49 +00:00
|
|
|
this.form.$element.append( this.targetInput.$element );
|
|
|
|
};
|
|
|
|
|
2014-10-22 00:35:28 +00:00
|
|
|
/**
|
|
|
|
* Handle target input change events.
|
|
|
|
*
|
|
|
|
* Updates the open button's hyperlink location.
|
|
|
|
*
|
|
|
|
* @param {string} value New target input value
|
|
|
|
*/
|
|
|
|
ve.ui.MWLinkNodeInspector.prototype.onTargetInputChange = function () {
|
|
|
|
var href = this.targetInput.getValue(),
|
|
|
|
inspector = this;
|
|
|
|
this.targetInput.isValid().done( function ( valid ) {
|
|
|
|
inspector.actions.forEach( { actions: 'open' }, function ( action ) {
|
|
|
|
action.setHref( href ).setTarget( '_blank' ).setDisabled( !valid );
|
|
|
|
// HACK: Chrome renders a dark outline around the action when it's a link, but causing it to
|
|
|
|
// re-render makes it magically go away; this is incredibly evil and needs further
|
|
|
|
// investigation
|
|
|
|
action.$element.hide().fadeIn( 0 );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2014-07-14 21:32:49 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWLinkNodeInspector.prototype.getActionProcess = function ( action ) {
|
|
|
|
if ( action === 'convert' ) {
|
|
|
|
return new OO.ui.Process( function () {
|
2014-08-22 20:50:48 +00:00
|
|
|
this.close( { action: action } );
|
2014-07-14 21:32:49 +00:00
|
|
|
}, 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 ) {
|
2014-07-15 22:49:51 +00:00
|
|
|
data = data || {};
|
2014-05-31 04:47:08 +00:00
|
|
|
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',
|
2014-07-01 19:46:23 +00:00
|
|
|
remove = data.action === 'remove' || !value;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
2014-05-31 04:47:08 +00:00
|
|
|
|
|
|
|
if ( remove ) {
|
|
|
|
surfaceModel.change(
|
|
|
|
ve.dm.Transaction.newFromRemoval( doc, nodeRange )
|
|
|
|
);
|
2014-07-01 19:46:23 +00:00
|
|
|
} else if ( convert ) {
|
2014-05-31 04:47:08 +00:00
|
|
|
annotation = new ve.dm.MWExternalLinkAnnotation( {
|
2014-08-22 20:50:48 +00:00
|
|
|
type: 'link/mwExternal',
|
|
|
|
attributes: {
|
|
|
|
href: value
|
2014-05-31 04:47:08 +00:00
|
|
|
}
|
|
|
|
} );
|
2014-07-01 19:46:23 +00:00
|
|
|
annotations = doc.data.getAnnotationsFromOffset( nodeRange.start ).clone();
|
2014-05-31 04:47:08 +00:00
|
|
|
annotations.push( annotation );
|
2014-10-08 04:52:26 +00:00
|
|
|
content = value.split( '' );
|
2014-05-31 04:47:08 +00:00
|
|
|
ve.dm.Document.static.addAnnotationsToData( content, annotations );
|
|
|
|
surfaceModel.change(
|
2014-07-01 19:46:23 +00:00
|
|
|
ve.dm.Transaction.newFromReplacement( doc, nodeRange, content )
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
surfaceModel.change(
|
|
|
|
ve.dm.Transaction.newFromAttributeChanges(
|
2014-08-22 20:50:48 +00:00
|
|
|
doc, nodeRange.start, { href: value }
|
2014-07-01 19:46:23 +00:00
|
|
|
)
|
2014-05-31 04:47:08 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}, this );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.ui.windowFactory.register( ve.ui.MWLinkNodeInspector );
|