Created page with "// <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:Mr. Stradivarius/gadgets/Draftify.js * Author: Mr. Stradivarius * Licence: MIT * * The MIT License (MIT) * * Copyright (c) 2015 Mr...." |
No edit summary |
||
| (2 intermediate revisions by the same user not shown) | |||
| Line 7: | Line 7: | ||
* | * | ||
* To install the script, add the following to your personal .js page: | * To install the script, add the following to your personal .js page: | ||
* | |||
importScript( 'User:WikiDwarf/gadgets/Draftify.js' ); // Linkback: [[User: | * importScript( 'User:WikiDwarf/gadgets/Draftify.js' ); // Linkback: [[User:WikiDwarf/gadgets/Draftify.js]] | ||
* | |||
* | * Original author: Mr. Stradivarius | ||
* Adapted for Bharatpedia | |||
* Licence: MIT | * Licence: MIT | ||
* | * | ||
| Line 16: | Line 17: | ||
* | * | ||
* Copyright (c) 2015 Mr. Stradivarius | * Copyright (c) 2015 Mr. Stradivarius | ||
* | * | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | * of this software and associated documentation files (the "Software"), to deal | ||
| Line 23: | Line 24: | ||
* copies of the Software, and to permit persons to whom the Software is | * copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | * furnished to do so, subject to the following conditions: | ||
* | * | ||
* The above copyright notice and this permission notice shall be included in | * The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | * all copies or substantial portions of the Software. | ||
* | * | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| Line 35: | Line 36: | ||
* THE SOFTWARE. | * THE SOFTWARE. | ||
*/ | */ | ||
mw.loader.using( [ | mw.loader.using( [ | ||
'mediawiki.api', | 'mediawiki.api', | ||
| Line 44: | Line 44: | ||
], function () { | ], function () { | ||
"use strict"; | "use strict"; | ||
var config, ApiManager, Dialog; | var config, ApiManager, Dialog; | ||
/************************************************************************** | /************************************************************************** | ||
* MediaWiki config and exit check | * MediaWiki config and exit check | ||
**************************************************************************/ | **************************************************************************/ | ||
config = {}; | config = {}; | ||
config.mw = mw.config.get( [ | config.mw = mw.config.get( [ | ||
'wgTitle', | 'wgTitle', | ||
| Line 66: | Line 60: | ||
'wgRelevantUserName', | 'wgRelevantUserName', | ||
] ); | ] ); | ||
if ( | if ( | ||
config.mw.wgNamespaceNumber !== 2 && config.mw.wgNamespaceNumber !== 3 || | config.mw.wgNamespaceNumber !== 2 && config.mw.wgNamespaceNumber !== 3 || | ||
config.mw.wgArticleId === 0 || | config.mw.wgArticleId === 0 || | ||
!config.mw.wgRelevantUserName || | !config.mw.wgRelevantUserName || | ||
config.mw.wgUserName === config.mw.wgRelevantUserName || | config.mw.wgUserName === config.mw.wgRelevantUserName || | ||
config.mw.wgPageContentModel !== 'wikitext' || | config.mw.wgPageContentModel !== 'wikitext' || | ||
mw.util.getParamValue( 'redirect', window.location.href ) === 'no' | mw.util.getParamValue( 'redirect', window.location.href ) === 'no' | ||
) { | ) { | ||
return; | return; | ||
} | } | ||
/************************************************************************** | /************************************************************************** | ||
* Messages | * Messages | ||
* | * | ||
* | * Localised for Bharatpedia (BP: shortcuts and local templates). | ||
**************************************************************************/ | **************************************************************************/ | ||
config.defaultMessages = { | config.defaultMessages = { | ||
'dfy-portlet-label': 'Draftify', | 'dfy-portlet-label': 'Draftify', | ||
'dfy-portlet-tooltip': 'Move this draft and notify the user', | 'dfy-portlet-tooltip': 'Move this draft and notify the user', | ||
'dfy-draft-prefix': 'Draft:', | 'dfy-draft-prefix': 'Draft:', | ||
'dfy-move-summary': 'the [[BP:DRAFTS|Draft namespace]] is the preferred location for [[BP:AFC|Articles for Creation]] submissions', | '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-template': '{{subst:AFC draft|1=$1}}', | ||
'dfy-tag-summary': 'Tag page as draft belonging to [[:User:$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-template': '{{subst:uw-softerblock|sig=yes}}', | ||
'dfy-softblock-summary': '{{uw-softerblock}} <!-- Promotional username, soft block -->', | 'dfy-softblock-summary': '{{uw-softerblock}} <!-- Promotional username, soft block -->', | ||
'dfy-notify-template': '{{subst:uw-draftmoved|1=$1|from=$2}} ~~~~', | '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-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-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-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-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-summary-suffix': '([[BP:DFY|DFY]])', | ||
'dfy-talk-heading': '$1 $2', | 'dfy-talk-heading': '$1 $2', | ||
'dfy-title-input-label': 'New draft name', | 'dfy-title-input-label': 'New draft name', | ||
'dfy-redirect-checkbox-label': 'Leave a redirect behind', | 'dfy-redirect-checkbox-label': 'Leave a redirect behind', | ||
'dfy-notify-checkbox-label': 'Notify the user of the page move', | '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-suggestrename-checkbox-label': 'Suggest that the user change their username', | ||
'dfy-softblock-checkbox-label': 'Soft-block the user and leave a block notice', | '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-user-talk-checkbox-label': 'Watch user talk page', | ||
'dfy-watch-draft-checkbox-label': 'Watch source page and target page', | 'dfy-watch-draft-checkbox-label': 'Watch source page and target page', | ||
'dfy-move-progress-label': 'Moving draft...', | 'dfy-move-progress-label': 'Moving draft...', | ||
'dfy-move-talk-progress-label': 'Moving draft talk page...', | |||
'dfy-tag-progress-label': 'Tagging draft...', | 'dfy-tag-progress-label': 'Tagging draft...', | ||
'dfy-softblock-progress-label': 'Soft-blocking $1...', | 'dfy-softblock-progress-label': 'Soft-blocking $1...', | ||
'dfy-notify-progress-label': 'Notifying $1...', | 'dfy-notify-progress-label': 'Notifying $1...', | ||
'dfy-open-talk-progress-label': 'Opening user talk...', | 'dfy-open-talk-progress-label': 'Opening user talk...', | ||
'dfy-progress-success': 'Done.', | 'dfy-progress-success': 'Done.', | ||
'dfy-progress-failed': 'Error: $1', | 'dfy-progress-failed': 'Error: $1', | ||
'dfy-progress-unknown-error': 'An unknown error occurred.', | 'dfy-progress-unknown-error': 'An unknown error occurred.', | ||
'dfy-nonexistent-page-error': 'The page "$1" does not exist.', | 'dfy-nonexistent-page-error': 'The page "$1" does not exist.', | ||
}; | }; | ||
config.months = [ | config.months = [ | ||
'January', | 'January', | ||
| Line 212: | Line 125: | ||
'December' | 'December' | ||
]; | ]; | ||
/************************************************************************** | /************************************************************************** | ||
* Config | * Config | ||
**************************************************************************/ | **************************************************************************/ | ||
config.rawPrefs = typeof window.Draftify === 'object' ? window.Draftify : {}; | config.rawPrefs = typeof window.Draftify === 'object' ? window.Draftify : {}; | ||
config.configurableMessages = { | config.configurableMessages = { | ||
movesummary: 'dfy-move-summary', | movesummary: 'dfy-move-summary', | ||
tagtemplate: 'dfy-tag-template', | tagtemplate: 'dfy-tag-template', | ||
tagsummary: 'dfy-tag-summary', | tagsummary: 'dfy-tag-summary', | ||
movetalksummary: 'dfy-move-talk-summary', | |||
notifytemplate: 'dfy-notify-template', | notifytemplate: 'dfy-notify-template', | ||
notifysummarymoveonly: 'dfy-notify-summary-moveonly', | notifysummarymoveonly: 'dfy-notify-summary-moveonly', | ||
| Line 235: | Line 142: | ||
suggestrenametemplate: 'dfy-suggestrename-template' | suggestrenametemplate: 'dfy-suggestrename-template' | ||
}; | }; | ||
config.messages = {}; | config.messages = {}; | ||
$.each( config.defaultMessages, function ( key, value ) { | $.each( config.defaultMessages, function ( key, value ) { | ||
| Line 246: | Line 151: | ||
} | } | ||
} ); | } ); | ||
mw.messages.set( config.messages ); | mw.messages.set( config.messages ); | ||
config.defaults = { | config.defaults = { | ||
redirect: false, | redirect: false, | ||
movetalk: true, | |||
notify: true, | notify: true, | ||
softblock: false, | softblock: false, | ||
| Line 262: | Line 163: | ||
menuposition: null | menuposition: null | ||
}; | }; | ||
config.aliases = { | config.aliases = { | ||
watchdraft: [ "watch" ] | watchdraft: [ "watch" ] | ||
} | }; | ||
config.prefs = {}; | config.prefs = {}; | ||
$.each( config.defaults, function ( key, value ) { | $.each( config.defaults, function ( key, value ) { | ||
| Line 277: | Line 174: | ||
if ( config.rawPrefs[ alias ] !== undefined ) { | if ( config.rawPrefs[ alias ] !== undefined ) { | ||
config.prefs[ key ] = config.rawPrefs[ alias ]; | config.prefs[ key ] = config.rawPrefs[ alias ]; | ||
return false; | return false; | ||
} | } | ||
} ); | } ); | ||
| Line 285: | Line 182: | ||
} | } | ||
} ); | } ); | ||
/************************************************************************** | /************************************************************************** | ||
* ApiManager class | * ApiManager class | ||
**************************************************************************/ | **************************************************************************/ | ||
ApiManager = function () { | ApiManager = function () { | ||
this.api = new mw.Api(); | this.api = new mw.Api(); | ||
this.currentSubpage = config.mw.wgTitle.replace( /^.*\//, '' ); | this.currentSubpage = config.mw.wgTitle.replace( /^.*\//, '' ); | ||
| Line 305: | Line 195: | ||
this.userTalkTitle = new mw.Title( this.getTargetUser(), 3 ); | this.userTalkTitle = new mw.Title( this.getTargetUser(), 3 ); | ||
}; | }; | ||
OO.initClass( ApiManager ); | OO.initClass( ApiManager ); | ||
ApiManager.static.titleValidationRegex = /^[^|<>{}\[\]#]+$/; | ApiManager.static.titleValidationRegex = /^[^|<>{}\[\]#]+$/; | ||
ApiManager.prototype.getPreference = function ( key ) { | ApiManager.prototype.getPreference = function ( key ) { | ||
return config.prefs[ key ]; | return config.prefs[ key ]; | ||
}; | }; | ||
ApiManager.prototype.setRights = function () { | ApiManager.prototype.setRights = function () { | ||
var apiManager = this; | var apiManager = this; | ||
return mw.user.getRights().then( | return mw.user.getRights().then( | ||
function ( rights ) { | function ( rights ) { | ||
apiManager.rights = { | apiManager.rights = { | ||
| Line 330: | Line 209: | ||
}; | }; | ||
}, | }, | ||
function () { | function () { | ||
apiManager.rights = {}; | apiManager.rights = {}; | ||
| Line 336: | Line 214: | ||
); | ); | ||
}; | }; | ||
ApiManager.prototype.currentUserCan = function ( action ) { | ApiManager.prototype.currentUserCan = function ( action ) { | ||
var right = this.rights[ action ]; | var right = this.rights[ action ]; | ||
| Line 345: | Line 222: | ||
} | } | ||
}; | }; | ||
ApiManager.prototype.getCurrentSubpage = function () { | ApiManager.prototype.getCurrentSubpage = function () { | ||
return this.currentSubpage; | return this.currentSubpage; | ||
}; | }; | ||
ApiManager.prototype.getTargetUser = function () { | ApiManager.prototype.getTargetUser = function () { | ||
return config.mw.wgRelevantUserName; | return config.mw.wgRelevantUserName; | ||
}; | }; | ||
ApiManager.prototype.getTitleValidationRegex = function () { | ApiManager.prototype.getTitleValidationRegex = function () { | ||
return this.constructor.static.titleValidationRegex; | return this.constructor.static.titleValidationRegex; | ||
}; | }; | ||
ApiManager.prototype.getCurrentDraftTitle = function () { | ApiManager.prototype.getCurrentDraftTitle = function () { | ||
return this.currentDraftTitle; | 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 () { | ApiManager.prototype.getTargetDraftTitle = function () { | ||
return this.targetDraftTitle; | return this.targetDraftTitle; | ||
}; | }; | ||
ApiManager.prototype.setTargetDraftTitle = function ( titleObj ) { | ApiManager.prototype.setTargetDraftTitle = function ( titleObj ) { | ||
this.targetDraftTitle = titleObj; | this.targetDraftTitle = titleObj; | ||
}; | }; | ||
ApiManager.prototype.getTargetDraftTalkTitle = function () { | |||
if ( this.targetDraftTitle ) { | |||
return this.targetDraftTitle.getTalkPage(); | |||
} else { | |||
return null; | |||
} | |||
}; | |||
ApiManager.prototype.addPortletLink = function () { | 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' ), | this.getPreference( 'menulocation' ), | ||
'#', | '#', | ||
label, | |||
'ca-draftify', | 'ca-draftify', | ||
tooltip, | |||
null, | null, | ||
this.getPreference( 'menuposition' ) | 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 () { | ApiManager.prototype.getNotificationDate = function () { | ||
var date, month; | var date, month; | ||
if ( !this.notificationDate ) { | if ( !this.notificationDate ) { | ||
| Line 398: | Line 332: | ||
return this.notificationDate; | return this.notificationDate; | ||
}; | }; | ||
ApiManager.prototype.getUserTalkTitle = function () { | ApiManager.prototype.getUserTalkTitle = function () { | ||
return this.userTalkTitle; | return this.userTalkTitle; | ||
}; | }; | ||
ApiManager.prototype.openTalkPage = function () { | ApiManager.prototype.openTalkPage = function () { | ||
window.open( | window.open( | ||
| Line 411: | Line 343: | ||
); | ); | ||
}; | }; | ||
ApiManager.prototype.getPageContent = function ( options ) { | ApiManager.prototype.getPageContent = function ( options ) { | ||
options = options || {}; | options = options || {}; | ||
| Line 427: | Line 358: | ||
var newTitle, content, | var newTitle, content, | ||
pageId = obj.query.pageids[ 0 ]; | pageId = obj.query.pageids[ 0 ]; | ||
if ( obj.query.redirects ) { | if ( obj.query.redirects ) { | ||
newTitle = obj.query.redirects[ 0 ].to; | newTitle = obj.query.redirects[ 0 ].to; | ||
| Line 434: | Line 363: | ||
newTitle = options.title; | newTitle = options.title; | ||
} | } | ||
if ( pageId === '-1' ) { | if ( pageId === '-1' ) { | ||
if ( options.allowNonexistent ) { | if ( options.allowNonexistent ) { | ||
content = ''; | content = ''; | ||
} else { | } else { | ||
return deferred.reject( 'dfy-nonexistent-page-error', { 'error': { | return deferred.reject( 'dfy-nonexistent-page-error', { 'error': { | ||
'id': 'dfy-nonexistent-page-error', | 'id': 'dfy-nonexistent-page-error', | ||
| Line 453: | Line 378: | ||
content = obj.query.pages[ pageId ].revisions[ 0 ][ '*' ]; | content = obj.query.pages[ pageId ].revisions[ 0 ][ '*' ]; | ||
} | } | ||
return deferred.resolve( { | return deferred.resolve( { | ||
title: newTitle, | title: newTitle, | ||
| Line 461: | Line 385: | ||
} ).promise(); | } ).promise(); | ||
}; | }; | ||
ApiManager.prototype.editPage = function ( options ) { | ApiManager.prototype.editPage = function ( options ) { | ||
options = options || {}; | options = options || {}; | ||
| Line 474: | Line 397: | ||
} ); | } ); | ||
}; | }; | ||
ApiManager.prototype.moveDraft = function ( options ) { | ApiManager.prototype.moveDraft = function ( options ) { | ||
options = options || {}; | options = options || {}; | ||
| Line 482: | Line 404: | ||
throw new Error( 'moveDraft was called but no target title object was available' ); | throw new Error( 'moveDraft was called but no target title object was available' ); | ||
} | } | ||
return self.getPageContent( { | return self.getPageContent( { | ||
title: self.currentDraftTitle.getPrefixedText(), | title: self.currentDraftTitle.getPrefixedText(), | ||
| Line 507: | Line 426: | ||
} ); | } ); | ||
}; | }; | ||
ApiManager.prototype.tagDraft = function ( options, contentObj ) { | ApiManager.prototype.tagDraft = function ( options, contentObj ) { | ||
if ( !contentObj ) { | if ( !contentObj ) { | ||
| Line 514: | Line 432: | ||
var content = contentObj.content, | var content = contentObj.content, | ||
tag = mw.message( 'dfy-tag-template', this.getTargetUser() ).plain(); | tag = mw.message( 'dfy-tag-template', this.getTargetUser() ).plain(); | ||
if ( /^\s*{{/.test( content ) ) { | if ( /^\s*{{/.test( content ) ) { | ||
content = tag + '\n' + content; | content = tag + '\n' + content; | ||
} else { | } else { | ||
content = tag + '\n\n' + content; | content = tag + '\n\n' + content; | ||
} | } | ||
return this.editPage( { | return this.editPage( { | ||
title: this.getTargetDraftTitle().getPrefixedText(), | title: this.getTargetDraftTitle().getPrefixedText(), | ||
| Line 530: | Line 444: | ||
} ); | } ); | ||
}; | }; | ||
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 ) { | ApiManager.prototype.softBlockUser = function ( options ) { | ||
return this.api.postWithEditToken( { | return this.api.postWithEditToken( { | ||
format: 'json', | format: 'json', | ||
action: 'block', | action: 'block', | ||
user: this | user: this.getTargetUser(), | ||
expiry: 'infinite', | expiry: 'infinite', | ||
reason: mw.message( 'dfy-softblock-summary' ).plain(), | reason: mw.message( 'dfy-softblock-summary' ).plain(), | ||
| Line 541: | Line 480: | ||
} ); | } ); | ||
}; | }; | ||
ApiManager.prototype.notifyUser = function ( options ) { | ApiManager.prototype.notifyUser = function ( options ) { | ||
options = options || {}; | options = options || {}; | ||
var summary, | var summary, | ||
self = this; | self = this; | ||
if ( options.softblock ) { | if ( options.softblock ) { | ||
summary = mw.message( 'dfy-notify-summary-softblock' ).plain(); | summary = mw.message( 'dfy-notify-summary-softblock' ).plain(); | ||
| Line 554: | Line 491: | ||
summary = mw.message( 'dfy-notify-summary-moveonly' ).plain(); | summary = mw.message( 'dfy-notify-summary-moveonly' ).plain(); | ||
} | } | ||
return self.getPageContent( { | return self.getPageContent( { | ||
title: self.getUserTalkTitle().getPrefixedText(), | title: self.getUserTalkTitle().getPrefixedText(), | ||
allowNonexistent: true | allowNonexistent: true | ||
} ).then( function ( contentObj ) { | } ).then( function ( contentObj ) { | ||
var lastHeading, headings, | var lastHeading, headings, | ||
content = contentObj.content, | content = contentObj.content, | ||
notificationDate = self.getNotificationDate(); | notificationDate = self.getNotificationDate(); | ||
if ( /\S/.test( content ) ) { | if ( /\S/.test( content ) ) { | ||
content += '\n\n'; | content += '\n\n'; | ||
} | } | ||
headings = content.match( /^==[^=].*==[ \t]*$/gm ); | headings = content.match( /^==[^=].*==[ \t]*$/gm ); | ||
if ( headings ) { | if ( headings ) { | ||
| Line 579: | Line 508: | ||
content += '== ' + notificationDate + ' ==\n\n'; | content += '== ' + notificationDate + ' ==\n\n'; | ||
} | } | ||
content += mw.message( | content += mw.message( | ||
'dfy-notify-template', | 'dfy-notify-template', | ||
| Line 591: | Line 518: | ||
content += '\n\n' + mw.message( 'dfy-suggestrename-template' ).plain(); | content += '\n\n' + mw.message( 'dfy-suggestrename-template' ).plain(); | ||
} | } | ||
return self.editPage( { | return self.editPage( { | ||
title: contentObj.title, | title: contentObj.title, | ||
| Line 599: | Line 525: | ||
} ); | } ); | ||
} ); | } ); | ||
}; | }; | ||
ApiManager.prototype.submit = function ( options ) { | ApiManager.prototype.submit = function ( options ) { | ||
var i, len, | var i, len, | ||
| Line 608: | Line 532: | ||
promises = {}, | promises = {}, | ||
whenArgs = []; | whenArgs = []; | ||
function makeDoneHandler( i ) { | function makeDoneHandler( i ) { | ||
return function ( obj ) { | return function ( obj ) { | ||
| Line 615: | Line 537: | ||
}; | }; | ||
} | } | ||
try { | try { | ||
this.setTargetDraftTitle( new mw.Title( | this.setTargetDraftTitle( new mw.Title( | ||
mw.message( 'dfy-draft-prefix' ).text() + options.title | mw.message( 'dfy-draft-prefix' ).text() + options.title | ||
) ); | ) ); | ||
actions.push( { action: 'move', method: 'moveDraft' } ); | actions.push( { action: 'move', method: 'moveDraft' } ); | ||
actions.push( { action: 'tag', method: 'tagDraft' } ); | actions.push( { action: 'tag', method: 'tagDraft' } ); | ||
if ( options.movetalk ) { | |||
actions.push( { action: 'movetalk', method: 'moveDraftTalkPage' } ); | |||
} | |||
if ( options.softblock ) { | if ( options.softblock ) { | ||
actions.push( { action: 'softblock', method: 'softBlockUser' } ); | actions.push( { action: 'softblock', method: 'softBlockUser' } ); | ||
| Line 630: | Line 552: | ||
actions.push( { action: 'notify', method: 'notifyUser' } ); | actions.push( { action: 'notify', method: 'notifyUser' } ); | ||
} | } | ||
promises[ actions[ 0 ].action ] = self[ actions[ 0 ].method ]( options ); | promises[ actions[ 0 ].action ] = self[ actions[ 0 ].method ]( options ); | ||
for ( i = 1, len = actions.length; i < len; i++ ) { | for ( i = 1, len = actions.length; i < len; i++ ) { | ||
| Line 640: | Line 559: | ||
} | } | ||
} catch ( e ) { | } catch ( e ) { | ||
promises.move = $.Deferred().reject( 'dfy-submit-error', { 'error': { | promises.move = $.Deferred().reject( 'dfy-submit-error', { 'error': { | ||
'id': 'dfy-submit-error', | 'id': 'dfy-submit-error', | ||
| Line 647: | Line 564: | ||
} } ).promise(); | } } ).promise(); | ||
} | } | ||
$.each( promises, function ( key, promise ) { | $.each( promises, function ( key, promise ) { | ||
whenArgs.push( promise ); | whenArgs.push( promise ); | ||
} ); | } ); | ||
promises.all = $.when.apply( this, whenArgs ); | promises.all = $.when.apply( this, whenArgs ); | ||
return promises; | return promises; | ||
}; | }; | ||
/************************************************************************** | /************************************************************************** | ||
* Dialog class | * Dialog class | ||
**************************************************************************/ | **************************************************************************/ | ||
Dialog = function ( options ) { | Dialog = function ( options ) { | ||
options = options || {}; | options = options || {}; | ||
| Line 669: | Line 580: | ||
Dialog.super.call( this, options ); | Dialog.super.call( this, options ); | ||
}; | }; | ||
OO.inheritClass( Dialog, OO.ui.ProcessDialog ); | OO.inheritClass( Dialog, OO.ui.ProcessDialog ); | ||
Dialog.static.name = 'DraftifyDialog'; | Dialog.static.name = 'DraftifyDialog'; | ||
Dialog.static.title = 'Draftify'; | Dialog.static.title = 'Draftify'; | ||
Dialog.static.actions = [ | Dialog.static.actions = [ | ||
{ action: 'submit', label: 'Submit', flags: [ 'primary', 'constructive' ] }, | { action: 'submit', label: 'Submit', flags: [ 'primary', 'constructive' ] }, | ||
{ label: 'Cancel', flags: 'safe' } | { label: 'Cancel', flags: 'safe' } | ||
]; | ]; | ||
Dialog.prototype.getApiManager = function () { | Dialog.prototype.getApiManager = function () { | ||
return this.apiManager; | return this.apiManager; | ||
}; | }; | ||
Dialog.prototype.getBodyHeight = function () { | Dialog.prototype.getBodyHeight = function () { | ||
return 300; | return 300; | ||
}; | }; | ||
Dialog.prototype.initialize = function () { | Dialog.prototype.initialize = function () { | ||
var apiManager = this.getApiManager(); | var apiManager = this.getApiManager(); | ||
Dialog.super.prototype.initialize.apply( this, arguments ); | Dialog.super.prototype.initialize.apply( this, arguments ); | ||
this.editPanel = new OO.ui.PanelLayout( { | this.editPanel = new OO.ui.PanelLayout( { | ||
expanded: false | expanded: false | ||
| Line 700: | Line 603: | ||
} ); | } ); | ||
this.editPanel.$element.append( this.editFieldset.$element ); | this.editPanel.$element.append( this.editFieldset.$element ); | ||
this.titleInput = new OO.ui.TextInputWidget( { | this.titleInput = new OO.ui.TextInputWidget( { | ||
validate: apiManager.getTitleValidationRegex(), | validate: apiManager.getTitleValidationRegex(), | ||
value: apiManager.getCurrentSubpage() | value: apiManager.getCurrentSubpage() | ||
} ); | } ); | ||
this.redirectCheckbox = new OO.ui.CheckboxInputWidget(); | this.redirectCheckbox = new OO.ui.CheckboxInputWidget(); | ||
this.redirectCheckbox.setSelected( true ); | this.redirectCheckbox.setSelected( true ); | ||
| Line 717: | Line 612: | ||
this.notifyCheckbox = new OO.ui.CheckboxInputWidget( { | this.notifyCheckbox = new OO.ui.CheckboxInputWidget( { | ||
selected: apiManager.getPreference( 'notify' ) | selected: apiManager.getPreference( 'notify' ) | ||
} ); | |||
this.moveTalkPageCheckbox = new OO.ui.CheckboxInputWidget( { | |||
selected: false, | |||
disabled: true | |||
} ); | } ); | ||
this.suggestrenameCheckbox = new OO.ui.CheckboxInputWidget( { | this.suggestrenameCheckbox = new OO.ui.CheckboxInputWidget( { | ||
| Line 729: | Line 628: | ||
selected: apiManager.getPreference( 'watchdraft' ) | selected: apiManager.getPreference( 'watchdraft' ) | ||
} ); | } ); | ||
this.editFieldset.addItems( [ | this.editFieldset.addItems( [ | ||
new OO.ui.FieldLayout( this.titleInput, { | new OO.ui.FieldLayout( this.titleInput, { | ||
| Line 740: | Line 637: | ||
} ), | } ), | ||
new OO.ui.FieldLayout( this.notifyCheckbox, { | new OO.ui.FieldLayout( this.notifyCheckbox, { | ||
label: mw.message( 'dfy-notify-checkbox-label' ).plain(), | 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' | align: 'inline' | ||
} ), | } ), | ||
new OO.ui.FieldLayout( this.suggestrenameCheckbox, { | new OO.ui.FieldLayout( this.suggestrenameCheckbox, { | ||
label: mw.message( 'dfy-suggestrename-checkbox-label' ).plain(), | label: mw.message( 'dfy-suggestrename-checkbox-label' ).plain(), | ||
align: 'inline' | align: 'inline' | ||
} ), | } ), | ||
new OO.ui.FieldLayout( this.softblockCheckbox, { | new OO.ui.FieldLayout( this.softblockCheckbox, { | ||
label: mw.message( 'dfy-softblock-checkbox-label' ).plain(), | label: mw.message( 'dfy-softblock-checkbox-label' ).plain(), | ||
align: 'inline' | align: 'inline' | ||
| Line 764: | Line 661: | ||
} ) | } ) | ||
] ); | ] ); | ||
this.submitPanel = new OO.ui.PanelLayout( { | this.submitPanel = new OO.ui.PanelLayout( { | ||
$: this.$, | $: this.$, | ||
| Line 778: | Line 671: | ||
this.moveProgressLabel = new OO.ui.LabelWidget(); | this.moveProgressLabel = new OO.ui.LabelWidget(); | ||
this.moveProgressField = new OO.ui.FieldLayout( this.moveProgressLabel ); | 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.tagProgressLabel = new OO.ui.LabelWidget(); | ||
this.tagProgressField = new OO.ui.FieldLayout( this.tagProgressLabel ); | this.tagProgressField = new OO.ui.FieldLayout( this.tagProgressLabel ); | ||
| Line 785: | Line 680: | ||
this.notifyProgressField = new OO.ui.FieldLayout( this.notifyProgressLabel ); | this.notifyProgressField = new OO.ui.FieldLayout( this.notifyProgressLabel ); | ||
this.openTalkProgressField = new OO.ui.FieldLayout( new OO.ui.LabelWidget() ); | this.openTalkProgressField = new OO.ui.FieldLayout( new OO.ui.LabelWidget() ); | ||
this.stackLayout= new OO.ui.StackLayout( { | this.stackLayout= new OO.ui.StackLayout( { | ||
items: [ this.editPanel, this.submitPanel ], | items: [ this.editPanel, this.submitPanel ], | ||
padded: true | padded: true | ||
} ); | } ); | ||
this.$body.append( this.stackLayout.$element ); | this.$body.append( this.stackLayout.$element ); | ||
}; | }; | ||
Dialog.prototype.getReadyProcess = function ( data ) { | Dialog.prototype.getReadyProcess = function ( data ) { | ||
data = data || {}; | data = data || {}; | ||
| Line 812: | Line 702: | ||
.fail( function () { | .fail( function () { | ||
dialog.popPending(); | dialog.popPending(); | ||
alert( 'There was a problem fetching your user rights from the API.' ); | alert( 'There was a problem fetching your user rights from the API.' ); | ||
} ); | } ); | ||
} | } | ||
if ( dialog.hasBeenSubmitted ) { | if ( dialog.hasBeenSubmitted ) { | ||
dialog.actions.setAbilities( { submit: false } ); | dialog.actions.setAbilities( { submit: false } ); | ||
} else { | } else { | ||
this.titleInput.focus(); | this.titleInput.focus(); | ||
var val = this.titleInput.getValue(); | var val = this.titleInput.getValue(); | ||
this.titleInput.setValue( '' ); | this.titleInput.setValue( '' ); | ||
| Line 837: | Line 715: | ||
}, this ); | }, this ); | ||
}; | }; | ||
Dialog.prototype.onLoad = function () { | Dialog.prototype.onLoad = function () { | ||
var apiManager = this.getApiManager(); | 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 () { | Dialog.prototype.onSuggestrenameChange = function () { | ||
if ( this.suggestrenameCheckbox.isSelected() ) { | if ( this.suggestrenameCheckbox.isSelected() ) { | ||
| Line 894: | Line 780: | ||
this.onNotifyChange(); | this.onNotifyChange(); | ||
}; | }; | ||
Dialog.prototype.onSoftblockChange = function () { | Dialog.prototype.onSoftblockChange = function () { | ||
if ( this.softblockCheckbox.isSelected() ) { | if ( this.softblockCheckbox.isSelected() ) { | ||
| Line 907: | Line 792: | ||
this.onNotifyChange(); | this.onNotifyChange(); | ||
}; | }; | ||
Dialog.prototype.onNotifyChange = function () { | Dialog.prototype.onNotifyChange = function () { | ||
if ( this.notifyCheckbox.isSelected() ) { | if ( this.notifyCheckbox.isSelected() ) { | ||
| Line 916: | Line 800: | ||
} | } | ||
}; | }; | ||
Dialog.prototype.onTitleChange = function () { | Dialog.prototype.onTitleChange = function () { | ||
var self = this, | var self = this, | ||
| Line 927: | Line 810: | ||
} ); | } ); | ||
}; | }; | ||
Dialog.prototype.onSubmit = function () { | Dialog.prototype.onSubmit = function () { | ||
var options, promises | var options, promises, | ||
self = this, | self = this, | ||
apiManager = this.getApiManager(), | apiManager = this.getApiManager(), | ||
targetUser = apiManager.getTargetUser(); | targetUser = apiManager.getTargetUser(); | ||
self.hasBeenSubmitted = true; | self.hasBeenSubmitted = true; | ||
self.actions.setAbilities( { submit: false } ); | self.actions.setAbilities( { submit: false } ); | ||
options = { | options = { | ||
title: this.titleInput.getValue(), | title: this.titleInput.getValue(), | ||
redirect: this.redirectCheckbox.isSelected(), | redirect: this.redirectCheckbox.isSelected(), | ||
movetalk: this.moveTalkPageCheckbox.isSelected(), | |||
notify: this.notifyCheckbox.isSelected(), | notify: this.notifyCheckbox.isSelected(), | ||
suggestrename: this.suggestrenameCheckbox.isSelected(), | suggestrename: this.suggestrenameCheckbox.isSelected(), | ||
| Line 951: | Line 827: | ||
watchdraft: this.watchDraftCheckbox.isSelected() | watchdraft: this.watchDraftCheckbox.isSelected() | ||
}; | }; | ||
promises = apiManager.submit( options ); | promises = apiManager.submit( options ); | ||
$.each( promises, function ( key, value ) { | $.each( promises, function ( key, value ) { | ||
if ( value ) { | if ( value ) { | ||
| Line 960: | Line 833: | ||
} | } | ||
} ); | } ); | ||
function addField( field, label, promise ) { | function addField( field, label, promise ) { | ||
if ( !promise ) { | if ( !promise ) { | ||
| Line 977: | Line 847: | ||
mw.message( 'dfy-move-progress-label' ).text(), | mw.message( 'dfy-move-progress-label' ).text(), | ||
promises.move | promises.move | ||
); | |||
addField( | |||
self.moveTalkProgressField, | |||
mw.message( 'dfy-move-talk-progress-label' ).text(), | |||
promises.movetalk | |||
); | ); | ||
addField( | addField( | ||
| Line 997: | Line 872: | ||
.toggle(); | .toggle(); | ||
self.submitFieldset.addItems( [ self.openTalkProgressField ] ); | self.submitFieldset.addItems( [ self.openTalkProgressField ] ); | ||
self.moveProgressField.toggle(); | self.moveProgressField.toggle(); | ||
$.each( self.submitFieldset.getItems(), function ( i, field ) { | $.each( self.submitFieldset.getItems(), function ( i, field ) { | ||
| Line 1,006: | Line 877: | ||
promise = field.getData(), | promise = field.getData(), | ||
label = field.getField(); | label = field.getField(); | ||
if ( !promise ) { | if ( !promise ) { | ||
return; | return; | ||
} | } | ||
promise | promise | ||
.done( function () { | .done( function () { | ||
var nextField; | var nextField; | ||
label.setLabel( $( '<span>' ) | label.setLabel( $( '<span>' ) | ||
.addClass( 'draftify-success' ) | .addClass( 'draftify-success' ) | ||
.text( mw.message( 'dfy-progress-success' ).text() ) | .text( mw.message( 'dfy-progress-success' ).text() ) | ||
); | ); | ||
nextField = self.submitFieldset.getItems()[ i + 1 ]; | nextField = self.submitFieldset.getItems()[ i + 1 ]; | ||
if ( nextField ) { | if ( nextField ) { | ||
| Line 1,046: | Line 911: | ||
} ); | } ); | ||
} ); | } ); | ||
promises.all.done( function () { | promises.all.done( function () { | ||
apiManager.openTalkPage(); | apiManager.openTalkPage(); | ||
} ); | } ); | ||
promises.all.fail( function () { | promises.all.fail( function () { | ||
self.popPending(); | self.popPending(); | ||
} ); | } ); | ||
self.stackLayout.setItem( self.submitPanel ); | self.stackLayout.setItem( self.submitPanel ); | ||
}; | }; | ||
Dialog.prototype.getActionProcess = function ( action ) { | Dialog.prototype.getActionProcess = function ( action ) { | ||
return Dialog.super.prototype.getActionProcess.call( this, action ) | return Dialog.super.prototype.getActionProcess.call( this, action ) | ||
| Line 1,071: | Line 929: | ||
}, this ); | }, this ); | ||
}; | }; | ||
/************************************************************************** | /************************************************************************** | ||
* Main | * Main | ||
**************************************************************************/ | **************************************************************************/ | ||
function main() { | |||
mw.util.addCSS( | |||
'.draftify-success { color: #14866d; font-weight: bold; }' + | |||
'.draftify-error { color: #d33; font-weight: bold; }' | |||
); | |||
var portletLink, windowManager, | var portletLink, windowManager, | ||
draftifyDialog = new Dialog( { size: 'medium' } ), | draftifyDialog = new Dialog( { size: 'medium' } ), | ||
apiManager = draftifyDialog.getApiManager(); | apiManager = draftifyDialog.getApiManager(); | ||
windowManager = new OO.ui.WindowManager(); | windowManager = new OO.ui.WindowManager(); | ||
$( 'body' ).append( windowManager.$element ); | $( 'body' ).append( windowManager.$element ); | ||
windowManager.addWindows( [ draftifyDialog ] ); | windowManager.addWindows( [ draftifyDialog ] ); | ||
portletLink = apiManager.addPortletLink(); | portletLink = apiManager.addPortletLink(); | ||
function openDraftify( event ) { | |||
event.preventDefault(); | event.preventDefault(); | ||
windowManager.openWindow( draftifyDialog ); | windowManager.openWindow( draftifyDialog ); | ||
} ); | } | ||
$( portletLink ).click( openDraftify ); | |||
$( document ).on( 'click', '.bpneo-draftify-btn, .bpneo-draftify-menu', openDraftify ); | |||
} | } | ||
main(); | main(); | ||
} ); | } ); | ||
// </nowiki> | // </nowiki> | ||
Latest revision as of 20:48, 21 September 2026
// <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>