2013-12-02 20:10:55 +00:00
|
|
|
/*
|
2013-06-11 19:16:04 +00:00
|
|
|
* VisualEditor user interface MWTransclusionDialog class.
|
|
|
|
*
|
2014-01-05 12:05:05 +00:00
|
|
|
* @copyright 2011-2014 VisualEditor Team and others; see AUTHORS.txt
|
2013-06-11 19:16:04 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
Single-click insertion
Objectives:
* Reduce the number of clicks and mouse maneuvers required to insert
media, references or template parameters
* Make use of highlighting with mouse movement or arrow key presses,
similar to menus, to suggest action when clicked
* Improve the way media search results look and feel
Changes:
ve.ui.SelectWidget.js
* Add mouseleave handler to un-highlight when the mouse exits the widget
* Document highlight events (already being emitted)
ve.ui.SearchWidget.js
* Propagate both select and highlight events from results widget
* Make arrow keys change highlight instead of selection
* Get rid of enter event, make enter key select highlighted item instead
* Provide direct access to results widget through getResults method
ve.ui.MenuWidget.js
* Use the selected item as a starting point if nothing is currently
highlighted when adjusting the highlight position
ve.ui.Dialog.js
* Add footless option to hide the foot element and make the body extend
all the way down to the bottom
* Remove applyButton, which only some dialogs need, and should be creating
themselves, along with other buttons as needed
ve.ui.Widget.css
* Change highlight and selected colors of option widgets to match other
selection colors used elsewhere
* Leave selected and highlighted widget looking selected
ve.ui.Frame.css
* Add background color to combat any color that might have been applied to
the frame body in the imported CSS from the parent frame
ve.ui.Dialog.css
* Add rules for footless mode
ve.ui.MWReferenceResultWidget.js,
ve.ui.MWParameterResultWidget.js,
ve.ui.MWMediaResultWidget.js
* Allow highlighting
ve.ui.MWParamterSearchWidget.js
* Switch from selecting the first item when filtering to highlighting
ve-mw/ve.ui.Widget.js
* Adjust media result widget styling to better match other elements
ve.ui.MWTransclusionDialog.js,
ve.ui.MWReferenceListDialog.js,
ve.ui.MWReferenceEditDialog.js,
ve.ui.MWMetaDialog.js
ve.ui.MWMediaEditDialog.js
* Add apply button, as per it being removed from parent class
ve.ui.MWTransclusionDialog.js,
ve.ui.MWReferenceInsertDialog.js,
ve.ui.MWMediaInsertDialog.js
* Insert parameter/reference/media on select, instead of clicking an
insert button
* Use 'insert' instead of 'apply' as argument for close method
Bug: 50774
Bug: 51143
Change-Id: Ia18e79f1f8df2540f465468edb01f5ce989bf843
2013-07-15 21:07:53 +00:00
|
|
|
* Dialog for inserting and editing MediaWiki transclusions.
|
2013-06-11 19:16:04 +00:00
|
|
|
*
|
|
|
|
* @class
|
2013-07-03 01:30:10 +00:00
|
|
|
* @extends ve.ui.MWDialog
|
2013-06-11 19:16:04 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
2013-11-05 00:29:50 +00:00
|
|
|
* @param {ve.ui.WindowSet} windowSet Window set this dialog is part of
|
2013-09-25 10:21:09 +00:00
|
|
|
* @param {Object} [config] Configuration options
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
2013-10-09 19:59:03 +00:00
|
|
|
ve.ui.MWTransclusionDialog = function VeUiMWTransclusionDialog( windowSet, config ) {
|
2013-06-11 19:16:04 +00:00
|
|
|
// Parent constructor
|
2013-10-09 19:59:03 +00:00
|
|
|
ve.ui.MWDialog.call( this, windowSet, config );
|
2013-07-03 01:30:10 +00:00
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
// Properties
|
|
|
|
this.node = null;
|
|
|
|
this.transclusion = null;
|
2013-12-02 20:10:55 +00:00
|
|
|
this.loaded = false;
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-10-11 21:44:09 +00:00
|
|
|
OO.inheritClass( ve.ui.MWTransclusionDialog, ve.ui.MWDialog );
|
2013-07-03 01:30:10 +00:00
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
/* Static Properties */
|
|
|
|
|
|
|
|
ve.ui.MWTransclusionDialog.static.icon = 'template';
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
2013-12-02 20:10:55 +00:00
|
|
|
* Handle parts being replaced.
|
2013-06-11 19:16:04 +00:00
|
|
|
*
|
2013-12-02 20:10:55 +00:00
|
|
|
* @param {ve.dm.MWTransclusionPartModel} removed Removed part
|
|
|
|
* @param {ve.dm.MWTransclusionPartModel} added Added part
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
2013-12-02 20:10:55 +00:00
|
|
|
ve.ui.MWTransclusionDialog.prototype.onReplacePart = function ( removed, added ) {
|
|
|
|
var i, len, page, name, names, params, selected,
|
|
|
|
removePages = [],
|
|
|
|
select = false;
|
2013-06-11 19:16:04 +00:00
|
|
|
|
2013-12-02 20:10:55 +00:00
|
|
|
if ( removed ) {
|
|
|
|
// Remove parameter pages of removed templates
|
|
|
|
if ( removed instanceof ve.dm.MWTemplateModel ) {
|
|
|
|
params = removed.getParameters();
|
|
|
|
for ( name in params ) {
|
|
|
|
removePages.push( this.bookletLayout.getPage( params[name].getId() ) );
|
2013-06-14 00:46:45 +00:00
|
|
|
}
|
2013-12-02 20:10:55 +00:00
|
|
|
removed.disconnect( this );
|
2013-06-14 00:46:45 +00:00
|
|
|
}
|
2013-12-06 20:24:36 +00:00
|
|
|
if ( this.bookletLayout.isOutlined() ) {
|
2013-06-28 21:38:40 +00:00
|
|
|
// Auto-select new part if placeholder is still selected
|
2013-12-02 20:10:55 +00:00
|
|
|
selected = this.bookletLayout.getOutline().getSelectedItem();
|
|
|
|
if ( selected && removed.getId() === selected.getData() ) {
|
|
|
|
select = true;
|
2013-06-28 21:38:40 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-02 20:10:55 +00:00
|
|
|
removePages.push( this.bookletLayout.getPage( removed.getId() ) );
|
|
|
|
this.bookletLayout.removePages( removePages );
|
2013-06-28 21:38:40 +00:00
|
|
|
}
|
2013-06-11 19:16:04 +00:00
|
|
|
|
2013-12-02 20:10:55 +00:00
|
|
|
if ( added ) {
|
|
|
|
if ( added instanceof ve.dm.MWTemplateModel ) {
|
|
|
|
page = new ve.ui.MWTemplatePage( added, added.getId(), { '$': this.$ } );
|
|
|
|
} else if ( added instanceof ve.dm.MWTransclusionContentModel ) {
|
|
|
|
page = new ve.ui.MWTransclusionContentPage( added, added.getId(), { '$': this.$ } );
|
|
|
|
} else if ( added instanceof ve.dm.MWTemplatePlaceholderModel ) {
|
|
|
|
page = new ve.ui.MWTemplatePlaceholderPage( added, added.getId(), { '$': this.$ } );
|
|
|
|
}
|
|
|
|
if ( page ) {
|
|
|
|
this.bookletLayout.addPages( [ page ], this.transclusion.getIndex( added ) );
|
|
|
|
if ( select && this.loaded ) {
|
|
|
|
this.setPageByName( added.getId() );
|
|
|
|
}
|
|
|
|
// Add existing params to templates (the template might be being moved)
|
|
|
|
if ( added instanceof ve.dm.MWTemplateModel ) {
|
|
|
|
names = added.getParameterNames();
|
|
|
|
params = added.getParameters();
|
|
|
|
for ( i = 0, len = names.length; i < len; i++ ) {
|
|
|
|
this.onAddParameter( params[names[i]] );
|
|
|
|
}
|
|
|
|
added.connect( this, { 'add': 'onAddParameter', 'remove': 'onRemoveParameter' } );
|
|
|
|
}
|
2013-06-11 19:16:04 +00:00
|
|
|
|
2013-12-02 20:10:55 +00:00
|
|
|
// Add required params to user created templates
|
|
|
|
if ( added instanceof ve.dm.MWTemplateModel && this.loaded ) {
|
|
|
|
added.addRequiredParameters();
|
|
|
|
}
|
2013-06-11 19:16:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle add param events.
|
|
|
|
*
|
|
|
|
* @param {ve.dm.MWTemplateParameterModel} param Added param
|
|
|
|
*/
|
|
|
|
ve.ui.MWTransclusionDialog.prototype.onAddParameter = function ( param ) {
|
2013-12-02 20:10:55 +00:00
|
|
|
var page = new ve.ui.MWTemplateParameterPage( param, param.getId(), { '$': this.$ } );
|
|
|
|
this.bookletLayout.addPages( [ page ], this.transclusion.getIndex( param ) );
|
|
|
|
if ( this.loaded ) {
|
|
|
|
this.setPageByName( param.getId() );
|
|
|
|
}
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle remove param events.
|
|
|
|
*
|
|
|
|
* @param {ve.dm.MWTemplateParameterModel} param Removed param
|
|
|
|
*/
|
|
|
|
ve.ui.MWTransclusionDialog.prototype.onRemoveParameter = function ( param ) {
|
2013-12-02 20:10:55 +00:00
|
|
|
this.bookletLayout.removePages( [ this.bookletLayout.getPage( param.getId() ) ] );
|
|
|
|
if ( this.loaded ) {
|
|
|
|
// Return to template page
|
|
|
|
this.setPageByName( param.getTemplate().getId() );
|
2013-06-11 19:16:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-18 21:24:16 +00:00
|
|
|
/**
|
2013-12-06 20:24:36 +00:00
|
|
|
* Get a booklet layout widget.
|
2013-06-18 21:24:16 +00:00
|
|
|
*
|
2013-12-02 20:10:55 +00:00
|
|
|
* @return {OO.ui.BookletLayout} Configured widget
|
2013-06-18 21:24:16 +00:00
|
|
|
*/
|
2013-12-02 20:10:55 +00:00
|
|
|
ve.ui.MWTransclusionDialog.prototype.getBookletLayout = function () {
|
|
|
|
return new OO.ui.BookletLayout( { '$': this.$, 'continuous': true } );
|
2013-06-18 21:24:16 +00:00
|
|
|
};
|
|
|
|
|
2013-11-05 00:29:50 +00:00
|
|
|
/**
|
|
|
|
* Set the page by name.
|
|
|
|
*
|
|
|
|
* Page names are always the ID of the part or param they represent.
|
|
|
|
*
|
|
|
|
* @param {string} name Page name
|
|
|
|
*/
|
|
|
|
ve.ui.MWTransclusionDialog.prototype.setPageByName = function ( name ) {
|
2013-12-06 20:24:36 +00:00
|
|
|
if ( this.bookletLayout.isOutlined() ) {
|
2013-12-02 20:10:55 +00:00
|
|
|
this.bookletLayout.getOutline().selectItem(
|
|
|
|
this.bookletLayout.getOutline().getItemFromData( name )
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this.bookletLayout.setPage( name );
|
|
|
|
}
|
2013-11-05 00:29:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWTransclusionDialog.prototype.initialize = function () {
|
|
|
|
// Parent method
|
|
|
|
ve.ui.MWDialog.prototype.initialize.call( this );
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.applyButton = new OO.ui.PushButtonWidget( {
|
|
|
|
'$': this.$,
|
2013-12-02 20:10:55 +00:00
|
|
|
'label': ve.msg( 'visualeditor-dialog-action-apply' ),
|
|
|
|
'flags': ['primary']
|
2013-11-05 00:29:50 +00:00
|
|
|
} );
|
2013-12-02 20:10:55 +00:00
|
|
|
this.bookletLayout = this.getBookletLayout();
|
2013-11-05 00:29:50 +00:00
|
|
|
|
|
|
|
// Events
|
|
|
|
this.applyButton.connect( this, { 'click': [ 'close', { 'action': 'apply' } ] } );
|
|
|
|
|
|
|
|
// Initialization
|
2013-12-02 20:10:55 +00:00
|
|
|
this.$body.append( this.bookletLayout.$element );
|
2013-11-05 00:29:50 +00:00
|
|
|
this.$foot.append( this.applyButton.$element );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWTransclusionDialog.prototype.setup = function ( data ) {
|
|
|
|
// Parent method
|
|
|
|
ve.ui.MWDialog.prototype.setup.call( this, data );
|
|
|
|
|
|
|
|
// Properties
|
2014-01-16 17:35:22 +00:00
|
|
|
this.node = this.surface.getView().getFocusedNode();
|
2013-11-05 00:29:50 +00:00
|
|
|
this.transclusion = new ve.dm.MWTransclusionModel();
|
2013-12-02 20:10:55 +00:00
|
|
|
this.loaded = false;
|
2013-11-05 00:29:50 +00:00
|
|
|
|
|
|
|
// Events
|
2013-12-02 20:10:55 +00:00
|
|
|
this.transclusion.connect( this, { 'replace': 'onReplacePart' } );
|
2013-11-05 00:29:50 +00:00
|
|
|
|
|
|
|
// Initialization
|
|
|
|
if ( this.node instanceof ve.ce.MWTransclusionNode ) {
|
2013-12-02 20:10:55 +00:00
|
|
|
this.transclusion
|
|
|
|
.load( ve.copy( this.node.getModel().getAttribute( 'mw' ) ) )
|
|
|
|
.always( ve.bind( function () {
|
|
|
|
this.loaded = true;
|
|
|
|
}, this ) );
|
2013-11-05 00:29:50 +00:00
|
|
|
} else {
|
2013-12-02 20:10:55 +00:00
|
|
|
this.loaded = true;
|
|
|
|
this.transclusion.addPart( new ve.dm.MWTemplatePlaceholderModel( this.transclusion ) );
|
2013-11-05 00:29:50 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWTransclusionDialog.prototype.teardown = function ( data ) {
|
|
|
|
var surfaceModel = this.surface.getModel(),
|
|
|
|
obj = this.transclusion.getPlainObject();
|
|
|
|
|
|
|
|
// Data initialization
|
|
|
|
data = data || {};
|
|
|
|
|
|
|
|
// Save changes
|
|
|
|
if ( data.action === 'apply' ) {
|
|
|
|
if ( this.node instanceof ve.ce.MWTransclusionNode ) {
|
|
|
|
if ( obj !== null ) {
|
|
|
|
surfaceModel.getFragment().changeAttributes( { 'mw': obj } );
|
|
|
|
} else {
|
|
|
|
surfaceModel.getFragment().removeContent();
|
|
|
|
}
|
|
|
|
} else if ( obj !== null ) {
|
|
|
|
surfaceModel.getFragment().collapseRangeToEnd().insertContent( [
|
|
|
|
{
|
|
|
|
'type': 'mwTransclusionInline',
|
|
|
|
'attributes': {
|
|
|
|
'mw': obj
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ 'type': '/mwTransclusionInline' }
|
2013-12-10 02:02:18 +00:00
|
|
|
] ).collapseRangeToEnd().select();
|
2013-11-05 00:29:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.transclusion.disconnect( this );
|
|
|
|
this.transclusion.abortRequests();
|
|
|
|
this.transclusion = null;
|
2013-12-02 20:10:55 +00:00
|
|
|
this.bookletLayout.clearPages();
|
2013-11-05 00:29:50 +00:00
|
|
|
this.node = null;
|
|
|
|
this.content = null;
|
|
|
|
|
|
|
|
// Parent method
|
|
|
|
ve.ui.MWDialog.prototype.teardown.call( this, data );
|
|
|
|
};
|