2013-05-15 18:44:32 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor user interface MWTemplateDialog class.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
2013-05-21 01:27:38 +00:00
|
|
|
/*global mw */
|
|
|
|
|
2013-05-15 18:44:32 +00:00
|
|
|
/**
|
|
|
|
* Document dialog.
|
|
|
|
*
|
2013-05-21 01:27:38 +00:00
|
|
|
* See https://raw.github.com/wikimedia/mediawiki-extensions-TemplateData/master/spec.templatedata.json
|
|
|
|
* for the latest version of the TemplateData specification.
|
|
|
|
*
|
2013-05-15 18:44:32 +00:00
|
|
|
* @class
|
2013-05-15 20:39:34 +00:00
|
|
|
* @extends ve.ui.PagedDialog
|
2013-05-15 18:44:32 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {ve.ui.Surface} surface
|
The Great ve.ui.Surface refactor of 2013
Prologue:
Farewell ve.Editor my good chap… Oh, hey there HTML frames - I didn't
see you there! In a world where iframes are outlaws, and symbols like
document and window are global, there were more than a few assumptions
about which document or window was being used. But fear not - for this
commit (probably) tracks them all down, leaving a trail of
iframe-compatible awesomeness in its wake. With the great ve.ui.Surface
now able to be used inside of iframes, let the reference editing
commence. But there, lurking in the darkness is a DM issue so fierce it
may take Roan and/or Ed up to 3 whole hours to sort it out.
Note to Roan and/or Ed:
Editing references seems to work fine, but when saving the page there
are "no changes" which is a reasonable indication to the contrary.
Objectives:
* Make it possible to have multiple surfaces be instantiated, get along
nicely, and be embedded inside of iframes if needed.
* Make reference content editable within a dialog
Approach:
* Move what's left of ve.Editor to ve.ui.Surface and essentially
obliterate all use of it
* Make even more stuff inherit from ve.Element (long live this.$$)
* Use the correct document or window anywhere it was being assumed to be
the top level one
* Resolve stacking order issues by removing the excessive use of z-index
and introducing global and local overlay elements for each editor
* Add a surface to the reference dialog, load up the reference contents
and save them back on apply
* Actually destroy what we create in ce and ui surfaces
* Add recursive frame offset calculation method to ve.Element
* Moved ve.ce.Surface's getSelectionRect method to the prototype
Bonus:
* Move ve.ce.DocumentNode.css contents to ve.ce.Node.css (not sure why it
was separate in the first place, but I'm likely the one to blame)
* Fix blatant lies in documentation
* Whitespace cleanup here and there
* Get rid of ve.ui.Window overlays - not used or needed
Change-Id: Iede83e7d24f7cb249b6ba3dc45d770445b862e08
2013-05-20 22:45:50 +00:00
|
|
|
* @param {Object} [config] Config options
|
2013-05-15 18:44:32 +00:00
|
|
|
*/
|
The Great ve.ui.Surface refactor of 2013
Prologue:
Farewell ve.Editor my good chap… Oh, hey there HTML frames - I didn't
see you there! In a world where iframes are outlaws, and symbols like
document and window are global, there were more than a few assumptions
about which document or window was being used. But fear not - for this
commit (probably) tracks them all down, leaving a trail of
iframe-compatible awesomeness in its wake. With the great ve.ui.Surface
now able to be used inside of iframes, let the reference editing
commence. But there, lurking in the darkness is a DM issue so fierce it
may take Roan and/or Ed up to 3 whole hours to sort it out.
Note to Roan and/or Ed:
Editing references seems to work fine, but when saving the page there
are "no changes" which is a reasonable indication to the contrary.
Objectives:
* Make it possible to have multiple surfaces be instantiated, get along
nicely, and be embedded inside of iframes if needed.
* Make reference content editable within a dialog
Approach:
* Move what's left of ve.Editor to ve.ui.Surface and essentially
obliterate all use of it
* Make even more stuff inherit from ve.Element (long live this.$$)
* Use the correct document or window anywhere it was being assumed to be
the top level one
* Resolve stacking order issues by removing the excessive use of z-index
and introducing global and local overlay elements for each editor
* Add a surface to the reference dialog, load up the reference contents
and save them back on apply
* Actually destroy what we create in ce and ui surfaces
* Add recursive frame offset calculation method to ve.Element
* Moved ve.ce.Surface's getSelectionRect method to the prototype
Bonus:
* Move ve.ce.DocumentNode.css contents to ve.ce.Node.css (not sure why it
was separate in the first place, but I'm likely the one to blame)
* Fix blatant lies in documentation
* Whitespace cleanup here and there
* Get rid of ve.ui.Window overlays - not used or needed
Change-Id: Iede83e7d24f7cb249b6ba3dc45d770445b862e08
2013-05-20 22:45:50 +00:00
|
|
|
ve.ui.MWTemplateDialog = function VeUiMWTemplateDialog( surface, config ) {
|
2013-05-15 18:44:32 +00:00
|
|
|
// Parent constructor
|
The Great ve.ui.Surface refactor of 2013
Prologue:
Farewell ve.Editor my good chap… Oh, hey there HTML frames - I didn't
see you there! In a world where iframes are outlaws, and symbols like
document and window are global, there were more than a few assumptions
about which document or window was being used. But fear not - for this
commit (probably) tracks them all down, leaving a trail of
iframe-compatible awesomeness in its wake. With the great ve.ui.Surface
now able to be used inside of iframes, let the reference editing
commence. But there, lurking in the darkness is a DM issue so fierce it
may take Roan and/or Ed up to 3 whole hours to sort it out.
Note to Roan and/or Ed:
Editing references seems to work fine, but when saving the page there
are "no changes" which is a reasonable indication to the contrary.
Objectives:
* Make it possible to have multiple surfaces be instantiated, get along
nicely, and be embedded inside of iframes if needed.
* Make reference content editable within a dialog
Approach:
* Move what's left of ve.Editor to ve.ui.Surface and essentially
obliterate all use of it
* Make even more stuff inherit from ve.Element (long live this.$$)
* Use the correct document or window anywhere it was being assumed to be
the top level one
* Resolve stacking order issues by removing the excessive use of z-index
and introducing global and local overlay elements for each editor
* Add a surface to the reference dialog, load up the reference contents
and save them back on apply
* Actually destroy what we create in ce and ui surfaces
* Add recursive frame offset calculation method to ve.Element
* Moved ve.ce.Surface's getSelectionRect method to the prototype
Bonus:
* Move ve.ce.DocumentNode.css contents to ve.ce.Node.css (not sure why it
was separate in the first place, but I'm likely the one to blame)
* Fix blatant lies in documentation
* Whitespace cleanup here and there
* Get rid of ve.ui.Window overlays - not used or needed
Change-Id: Iede83e7d24f7cb249b6ba3dc45d770445b862e08
2013-05-20 22:45:50 +00:00
|
|
|
ve.ui.PagedDialog.call( this, surface, config );
|
2013-05-28 23:14:43 +00:00
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.node = null;
|
|
|
|
this.content = null;
|
|
|
|
this.specs = {};
|
2013-05-15 18:44:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-05-15 20:39:34 +00:00
|
|
|
ve.inheritClass( ve.ui.MWTemplateDialog, ve.ui.PagedDialog );
|
2013-05-15 18:44:32 +00:00
|
|
|
|
|
|
|
/* Static Properties */
|
|
|
|
|
|
|
|
ve.ui.MWTemplateDialog.static.titleMessage = 'visualeditor-dialog-template-title';
|
|
|
|
|
|
|
|
ve.ui.MWTemplateDialog.static.icon = 'template';
|
|
|
|
|
|
|
|
ve.ui.MWTemplateDialog.static.modelClasses = [ ve.dm.MWTemplateNode ];
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
2013-05-28 23:14:43 +00:00
|
|
|
* Handle frame open events.
|
|
|
|
*
|
|
|
|
* @method
|
2013-05-15 18:44:32 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWTemplateDialog.prototype.onOpen = function () {
|
2013-05-28 23:14:43 +00:00
|
|
|
var i, len, template, title,
|
|
|
|
templates = [];
|
2013-05-17 03:35:06 +00:00
|
|
|
|
2013-05-28 23:14:43 +00:00
|
|
|
this.node = this.surface.getView().getFocusedNode();
|
|
|
|
if ( !this.node ) {
|
|
|
|
throw new Error( 'No focused node to edit' );
|
|
|
|
}
|
2013-05-17 03:35:06 +00:00
|
|
|
|
2013-05-28 23:14:43 +00:00
|
|
|
// Get content values
|
|
|
|
this.content = ve.copyObject( this.node.getModel().getAttribute( 'mw' ) );
|
|
|
|
// Convert single template format to multiple template format
|
|
|
|
if ( this.content.params ) {
|
|
|
|
this.content = { 'parts': [ { 'template': this.content } ] };
|
|
|
|
}
|
|
|
|
// Get all template data asynchronously
|
|
|
|
for ( i = 0, len = this.content.parts.length; i < len; i++ ) {
|
|
|
|
template = this.content.parts[i].template;
|
|
|
|
if ( template ) {
|
|
|
|
if ( template.target.url ) {
|
|
|
|
try {
|
|
|
|
title = new mw.Title( template.target.url );
|
|
|
|
templates.push( {
|
|
|
|
'title': title.toString(),
|
|
|
|
'params': template.params
|
|
|
|
} );
|
|
|
|
} catch ( e ) {}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Wrap plain wikitext in object so editor has something to reference
|
|
|
|
this.content.parts[i] = { 'wt': this.content.parts[i] };
|
2013-05-17 03:35:06 +00:00
|
|
|
}
|
2013-05-21 01:27:38 +00:00
|
|
|
}
|
2013-05-28 23:14:43 +00:00
|
|
|
if ( templates.length ) {
|
|
|
|
this.getTemplateData( templates )
|
|
|
|
.done( ve.bind( function ( specs ) {
|
|
|
|
this.specs = specs;
|
|
|
|
}, this ) )
|
|
|
|
.always( ve.bind( this.setupPages, this ) );
|
|
|
|
} else {
|
|
|
|
this.setupPages();
|
|
|
|
}
|
2013-05-15 23:16:42 +00:00
|
|
|
};
|
2013-05-15 22:13:38 +00:00
|
|
|
|
2013-05-15 23:16:42 +00:00
|
|
|
/**
|
2013-05-28 23:14:43 +00:00
|
|
|
* Handle window close events.
|
2013-05-15 23:16:42 +00:00
|
|
|
*
|
2013-05-28 23:14:43 +00:00
|
|
|
* @method
|
|
|
|
* @param {string} action Action that caused the window to be closed
|
2013-05-15 23:16:42 +00:00
|
|
|
*/
|
2013-05-28 23:14:43 +00:00
|
|
|
ve.ui.MWTemplateDialog.prototype.onClose = function ( action ) {
|
|
|
|
var i, len, wt,
|
|
|
|
surfaceModel = this.surface.getModel();
|
2013-05-21 01:27:38 +00:00
|
|
|
|
2013-05-28 23:14:43 +00:00
|
|
|
// Save changes
|
|
|
|
if ( action === 'apply' ) {
|
|
|
|
// Expand wikitext content
|
|
|
|
for ( i = 0, len = this.content.parts.length; i < len; i++ ) {
|
|
|
|
wt = this.content.parts[i].wt;
|
|
|
|
if ( typeof wt === 'string' ) {
|
|
|
|
// Replace object wrapper with plain text
|
|
|
|
this.content.parts[i] = wt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Restore single template format
|
|
|
|
if ( this.content.parts.length === 1 ) {
|
|
|
|
this.content = this.content.parts[0].template;
|
|
|
|
}
|
|
|
|
// TODO: Wrap attribute changes in ve.dm.SurfaceFragment
|
|
|
|
surfaceModel.change(
|
|
|
|
ve.dm.Transaction.newFromAttributeChange(
|
|
|
|
surfaceModel.getDocument(), this.node.getOffset(), 'mw', this.content
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2013-05-15 23:16:42 +00:00
|
|
|
|
2013-05-28 23:14:43 +00:00
|
|
|
this.clearPages();
|
|
|
|
this.node = null;
|
|
|
|
this.content = null;
|
|
|
|
this.specs = {};
|
2013-05-15 23:16:42 +00:00
|
|
|
|
2013-05-28 23:14:43 +00:00
|
|
|
// Parent method
|
|
|
|
ve.ui.PagedDialog.prototype.onClose.call( this );
|
|
|
|
};
|
2013-05-15 21:36:44 +00:00
|
|
|
|
2013-05-28 23:14:43 +00:00
|
|
|
/**
|
|
|
|
* Handle template data load events.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.ui.MWTemplateDialog.prototype.setupPages = function () {
|
|
|
|
// Build pages from parts
|
|
|
|
var i, len, template, spec, param,
|
|
|
|
parts = this.content.parts,
|
|
|
|
specs = this.specs;
|
2013-05-21 01:27:38 +00:00
|
|
|
|
2013-05-28 23:14:43 +00:00
|
|
|
// Parent method
|
|
|
|
ve.ui.PagedDialog.prototype.onOpen.call( this );
|
2013-05-16 00:17:06 +00:00
|
|
|
|
2013-05-28 23:14:43 +00:00
|
|
|
// Populate pages
|
|
|
|
for ( i = 0, len = parts.length; i < len; i++ ) {
|
|
|
|
if ( parts[i].template ) {
|
|
|
|
template = parts[i].template;
|
|
|
|
spec = specs[template.target.url];
|
|
|
|
// Add template page
|
|
|
|
this.addPage( 'part_' + i, { 'label': template.target.url, 'icon': 'template' } );
|
|
|
|
// Add parameter pages
|
|
|
|
for ( param in template.params ) {
|
|
|
|
this.addParameterPage(
|
|
|
|
'part_' + i + '_param_' + param,
|
|
|
|
param,
|
|
|
|
template.params[param],
|
|
|
|
spec.params[param]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else if ( parts[i].wt ) {
|
|
|
|
// Add wikitext page
|
|
|
|
this.addWikitextPage( 'part_' + i, parts[i] );
|
|
|
|
}
|
|
|
|
}
|
2013-05-15 18:44:32 +00:00
|
|
|
};
|
|
|
|
|
2013-05-21 01:27:38 +00:00
|
|
|
/**
|
2013-05-28 23:14:43 +00:00
|
|
|
* Get a promise for template data.
|
|
|
|
*
|
|
|
|
* TODO: Backfill template info from params objects
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {Object[]} templates Template information containing `title` and `params` properties
|
|
|
|
* @return {jQuery.Promise} Template data blob on success, or an error code on failure
|
2013-05-21 01:27:38 +00:00
|
|
|
*/
|
2013-05-28 23:14:43 +00:00
|
|
|
ve.ui.MWTemplateDialog.prototype.getTemplateData = function ( templates ) {
|
|
|
|
var i, len,
|
|
|
|
titles = [],
|
|
|
|
specs = {},
|
|
|
|
deferred = $.Deferred();
|
|
|
|
|
|
|
|
// Collect all titles
|
|
|
|
for ( i = 0, len = templates.length; i < len; i++ ) {
|
|
|
|
titles.push( templates[i].title );
|
2013-05-21 01:27:38 +00:00
|
|
|
}
|
2013-05-28 23:14:43 +00:00
|
|
|
|
|
|
|
// Request template data from server
|
2013-05-21 01:27:38 +00:00
|
|
|
$.ajax( {
|
|
|
|
'url': mw.util.wikiScript( 'api' ),
|
|
|
|
'dataType': 'json',
|
|
|
|
'data': {
|
|
|
|
'format': 'json',
|
|
|
|
'action': 'templatedata',
|
2013-05-28 23:14:43 +00:00
|
|
|
'titles': titles.join( '|' )
|
2013-05-21 01:27:38 +00:00
|
|
|
}
|
2013-05-28 23:14:43 +00:00
|
|
|
} )
|
|
|
|
.done( function ( data ) {
|
|
|
|
var id;
|
|
|
|
if ( data && data.pages ) {
|
|
|
|
// Add template data to spec
|
|
|
|
for ( id in data.pages ) {
|
|
|
|
specs[data.pages[id].title] = data.pages[id];
|
2013-05-17 03:35:06 +00:00
|
|
|
}
|
2013-05-28 23:14:43 +00:00
|
|
|
deferred.resolve( specs );
|
2013-05-15 22:36:38 +00:00
|
|
|
}
|
2013-05-28 23:14:43 +00:00
|
|
|
deferred.reject( 'unavailable', arguments );
|
|
|
|
} )
|
|
|
|
.fail( function () {
|
|
|
|
deferred.reject( 'http', arguments );
|
|
|
|
} );
|
|
|
|
|
|
|
|
return deferred.promise();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add page for wikitext.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {string} page Unique page name
|
|
|
|
* @param {Object} value Parameter value
|
|
|
|
*/
|
|
|
|
ve.ui.MWTemplateDialog.prototype.addWikitextPage = function ( page, value ) {
|
|
|
|
var fieldset, textInput;
|
|
|
|
|
|
|
|
fieldset = new ve.ui.FieldsetLayout( {
|
|
|
|
'$$': this.frame.$$,
|
|
|
|
'label': 'Content',
|
|
|
|
'icon': 'source'
|
2013-05-21 01:27:38 +00:00
|
|
|
} );
|
|
|
|
|
2013-05-28 23:14:43 +00:00
|
|
|
textInput = new ve.ui.TextInputWidget( { '$$': this.frame.$$, 'multiline': true } );
|
|
|
|
textInput.$input.css( { 'height': 100, 'width': '100%' } );
|
|
|
|
textInput.setValue( value.wt );
|
|
|
|
textInput.on( 'change', function () {
|
|
|
|
value.wt = textInput.getValue();
|
|
|
|
} );
|
|
|
|
|
|
|
|
this.addPage( page, { 'label': 'Content', 'icon': 'source' } );
|
|
|
|
this.pages[page].$.append( fieldset.$.append( textInput.$ ) );
|
2013-05-15 22:36:38 +00:00
|
|
|
};
|
|
|
|
|
2013-05-15 18:44:32 +00:00
|
|
|
/**
|
2013-05-28 23:14:43 +00:00
|
|
|
* Add page for a parameter.
|
2013-05-15 18:44:32 +00:00
|
|
|
*
|
2013-05-28 23:14:43 +00:00
|
|
|
* @method
|
|
|
|
* @param {string} page Unique page name
|
|
|
|
* @param {string} name Parameter name
|
|
|
|
* @param {Object} value Parameter value
|
|
|
|
* @param {Object} spec Parameter specification
|
2013-05-15 18:44:32 +00:00
|
|
|
*/
|
2013-05-28 23:14:43 +00:00
|
|
|
ve.ui.MWTemplateDialog.prototype.addParameterPage = function ( page, name, value, spec ) {
|
|
|
|
var fieldset, textInput, inputLabel,
|
|
|
|
label = spec && spec.label ? spec.label.en : name,
|
|
|
|
description = spec && spec.description && spec.description.en;
|
2013-05-16 00:17:06 +00:00
|
|
|
|
2013-05-28 23:14:43 +00:00
|
|
|
fieldset = new ve.ui.FieldsetLayout( {
|
|
|
|
'$$': this.frame.$$,
|
|
|
|
'label': label,
|
|
|
|
'icon': 'parameter'
|
|
|
|
} );
|
|
|
|
|
|
|
|
textInput = new ve.ui.TextInputWidget( { '$$': this.frame.$$, 'multiline': true } );
|
|
|
|
textInput.$input.css( { 'height': 100, 'width': '100%' } );
|
|
|
|
textInput.setValue( value.wt );
|
|
|
|
textInput.on( 'change', function () {
|
|
|
|
value.wt = textInput.getValue();
|
|
|
|
} );
|
|
|
|
|
|
|
|
if ( description ) {
|
|
|
|
inputLabel = new ve.ui.InputLabelWidget( {
|
|
|
|
'$$': this.frame.$$,
|
|
|
|
'input': textInput,
|
|
|
|
'label': description
|
|
|
|
} );
|
|
|
|
fieldset.$.append( inputLabel.$ );
|
2013-05-16 00:17:06 +00:00
|
|
|
}
|
|
|
|
|
2013-05-28 23:14:43 +00:00
|
|
|
// TODO: Use spec.required
|
|
|
|
// TODO: Use spec.deprecation
|
|
|
|
// TODO: Use spec.default
|
|
|
|
// TODO: Use spec.type
|
|
|
|
|
|
|
|
this.addPage( page, { 'label': label, 'icon': 'parameter', 'level': 1 } );
|
|
|
|
this.pages[page].$.append( fieldset.$.append( textInput.$ ) );
|
2013-05-15 18:44:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.ui.dialogFactory.register( 'mwTemplate', ve.ui.MWTemplateDialog );
|
|
|
|
|
|
|
|
ve.ui.viewRegistry.register( 'mwTemplate', ve.ui.MWTemplateDialog );
|