2021-05-17 15:43:49 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor user interface MWTransclusionOutlineContainerWidget class.
|
|
|
|
*
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Container for transclusion, may contain a single or multiple templates.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends OO.ui.Widget
|
|
|
|
*
|
|
|
|
* @constructor
|
2021-07-09 15:33:16 +00:00
|
|
|
* @property {Object.<string,ve.ui.MWTransclusionOutlinePartWidget>} partWidgets Map of top-level
|
|
|
|
* items currently visible in this container, indexed by part id
|
2021-05-17 15:43:49 +00:00
|
|
|
*/
|
2021-08-19 11:02:11 +00:00
|
|
|
ve.ui.MWTransclusionOutlineContainerWidget = function VeUiMWTransclusionOutlineContainerWidget() {
|
2021-05-17 15:43:49 +00:00
|
|
|
// Parent constructor
|
2021-07-13 11:02:26 +00:00
|
|
|
ve.ui.MWTransclusionOutlineContainerWidget.super.call( this );
|
2021-05-17 15:43:49 +00:00
|
|
|
|
|
|
|
// Initialization
|
2021-07-09 15:33:16 +00:00
|
|
|
this.partWidgets = {};
|
2021-06-17 11:20:50 +00:00
|
|
|
|
2021-07-13 07:07:59 +00:00
|
|
|
this.$element.addClass( 've-ui-mwTransclusionOutlineContainerWidget' );
|
2021-05-17 15:43:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( ve.ui.MWTransclusionOutlineContainerWidget, OO.ui.Widget );
|
2021-06-17 11:20:50 +00:00
|
|
|
|
|
|
|
/* Events */
|
|
|
|
|
2021-08-05 12:36:10 +00:00
|
|
|
/**
|
2021-08-11 06:06:22 +00:00
|
|
|
* @event filterParameters
|
|
|
|
* @param {Object.<string,boolean>} visibility Keyed by unique id of the parameter, e.g. something
|
|
|
|
* like "part_1/param1". Note this lists only parameters that are currently in use.
|
2021-08-05 12:36:10 +00:00
|
|
|
*/
|
|
|
|
|
2021-08-19 11:02:11 +00:00
|
|
|
/**
|
|
|
|
* @event focusPart
|
|
|
|
* @param {string} partId Unique id of the part, e.g. something "part_1" or "part_1/param1".
|
|
|
|
*/
|
|
|
|
|
2021-06-17 11:20:50 +00:00
|
|
|
/**
|
2021-07-07 08:04:40 +00:00
|
|
|
* @param {ve.dm.MWTransclusionPartModel|null} removed Removed part
|
|
|
|
* @param {ve.dm.MWTransclusionPartModel|null} added Added part
|
2021-07-15 15:07:21 +00:00
|
|
|
* @param {number} [newPosition]
|
2021-06-17 11:20:50 +00:00
|
|
|
*/
|
2021-07-15 15:07:21 +00:00
|
|
|
ve.ui.MWTransclusionOutlineContainerWidget.prototype.onReplacePart = function ( removed, added, newPosition ) {
|
2021-07-09 15:33:16 +00:00
|
|
|
if ( removed ) {
|
|
|
|
this.removePartWidget( removed );
|
2021-06-17 11:20:50 +00:00
|
|
|
}
|
|
|
|
// TODO: reselect if active part was in a removed template
|
|
|
|
|
2021-07-09 15:33:16 +00:00
|
|
|
if ( added ) {
|
2021-07-15 15:07:21 +00:00
|
|
|
this.addPartWidget( added, newPosition );
|
2021-06-17 11:20:50 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-13 07:27:22 +00:00
|
|
|
/**
|
2021-07-15 15:07:21 +00:00
|
|
|
* @param {ve.dm.MWTransclusionModel} transclusionModel
|
2021-07-13 07:27:22 +00:00
|
|
|
*/
|
2021-07-15 15:07:21 +00:00
|
|
|
ve.ui.MWTransclusionOutlineContainerWidget.prototype.onTransclusionModelChange = function ( transclusionModel ) {
|
|
|
|
var newOrder = transclusionModel.getParts();
|
2021-07-13 07:27:22 +00:00
|
|
|
|
|
|
|
for ( var i = 0; i < newOrder.length; i++ ) {
|
|
|
|
var expectedWidget = this.partWidgets[ newOrder[ i ].getId() ],
|
|
|
|
$expectedElement = expectedWidget && expectedWidget.$element,
|
|
|
|
$currentElement = this.$element.children().eq( i );
|
|
|
|
|
|
|
|
if ( !$currentElement.is( $expectedElement ) ) {
|
|
|
|
// Move each widget to the correct position if it wasn't there before
|
|
|
|
$currentElement.before( $expectedElement );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-17 11:20:50 +00:00
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
2021-07-09 15:33:16 +00:00
|
|
|
* @param {ve.dm.MWTransclusionPartModel} part
|
2021-06-17 11:20:50 +00:00
|
|
|
*/
|
2021-07-09 15:33:16 +00:00
|
|
|
ve.ui.MWTransclusionOutlineContainerWidget.prototype.removePartWidget = function ( part ) {
|
|
|
|
var partId = part.getId(),
|
|
|
|
widget = this.partWidgets[ partId ];
|
2021-06-17 11:20:50 +00:00
|
|
|
|
2021-07-09 15:33:16 +00:00
|
|
|
if ( widget ) {
|
|
|
|
widget.$element.remove();
|
|
|
|
delete this.partWidgets[ partId ];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @param {ve.dm.MWTransclusionPartModel} part
|
2021-07-15 15:07:21 +00:00
|
|
|
* @param {number} [newPosition]
|
2021-07-09 15:33:16 +00:00
|
|
|
*/
|
2021-07-15 15:07:21 +00:00
|
|
|
ve.ui.MWTransclusionOutlineContainerWidget.prototype.addPartWidget = function ( part, newPosition ) {
|
2021-07-09 15:33:16 +00:00
|
|
|
var widget;
|
|
|
|
|
|
|
|
if ( part instanceof ve.dm.MWTemplateModel ) {
|
|
|
|
widget = new ve.ui.MWTransclusionOutlineTemplateWidget( part );
|
2021-08-05 12:36:10 +00:00
|
|
|
// This forwards events from the nested ve.ui.MWTransclusionOutlineTemplateWidget upwards.
|
2021-08-11 06:06:22 +00:00
|
|
|
// The array syntax is a way to call `this.emit( 'filterParameters' )`.
|
2021-08-19 11:02:11 +00:00
|
|
|
widget.connect( this, { filterParameters: [ 'emit', 'filterParameters' ] } );
|
2021-07-09 15:33:16 +00:00
|
|
|
} else if ( part instanceof ve.dm.MWTemplatePlaceholderModel ) {
|
|
|
|
widget = new ve.ui.MWTransclusionOutlinePlaceholderWidget( part );
|
|
|
|
} else if ( part instanceof ve.dm.MWTransclusionContentModel ) {
|
|
|
|
widget = new ve.ui.MWTransclusionOutlineWikitextWidget( part );
|
|
|
|
}
|
|
|
|
|
2021-08-19 11:02:11 +00:00
|
|
|
widget.connect( this, { focusPart: [ 'emit', 'focusPart' ] } );
|
2021-07-13 11:02:26 +00:00
|
|
|
|
2021-07-09 15:33:16 +00:00
|
|
|
this.partWidgets[ part.getId() ] = widget;
|
2021-07-15 15:07:21 +00:00
|
|
|
if ( typeof newPosition === 'number' && newPosition < this.$element.children().length ) {
|
|
|
|
this.$element.children().eq( newPosition ).before( widget.$element );
|
|
|
|
} else {
|
|
|
|
this.$element.append( widget.$element );
|
|
|
|
}
|
2021-06-17 11:20:50 +00:00
|
|
|
};
|
2021-07-13 11:02:26 +00:00
|
|
|
|
2021-07-16 12:37:34 +00:00
|
|
|
/**
|
|
|
|
* Removes all {@see ve.ui.MWTransclusionOutlinePartWidget}, i.e. empties the list.
|
|
|
|
*/
|
|
|
|
ve.ui.MWTransclusionOutlineContainerWidget.prototype.clear = function () {
|
|
|
|
for ( var partId in this.partWidgets ) {
|
2021-08-13 14:41:48 +00:00
|
|
|
var widget = this.partWidgets[ partId ];
|
|
|
|
widget.disconnect( this );
|
|
|
|
widget.$element.remove();
|
2021-07-16 12:37:34 +00:00
|
|
|
}
|
|
|
|
this.partWidgets = {};
|
|
|
|
};
|