MediaWiki:Gadget-wkcategorize.js

var cg = { version:  2.06, require:  2.64, textspan: "WKCGSpan", editspan: "WKCGFormSpanEdit", statspan: "WKCGStatSpan", formspan: "WKCGFormSpan" } cg.tags = ""; cg.notes = ""; cg.subjects = new Array( ["opt",                     "Computing"],  ["Software (general)",      "Computer software"],  ["Programming Language",    "Programming language"],  ["Business Software",       "Computer software"],  ["Computer Science",        "Computer science"],  ["Operating Systems",       "Computer software"],  ["Computer Graphics",       "Computer science"],  ["Information Technology",  "Information technology"],  ["Meta Languages",          "Domain-specific languages"],  ["opt",                     "Engineering"],  ["Engineering (general)",   "Engineering"],  ["Electrical Engineering",  "Electrical Engineering"],  ["Computer Engineering",    "Computer Engineering"],  ["Elec / Comp Engineering", "Electrical Engineering|Computer Engineering"],  ["Mechanical Engineering",  "Mechanical Engineering"],  ["Civil Engineering",       "Civil Engineering"],  ["Chemical Engineering",    "Chemical Engineering"], ["opt",                    "Science and Technology"], ["Science (general)",      "Science"], ["Physics",                "Physics"], ["Medicine",               "Health science"], ["Technology",             "Technology"], ["Biology",                "Biology"], ["Life Science",           "Life Science"], ["Mathematics",            "Mathematics"], ["Chemistry",              "Chemistry"], ["Earth Sciences",         "Earth Sciences"], ["Environmental Science",  "Environmental Science"], ["opt",                    "Arts and Humanities"], ["Art",                    "Arts"], ["Music",                  "Music"], ["Architecture",           "Architecture"], ["Education",              "Education"], ["Languages",              "Languages"], ["English and Literature", "Language and literature"], ["Philosophy",             "Philosophy"], ["Psychology",             "Social Sciences"], ["Religion",               "Religion"], ["Geography",              "Humanities"], ["opt",                    "Social sciences"], ["History (general)",      "History"], ["History (usa)",          "History"], ["Business and Economics", "Business and economics"], ["Law",                    "Law"], ["Social Science",         "Social sciences"], ["Agriculture",            "Social sciences"], ["Political Science",      "Political Science"], ["Military Science",       "Military science"], ["Naval Science",          "Naval science"], ["opt",                    "Miscellaneous"], ["Games and Athletics",    "Games and athletics"], ["How To",                 "How-tos"], ["Study Guides",           "Study guides"], ["Help",                   "Help"], ["Misc",                   "Miscellaneous"] );

cg._form = null; cg.getForm = function { if(cg._form == null) cg.createForm; return cg._form; } cg.bookName = ""; cg.getBookName = function { cg.bookName = cg._booknameB.value; return cg.bookName; }

cg.makeOptgroup = function (parent, i) { for(i < cg.subjects.length; i++) { if(cg.subjects[i][0] == "opt") return i - 1; else { parent.appendChild(wk.makeElement("option", {value:i}, cg.subjects[i][0])); } }  return i; }

cg.makeSubjectSelect = function(num, multi) { var s = (num == null)?(1):(num); cg._subjectB = wk.makeElement("select", {id:"WKCGSubjectB", size:s, name:"WKCGSubjectB"}); if(multi) { cg._subjectB.multiple = true; } for(i = 0; i < cg.subjects.length; i++) { if(cg.subjects[i][0] == "opt") { var node = wk.makeElement("optgroup", {label:cg.subjects[i][1]}); i = cg.makeOptgroup(node, i + 1); cg._subjectB.appendChild(node); } else { var node = wk.makeElement("option", {value:i}, subjects[i][0]); cg._subjectB.appendChild(node); } }  return cg._subjectB; }

cg.writeCats = function (subject, bookname) { var temp = "{" + "{Subject|" + subject[1] + "}}\n"; if(bookname == "") temp += "{" + "{Alphabetical}}\n"; else temp += "{" + "{Alphabetical|" + bookname.substr(0, 1) + "}}"; return temp; }

cg.createForm = function { var div = wk.spanText(cg.formspan, ""); if(div == null) return; cg._displayB = wk.makeButton("WKCGDisplayB",   "Display",    cg.display); cg._editB = wk.makeButton("WKCGEditB",         "Edit",       cg.edit); cg._booknameB = wk.makeInput("WKCGBookNameB", "") cg._form = wk.makeElement("form", {name:"WKCGFormB", method:"GET"}, [   cg._booknameB,    " ",    cg.makeSubjectSelect,    wk.makeButton("WKCGCategorizeB", "Categorize", cg.categorize),    " ",    cg._displayB,    cg._editB,    wk.makeButton("WKCGClearB",      "Clear",      cg.clear)  ]); wk.toggleDisable(cg._displayB, true); wk.toggleDisable(cg._editB, true); div.appendChild(cg._form); } if(typeof wk == 'object' && wk.testVersion(cg.require)) { addOnloadHook(cg.createForm); }

cg.clear = function { wk.toggleDisable(cg._displayB, true); wk.toggleDisable(cg._editB,   true); wk.spanText(cg.textspan, ""); wk.spanText(cg.statspan, ""); }

cg.categorize = function { cg.clear; cg.getBookName; var form = cg.getForm; var select = cg._subjectB; var index = select.options[select.selectedIndex].value; var subject = cg.subjects[index]; cg.tags = cg.writeCats(subject, cg.bookName); cg.notes = "Notes: " + "These are the templates necessary to categorize your book as being \"" +    "" + subject[0] + "\""; cg.notes = cg.notes + ""; wk.toggleDisable(cg._displayB, false); wk.toggleDisable(cg._editB, false); wk.spanText(cg.statspan, "Created categories for " +   ((cg.bookName == "")?("Unknown Book"):(wk.wikiLinkText(cg.bookName))) +    ". Click Display to see the created category templates. " +    "Click Edit to display an edit window and save changes."); }

cg.display = function { wk.spanText(cg.textspan,    " "); if(cg.getBookName == "") { wk.spanText(cg.statspan,      "These are the categorization templates for your book. " +      "Copy and Paste these templates into your book."); } else { wk.spanText(cg.statspan,      "These are the categorization templates for " +      wk.wikiLinkText(cg.bookName) +      ". Copy and Paste these templates into your book, or click " +      "Edit to display an edit window and save these changes."); } }

cg.edit = function { if(cg.getBookName == "") { setSpanText(cg.statspan, "You must enter a valid book name to Edit"); return; } wk.spanText(cg.statspan, "Loading edit window..."); wk.loadEditPage(cg.bookName, function(xml) {   var editform = wk.showEditWindow(xml, cg.textspan);    var temp = editform.text.value;    if(temp.length < 20) {      wk.spanText(cg.statspan, "The page " +        wk.wikiLinkText(cg.bookName) +        " does not exist or is not a proper book table of contents (TOC). " + "You should only categorize proper book pages or " + "TOCs. Double-check the spelling of the title, if the page is supposed " + "to exist. If the book does not yet exist, create it first with Whiteknight's " + wk.wikiLinkText("User:Whiteknight/Book Designer Gadget", "Book Designer Gadget") + ".");     wk.spanText(cg.textspan, "");      return;    }    editform.summary.value = cg.bookName +       ": Autocategorizing using Whiteknight's Categorization Gadget";    temp = temp.replace(/\{\{cleanup-link\}\}\n*/gi, "");    temp = temp.replace(/\{\{(alphabetical|DDC|LOC|Shelf)([^\}])*\}\}/gi, "");    temp = temp.replace(/\{\{(software|programming language|business software|computer science|operating systems|computer graphics)\}\}/gi, "");    temp = temp.replace(/\n\n\n+/g, "\n\n");    editform.text.value = temp + "\n" + cg.tags;    wk.spanText(cg.statspan, "You are editing the text for " +     wk.wikiLinkText(cg.bookName) +      ". The categorization templates have been appended to the end of the page. " + "Make sure that the text is correct, and then click Save Page." ); }); }

cg.createForm2 = function { var div = document.getElementById(cg.editspan); if(div == null) return; wk.toggleDisplay(div, "block"); cg.bookName = div.innerHTML; cg._booknameB = wk.makeElement("input", {type:"hidden", id:"WKCGBookNameB", value:cg.bookName}) cg._form = wk.makeElement("form", {name:"WKCGFormB", method:"GET"}, [   cg._booknameB,    cg.makeSubjectSelect,    wk.makeButton("WKCGCategorizeB", "Categorize", cg.categorize2)  ]); div.appendChild(cg._form); } if(typeof wk == "object" && wk.testVersion(cg.require)) { addOnloadHook(cg.createForm2); }

cg.categorize2 = function { cg.getBookName; var select = cg._subjectB; var index = select.options[select.selectedIndex].value; tags = cg.writeCats(cg.subjects[index], cg.bookName); var editwin = document.getElementById("wpTextbox1"); if(editwin) { var temp = editwin.value; temp = temp.replace(/\{\{(Alphabetical|DDC|LOC|UDC|Shelf|Subject)([^\}])*\}\}/gi, ""); temp = temp.replace(/\{\{(software|programming language|business software|computer science|operating systems|computer graphics|engineering)\}\}/gi, ""); temp = temp.replace(/\n\n\n+/g, "\n\n"); editwin.value = temp + "\n" + tags; } }

cg.returnAllSelected = function { var subjects = cg._subjectB; var opts = new Array; for(var i = 0; i < subjects.options.length; i++) { if(subjects.options[i].selected) { var index = subjects.options[i].value; opts.push(cg.subjects[index][1]); } }  return opts; }

cg.getItemIndex = function (item) { for(var i = 0; i < cg.subjects.length; i++) { if(cg.subjects[i][0] == 'opt') continue; if(cg.subjects[i][0] == item) return i;   if(cg.subjects[i][1] == item) return i;  } return 0; }

cg.setItemSelected = function(value) { for(var i = 0; i < cg._subjectB.options.length; i++) { if(cg._subjectB.options[i].value == value) { cg._subjectB.options[i].selected = true; return i;   } } return 0; }

cg.setSelected = function(opts) { var subjects = cg._subjectB; var index = 0; for(var i = 0; i < opts.length; i++) { if(typeof opts[i] == 'string') { index = cg.getItemIndex(opts[i]); } else { index = opts[i]; }        cg.setItemSelected(index); } }