diff --git a/tfdclerk.js b/tfdclerk.js index cbfeeb5..2016d9b 100644 --- a/tfdclerk.js +++ b/tfdclerk.js @@ -42,7 +42,7 @@ var is_tfd_page = function() { 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() { @@ -50,36 +50,57 @@ if (is_tfd_page()) { /* Main script starts here */ TFDClerk = { - api: new mw.Api(), sysop: $.inArray("sysop", mw.config.get("wgUserGroups")) >= 0, + tfds: [], + _api: new mw.Api() // TODO: access time? - _counter: 1, - _wikitext_cache: {}, - _wikitext_cache_extra: {} }; -TFDClerk._get_today = function() { - return new Date().toISOString().slice(0, 10); +TFD = function(id, head) { + this.id = id; + this.head = head; + this.box = null; + // TODO: pending conditions for submit button + this._guard = false; + this._wikitext = undefined; + this._wikitext_callbacks = []; }; -TFDClerk._guard = function(head) { - if (head.data("guard")) - return false; - head.data("guard", true); - return true; +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._unguard = function(head) { - head.removeData("guard"); -}; +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; -TFDClerk._get_section_number = function(head) { - var url = head.find(".mw-editsection a").first().prop("href"); - var match = url.match(/section=(.*?)(\&|$)/); - return match ? match[1] : null; + var tfd = new TFD(TFDClerk.tfds.length, head); + TFDClerk.tfds.push(tfd); + tfd.add_hooks(); + }); }; -TFDClerk._error = function(box, msg, extra) { +Date.prototype.toDatePickerFormat = function() { + return this.toISOString().slice(0, 10); +} + +TFD.prototype._error = function(msg, extra) { var elem = $("", { text: "Error: " + (extra ? msg + ": " : msg), style: "color: #A00;" @@ -89,62 +110,60 @@ TFDClerk._error = function(box, msg, extra) { text: extra, style: "font-family: monospace;" })); - elem.insertAfter(box.find("h5")); - box.find(".tfdclerk-submit").prop("disabled", true); + elem.insertAfter(this.box.find("h5")); + this.box.find(".tfdclerk-submit").prop("disabled", true); +}; + +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; }; -TFDClerk._with_section_content = function(head, box, callback) { - var section = TFDClerk._get_section_number(head); +TFD.prototype._with_content = function(callback) { + var section = this._get_section_number(); if (section === null) - return TFDClerk._error(box, "couldn't get section number"); - - var cache = TFDClerk._wikitext_cache, - extra_cache = TFDClerk._wikitext_cache_extra; - if (section in cache) { - if (cache[section] === null) { - if (section in extra_cache) - extra_cache[section].push(callback); - else - extra_cache[section] = [callback]; - } else - callback(cache[section]); + return this._error("couldn't get section number"); + + if (this._wikitext !== undefined) { + if (this._wikitext === null) + this._wikitext_callbacks.push(callback); + else + callback.call(this, this._wikitext); return; } - cache[section] = null; + this._wikitext = null; - TFDClerk.api.get({ + TFDClerk.api_get(this, { action: "query", prop: "revisions", rvprop: "content", rvsection: section, revids: mw.config.get("wgRevisionId") - }).done(function(data) { + }, function(data) { var pageid = mw.config.get("wgArticleId"); var content = data.query.pages[pageid].revisions[0]["*"]; - cache[section] = content; - callback(content); - if (section in extra_cache) { - for (var i in extra_cache[section]) - extra_cache[section][i](content); - } - }).fail(function(err) { - TFDClerk._error(box, "API query failure", err); - }).always(function() { - if (section in extra_cache) - delete extra_cache[section]; + 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 = []; }); }; -TFDClerk._remove_option_box = function(box) { - var head = box.prev("h4"); - box.remove(); - TFDClerk._unguard(head); +TFD.prototype._remove_option_box = function() { + this.box.remove(); + this.box = null; + this._guard = false; }; -TFDClerk._add_option_box = function(head, verb, title, callback, options) { - var box_id = "tfdclerk-" + verb + "-box-" + TFDClerk._counter++; - var box = $("
", { - id: box_id, +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("border", "1px solid #AAA") @@ -156,28 +175,23 @@ TFDClerk._add_option_box = function(head, verb, title, callback, options) { text: title, style: "margin: 0; padding: 0 0 0.25em 0;" })); - options(box, head, box_id + "-"); - box.append($("