MediaWiki:StickySummary.js

/* StickySummary - auto-fill summary/reason box when making repetitive changes manually */

/*jslint browser */ /*global window, jQuery, mediaWiki */

(function ($, mw) {   "use strict";

var context = (function {        var actions = {            "delete": "delete",            edit: "edit",            protect: "protect",            submit: "edit"        };        var specialPages = {            Movepage: "move"        };

return specialPages[mw.config.get("wgCanonicalSpecialPageName")] || actions[mw.config.get("wgAction")] || null; });

// run only on suitable pages + double-run protection if (!context || window.loadedStickySummary) { return; }   window.loadedStickySummary = true;

// icons from Font-Awesome-SVG-PNG (https://github.com/encharm/Font-Awesome-SVG-PNG), licensed under MIT license (https://opensource.org/licenses/MIT) // originally from FontAwesome (http://fontawesome.io/), licensed under SIL Open Font License 1.1 (http://scripts.sil.org/OFL) var icons = { lock: " ", unlock: " " };   var targets = { "delete": "#wpReason", edit: "#wpSummary", move: "#wpReason", protect: "#mwProtect-reason" };

var $button; var $target; var defaultValue = ""; var storageId = "StickySummary-" + context;

function updateState(event) { // ignore non-stickysummary events if (event.key !== storageId) { return; }

var currentValue = $target.val;

// event.newValue should be null for removed item, but IE uses empty string instead if (event.newValue/* !== null*/) { $button.addClass("stickysummary-enabled");

// update existing summary if it hasn't been changed if (currentValue === defaultValue) { $target.val(event.newValue); }       } else { $button.removeClass("stickysummary-enabled");

// clear existing summary (except on current window) if it matches old storage value if (!event.ownWindow && currentValue === event.oldValue) { $target.val(defaultValue); }       }    }

function saveSummary(event) { event.preventDefault;

var storageValue = localStorage.getItem(storageId); var newValue = $target.val;

// if no summary is saved, then save the current summary, else clear the saved summary if (!storageValue) {//if (storageValue === null) { localStorage.setItem(storageId, newValue); } else { localStorage.removeItem(storageId); newValue = null; }

// fire faux event, as storage events don't fire for the window in which a change is made updateState({           ownWindow: true,            key: storageId,            oldValue: storageValue,            newValue: newValue        }); }

function initSummary { // remember default value except if submitting edit (don't want to clear summary in that case) if (mw.config.get("wgAction") !== "submit") { defaultValue = $target[0].getAttribute("value") || ""; }

updateState({           key: storageId,            newValue: localStorage.getItem(storageId)        }); }

function main { mw.util.addCSS(           ".stickysummary {" +                "vertical-align: text-top;" +            "}" +            ".stickysummary > #stickysummary-lock," +            ".stickysummary-enabled > #stickysummary-unlock {" +                "display: none;" +            "}" +            ".stickysummary-enabled > #stickysummary-lock {" +                "display: inline;" +            "}" +            // oasis-specific styles            ".module_content #wpSummaryLabel {" +                "position: relative;" +            "}" +            ".module_content #wpSummaryLabel > .stickysummary {" +                "position: absolute;" +                "right: -18px;" +                "top: 0;" +            "}"        );

$target = $(targets[context]);

$button = $("") .attr({"class": "stickysummary", href: "#", title: "StickySummary"}) .click(saveSummary) .append(icons.unlock, icons.lock);

if (context === "edit" && mw.config.get("skin") !== "oasis") { $target.before($button); } else { $target.after($button); }

initSummary; window.addEventListener("storage", updateState); }

$(main); }(jQuery, mediaWiki));