mediawiki-extensions-Visual.../modules/ve/ui/dialogs/ve.ui.MWTemplateDialog.js

216 lines
5.9 KiB
JavaScript
Raw Normal View History

/*!
* VisualEditor user interface MWTemplateDialog class.
*
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/*global mw */
/**
* Document dialog.
*
* See https://raw.github.com/wikimedia/mediawiki-extensions-TemplateData/master/spec.templatedata.json
* for the latest version of the TemplateData specification.
*
* @class
* @extends ve.ui.PagedDialog
*
* @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
*/
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 ) {
// 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 );
};
/* Inheritance */
ve.inheritClass( ve.ui.MWTemplateDialog, ve.ui.PagedDialog );
/* 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 () {
var self = this,
mwAttr = this.surface.view.focusedNode.model.getAttribute( 'mw' );
function handle( templateData ) {
var param,
paramsData = templateData && templateData.params;
self.paramsKeys = [];
self.paramsToInputs = {};
// Parent method
ve.ui.PagedDialog.prototype.onOpen.call( self );
// Add template page
self.addPage( 'template', { 'label': mwAttr.target.wt, 'icon': 'template' } );
// Loop through parameters
for ( param in mwAttr.params ) {
self.createParamPage( param, mwAttr.params[param], paramsData && paramsData[param] );
}
// 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();
} );
};
/**
* Handle creating page for single parameter.
*
* @param {string} key Template parameter name
* @param {Object} value Value container with `wt` property
* @param {Object} paramData Param object from TemplateData
*/
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 );
// TODO: Use paramData.requred
// TODO: Use paramData.deprecation
// TODO: Use paramData.default
// TODO: Use paramData.type
if ( description ) {
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.$$,
'input': textInput,
'label': description
} );
fieldset.$.append( inputLabel.$ );
}
fieldset.$.append( textInput.$ );
this.addPage( pageName, { 'label': label, 'icon': 'parameter', 'level': 1 } );
this.pages[pageName].$.append( fieldset.$ );
this.paramsKeys.push( key );
this.paramsToInputs[key] = textInput;
};
/**
* @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;
}
}
}
d.reject( 'unavailable', arguments );
} ).fail( function () {
d.reject( 'http', arguments );
} );
return d.promise();
};
/**
* Handle frame ready events.
*
* @param {string} action Action that caused the window to be closed
*/
ve.ui.MWTemplateDialog.prototype.onClose = function ( action ) {
var mwAttr, i;
// Parent method
ve.ui.PagedDialog.prototype.onOpen.call( this );
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
)
);
}
this.clearPages();
};
/* Registration */
ve.ui.dialogFactory.register( 'mwTemplate', ve.ui.MWTemplateDialog );
ve.ui.viewRegistry.register( 'mwTemplate', ve.ui.MWTemplateDialog );