2015-02-17 23:56:44 +00:00
|
|
|
( function () {
|
2015-03-07 01:20:26 +00:00
|
|
|
'use strict';
|
2014-10-11 05:22:21 +00:00
|
|
|
/**
|
|
|
|
* TemplateData Generator data model.
|
|
|
|
* This singleton is independent of any UI; it expects
|
|
|
|
* a templatedata string, converts it into the object
|
|
|
|
* model and manages it, fully event-driven.
|
|
|
|
*
|
2015-03-07 01:20:26 +00:00
|
|
|
* @class
|
|
|
|
* @singleton
|
2014-10-11 05:22:21 +00:00
|
|
|
*/
|
|
|
|
mw.libs.tdgUi = ( function () {
|
|
|
|
var isPageSubLevel,
|
2015-02-18 22:55:17 +00:00
|
|
|
isDocPage,
|
|
|
|
pageName,
|
2014-10-11 05:22:21 +00:00
|
|
|
parentPage,
|
|
|
|
$textbox,
|
2015-03-07 01:20:26 +00:00
|
|
|
// ooui Window Manager
|
2015-03-07 00:17:21 +00:00
|
|
|
sourceHandler,
|
2014-10-11 05:22:21 +00:00
|
|
|
tdgDialog,
|
2015-03-07 00:17:21 +00:00
|
|
|
messageDialog,
|
2014-10-11 05:22:21 +00:00
|
|
|
windowManager,
|
2015-03-07 01:20:26 +00:00
|
|
|
// Edit window elements
|
2014-10-11 05:22:21 +00:00
|
|
|
editOpenDialogButton,
|
|
|
|
editNoticeLabel,
|
|
|
|
|
|
|
|
editArea = {
|
|
|
|
/**
|
|
|
|
* Display error message in the edit window
|
2015-03-07 01:20:26 +00:00
|
|
|
*
|
2014-10-11 05:22:21 +00:00
|
|
|
* @param {string} msg Message to display
|
|
|
|
* @param {string} type Message type 'notice' or 'error'
|
2015-02-18 22:55:17 +00:00
|
|
|
* @param {boolean} [parseHTML] The message should be parsed
|
2014-10-11 05:22:21 +00:00
|
|
|
*/
|
2015-02-18 22:55:17 +00:00
|
|
|
setNoticeMessage: function ( msg, type, parseHTML ) {
|
2014-10-11 05:22:21 +00:00
|
|
|
type = type || 'error';
|
|
|
|
editNoticeLabel.$element.toggleClass( 'errorbox', type === 'error' );
|
|
|
|
|
2015-02-18 22:55:17 +00:00
|
|
|
if ( parseHTML ) {
|
|
|
|
// OOUI's label elements do not parse strings and display them
|
|
|
|
// as-is. If the message contains html that should be parsed,
|
|
|
|
// we have to transform it into a jQuery object
|
|
|
|
msg = $( '<span>' ).append( $.parseHTML( msg ) );
|
|
|
|
}
|
2014-10-11 05:22:21 +00:00
|
|
|
editNoticeLabel.setLabel( msg );
|
2015-02-18 22:55:17 +00:00
|
|
|
editNoticeLabel.toggle( true );
|
2014-10-11 05:22:21 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the error message in the edit window
|
|
|
|
*/
|
|
|
|
resetNoticeMessage: function () {
|
|
|
|
editNoticeLabel.setLabel( '' );
|
2015-02-18 22:55:17 +00:00
|
|
|
editNoticeLabel.toggle( false );
|
2014-10-11 05:22:21 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Respond to edit dialog button click.
|
2015-03-07 01:20:26 +00:00
|
|
|
*
|
|
|
|
* @method onEditOpenDialogButton
|
2014-10-11 05:22:21 +00:00
|
|
|
*/
|
|
|
|
onEditOpenDialogButton = function () {
|
|
|
|
// Reset notice message
|
|
|
|
editArea.resetNoticeMessage();
|
2015-03-07 00:17:21 +00:00
|
|
|
|
|
|
|
// Build the model
|
|
|
|
sourceHandler.buildModel( $textbox.val() )
|
|
|
|
.then(
|
|
|
|
// Success
|
|
|
|
function ( model ) {
|
|
|
|
openEditDialog( model );
|
|
|
|
},
|
|
|
|
// Failure
|
|
|
|
function () {
|
|
|
|
// Open a message dialog
|
|
|
|
windowManager.openWindow( messageDialog, {
|
|
|
|
title: mw.msg( 'templatedata-modal-title' ),
|
|
|
|
message: mw.msg( 'templatedata-errormsg-jsonbadformat' ),
|
|
|
|
actions: [
|
|
|
|
{
|
|
|
|
action: 'accept',
|
|
|
|
label: mw.msg( 'templatedata-modal-json-error-replace' ),
|
|
|
|
flags: [ 'primary', 'destructive' ]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
action: 'reject',
|
|
|
|
label: OO.ui.deferMsg( 'ooui-dialog-message-reject' ),
|
|
|
|
flags: 'safe'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
} )
|
|
|
|
.then( function ( opening ) {
|
|
|
|
return opening;
|
|
|
|
} )
|
|
|
|
.then( function ( opened ) {
|
|
|
|
return opened;
|
|
|
|
} )
|
|
|
|
.then( function ( data ) {
|
|
|
|
var model;
|
|
|
|
if ( data && data.action === 'accept' ) {
|
|
|
|
// Open the dialog with an empty model
|
|
|
|
model = new mw.TemplateData.Model.static.newFromObject(
|
|
|
|
{ params: {} },
|
|
|
|
sourceHandler.getTemplateSourceCodeParams()
|
|
|
|
);
|
|
|
|
openEditDialog( model );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open the templatedata edit dialog
|
|
|
|
*
|
|
|
|
* @method openEditDialog
|
|
|
|
* @param {mw.TemplateData.Model} dataModel The data model
|
|
|
|
* associated with this edit dialog.
|
|
|
|
*/
|
|
|
|
openEditDialog = function ( dataModel ) {
|
|
|
|
// Open the edit dialog
|
2014-10-11 05:22:21 +00:00
|
|
|
windowManager.openWindow( tdgDialog, {
|
2015-03-07 00:17:21 +00:00
|
|
|
model: dataModel
|
2014-10-11 05:22:21 +00:00
|
|
|
} );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Respond to edit dialog apply event
|
2015-03-07 01:20:26 +00:00
|
|
|
*
|
|
|
|
* @method onDialogApply
|
2014-10-11 05:22:21 +00:00
|
|
|
* @param {string} templateDataString New templatedata string
|
|
|
|
*/
|
|
|
|
onDialogApply = function ( templateDataString ) {
|
|
|
|
$textbox.val( replaceTemplateData( templateDataString ) );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace the old templatedata string with the new one, or
|
|
|
|
* insert the new one into the page if an old one doesn't exist
|
2015-03-07 01:20:26 +00:00
|
|
|
*
|
|
|
|
* @method replaceTemplateData
|
2014-10-11 05:22:21 +00:00
|
|
|
* @param {string} newTemplateData New templatedata string
|
|
|
|
* @return {string} Full wikitext content with the new templatedata
|
|
|
|
* string.
|
|
|
|
*/
|
|
|
|
replaceTemplateData = function ( newTemplateData ) {
|
|
|
|
var finalOutput,
|
|
|
|
fullWikitext = $textbox.val(),
|
|
|
|
parts = fullWikitext.match(
|
|
|
|
/<templatedata>([\s\S]*?)<\/templatedata>/i
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( parts && parts[1] ) {
|
|
|
|
// <templatedata> exists. Replace it
|
|
|
|
finalOutput = fullWikitext.replace(
|
|
|
|
/(<templatedata>)([\s\S]*?)(<\/templatedata>)/i,
|
|
|
|
'<templatedata>\n' + newTemplateData + '\n</templatedata>'
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
if ( isPageSubLevel ) {
|
|
|
|
// Add the <templatedata>
|
|
|
|
finalOutput = fullWikitext + '\n<templatedata>\n' +
|
|
|
|
newTemplateData +
|
|
|
|
'\n</templatedata>\n';
|
|
|
|
} else {
|
|
|
|
// If we are not in a subpage, add <noinclude> tags
|
|
|
|
finalOutput = fullWikitext + '\n<noinclude>\n<templatedata>\n' +
|
|
|
|
newTemplateData +
|
|
|
|
'\n</templatedata>\n</noinclude>\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return finalOutput;
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
/**
|
|
|
|
* Initialize UI
|
|
|
|
* @param {jQuery} $container The container to attach UI buttons to
|
|
|
|
* @param {jQuery} $editorTextbox The editor textbox to take the
|
|
|
|
* current wikitext from.
|
|
|
|
*/
|
|
|
|
init: function ( $container, $editorTextbox, userConfig ) {
|
2015-02-18 22:55:17 +00:00
|
|
|
var editHelpButtonWidget, relatedPage,
|
2014-10-11 05:22:21 +00:00
|
|
|
config = userConfig;
|
|
|
|
|
|
|
|
$textbox = $editorTextbox;
|
|
|
|
|
2015-02-18 22:55:17 +00:00
|
|
|
pageName = config.pageName;
|
2014-10-11 05:22:21 +00:00
|
|
|
parentPage = config.parentPage;
|
|
|
|
isPageSubLevel = !!config.isPageSubLevel;
|
2015-02-18 22:55:17 +00:00
|
|
|
isDocPage = !!config.isDocPage;
|
2014-10-11 05:22:21 +00:00
|
|
|
|
|
|
|
editOpenDialogButton = new OO.ui.ButtonWidget( {
|
|
|
|
label: mw.msg( 'templatedata-editbutton' )
|
|
|
|
} );
|
|
|
|
|
|
|
|
editNoticeLabel = new OO.ui.LabelWidget( {
|
|
|
|
classes: [ 'tdg-editscreen-error-msg' ]
|
|
|
|
} )
|
|
|
|
.toggle( false );
|
|
|
|
|
|
|
|
editHelpButtonWidget = new OO.ui.ButtonWidget( {
|
|
|
|
label: mw.msg( 'templatedata-helplink' ),
|
|
|
|
classes: [ 'tdg-editscreen-main-helplink' ],
|
2015-09-10 21:40:22 +00:00
|
|
|
href: mw.msg( 'templatedata-helplink-target' ),
|
2014-10-11 05:22:21 +00:00
|
|
|
target: '_blank',
|
|
|
|
framed: false
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Dialog
|
|
|
|
windowManager = new OO.ui.WindowManager();
|
2015-02-17 23:56:44 +00:00
|
|
|
tdgDialog = new mw.TemplateData.Dialog( config );
|
2015-03-07 00:17:21 +00:00
|
|
|
messageDialog = new OO.ui.MessageDialog();
|
|
|
|
windowManager.addWindows( [ tdgDialog, messageDialog ] );
|
|
|
|
|
|
|
|
sourceHandler = new mw.TemplateData.SourceHandler( {
|
|
|
|
fullPageName: pageName,
|
|
|
|
parentPage: parentPage,
|
|
|
|
isPageSubLevel: isPageSubLevel
|
|
|
|
} );
|
2014-10-11 05:22:21 +00:00
|
|
|
|
2015-02-18 22:55:17 +00:00
|
|
|
// Check if there's already a templatedata in a related page
|
2015-03-07 00:17:21 +00:00
|
|
|
// TODO: Hard-coding 'doc' is dangerous for i18n. We need to find
|
|
|
|
// a better way to define 'related' pages for a template.
|
2015-02-18 22:55:17 +00:00
|
|
|
relatedPage = isDocPage ? parentPage : pageName + '/doc';
|
2015-03-07 00:17:21 +00:00
|
|
|
sourceHandler.getApi( relatedPage )
|
2015-02-18 22:55:17 +00:00
|
|
|
.then( function ( result ) {
|
2015-02-19 21:20:55 +00:00
|
|
|
var msg, matches, content,
|
|
|
|
response = result.query.pages[ result.query.pageids[0] ];
|
|
|
|
// HACK: When checking whether a related page (parent for /doc page or
|
|
|
|
// vice versa) already has a templatedata string, we shouldn't
|
|
|
|
// ask for the 'templatedata' action but rather the actual content
|
|
|
|
// of the related page, otherwise we get embedded templatedata and
|
|
|
|
// wrong information is presented.
|
|
|
|
if ( response.missing === undefined ) {
|
|
|
|
content = response.revisions[0]['*'];
|
|
|
|
matches = content.match( /<templatedata>/i );
|
|
|
|
// There's a templatedata string
|
|
|
|
if ( matches ) {
|
|
|
|
// HACK: Setting a link in the messages doesn't work. The bug report offers
|
|
|
|
// a somewhat hacky work around that includes setting a separate message
|
|
|
|
// to be parsed.
|
|
|
|
// https://phabricator.wikimedia.org/T49395#490610
|
|
|
|
msg = mw.message( 'templatedata-exists-on-related-page', relatedPage ).plain();
|
|
|
|
mw.messages.set( { 'templatedata-string-exists-hack-message': msg } );
|
|
|
|
msg = mw.message( 'templatedata-string-exists-hack-message' ).parse();
|
|
|
|
|
|
|
|
editArea.setNoticeMessage( msg, 'error', true );
|
|
|
|
}
|
2015-02-18 22:55:17 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2014-10-11 05:22:21 +00:00
|
|
|
// Prepend to container
|
|
|
|
$container
|
|
|
|
.prepend(
|
|
|
|
$( '<div>' )
|
|
|
|
.addClass( 'tdg-editscreen-main' )
|
|
|
|
.append(
|
|
|
|
editOpenDialogButton.$element,
|
|
|
|
editHelpButtonWidget.$element,
|
|
|
|
editNoticeLabel.$element
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$( 'body' )
|
|
|
|
.append( windowManager.$element );
|
|
|
|
|
|
|
|
// UI events
|
|
|
|
editOpenDialogButton.connect( this, { click: onEditOpenDialogButton } );
|
|
|
|
|
|
|
|
tdgDialog.connect( this, {
|
|
|
|
apply: onDialogApply
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}() );
|
2015-02-17 23:56:44 +00:00
|
|
|
}() );
|