Make generateCitationFeatures() idempotent

Add checks so it doesn't generate and register the same
tool twice. This makes it safe to run it multiple times.

Change-Id: I882b51bd0422222f2e80555910c69b059136a503
This commit is contained in:
Roan Kattouw 2015-02-27 10:47:42 -08:00
parent 34da7d56b8
commit 2b67617179

View file

@ -804,81 +804,94 @@ ve.init.mw.Target.prototype.generateCitationFeatures = function () {
for ( i = 0, len = Math.min( limit, tools.length ); i < len; i++ ) {
item = tools[i];
data = { template: item.template };
// Generate transclusion tool
name = 'cite-transclusion-' + item.name;
tool = function GeneratedMWTransclusionDialogTool( toolbar, config ) {
ve.ui.MWTransclusionDialogTool.call( this, toolbar, config );
};
OO.inheritClass( tool, ve.ui.MWTransclusionDialogTool );
tool.static.group = 'cite-transclusion';
tool.static.name = name;
tool.static.icon = item.icon;
tool.static.title = item.title;
tool.static.commandName = name;
tool.static.template = item.template;
tool.static.autoAddToCatchall = false;
tool.static.autoAddToGroup = true;
ve.ui.toolFactory.register( tool );
ve.ui.commandRegistry.register(
new ve.ui.Command(
name, 'window', 'open',
{ args: ['transclusion', data], supportedSelections: ['linear'] }
)
);
if ( !ve.ui.toolFactory.lookup( name ) ) {
tool = function GeneratedMWTransclusionDialogTool( toolbar, config ) {
ve.ui.MWTransclusionDialogTool.call( this, toolbar, config );
};
OO.inheritClass( tool, ve.ui.MWTransclusionDialogTool );
tool.static.group = 'cite-transclusion';
tool.static.name = name;
tool.static.icon = item.icon;
tool.static.title = item.title;
tool.static.commandName = name;
tool.static.template = item.template;
tool.static.autoAddToCatchall = false;
tool.static.autoAddToGroup = true;
ve.ui.toolFactory.register( tool );
ve.ui.commandRegistry.register(
new ve.ui.Command(
name, 'window', 'open',
{ args: ['transclusion', data], supportedSelections: ['linear'] }
)
);
}
// Generate transclusion context item
if ( !ve.ui.contextItemFactory.lookup( name ) ) {
contextItem = function GeneratedMWTransclusionContextItem( toolbar, config ) {
ve.ui.MWTransclusionContextItem.call( this, toolbar, config );
};
OO.inheritClass( contextItem, ve.ui.MWTransclusionContextItem );
contextItem.static.name = name;
contextItem.static.icon = item.icon;
contextItem.static.label = item.title;
contextItem.static.commandName = name;
contextItem.static.template = item.template;
ve.ui.contextItemFactory.register( contextItem );
}
// Generate citation tool
name = 'cite-' + item.name;
tool = function GeneratedMWCitationDialogTool( toolbar, config ) {
ve.ui.MWCitationDialogTool.call( this, toolbar, config );
};
OO.inheritClass( tool, ve.ui.MWCitationDialogTool );
tool.static.group = 'cite';
tool.static.name = name;
tool.static.icon = item.icon;
tool.static.title = item.title;
tool.static.commandName = name;
tool.static.template = item.template;
tool.static.autoAddToCatchall = false;
tool.static.autoAddToGroup = true;
ve.ui.toolFactory.register( tool );
ve.ui.commandRegistry.register(
new ve.ui.Command(
name, 'window', 'open',
{ args: [name, data], supportedSelections: ['linear'] }
)
);
// Generate transclusion context item
name = 'cite-transclusion-' + item.name;
contextItem = function GeneratedMWTransclusionContextItem( toolbar, config ) {
ve.ui.MWTransclusionContextItem.call( this, toolbar, config );
};
OO.inheritClass( contextItem, ve.ui.MWTransclusionContextItem );
contextItem.static.name = name;
contextItem.static.icon = item.icon;
contextItem.static.label = item.title;
contextItem.static.commandName = name;
contextItem.static.template = item.template;
ve.ui.contextItemFactory.register( contextItem );
if ( !ve.ui.toolFactory.lookup( name ) ) {
tool = function GeneratedMWCitationDialogTool( toolbar, config ) {
ve.ui.MWCitationDialogTool.call( this, toolbar, config );
};
OO.inheritClass( tool, ve.ui.MWCitationDialogTool );
tool.static.group = 'cite';
tool.static.name = name;
tool.static.icon = item.icon;
tool.static.title = item.title;
tool.static.commandName = name;
tool.static.template = item.template;
tool.static.autoAddToCatchall = false;
tool.static.autoAddToGroup = true;
ve.ui.toolFactory.register( tool );
ve.ui.commandRegistry.register(
new ve.ui.Command(
name, 'window', 'open',
{ args: [name, data], supportedSelections: ['linear'] }
)
);
}
// Generate citation context item
name = 'cite-' + item.name;
contextItem = function GeneratedMWCitationContextItem( toolbar, config ) {
ve.ui.MWCitationContextItem.call( this, toolbar, config );
};
OO.inheritClass( contextItem, ve.ui.MWCitationContextItem );
contextItem.static.name = name;
contextItem.static.icon = item.icon;
contextItem.static.label = item.title;
contextItem.static.commandName = name;
contextItem.static.template = item.template;
ve.ui.contextItemFactory.register( contextItem );
if ( !ve.ui.contextItemFactory.lookup( name ) ) {
contextItem = function GeneratedMWCitationContextItem( toolbar, config ) {
ve.ui.MWCitationContextItem.call( this, toolbar, config );
};
OO.inheritClass( contextItem, ve.ui.MWCitationContextItem );
contextItem.static.name = name;
contextItem.static.icon = item.icon;
contextItem.static.label = item.title;
contextItem.static.commandName = name;
contextItem.static.template = item.template;
ve.ui.contextItemFactory.register( contextItem );
}
// Generate dialog
dialog = function GeneratedMWCitationDialog( config ) {
ve.ui.MWCitationDialog.call( this, config );
};
OO.inheritClass( dialog, ve.ui.MWCitationDialog );
dialog.static.name = name;
dialog.static.icon = item.icon;
dialog.static.title = item.title;
ve.ui.windowFactory.register( dialog );
if ( !ve.ui.windowFactory.lookup( name ) ) {
dialog = function GeneratedMWCitationDialog( config ) {
ve.ui.MWCitationDialog.call( this, config );
};
OO.inheritClass( dialog, ve.ui.MWCitationDialog );
dialog.static.name = name;
dialog.static.icon = item.icon;
dialog.static.title = item.title;
ve.ui.windowFactory.register( dialog );
}
}
}
};