// <nowiki>
// Fork of [[User:DannyS712/EFFPRH/sandbox.js]]
$(() => {
const CHUHelper = {
config: {
debug: false
},
responseOptions: [
{ value: 'done', label: 'Done' },
{ value: 'notdone', label: 'Not done' },
{ value: 'alreadydone', label: 'Already done' },
{ value: 'withdrawn', label: 'Withdrawn' }
],
init() {
const allowedPages = [
'Wikipedia:Changing username/Simple'
];
const currentPage = mw.config.get('wgPageName').replace(/_/g, ' ');
if (!allowedPages.includes(currentPage)) return;
mw.loader.using(['vue', '@wikimedia/codex', 'mediawiki.util', 'mediawiki.api'], this.run.bind(this));
},
run() {
this.addStyle();
const headingSelector = 'div.mw-heading3';
$(headingSelector).each((_, heading) => {
const editLinks = $(heading).find('.mw-editsection');
const sectionNum = this.getSectionNumber(editLinks);
if (sectionNum !== -1) {
$(heading).after($('<div>').attr('id', `script-CHUHelper-${sectionNum}`));
const sectionTitle = $(heading).contents().not(editLinks).text().trim();
this.addHelperLink(editLinks, sectionTitle, sectionNum);
}
});
},
addStyle() {
mw.util.addCSS(`
.script-CHUHelper-helper { background-color: #f5f5f5; border: 1px solid #ccc; margin: 10px 0; padding: 10px; border-radius: 5px; }
.cdx-menu ul { margin-left: 0px; }
.cdx-menu { margin-bottom: 10px; }
.cdx-menu-item__content { line-height: 1em; }
.script-CHUHelper-helper td { vertical-align: middle; padding: 5px; }
.script-CHUHelper-preview { background-color: white; }
`);
},
getSectionNumber(editLinks) {
const editSectionUrl = editLinks.find('a:first').attr('href');
const sectionMatch = editSectionUrl && editSectionUrl.match(/§ion=(\d+)(?:$|&)/);
return sectionMatch ? parseInt(sectionMatch[1]) : -1;
},
addHelperLink(editLinks, reporterName, sectionNum) {
const helperLink = $('<a>').attr('id', `script-CHUHelper-launch-${sectionNum}`).text('Review username change');
helperLink.click(() => {
if (!helperLink.hasClass('script-CHUHelper-disabled')) {
helperLink.addClass('script-CHUHelper-disabled');
this.showHelper(reporterName, sectionNum);
}
});
editLinks.children().last().before(' | ', helperLink);
},
showHelper(reporterName, sectionNum) {
const vueAppInstance = Vue.createMwApp({
components: {
CdxButton: mw.loader.require('@wikimedia/codex').CdxButton,
CdxSelect: mw.loader.require('@wikimedia/codex').CdxSelect,
CdxTextInput: mw.loader.require('@wikimedia/codex').CdxTextInput,
CdxToggleButton: mw.loader.require('@wikimedia/codex').CdxToggleButton,
previewRenderer: this.getPreviewComponent()
},
data() {
return {
reporterName,
sectionNum,
responseOptions: CHUHelper.responseOptions,
selectedResponse: 'none',
commentValue: '',
showDebug: CHUHelper.config.debug,
showPreview: false,
haveSubmitted: false,
editMade: false,
editError: false
};
},
computed: {
canSubmit() {
return !this.haveSubmitted && (this.selectedResponse !== 'none' || this.commentValue.trim().length > 0);
},
previewToggleLabel() {
return this.showPreview ? 'Hide preview' : 'Show preview';
},
responseWikiText() {
return `: {{${this.selectedResponse}}} ${this.commentValue} ~~~~`;
}
},
methods: {
reloadPage() {
location.assign(`${mw.util.getUrl(mw.config.get('wgPageName'))}#${this.reporterName}`);
location.reload();
},
submitHelper() {
this.haveSubmitted = true;
CHUHelper.respondToReport(this.reporterName, this.sectionNum, this.responseWikiText).then(
() => this.editMade = true,
() => this.editError = true
);
},
cancelHelper() {
vueAppInstance && vueAppInstance.unmount();
$(`#script-CHUHelper-launch-${this.sectionNum}`).removeClass('script-CHUHelper-disabled');
}
},
template: `
<div class="script-CHUHelper-helper">
<p>Responding to username change request {{ reporterName }}.</p>
<p v-if="showDebug">Section {{ sectionNum }}, selected response: {{ selectedResponse }}, comment: {{ commentValue }}.</p>
<table>
<tr><td><label>Action:</label></td><td><cdx-select v-model:selected="selectedResponse" :menu-items="responseOptions" default-label="Response to request" :disabled="haveSubmitted" /></td></tr>
<tr><td><label>Comment:</label></td><td><cdx-text-input v-model="commentValue" :disabled="haveSubmitted" /></td></tr>
</table>
<ul v-show="haveSubmitted">
<li>Submitting...</li>
<li v-show="editMade">Success! <a v-on:click="reloadPage"><strong>Reload the page</strong></a></li>
<li v-show="editError">Uh-oh, something went wrong. Please check the console for details.</li>
</ul>
<cdx-button weight="primary" action="progressive" :disabled="!canSubmit" v-on:click="submitHelper">Submit</cdx-button>
<cdx-button weight="primary" action="destructive" :disabled="haveSubmitted" v-on:click="cancelHelper">Cancel</cdx-button>
<cdx-toggle-button v-model="showPreview" :disabled="!canSubmit">{{ previewToggleLabel }}</cdx-toggle-button>
<preview-renderer v-if="showPreview && canSubmit" :wikitext="responseWikiText"></preview-renderer>
</div>
`
});
vueAppInstance.mount(`#script-CHUHelper-${sectionNum}`);
},
getPreviewComponent() {
return {
props: { wikitext: { type: String, default: '' } },
data() {
return { previewHtml: '', haveHtml: false };
},
methods: {
loadPreview(wikitextToPreview) {
new mw.Api().get({
action: 'parse',
formatversion: 2,
title: mw.config.get('wgPageName'),
text: wikitextToPreview,
prop: 'text|wikitext',
pst: true,
disablelimitreport: true,
disableeditsection: true,
sectionpreview: true
}).then(res => {
if (res && res.parse && res.parse.wikitext === this.wikitext && res.parse.text) {
this.previewHtml = res.parse.text;
this.haveHtml = true;
}
});
}
},
watch: {
wikitext(newValue) {
this.previewHtml = '';
this.haveHtml = false;
this.loadPreview(newValue);
}
},
mounted() {
this.loadPreview(this.wikitext);
},
template: `<div class="script-CHUHelper-preview"><hr><div v-if="haveHtml" v-html="previewHtml"></div><div v-else>Loading preview of {{ wikitext }}</div></div>`
};
},
respondToReport(reporterName, sectionNum, responseWikiText) {
return new Promise((resolve, reject) => {
const wikitextToAdd = `\n${responseWikiText}`;
const editParams = {
action: 'edit',
title: mw.config.get('wgPageName'),
section: sectionNum,
summary: `Respond to username change request (using [[User:DreamRimmer/CHUHelper|CHUHelper]])`,
appendtext: wikitextToAdd,
token: mw.user.tokens.get('csrfToken')
};
new mw.Api().postWithEditToken(editParams).then(resolve, reject);
});
}
};
CHUHelper.init();
});
// </nowiki>