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-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 */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle frame ready events.
|
|
|
|
*/
|
|
|
|
ve.ui.MWTemplateDialog.prototype.onOpen = function () {
|
2013-05-21 01:27:38 +00:00
|
|
|
var self = this,
|
|
|
|
mwAttr = this.surface.view.focusedNode.model.getAttribute( 'mw' );
|
|
|
|
|
|
|
|
function handle( templateData ) {
|
|
|
|
var param,
|
|
|
|
paramsData = templateData && templateData.params;
|
|
|
|
self.paramsKeys = [];
|
|
|
|
self.paramsToInputs = {};
|
2013-05-17 03:35:06 +00:00
|
|
|
|
|
|
|
// Parent method
|
2013-05-21 01:27:38 +00:00
|
|
|
ve.ui.PagedDialog.prototype.onOpen.call( self );
|
2013-05-17 03:35:06 +00:00
|
|
|
|
|
|
|
// Add template page
|
2013-05-21 01:27:38 +00:00
|
|
|
self.addPage( 'template', mwAttr.target.wt, 'template' );
|
2013-05-17 03:35:06 +00:00
|
|
|
|
|
|
|
// Loop through parameters
|
|
|
|
for ( param in mwAttr.params ) {
|
2013-05-21 01:27:38 +00:00
|
|
|
self.createParamPage(
|
2013-05-17 03:35:06 +00:00
|
|
|
param,
|
|
|
|
mwAttr.params[param],
|
2013-05-21 01:27:38 +00:00
|
|
|
paramsData && paramsData[param]
|
2013-05-17 03:35:06 +00:00
|
|
|
);
|
|
|
|
}
|
2013-05-21 01:27:38 +00:00
|
|
|
|
|
|
|
// TODO: Ability to remove parameters
|
|
|
|
// TODO: Ability to add other parameters in paramsData
|
|
|
|
// Also account for paramsData#aliases
|
|
|
|
// TODO: Ability to add arbitrary parameters
|
|
|
|
// TODO: Use templateData.sets
|
|
|
|
}
|
|
|
|
|
|
|
|
this.getTemplateData( mwAttr.target )
|
|
|
|
.done( handle )
|
|
|
|
.fail( function ( errorCode, details ) {
|
|
|
|
mw.log( 'TemplateData unavailable: ' + errorCode, mwAttr.target, details );
|
|
|
|
handle();
|
|
|
|
} );
|
2013-05-15 23:16:42 +00:00
|
|
|
};
|
2013-05-15 22:13:38 +00:00
|
|
|
|
2013-05-15 23:16:42 +00:00
|
|
|
/**
|
|
|
|
* Handle creating page for single parameter.
|
|
|
|
*
|
2013-05-21 01:27:38 +00:00
|
|
|
* @param {string} key Template parameter name
|
|
|
|
* @param {Object} value Value container with `wt` property
|
|
|
|
* @param {Object} paramData Param object from TemplateData
|
2013-05-15 23:16:42 +00:00
|
|
|
*/
|
2013-05-21 01:27:38 +00:00
|
|
|
ve.ui.MWTemplateDialog.prototype.createParamPage = function ( key, value, paramData ) {
|
|
|
|
var fieldset, textInput, inputLabel,
|
|
|
|
pageName = 'parameter_' + key,
|
|
|
|
label = paramData && paramData.label ? paramData.label.en : key,
|
|
|
|
description = paramData && paramData.description && paramData.description.en;
|
|
|
|
|
|
|
|
// Label
|
|
|
|
fieldset = new ve.ui.FieldsetLayout( {
|
|
|
|
'$$': this.$$,
|
|
|
|
'label': label,
|
|
|
|
'icon': 'parameter'
|
|
|
|
} );
|
|
|
|
|
|
|
|
textInput = new ve.ui.TextInputWidget( {
|
|
|
|
'$$': this.$$,
|
|
|
|
'multiline': true
|
|
|
|
} );
|
|
|
|
textInput.$input.css( 'height', 100 );
|
|
|
|
textInput.setValue( value.wt );
|
2013-05-15 23:16:42 +00:00
|
|
|
|
2013-05-21 01:27:38 +00:00
|
|
|
// TODO: Use paramData.requred
|
|
|
|
// TODO: Use paramData.deprecation
|
|
|
|
// TODO: Use paramData.default
|
|
|
|
// TODO: Use paramData.type
|
2013-05-15 23:16:42 +00:00
|
|
|
|
2013-05-21 01:27:38 +00:00
|
|
|
if ( description ) {
|
2013-05-15 23:16:42 +00:00
|
|
|
inputLabel = new ve.ui.InputLabelWidget( {
|
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
|
|
|
'$$': this.frame.$$,
|
2013-05-15 23:16:42 +00:00
|
|
|
'input': textInput,
|
2013-05-21 01:27:38 +00:00
|
|
|
'label': description
|
2013-05-15 22:13:38 +00:00
|
|
|
} );
|
2013-05-15 23:16:42 +00:00
|
|
|
fieldset.$.append( inputLabel.$ );
|
2013-05-15 21:36:44 +00:00
|
|
|
}
|
|
|
|
|
2013-05-15 23:16:42 +00:00
|
|
|
fieldset.$.append( textInput.$ );
|
2013-05-21 01:27:38 +00:00
|
|
|
|
2013-05-15 23:16:42 +00:00
|
|
|
this.addPage( pageName, label, 'parameter', 1 );
|
|
|
|
this.pages[pageName].$.append( fieldset.$ );
|
2013-05-16 00:17:06 +00:00
|
|
|
|
|
|
|
this.paramsKeys.push( key );
|
|
|
|
this.paramsToInputs[key] = textInput;
|
2013-05-15 18:44:32 +00:00
|
|
|
};
|
|
|
|
|
2013-05-21 01:27:38 +00:00
|
|
|
/**
|
|
|
|
* @param {Object} template Information about the template target from Parsoid.
|
|
|
|
* Contains a `wt` property containing the wikitext of the invocation target.
|
|
|
|
* This is unprocessed so it can start with : and/or lack the proper namespace
|
|
|
|
* prefix for templates in the Template namespace.
|
|
|
|
* @return {jQuery.Promise} Template data blob on success, or an error code on failure.
|
|
|
|
*/
|
|
|
|
ve.ui.MWTemplateDialog.prototype.getTemplateData = function ( template ) {
|
|
|
|
var title,
|
|
|
|
d = $.Deferred();
|
|
|
|
if ( !template.wt || template.wt.indexOf( '{' ) !== -1 ) {
|
|
|
|
// Name contains wikitext, need Parsoid to provide name (bug 48663)
|
|
|
|
d.reject( 'complicated' );
|
|
|
|
}
|
|
|
|
// In sample cases we'll handle the namespace fallback (should ultimately
|
|
|
|
// be done by Parsoid, bug 48663). If the title has no namespace prefix,
|
|
|
|
// assume NS_TEMPLATE namespace (like MediaWiki does)
|
|
|
|
title = new mw.Title( template.wt );
|
|
|
|
if ( title.getNamespaceId() === 0 && title.toString()[0] !== ':' ) {
|
|
|
|
title = new mw.Title( template.wt, mw.config.get( 'wgNamespaceIds' ).template );
|
|
|
|
}
|
|
|
|
title = title.toString();
|
|
|
|
$.ajax( {
|
|
|
|
'url': mw.util.wikiScript( 'api' ),
|
|
|
|
'dataType': 'json',
|
|
|
|
'data': {
|
|
|
|
'format': 'json',
|
|
|
|
'action': 'templatedata',
|
|
|
|
'titles': title
|
|
|
|
}
|
|
|
|
} ).done( function ( data ) {
|
|
|
|
var pageid, page;
|
|
|
|
if ( data && data.pages ) {
|
|
|
|
for ( pageid in data.pages ) {
|
|
|
|
page = data.pages[pageid];
|
|
|
|
if ( page.title === title ) {
|
|
|
|
d.resolve( page );
|
|
|
|
return;
|
2013-05-17 03:35:06 +00:00
|
|
|
}
|
2013-05-15 22:36:38 +00:00
|
|
|
}
|
2013-05-21 01:27:38 +00:00
|
|
|
}
|
|
|
|
d.reject( 'unavailable', arguments );
|
|
|
|
} ).fail( function () {
|
|
|
|
d.reject( 'http', arguments );
|
|
|
|
} );
|
|
|
|
|
|
|
|
return d.promise();
|
2013-05-15 22:36:38 +00:00
|
|
|
};
|
|
|
|
|
2013-05-15 18:44:32 +00:00
|
|
|
/**
|
|
|
|
* Handle frame ready events.
|
|
|
|
*
|
|
|
|
* @param {string} action Action that caused the window to be closed
|
|
|
|
*/
|
2013-05-16 00:17:06 +00:00
|
|
|
ve.ui.MWTemplateDialog.prototype.onClose = function ( action ) {
|
|
|
|
var mwAttr, i;
|
2013-05-15 18:44:32 +00:00
|
|
|
// Parent method
|
2013-05-15 21:36:44 +00:00
|
|
|
ve.ui.PagedDialog.prototype.onOpen.call( this );
|
2013-05-16 00:17:06 +00:00
|
|
|
|
|
|
|
if ( action === 'apply' ) {
|
|
|
|
mwAttr = ve.cloneObject( this.surface.view.focusedNode.model.getAttribute( 'mw' ) );
|
|
|
|
mwAttr.params = {};
|
|
|
|
for ( i = 0; i < this.paramsKeys.length; i++ ) {
|
|
|
|
mwAttr.params[this.paramsKeys[i]] = {
|
|
|
|
'wt': this.paramsToInputs[this.paramsKeys[i]].getValue()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
this.surface.model.change(
|
|
|
|
ve.dm.Transaction.newFromAttributeChange(
|
|
|
|
this.surface.model.documentModel,
|
|
|
|
this.surface.view.focusedNode.getOffset(),
|
|
|
|
'mw',
|
|
|
|
mwAttr
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-05-15 21:36:44 +00:00
|
|
|
this.clearPages();
|
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 );
|