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-13 11:02:26 +00:00
|
|
|
* @param {OO.ui.BookletLayout} bookletLayout
|
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-07-15 15:07:21 +00:00
|
|
|
ve.ui.MWTransclusionOutlineContainerWidget = function VeUiMWTransclusionOutlineContainerWidget( bookletLayout ) {
|
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-13 11:02:26 +00:00
|
|
|
this.bookletLayout = bookletLayout;
|
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-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 );
|
|
|
|
} 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-07-13 11:02:26 +00:00
|
|
|
widget.connect( this, { partHeaderClick: 'onPartHeaderClick' } );
|
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} partId
|
|
|
|
*/
|
|
|
|
ve.ui.MWTransclusionOutlineContainerWidget.prototype.onPartHeaderClick = function ( partId ) {
|
|
|
|
this.bookletLayout.setPage( partId );
|
|
|
|
};
|