// Templates for discussion clerk script by [[User:The Earwig]] // Version: @TFDCLERK_VERSION_FULL@ // Development and bug reports: https://github.com/earwig/tfdclerk // To install, add: // importScript("User:The Earwig/tfdclerk.js"); // [[User:The Earwig/tfdclerk.js]] // to [[Special:MyPage/common.js]] or [[Special:MyPage/skin.js]] /* Copyright (C) 2015 Ben Kurtovic 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. */ var dependencies = [ "mediawiki.api", "mediawiki.ui", "mediawiki.util", "jquery.ui.core" ]; var is_tfd_page = function() { return mw.config.get("wgAction") == "view" && mw.config.get("wgIsProbablyEditable") == true && mw.config.get("wgRevisionId") == mw.config.get("wgCurRevisionId") && mw.config.get("wgNamespaceNumber") == 4 && ( mw.config.get("wgTitle") == "Templates for discussion" || mw.config.get("wgTitle").indexOf("Templates for discussion/Log/2") == 0); }; if (is_tfd_page()) { mw.loader.using(dependencies, function() { /* Main script starts here */ TFDClerk = { version: "@TFDCLERK_VERSION@", script_url: "https://en.wikipedia.org/wiki/User:The_Earwig/tfdclerk.js", author_url: "https://en.wikipedia.org/wiki/User_talk:The_Earwig", github_url: "https://github.com/earwig/tfdclerk", tfds: [], _api: new mw.Api(), _sysop: $.inArray("sysop", mw.config.get("wgUserGroups")) >= 0 // TODO: access time? }; TFD = function(id, head) { this.id = id; this.head = head; this.box = null; this._guard = false; this._submit_blockers = []; this._wikitext = undefined; this._wikitext_callbacks = []; }; TFDClerk.api_get = function(tfd, params, done, fail, always) { TFDClerk._api.get(params) .done(function(data) { if (done !== undefined) done.call(tfd, data); }) .fail(function(error) { if (done !== undefined) fail.call(tfd, error); }) .always(function() { if (always !== undefined) always.call(tfd); }); }; TFDClerk.install = function() { $("h4").each(function(i, elem) { var head = $(elem); if (head.next().hasClass("tfd-closed")) return; if (head.find(".mw-editsection").length == 0) return; var tfd = new TFD(TFDClerk.tfds.length, head); TFDClerk.tfds.push(tfd); tfd.add_hooks(); }); }; Date.prototype.toDatePickerFormat = function() { return this.toISOString().slice(0, 10); } TFD.prototype._block_submit = function(reason) { if (this._submit_blockers.indexOf(reason) < 0) this._submit_blockers.push(reason); this.box.find(".tfdclerk-submit").prop("disabled", true); }; TFD.prototype._unblock_submit = function(reason) { var index = this._submit_blockers.indexOf(reason); if (index >= 0) this._submit_blockers.splice(index, 1); if (this._submit_blockers.length == 0) this.box.find(".tfdclerk-submit").prop("disabled", false); }; TFD.prototype._error = function(msg, extra) { var elem = $("", { html: "Error: " + (extra ? msg + ": " : msg), style: "color: #A00;" }); if (extra) elem.append($("", { text: extra, style: "font-family: monospace;" })); var contact = $("", { href: TFDClerk.author_url, title: TFDClerk.author_url.split("/").pop().replace(/_/g, " "), text: "contact me" }), file_bug = $("", { href: TFDClerk.github_url, title: TFDClerk.github_url.split("/").splice(-2).join("/"), text: "file a bug report" }); elem.append($("
")) .append($("", { html: "This may be caused by an edit conflict or other " + "intermittent problem. Try refreshing the page. If the error " + "persists, you can " + contact.prop("outerHTML") + " or " + file_bug.prop("outerHTML") + "." })); elem.insertAfter(this.box.find("h5")); this._block_submit("error"); }; TFD.prototype._get_discussion_page = function() { var url = this.head.find(".mw-editsection a").first().prop("href"); var match = url.match(/title=(.*?)(\&|$)/); return match ? match[1] : null; }; TFD.prototype._get_section_number = function() { var url = this.head.find(".mw-editsection a").first().prop("href"); var match = url.match(/section=(.*?)(\&|$)/); return match ? match[1] : null; }; TFD.prototype._with_content = function(callback) { var title = this._get_discussion_page(), section = this._get_section_number(); if (title === null || section === null) return this._error("couldn't find discussion section"); if (this._wikitext !== undefined) { if (this._wikitext === null) this._wikitext_callbacks.push(callback); else callback.call(this, this._wikitext); return; } this._wikitext = null; TFDClerk.api_get(this, { action: "query", prop: "revisions", rvprop: "content", indexpageids: "", rvsection: section, titles: title }, function(data) { var pageid = data.query.pageids[0]; var content = data.query.pages[pageid].revisions[0]["*"]; this._wikitext = content; callback.call(this, content); for (var i in this._wikitext_callbacks) this._wikitext_callbacks[i].call(this, content); }, function(error) { this._error("API query failure", error); }, function() { this._wikitext_callbacks = []; }); }; TFD.prototype._remove_option_box = function() { this.box.remove(); this.box = null; this._guard = false; this._submit_blockers = []; }; TFD.prototype._add_option_box = function(verb, title, callback, options) { var self = this; this.box = $("
", { id: "tfdclerk-" + verb + "-box-" + this.id, addClass: "tfdclerk-" + verb + "-box" }) .css("position", "relative") .css("border", "1px solid #AAA") .css("color", "#000") .css("background-color", "#F9F9F9") .css("margin", "0.5em 0") .css("padding", "1em") .append($("
") .css("position", "absolute") .css("right", "1em") .css("top", "0.5em") .css("font-size", "75%") .css("color", "#777") .append($("", { href: TFDClerk.script_url, title: "tfdclerk.js", text: "tfdclerk.js" })) .append($("", {text: " version " + TFDClerk.version}))) .append($("
", { text: title, style: "margin: 0; padding: 0 0 0.25em 0;" })); options.call(this); this.box.append($("