A Wikipedia user script to automate common TfD operations
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

tfdclerk.js 6.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Templates for discussion clerk script by [[User:The Earwig]]
  2. // Development and bug reports: https://github.com/earwig/tfdclerk
  3. // To install, add:
  4. // importScript("User:The Earwig/tfdclerk.js"); // [[User:The Earwig/tfdclerk.js]]
  5. // to [[Special:MyPage/common.js]] or [[Special:MyPage/skin.js]]
  6. /*
  7. Copyright (C) 2015 Ben Kurtovic <ben.kurtovic@gmail.com>
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights to
  11. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  12. of the Software, and to permit persons to whom the Software is furnished to do
  13. so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in all
  15. copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. SOFTWARE.
  23. */
  24. mw.loader.using(["mediawiki.ui", "jquery.ui.core"], function() {
  25. if (mw.config.get("wgNamespaceNumber") != 4 || (
  26. mw.config.get("wgTitle") != "Templates for discussion" &&
  27. mw.config.get("wgTitle").indexOf("Templates for discussion/Log/2") != 0))
  28. return;
  29. TFDClerk = {
  30. sysop: $.inArray("sysop", mw.config.get("wgUserGroups")) >= 0
  31. };
  32. TFDClerk._get_today = function() {
  33. return new Date().toISOString().slice(0, 10);
  34. };
  35. TFDClerk._guard = function(head) {
  36. if (head.data("guard"))
  37. return false;
  38. head.data("guard", true);
  39. return true;
  40. };
  41. TFDClerk._unguard = function(head) {
  42. head.removeData("guard");
  43. };
  44. TFDClerk._remove_option_box = function(box) {
  45. var head = box.prev("h4");
  46. box.remove();
  47. TFDClerk._unguard(head);
  48. };
  49. TFDClerk._add_option_box = function(head, verb, title, callback, options) {
  50. var box = $("<div/>", {
  51. id: "tfdclerk-" + verb + "-box-" + head.uniqueId().prop("id"),
  52. addClass: "tfdclerk-" + verb + "-box"
  53. })
  54. .css("border", "1px solid #AAA")
  55. .css("background-color", "#F9F9F9")
  56. .css("margin", "0.5em 0")
  57. .css("padding", "1em")
  58. .append($("<h5/>", {text: title, style: "padding-top: 0;"}));
  59. options(box);
  60. box.append($("<button/>", {
  61. text: verb.charAt(0).toUpperCase() + verb.slice(1),
  62. addClass: "mw-ui-button mw-ui-progressive",
  63. click: function() {
  64. callback(head, box);
  65. }
  66. }))
  67. .append($("<button/>", {
  68. text: "Cancel",
  69. addClass: "mw-ui-button",
  70. click: function() {
  71. TFDClerk._remove_option_box(box);
  72. }
  73. }))
  74. .insertAfter(head);
  75. };
  76. TFDClerk._add_option_table = function(box, options) {
  77. var table = $("<table/>");
  78. $.each(options, function(i, opt) {
  79. opt.attrs.id = box.prop("id") + "-" + opt.id;
  80. table.append(
  81. $("<tr/>").append(
  82. $("<td/>").append(
  83. $("<label/>", {
  84. for: opt.attrs.id,
  85. text: opt.label + ":"
  86. })))
  87. .append($("<td/>").append($(opt.tag, opt.attrs)))
  88. );
  89. });
  90. box.append(table);
  91. };
  92. TFDClerk._do_close = function(head, box) {
  93. // TODO
  94. TFDClerk._remove_option_box(box);
  95. };
  96. TFDClerk._do_relist = function(head, box) {
  97. // TODO
  98. TFDClerk._remove_option_box(box);
  99. };
  100. TFDClerk.close = function(head) {
  101. if (!TFDClerk._guard(head))
  102. return;
  103. TFDClerk._add_option_box(
  104. head, "close", "Closing discussion", TFDClerk._do_close,
  105. function(box) {
  106. // TODO
  107. });
  108. };
  109. TFDClerk.relist = function(head) {
  110. if (!TFDClerk._guard(head))
  111. return;
  112. TFDClerk._add_option_box(
  113. head, "relist", "Relisting discussion", TFDClerk._do_relist,
  114. function(box) {
  115. TFDClerk._add_option_table(box, [
  116. {
  117. id: "date",
  118. label: "New date",
  119. tag: "<input/>",
  120. attrs: {
  121. type: "date",
  122. value: TFDClerk._get_today()
  123. }
  124. },
  125. {
  126. id: "comments",
  127. label: "Comments",
  128. tag: "<textarea/>",
  129. attrs: {
  130. rows: 2,
  131. cols: 60,
  132. placeholder: "Optional. Do not sign."
  133. }
  134. }
  135. ]);
  136. });
  137. };
  138. TFDClerk._build_hook = function(head, verb, callback) {
  139. return $("<span/>", {style: "margin-left: 1em;"})
  140. .append($("<span/>", {addClass: "mw-editsection-bracket", text: "["}))
  141. .append($("<a/>", {
  142. href: "#",
  143. text: verb,
  144. title: "tfdclerk: " + verb + " discussion",
  145. click: function() {
  146. callback($(head));
  147. return false;
  148. }
  149. }))
  150. .append($("<span/>", {addClass: "mw-editsection-bracket", text: "]"}));
  151. };
  152. TFDClerk.install = function() {
  153. $("h4").each(function(i, head) {
  154. if ($(head).next().hasClass("tfd-closed"))
  155. return;
  156. $("<span/>", {addClass: "tfdclerk-hooks"})
  157. .append(TFDClerk._build_hook(head, "close", TFDClerk.close))
  158. .append(TFDClerk._build_hook(head, "relist", TFDClerk.relist))
  159. .appendTo($(head).find(".mw-editsection"));
  160. });
  161. };
  162. $(TFDClerk.install);
  163. });