mediawiki-extensions-Visual.../modules/ve-mw/ui/dialogs/ve.ui.MWReferenceListDialog.js
Trevor Parscal be199c0bf2 Ultra-mega-hyper-citation editing on crack
Objectives:
* Allow users on-wiki to create tools and dialogs for citation templates
of their choosing
* Allow editing of citation templates directly, without having to go
through the reference dialog
* Provide citation template tools within reference editing that use the
same titles and icons as the citation tools do, but don't wrap the
inserted content in a ref tag

Changes:

* Reference list was cloning the DOM element it was inserting into its
view before the generated content node could finish rendering, so it
never ended up showing the finished rendering in the reference list
* Documenting hack about use of reference list node's destroy method,
and how we are depending on destroy not canceling generated content
rendering
* Introduced reference model
* Added saving/updating method to transclusion model
* Added getPartsList method to dm transclusion node, which caches the
result and invalidates the cache on update
* Added citation dialog, which extends transclusion dialog
* Added cite group to toolbars, cite-template in reference dialog toolbar
* Factored out getting the node to edit and saving changes procedures in
transclusion dialog so they could be extended in citation dialog
* Updated uses of autoAdd as per changes in oojs-ui (Ic353f91)
* Renamed MWDialogTool file since there was only one tool in it
* Expanded TransclusionDialogTool file out since there is now more logic
to it
* Switched to using ve.dm.MWReferenceModel instead of plain objects in
reference search widget

Configuration:

If you add to MediaWiki:Visualeditor-cite-tool-definition.json the
following code you will magically be presented with a delightful array
of citation options:

[
    { "name": "web", "icon": "ref-cite-web", "template": "Cite web" },
    { "name": "book", "icon": "ref-cite-book", "template": "Cite book" },
    { "name": "news", "icon": "ref-cite-news", "template": "Cite news" },
    { "name": "journal", "icon": "ref-cite-journal", "template": "Cite journal" }
]

...or...

[
    {
         "name": "any-name",
         "icon": "any-ooui-icon",
         "template": "Any template",
         "title": "Any title text"
     }
]

The title text is derived either from the title property or from the name
property by pre-pending the string 'visualeditor-cite-tool-name-' to
generate a message key. Titles for 'web', 'book', 'news' and 'journal' are
provided. The icon is a normal oo-ui-icon name, and more icons can be
added, as usual, by adding a class called .oo-ui-icon-{icon name} to
MediaWiki:Common.css. 'ref-cite-web', 'ref-cite-book', 'ref-cite-news'
and 'ref-cite-journal' are provided. The template name is simply the name
of the template without its namespace prefix.

Depends on Ic353f91 in oojs-ui

Bug: 50110
Bug: 50768
Change-Id: Id401d973b8d5fe2faec481cc777c17a24fd19dd4
2014-03-24 19:24:38 +00:00

176 lines
4.2 KiB
JavaScript

/*!
* VisualEditor user interface MWReferenceListDialog class.
*
* @copyright 2011-2014 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* Dialog for inserting and editing MediaWiki reference lists.
*
* @class
* @extends ve.ui.MWDialog
*
* @constructor
* @param {ve.ui.Surface} surface Surface dialog is for
* @param {Object} [config] Configuration options
*/
ve.ui.MWReferenceListDialog = function VeUiMWReferenceListDialog( surface, config ) {
// Configuration initialization
config = ve.extendObject( { 'size': 'small' }, config );
// Parent constructor
ve.ui.MWDialog.call( this, surface, config );
// Properties
this.inserting = false;
};
/* Inheritance */
OO.inheritClass( ve.ui.MWReferenceListDialog, ve.ui.MWDialog );
/* Static Properties */
ve.ui.MWReferenceListDialog.static.name = 'referenceList';
ve.ui.MWReferenceListDialog.static.title =
OO.ui.deferMsg( 'visualeditor-dialog-referencelist-title' );
ve.ui.MWReferenceListDialog.static.icon = 'references';
/* Methods */
/**
* @inheritdoc
*/
ve.ui.MWReferenceListDialog.prototype.initialize = function () {
// Parent method
ve.ui.MWDialog.prototype.initialize.call( this );
// Properties
this.editPanel = new OO.ui.PanelLayout( {
'$': this.$, 'scrollable': true, 'padded': true
} );
this.optionsFieldset = new OO.ui.FieldsetLayout( {
'$': this.$
} );
this.groupInput = new OO.ui.TextInputWidget( {
'$': this.$,
'placeholder': ve.msg( 'visualeditor-dialog-reference-options-group-placeholder' )
} );
this.groupField = new OO.ui.FieldLayout( this.groupInput, {
'$': this.$,
'align': 'top',
'label': ve.msg( 'visualeditor-dialog-reference-options-group-label' )
} );
this.applyButton = new OO.ui.ButtonWidget( {
'$': this.$,
'flags': ['primary']
} );
// Events
this.applyButton.connect( this, { 'click': [ 'close', { 'action': 'apply' } ] } );
// Initialization
this.optionsFieldset.addItems( [ this.groupField ] );
this.editPanel.$element.append( this.optionsFieldset.$element );
this.$body.append( this.editPanel.$element );
this.$foot.append( this.applyButton.$element );
};
/**
* @inheritdoc
*/
ve.ui.MWReferenceListDialog.prototype.setup = function ( data ) {
// Parent method
ve.ui.MWDialog.prototype.setup.call( this, data );
var node, refGroup;
// Prepopulate from existing node if we're editing a node
// instead of inserting a new one
node = this.surface.getView().getFocusedNode();
if ( node instanceof ve.ce.MWReferenceListNode ) {
refGroup = node.getModel().getAttribute( 'refGroup' );
this.inserting = false;
} else {
refGroup = '';
this.inserting = true;
}
this.groupInput.setValue( refGroup );
this.applyButton.setLabel ( ve.msg (
!this.inserting ?
'visualeditor-dialog-action-apply' :
'visualeditor-dialog-referencelist-insert-button'
) );
/**
* Focused node.
*
* @private
* @property {ve.ce.MWReferenceListNode|undefined}
*/
this.node = node;
};
/**
* @inheritdoc
*/
ve.ui.MWReferenceListDialog.prototype.teardown = function ( data ) {
var refGroup, listGroup, oldListGroup, attrChanges,
doc, model,
surfaceModel = this.surface.getModel(),
node = this.node;
// Data initialization
data = data || {};
// Save changes
if ( data.action === 'apply' ) {
refGroup = this.groupInput.getValue();
listGroup = 'mwReference/' + refGroup;
if ( node ) {
// Edit existing model
doc = surfaceModel.getDocument();
model = node.getModel();
oldListGroup = model.getAttribute( 'listGroup' );
if ( listGroup !== oldListGroup ) {
attrChanges = {
listGroup: listGroup,
refGroup: refGroup
};
surfaceModel.change(
ve.dm.Transaction.newFromAttributeChanges(
doc, model.getOuterRange().start, attrChanges
)
);
}
} else {
// Create new model
surfaceModel.getFragment().collapseRangeToEnd().insertContent( [
{
'type': 'mwReferenceList',
'attributes': {
'listGroup': listGroup,
'refGroup': refGroup
}
},
{ 'type': '/mwReferenceList' }
] ).collapseRangeToEnd().select();
}
}
// Parent method
ve.ui.MWDialog.prototype.teardown.call( this, data );
};
/* Registration */
ve.ui.dialogFactory.register( ve.ui.MWReferenceListDialog );