User:Xover/Headertools.js

/* global $, mw, OO */ "use strict";

var dependencies = []; dependencies.push('mediawiki.util');   // Basic MW utility functions dependencies.push('mediawiki.storage'); // Wrapper for Web Storage API dependencies.push('oojs-ui-core');     // OOUI core module dependencies.push('oojs-ui-windows');  // OOUI windowing functions for dialog

// Make sure the necessary modules are loaded mw.loader.using(dependencies, => {

// Wait for the page to be parsed (new-style $(document).ready) $( => {

/*    *  First check that this is a context we should be active in. */

// Only active on Page:-namespace pages. if (mw.config.get('wgCanonicalNamespace') !== 'Page') { return; }

// Only active on pages with content model 'proofread-page'. if (mw.config.get('wgPageContentModel') !== 'proofread-page') { return; }

// Only active when in edit/preview/diff mode. if ($.inArray(mw.config.get('wgAction'), ['edit', 'submit']) < 0) { return; }

/*    *  Extract name for the current work, and construct storage keys for it. */

var curWork = /^(.*)\/\d+$/.exec(mw.config.get('wgTitle'))[1]; var curWorkChapterKey = "headertools-" + curWork + "-chapter"; var curWorkAutoKey = "headertools-" + curWork + "-auto";

/*    *  Add portlets for the various commands. */

var headerPortlet = mw.util.addPortletLink(     'p-tb', '#', 'Reset header and footer', 'ca-resetheader',      'Reset the header and footer to the values given in the Index.'    ); $(headerPortlet).click(event => {     event.preventDefault;      doResetHeader(curWorkChapterKey);    });

var chapterPortlet = mw.util.addPortletLink(     'p-tb', '#', 'Set chapter title', 'ca-setchapter',      'Save the selected text as the current chapter title.'    ); $(chapterPortlet).click(event => {     event.preventDefault;      doSetChapter(curWorkChapterKey);    });

var autoOnPortlet = mw.util.addPortletLink(     'p-tb', '#', 'Auto-reset header and footer', 'ca-autoadd',      'Automatically reset header and footer on edit.'    ); $(autoOnPortlet).click(event => {     event.preventDefault;      doSetAutoOn(curWorkAutoKey);    });

var autoOffPortlet = mw.util.addPortletLink(     'p-tb', '#', 'Stop auto-resetting header and footer', 'ca-noautoadd',      'Stop automatically resetting header and footer on edit.'    ); $(autoOffPortlet).click(event => {     event.preventDefault;      doSetAutoOff(curWorkAutoKey);    });

/*    *  Automatically reset header and footer if the user requested it. */

if (mw.storage.get(curWorkAutoKey) === "yes") { if (mw.config.get('wgAction') === "edit") { // Not on preview or diff // mw.Title.exists is broken, cf. T184953 // Workaround is to check revision ID which is 0 for non-existent pages. // Also auto-reset for "Not Proofread" pages. if (mw.config.get("wgArticleId") === 0 || mw.config.get("prpPageQuality") === 1) { doResetHeader(curWorkChapterKey); }     }    }  }); // END: $(document).ready }); // END: mw.loader.using

function doResetHeader(curWorkChapterKey) { var curChapter = mw.storage.get(curWorkChapterKey);

const ppIndexPageLabel = mw.config.get("prpFormattedPageNumber");

var h = mw.config.get("prpIndexFields").Header; h = h.replaceAll('', ppIndexPageLabel); h = h.replaceAll('', curChapter); $('#wpHeaderTextbox').val(h);

var f = mw.config.get("prpIndexFields").Footer; f = f.replaceAll('', ppIndexPageLabel); f = f.replaceAll('', curChapter); $('#wpFooterTextbox').val(f); } // END: doResetHeader

function doSetChapter(curWorkChapterKey) { var oldChapter = mw.storage.get(curWorkChapterKey);

OO.ui.prompt(   'Set chapter title',    {      textInput: {        placeholder: 'Chapter title',        value: oldChapter      }    }  ).done((chapter) => {    if (chapter !== null) {      mw.storage.set(curWorkChapterKey, chapter);    }  }); } // END: doSetChapter

function doSetAutoOn(curWorkAutoKey) { mw.storage.set(curWorkAutoKey, "yes"); }

function doSetAutoOff(curWorkAutoKey) { mw.storage.set(curWorkAutoKey, "no"); }