Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
// <nowiki>
/*
* Draftify
*
* This gadget allows you to move a user page to a different location (usually
* the Draft namespace), notify the user, and optionally soft-block them.
*
* To install the script, add the following to your personal .js page:
*
* importScript( 'User:WikiDwarf/gadgets/Draftify.js' ); // Linkback: [[User:WikiDwarf/gadgets/Draftify.js]]
*
* Original author: Mr. Stradivarius
* Adapted for Bharatpedia
* Licence: MIT
*
* The MIT License (MIT)
*
* Copyright (c) 2015 Mr. Stradivarius
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
mw.loader.using( [
'mediawiki.api',
'mediawiki.jqueryMsg',
'mediawiki.Title',
'mediawiki.util',
'oojs-ui'
], function () {
"use strict";
var config, ApiManager, Dialog;
/**************************************************************************
* MediaWiki config and exit check
**************************************************************************/
config = {};
config.mw = mw.config.get( [
'wgTitle',
'wgNamespaceNumber',
'wgArticleId',
'wgFormattedNamespaces',
'wgPageContentModel',
'wgPageName',
'wgUserName',
'wgUserGroups',
'wgRelevantUserName',
] );
if (
config.mw.wgNamespaceNumber !== 2 && config.mw.wgNamespaceNumber !== 3 ||
config.mw.wgArticleId === 0 ||
!config.mw.wgRelevantUserName ||
config.mw.wgUserName === config.mw.wgRelevantUserName ||
config.mw.wgPageContentModel !== 'wikitext' ||
mw.util.getParamValue( 'redirect', window.location.href ) === 'no'
) {
return;
}
/**************************************************************************
* Messages
*
* Localised for Bharatpedia (BP: shortcuts and local templates).
**************************************************************************/
config.defaultMessages = {
'dfy-portlet-label': 'Draftify',
'dfy-portlet-tooltip': 'Move this draft and notify the user',
'dfy-draft-prefix': 'Draft:',
'dfy-move-summary': 'the [[BP:DRAFTS|Draft namespace]] is the preferred location for [[BP:AFC|Articles for Creation]] submissions',
'dfy-tag-template': '{{subst:AFC draft|1=$1}}',
'dfy-tag-summary': 'Tag page as draft belonging to [[:User:$1]]',
'dfy-move-talk-summary': 'follow draft moved to the [[BP:DRAFTS|Draft namespace]]',
'dfy-softblock-template': '{{subst:uw-softerblock|sig=yes}}',
'dfy-softblock-summary': '{{uw-softerblock}} <!-- Promotional username, soft block -->',
'dfy-notify-template': '{{subst:uw-draftmoved|1=$1|from=$2}} ~~~~',
'dfy-suggestrename-template': '{{subst:uw-username|1=it gives the impression that your account represents a group, organization or website}} ~~~~',
'dfy-notify-summary-moveonly': 'Your draft page has been moved',
'dfy-notify-summary-suggestrename': 'Your draft page has been moved, and you may need to change your [[BP:U|username]]',
'dfy-notify-summary-softblock': 'Your draft page has been moved, and you have been indefinitely blocked from editing because your [[BP:U|username]] gives the impression that the account represents a group, organization or website',
'dfy-summary-suffix': '([[BP:DFY|DFY]])',
'dfy-talk-heading': '$1 $2',
'dfy-title-input-label': 'New draft name',
'dfy-redirect-checkbox-label': 'Leave a redirect behind',
'dfy-notify-checkbox-label': 'Notify the user of the page move',
'dfy-move-talk-checkbox-label': 'Move the draft talk page',
'dfy-suggestrename-checkbox-label': 'Suggest that the user change their username',
'dfy-softblock-checkbox-label': 'Soft-block the user and leave a block notice',
'dfy-watch-user-talk-checkbox-label': 'Watch user talk page',
'dfy-watch-draft-checkbox-label': 'Watch source page and target page',
'dfy-move-progress-label': 'Moving draft...',
'dfy-move-talk-progress-label': 'Moving draft talk page...',
'dfy-tag-progress-label': 'Tagging draft...',
'dfy-softblock-progress-label': 'Soft-blocking $1...',
'dfy-notify-progress-label': 'Notifying $1...',
'dfy-open-talk-progress-label': 'Opening user talk...',
'dfy-progress-success': 'Done.',
'dfy-progress-failed': 'Error: $1',
'dfy-progress-unknown-error': 'An unknown error occurred.',
'dfy-nonexistent-page-error': 'The page "$1" does not exist.',
};
config.months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
/**************************************************************************
* Config
**************************************************************************/
config.rawPrefs = typeof window.Draftify === 'object' ? window.Draftify : {};
config.configurableMessages = {
movesummary: 'dfy-move-summary',
tagtemplate: 'dfy-tag-template',
tagsummary: 'dfy-tag-summary',
movetalksummary: 'dfy-move-talk-summary',
notifytemplate: 'dfy-notify-template',
notifysummarymoveonly: 'dfy-notify-summary-moveonly',
notifysummarysuggestrename: 'dfy-notify-summary-suggestrename',
notifysummarysoftblock: 'dfy-notify-summary-softblock',
softblocktemplate: 'dfy-softblock-template',
softblocksummary: 'dfy-softblock-summary',
suggestrenametemplate: 'dfy-suggestrename-template'
};
config.messages = {};
$.each( config.defaultMessages, function ( key, value ) {
config.messages[ key ] = value;
} );
$.each( config.configurableMessages, function ( prefKey, messageKey ) {
if ( typeof config.rawPrefs[ prefKey ] === 'string' ) {
config.messages[ messageKey ] = config.rawPrefs[ prefKey ];
}
} );
mw.messages.set( config.messages );
config.defaults = {
redirect: false,
movetalk: true,
notify: true,
softblock: false,
suggestrename: false,
watchusertalk: true,
watchdraft: true,
menulocation: 'p-cactions',
menuposition: null
};
config.aliases = {
watchdraft: [ "watch" ]
};
config.prefs = {};
$.each( config.defaults, function ( key, value ) {
if ( config.rawPrefs[ key ] !== undefined ) {
config.prefs[ key ] = config.rawPrefs[ key ];
} else if ( config.aliases[ key ] !== undefined ) {
$.each( config.aliases[ key ], function ( index, alias ) {
if ( config.rawPrefs[ alias ] !== undefined ) {
config.prefs[ key ] = config.rawPrefs[ alias ];
return false;
}
} );
}
if ( config.prefs[ key ] === undefined ) {
config.prefs[ key ] = value;
}
} );
/**************************************************************************
* ApiManager class
**************************************************************************/
ApiManager = function () {
this.api = new mw.Api();
this.currentSubpage = config.mw.wgTitle.replace( /^.*\//, '' );
this.currentDraftTitle = new mw.Title(
config.mw.wgTitle,
config.mw.wgNamespaceNumber
);
this.targetDraftTitle = null;
this.userTalkTitle = new mw.Title( this.getTargetUser(), 3 );
};
OO.initClass( ApiManager );
ApiManager.static.titleValidationRegex = /^[^|<>{}\[\]#]+$/;
ApiManager.prototype.getPreference = function ( key ) {
return config.prefs[ key ];
};
ApiManager.prototype.setRights = function () {
var apiManager = this;
return mw.user.getRights().then(
function ( rights ) {
apiManager.rights = {
suppressredirect: $.inArray( 'suppressredirect', rights ) !== -1,
softblock: $.inArray( 'block', rights ) !== -1
};
},
function () {
apiManager.rights = {};
}
);
};
ApiManager.prototype.currentUserCan = function ( action ) {
var right = this.rights[ action ];
if ( right === undefined ) {
return true;
} else {
return right;
}
};
ApiManager.prototype.getCurrentSubpage = function () {
return this.currentSubpage;
};
ApiManager.prototype.getTargetUser = function () {
return config.mw.wgRelevantUserName;
};
ApiManager.prototype.getTitleValidationRegex = function () {
return this.constructor.static.titleValidationRegex;
};
ApiManager.prototype.getCurrentDraftTitle = function () {
return this.currentDraftTitle;
};
ApiManager.prototype.getCurrentDraftTalkTitle = function () {
if ( this.currentDraftTitle ) {
return this.currentDraftTitle.getTalkPage();
} else {
return null;
}
};
ApiManager.prototype.talkTitleExists = function () {
if ( this.getCurrentDraftTitle().isTalkPage() ) {
return $.Deferred().resolve( false );
}
var talkTitle = this.getCurrentDraftTalkTitle();
if ( !talkTitle ) {
return $.Deferred().resolve( false );
}
return this.api.get( {
action: 'query',
format: 'json',
titles: talkTitle.getPrefixedText()
} ).then( function ( response ) {
var pageObj = Object.values(response.query.pages)[0];
var missing = 'missing' in pageObj;
return !missing;
} );
};
ApiManager.prototype.getTargetDraftTitle = function () {
return this.targetDraftTitle;
};
ApiManager.prototype.setTargetDraftTitle = function ( titleObj ) {
this.targetDraftTitle = titleObj;
};
ApiManager.prototype.getTargetDraftTalkTitle = function () {
if ( this.targetDraftTitle ) {
return this.targetDraftTitle.getTalkPage();
} else {
return null;
}
};
ApiManager.prototype.addPortletLink = function () {
var label = mw.message( 'dfy-portlet-label' ).plain();
var tooltip = mw.message( 'dfy-portlet-tooltip' ).plain();
var portlet = mw.util.addPortletLink(
this.getPreference( 'menulocation' ),
'#',
label,
'ca-draftify',
tooltip,
null,
this.getPreference( 'menuposition' )
);
// BharatpediaNeo hides #p-cactions. Surface the same action in the
// visible title More menu and as a title-bar button.
this.addBharatpediaNeoLink( label, tooltip );
return portlet;
};
ApiManager.prototype.addBharatpediaNeoLink = function ( label, tooltip ) {
var actions = document.querySelector( '.bpneo-title-actions' );
var menu = document.querySelector( '.bpneo-action-menu' );
var menuBtn = document.querySelector( '.bpneo-menu-btn' );
if ( !actions && !menu ) {
return;
}
if ( document.getElementById( 'bpneo-ca-draftify' ) ) {
return;
}
if ( actions && menuBtn && !actions.querySelector( '.bpneo-draftify-btn' ) ) {
var btn = document.createElement( 'button' );
btn.type = 'button';
btn.id = 'bpneo-ca-draftify';
btn.className = 'bpneo-title-btn bpneo-draftify-btn';
btn.title = tooltip;
btn.setAttribute( 'aria-label', label );
btn.innerHTML = '<i class="ri-draft-line"></i>';
menuBtn.parentNode.insertBefore( btn, menuBtn );
}
if ( menu && !menu.querySelector( '.bpneo-draftify-menu' ) ) {
var row = document.createElement( 'a' );
row.href = '#';
row.className = 'bpneo-draftify-menu';
row.title = tooltip;
row.innerHTML = '<i class="ri-draft-line"></i><span></span>';
row.querySelector( 'span' ).textContent = label;
menu.insertBefore( row, menu.firstChild );
}
};
ApiManager.prototype.getNotificationDate = function () {
var date, month;
if ( !this.notificationDate ) {
date = new Date();
month = config.months[ date.getMonth() ];
this.notificationDate = mw.message(
'dfy-talk-heading',
month,
date.getFullYear()
).text();
}
return this.notificationDate;
};
ApiManager.prototype.getUserTalkTitle = function () {
return this.userTalkTitle;
};
ApiManager.prototype.openTalkPage = function () {
window.open(
this.getUserTalkTitle().getUrl() +
'#' +
mw.util.wikiUrlencode( this.getNotificationDate() ),
'_top'
);
};
ApiManager.prototype.getPageContent = function ( options ) {
options = options || {};
var self = this;
return $.Deferred( function ( deferred ) {
self.api.get( {
format: 'json',
action: 'query',
prop: 'revisions',
rvprop: 'content',
indexpageids: '',
titles: options.title,
redirects: ''
} ).then( function ( obj ) {
var newTitle, content,
pageId = obj.query.pageids[ 0 ];
if ( obj.query.redirects ) {
newTitle = obj.query.redirects[ 0 ].to;
} else {
newTitle = options.title;
}
if ( pageId === '-1' ) {
if ( options.allowNonexistent ) {
content = '';
} else {
return deferred.reject( 'dfy-nonexistent-page-error', { 'error': {
'id': 'dfy-nonexistent-page-error',
'info': mw.message(
'dfy-nonexistent-page-error',
newTitle
).text()
} } );
}
} else {
content = obj.query.pages[ pageId ].revisions[ 0 ][ '*' ];
}
return deferred.resolve( {
title: newTitle,
content: content
} );
} );
} ).promise();
};
ApiManager.prototype.editPage = function ( options ) {
options = options || {};
return this.api.postWithEditToken( {
format: 'json',
action: 'edit',
title: options.title,
summary: options.editSummary + ' ' + mw.message( 'dfy-summary-suffix' ).plain(),
notminor: '',
text: options.content,
watchlist: options.watch ? 'watch' : 'unwatch'
} );
};
ApiManager.prototype.moveDraft = function ( options ) {
options = options || {};
var self = this,
targetTitle = this.getTargetDraftTitle();
if ( !targetTitle ) {
throw new Error( 'moveDraft was called but no target title object was available' );
}
return self.getPageContent( {
title: self.currentDraftTitle.getPrefixedText(),
allowNonexistent: false
} ).then( function ( contentObj ) {
var apiArgs = {
action: 'move',
format: 'json',
from: self.getCurrentDraftTitle().getPrefixedText(),
to: targetTitle.getPrefixedText(),
reason: mw.message( 'dfy-move-summary' ).plain() +
' ' +
mw.message( 'dfy-summary-suffix' ).plain(),
watchlist: options.watchdraft ? 'watch' : 'unwatch'
};
if ( !options.redirect ) {
apiArgs.noredirect = 1;
}
return self.api.postWithEditToken( apiArgs ).then( function () {
return contentObj;
} );
} );
};
ApiManager.prototype.tagDraft = function ( options, contentObj ) {
if ( !contentObj ) {
throw new Error( 'tagDraft was called without contentObj' );
}
var content = contentObj.content,
tag = mw.message( 'dfy-tag-template', this.getTargetUser() ).plain();
if ( /^\s*{{/.test( content ) ) {
content = tag + '\n' + content;
} else {
content = tag + '\n\n' + content;
}
return this.editPage( {
title: this.getTargetDraftTitle().getPrefixedText(),
editSummary: mw.message( 'dfy-tag-summary', this.getTargetUser() ).plain(),
content: content,
watch: options.watchdraft
} );
};
ApiManager.prototype.moveDraftTalkPage = function ( options ) {
options = options || {};
var self = this,
targetTalkTitle = this.getTargetDraftTalkTitle();
if ( !targetTalkTitle ) {
throw new Error( 'moveDraftTalkPage was called but no target title object was available' );
}
var currentTalkTitle = self.getCurrentDraftTalkTitle();
if ( !currentTalkTitle ) {
return $.Deferred().resolve();
}
var apiArgs = {
action: 'move',
format: 'json',
from: currentTalkTitle.getPrefixedText(),
to: targetTalkTitle.getPrefixedText(),
reason: mw.message( 'dfy-move-talk-summary' ).plain() +
' ' +
mw.message( 'dfy-summary-suffix' ).plain(),
watchlist: options.watchdraft ? 'watch' : 'unwatch'
};
if ( !options.redirect ) {
apiArgs.noredirect = 1;
}
return self.api.postWithEditToken( apiArgs );
};
ApiManager.prototype.softBlockUser = function ( options ) {
return this.api.postWithEditToken( {
format: 'json',
action: 'block',
user: this.getTargetUser(),
expiry: 'infinite',
reason: mw.message( 'dfy-softblock-summary' ).plain(),
allowusertalk: 1
} );
};
ApiManager.prototype.notifyUser = function ( options ) {
options = options || {};
var summary,
self = this;
if ( options.softblock ) {
summary = mw.message( 'dfy-notify-summary-softblock' ).plain();
} else if ( options.suggestrename ) {
summary = mw.message( 'dfy-notify-summary-suggestrename' ).plain();
} else {
summary = mw.message( 'dfy-notify-summary-moveonly' ).plain();
}
return self.getPageContent( {
title: self.getUserTalkTitle().getPrefixedText(),
allowNonexistent: true
} ).then( function ( contentObj ) {
var lastHeading, headings,
content = contentObj.content,
notificationDate = self.getNotificationDate();
if ( /\S/.test( content ) ) {
content += '\n\n';
}
headings = content.match( /^==[^=].*==[ \t]*$/gm );
if ( headings ) {
lastHeading = headings[ headings.length - 1 ].slice( 2, -2 ).trim();
}
if ( !lastHeading || lastHeading !== notificationDate ) {
content += '== ' + notificationDate + ' ==\n\n';
}
content += mw.message(
'dfy-notify-template',
self.getTargetDraftTitle() ? self.getTargetDraftTitle().getPrefixedText() : '',
self.getCurrentDraftTitle().getPrefixedText()
).plain();
if ( options.softblock ) {
content += '\n\n' + mw.message( 'dfy-softblock-template' ).plain();
} else if ( options.suggestrename ) {
content += '\n\n' + mw.message( 'dfy-suggestrename-template' ).plain();
}
return self.editPage( {
title: contentObj.title,
editSummary: summary,
content: content,
watch: options.watchusertalk
} );
} );
};
ApiManager.prototype.submit = function ( options ) {
var i, len,
self = this,
actions = [],
promises = {},
whenArgs = [];
function makeDoneHandler( i ) {
return function ( obj ) {
return self[ actions[ i ].method ]( options, obj );
};
}
try {
this.setTargetDraftTitle( new mw.Title(
mw.message( 'dfy-draft-prefix' ).text() + options.title
) );
actions.push( { action: 'move', method: 'moveDraft' } );
actions.push( { action: 'tag', method: 'tagDraft' } );
if ( options.movetalk ) {
actions.push( { action: 'movetalk', method: 'moveDraftTalkPage' } );
}
if ( options.softblock ) {
actions.push( { action: 'softblock', method: 'softBlockUser' } );
}
if ( options.notify ) {
actions.push( { action: 'notify', method: 'notifyUser' } );
}
promises[ actions[ 0 ].action ] = self[ actions[ 0 ].method ]( options );
for ( i = 1, len = actions.length; i < len; i++ ) {
promises[ actions[ i ].action ] = promises[ actions[ i - 1 ].action ].then(
makeDoneHandler( i )
);
}
} catch ( e ) {
promises.move = $.Deferred().reject( 'dfy-submit-error', { 'error': {
'id': 'dfy-submit-error',
'info': e.message || mw.message( 'dfy-progress-unknown-error' ).text()
} } ).promise();
}
$.each( promises, function ( key, promise ) {
whenArgs.push( promise );
} );
promises.all = $.when.apply( this, whenArgs );
return promises;
};
/**************************************************************************
* Dialog class
**************************************************************************/
Dialog = function ( options ) {
options = options || {};
this.apiManager = new ApiManager();
this.isLoaded = false;
this.hasBeenSubmitted = false;
Dialog.super.call( this, options );
};
OO.inheritClass( Dialog, OO.ui.ProcessDialog );
Dialog.static.name = 'DraftifyDialog';
Dialog.static.title = 'Draftify';
Dialog.static.actions = [
{ action: 'submit', label: 'Submit', flags: [ 'primary', 'constructive' ] },
{ label: 'Cancel', flags: 'safe' }
];
Dialog.prototype.getApiManager = function () {
return this.apiManager;
};
Dialog.prototype.getBodyHeight = function () {
return 300;
};
Dialog.prototype.initialize = function () {
var apiManager = this.getApiManager();
Dialog.super.prototype.initialize.apply( this, arguments );
this.editPanel = new OO.ui.PanelLayout( {
expanded: false
} );
this.editFieldset = new OO.ui.FieldsetLayout( {
classes: [ 'container' ]
} );
this.editPanel.$element.append( this.editFieldset.$element );
this.titleInput = new OO.ui.TextInputWidget( {
validate: apiManager.getTitleValidationRegex(),
value: apiManager.getCurrentSubpage()
} );
this.redirectCheckbox = new OO.ui.CheckboxInputWidget();
this.redirectCheckbox.setSelected( true );
this.redirectCheckbox.setDisabled( true );
this.notifyCheckbox = new OO.ui.CheckboxInputWidget( {
selected: apiManager.getPreference( 'notify' )
} );
this.moveTalkPageCheckbox = new OO.ui.CheckboxInputWidget( {
selected: false,
disabled: true
} );
this.suggestrenameCheckbox = new OO.ui.CheckboxInputWidget( {
selected: apiManager.getPreference( 'suggestrename' )
} );
this.softblockCheckbox = new OO.ui.CheckboxInputWidget();
this.softblockCheckbox.setDisabled( true );
this.watchUserTalkCheckbox = new OO.ui.CheckboxInputWidget( {
selected: apiManager.getPreference( 'watchusertalk' )
} );
this.watchDraftCheckbox = new OO.ui.CheckboxInputWidget( {
selected: apiManager.getPreference( 'watchdraft' )
} );
this.editFieldset.addItems( [
new OO.ui.FieldLayout( this.titleInput, {
label: mw.message( 'dfy-title-input-label' ).plain()
} ),
new OO.ui.FieldLayout( this.redirectCheckbox, {
label: mw.message( 'dfy-redirect-checkbox-label' ).plain(),
align: 'inline'
} ),
new OO.ui.FieldLayout( this.notifyCheckbox, {
label: mw.message( 'dfy-notify-checkbox-label' ).plain(),
align: 'inline'
} ),
new OO.ui.FieldLayout( this.moveTalkPageCheckbox, {
label: mw.message( 'dfy-move-talk-checkbox-label' ).plain(),
align: 'inline'
} ),
new OO.ui.FieldLayout( this.suggestrenameCheckbox, {
label: mw.message( 'dfy-suggestrename-checkbox-label' ).plain(),
align: 'inline'
} ),
new OO.ui.FieldLayout( this.softblockCheckbox, {
label: mw.message( 'dfy-softblock-checkbox-label' ).plain(),
align: 'inline'
} ),
new OO.ui.FieldLayout( this.watchUserTalkCheckbox, {
label: mw.message( 'dfy-watch-user-talk-checkbox-label' ).plain(),
align: 'inline'
} ),
new OO.ui.FieldLayout( this.watchDraftCheckbox, {
label: mw.message( 'dfy-watch-draft-checkbox-label' ).plain(),
align: 'inline'
} )
] );
this.submitPanel = new OO.ui.PanelLayout( {
$: this.$,
expanded: false
} );
this.submitFieldset = new OO.ui.FieldsetLayout( {
classes: [ 'container' ]
} );
this.submitPanel.$element.append( this.submitFieldset.$element );
this.moveProgressLabel = new OO.ui.LabelWidget();
this.moveProgressField = new OO.ui.FieldLayout( this.moveProgressLabel );
this.moveTalkProgressLabel = new OO.ui.LabelWidget();
this.moveTalkProgressField = new OO.ui.FieldLayout( this.moveTalkProgressLabel );
this.tagProgressLabel = new OO.ui.LabelWidget();
this.tagProgressField = new OO.ui.FieldLayout( this.tagProgressLabel );
this.softblockProgressLabel = new OO.ui.LabelWidget();
this.softblockProgressField = new OO.ui.FieldLayout( this.softblockProgressLabel );
this.notifyProgressLabel = new OO.ui.LabelWidget();
this.notifyProgressField = new OO.ui.FieldLayout( this.notifyProgressLabel );
this.openTalkProgressField = new OO.ui.FieldLayout( new OO.ui.LabelWidget() );
this.stackLayout= new OO.ui.StackLayout( {
items: [ this.editPanel, this.submitPanel ],
padded: true
} );
this.$body.append( this.stackLayout.$element );
};
Dialog.prototype.getReadyProcess = function ( data ) {
data = data || {};
var dialog = this;
return Dialog.super.prototype.getReadyProcess.call( this, data )
.next( function () {
if ( !dialog.isLoaded ) {
dialog.pushPending();
dialog.actions.setAbilities( { submit: false } );
dialog.getApiManager().setRights()
.done( function () {
dialog.onLoad();
dialog.actions.setAbilities( { submit: true } );
dialog.popPending();
} )
.fail( function () {
dialog.popPending();
alert( 'There was a problem fetching your user rights from the API.' );
} );
}
if ( dialog.hasBeenSubmitted ) {
dialog.actions.setAbilities( { submit: false } );
} else {
this.titleInput.focus();
var val = this.titleInput.getValue();
this.titleInput.setValue( '' );
this.titleInput.setValue( val );
}
}, this );
};
Dialog.prototype.onLoad = function () {
var apiManager = this.getApiManager();
var self = this;
apiManager.talkTitleExists()
.done( function ( exists ) {
if ( exists ) {
self.moveTalkPageCheckbox.setSelected(
apiManager.getPreference( 'movetalk' )
);
self.moveTalkPageCheckbox.setDisabled( false );
} else {
self.moveTalkPageCheckbox.setSelected( false );
self.moveTalkPageCheckbox.setDisabled( true );
}
} )
.fail( function () {
self.moveTalkPageCheckbox.setSelected( false );
self.moveTalkPageCheckbox.setDisabled( true );
} )
.always( function () {
if ( apiManager.currentUserCan( 'suppressredirect' ) ) {
self.redirectCheckbox.setSelected(
apiManager.getPreference( 'redirect' )
);
self.redirectCheckbox.setDisabled( false );
} else {
self.redirectCheckbox.setSelected( true );
self.redirectCheckbox.setDisabled( true );
}
if ( apiManager.currentUserCan( 'softblock' ) ) {
self.softblockCheckbox.setSelected(
apiManager.getPreference( 'softblock' )
);
self.redirectCheckbox.setDisabled( false );
} else {
self.softblockCheckbox.setSelected( false );
self.softblockCheckbox.setDisabled( true );
}
self.onSuggestrenameChange();
if ( apiManager.currentUserCan( 'softblock' ) ) {
self.onSoftblockChange();
}
self.onNotifyChange();
self.onTitleChange();
self.softblockCheckbox.on( 'change', self.onSoftblockChange, null, self );
self.suggestrenameCheckbox.on( 'change', self.onSuggestrenameChange, null, self );
self.notifyCheckbox.on( 'change', self.onNotifyChange, null, self );
self.titleInput.on( 'change', self.onTitleChange, null, self );
self.isLoaded = true;
} );
};
Dialog.prototype.onSuggestrenameChange = function () {
if ( this.suggestrenameCheckbox.isSelected() ) {
this.notifyCheckbox.setSelected( true );
this.notifyCheckbox.setDisabled( true );
this.softblockCheckbox.setSelected( false );
this.softblockCheckbox.setDisabled( true );
} else {
if ( this.getApiManager().currentUserCan( 'softblock' ) ) {
this.softblockCheckbox.setDisabled( false );
}
this.notifyCheckbox.setDisabled( false );
}
this.onNotifyChange();
};
Dialog.prototype.onSoftblockChange = function () {
if ( this.softblockCheckbox.isSelected() ) {
this.notifyCheckbox.setSelected( true );
this.notifyCheckbox.setDisabled( true );
this.suggestrenameCheckbox.setSelected( false );
this.suggestrenameCheckbox.setDisabled( true );
} else {
this.suggestrenameCheckbox.setDisabled( false );
this.notifyCheckbox.setDisabled( false );
}
this.onNotifyChange();
};
Dialog.prototype.onNotifyChange = function () {
if ( this.notifyCheckbox.isSelected() ) {
this.watchUserTalkCheckbox.setDisabled( false );
} else {
this.watchUserTalkCheckbox.setSelected( false );
this.watchUserTalkCheckbox.setDisabled( true );
}
};
Dialog.prototype.onTitleChange = function () {
var self = this,
promise = this.titleInput.getValidity();
promise.done( function () {
self.actions.setAbilities( { submit: true } );
} );
promise.fail( function () {
self.actions.setAbilities( { submit: false } );
} );
};
Dialog.prototype.onSubmit = function () {
var options, promises,
self = this,
apiManager = this.getApiManager(),
targetUser = apiManager.getTargetUser();
self.hasBeenSubmitted = true;
self.actions.setAbilities( { submit: false } );
options = {
title: this.titleInput.getValue(),
redirect: this.redirectCheckbox.isSelected(),
movetalk: this.moveTalkPageCheckbox.isSelected(),
notify: this.notifyCheckbox.isSelected(),
suggestrename: this.suggestrenameCheckbox.isSelected(),
softblock: this.softblockCheckbox.isSelected(),
watchusertalk: this.watchUserTalkCheckbox.isSelected(),
watchdraft: this.watchDraftCheckbox.isSelected()
};
promises = apiManager.submit( options );
$.each( promises, function ( key, value ) {
if ( value ) {
self.pushPending();
}
} );
function addField( field, label, promise ) {
if ( !promise ) {
return;
}
field
.setLabel( label )
.setData( promise )
.toggle();
self.submitFieldset.addItems( [ field ] );
}
addField(
self.moveProgressField,
mw.message( 'dfy-move-progress-label' ).text(),
promises.move
);
addField(
self.moveTalkProgressField,
mw.message( 'dfy-move-talk-progress-label' ).text(),
promises.movetalk
);
addField(
self.tagProgressField,
mw.message( 'dfy-tag-progress-label' ).text(),
promises.tag
);
addField(
self.softblockProgressField,
mw.message( 'dfy-softblock-progress-label', targetUser ).text(),
promises.softblock
);
addField(
self.notifyProgressField,
mw.message( 'dfy-notify-progress-label', targetUser ).text(),
promises.notify
);
self.openTalkProgressField
.setLabel( mw.message( 'dfy-open-talk-progress-label' ).text() )
.toggle();
self.submitFieldset.addItems( [ self.openTalkProgressField ] );
self.moveProgressField.toggle();
$.each( self.submitFieldset.getItems(), function ( i, field ) {
var
promise = field.getData(),
label = field.getField();
if ( !promise ) {
return;
}
promise
.done( function () {
var nextField;
label.setLabel( $( '<span>' )
.addClass( 'draftify-success' )
.text( mw.message( 'dfy-progress-success' ).text() )
);
nextField = self.submitFieldset.getItems()[ i + 1 ];
if ( nextField ) {
nextField.toggle();
}
} )
.fail( function ( id, obj ) {
if ( obj && obj.error && obj.error.info ) {
label.setLabel( $( '<span>' )
.addClass( 'draftify-error' )
.text( mw.message(
'dfy-progress-failed',
obj.error.info
).text() )
);
} else {
label.setLabel(
mw.message( 'dfy-progress-unknown-error' ).text()
);
}
} )
.always( function () {
self.popPending();
} );
} );
promises.all.done( function () {
apiManager.openTalkPage();
} );
promises.all.fail( function () {
self.popPending();
} );
self.stackLayout.setItem( self.submitPanel );
};
Dialog.prototype.getActionProcess = function ( action ) {
return Dialog.super.prototype.getActionProcess.call( this, action )
.next( function () {
if ( action === 'submit' ) {
return this.onSubmit();
} else {
return Dialog.super.prototype.getActionProcess.call( this, action );
}
}, this );
};
/**************************************************************************
* Main
**************************************************************************/
function main() {
mw.util.addCSS(
'.draftify-success { color: #14866d; font-weight: bold; }' +
'.draftify-error { color: #d33; font-weight: bold; }'
);
var portletLink, windowManager,
draftifyDialog = new Dialog( { size: 'medium' } ),
apiManager = draftifyDialog.getApiManager();
windowManager = new OO.ui.WindowManager();
$( 'body' ).append( windowManager.$element );
windowManager.addWindows( [ draftifyDialog ] );
portletLink = apiManager.addPortletLink();
function openDraftify( event ) {
event.preventDefault();
windowManager.openWindow( draftifyDialog );
}
$( portletLink ).click( openDraftify );
$( document ).on( 'click', '.bpneo-draftify-btn, .bpneo-draftify-menu', openDraftify );
}
main();
} );
// </nowiki>