User:Proteins/hottables.js

// // Inspired by HotCat, this script tries to make table editing easy. // After clicking once, it adds a "(+)" symbol before every paragraph and "(±)(-)" symbols to each existing image

// This script should have two states: add table symbols, or remove them. Clicking twice should not add the table symbols twice. // Global variables

var show_HotTables = false; var hot_table_string = "tables";

function hotTablePopup(arg_num_rows, arg_num_columns) { window.alert("Once this function is written, you'll enter a " + arg_num_rows + "x" + arg_num_columns + " table here\n"); } // closes hotTablePopup

function hotTables { var alert_string = ""; var on_main_page = false; var using_Internet_Explorer = false;

var hot_node; var hot_link; var hot_text; var hot_typeface; var num_hot_links_added = 0;

var anchor_index = 0; var num_H2_anchors = 0;

var anchor_level = 0; var prev_anchor_level = 1; //begin at the H1 heading var read_entire_article = true; var anchors; var temp_anchor; var num_anchors = 0; var temp_anchor_name = ""; var section_name = "lead section";

var parent_node; var sibling_node; var next_sibling_node;

var cutoff_anchor_index = 0; var cutoff_H2_anchor_index = 0;

var num_rows; var num_columns;

var hot_tables; var temp_hot_table; var num_hot_tables; var hot_table_index;

// See if state can be stored between clicks on the tab if (show_HotTables) { hot_tables = document.getElementsByName("hottable"); num_hot_tables = hot_tables.length; for (hot_table_index=0; hot_table_index<num_hot_tables; hot_table_index++) { temp_hot_table = hot_tables[hot_table_index]; parent_node = temp_hot_table.parentNode; if (!parent_node) { continue; } parent_node.removeChild(temp_hot_table); } // closes loop over HotTables hot_table_string = "no tables"; show_HotTables = false; return; } else { hot_table_string = "tables"; show_HotTables = true; }

// Check whether we're on the Main Page on_main_page = false; if (document.getElementById("mp-topbanner")) { on_main_page = true; window.alert("Sorry, this script is intended for regular articles, not the Main Page."); return; } // closes check whether we're on the Main Page

// check for Internet Explorer browser using_Internet_Explorer = false; if (navigator.userAgent.indexOf("MSIE") > -1) { using_Internet_Explorer = true; }

// Find the cutoff H2 anchor index, where we inserting hot-table tags alert_string = ""; num_H2_anchors = 0; num_hot_links_added = 0; section_name = "lead section"; prev_anchor_level = 1; //begin at the H1 heading read_entire_article = true; anchors = document.anchors; num_anchors = anchors.length; for (anchor_index=1; anchor_index<num_anchors; anchor_index++) { temp_anchor = anchors[anchor_index]; parent_node = temp_anchor.parentNode; if (!parent_node) { continue; } sibling_node = parent_node.nextSibling; if (!sibling_node) { continue; } // Check headings for jumps upwards in heading level anchor_level = 0; if (sibling_node.nodeName == "H1") { alert_string += " WARNING: Illegal H1 heading in this section\n"; } else if (sibling_node.nodeName == "H2") { anchor_level = 2; } else if (sibling_node.nodeName == "H3") { anchor_level = 3; } else if (sibling_node.nodeName == "H4") { anchor_level = 4; } else if (sibling_node.nodeName == "H5") { anchor_level = 5; } else { next_sibling_node = sibling_node.nextSibling; if (!next_sibling_node) { continue; } sibling_node = next_sibling_node;

// Check headings for jumps upwards in heading level if (next_sibling_node.nodeName == "H1") { alert_string += " WARNING: Illegal H1 heading in this section\n"; } else if (next_sibling_node.nodeName == "H2") { anchor_level = 2; } else if (next_sibling_node.nodeName == "H3") { anchor_level = 3; } else if (next_sibling_node.nodeName == "H4") { anchor_level = 4; } else if (next_sibling_node.nodeName == "H5") { anchor_level = 5; } else { continue; } } // closes assignment of the anchor level, if any

/*		if (((anchor_level - prev_anchor_level) > 1) && (prev_anchor_level != 0)) { if (num_H2_anchors == 0) { alert_string += " WARNING: H" + prev_anchor_level + " to H" + anchor_level + " jump in the lead\n"; } else { alert_string += " WARNING: H" + prev_anchor_level + " to H" + anchor_level + " jump in \"" + section_name.replace(/(_+)/ig, " ") + "\"\n"; }		}		if (anchor_level > 0) { prev_anchor_level = anchor_level; }

//Check major section headings for closing sections if (anchor_level == 2) { num_H2_anchors++; section_name = temp_anchor.name; temp_anchor_name = temp_anchor.name; alert_string += "Section " + num_H2_anchors + " : " + section_name.replace(/(_+)/ig, " ") + "\n"; temp_anchor_name = temp_anchor_name.replace(/:$/ig,""); // eliminate colons at end temp_anchor_name = temp_anchor_name.replace(/s$/ig,""); // eliminate plurals at end temp_anchor_name = temp_anchor_name.replace(/See_also/ig,""); temp_anchor_name = temp_anchor_name.replace(/Related_topic/ig,""); temp_anchor_name = temp_anchor_name.replace(/Related_article/ig,""); temp_anchor_name = temp_anchor_name.replace(/Further_reading/ig,""); temp_anchor_name = temp_anchor_name.replace(/External_link/ig,""); temp_anchor_name = temp_anchor_name.replace(/Footnote/ig,""); temp_anchor_name = temp_anchor_name.replace(/Note/ig,""); temp_anchor_name = temp_anchor_name.replace(/Reference/ig,""); temp_anchor_name = temp_anchor_name.replace(/Citation/ig,""); temp_anchor_name = temp_anchor_name.replace(/Source/ig,""); temp_anchor_name = temp_anchor_name.replace(/Link/ig,""); temp_anchor_name = temp_anchor_name.replace(/s([_\s]+)and([_\s]+)/ig,""); temp_anchor_name = temp_anchor_name.replace(/([_\s]+)and([_\s]+)/ig,""); temp_anchor_name = temp_anchor_name.replace(/([_\s]+)/ig,""); if (temp_anchor_name == "") { break; }

} // closes check for H2 anchor

if (anchor_level > 0) { next_sibling_node = sibling_node.nextSibling; if (!next_sibling_node) { continue; }

if (!using_Internet_Explorer) { next_sibling_node = next_sibling_node.nextSibling; if (!next_sibling_node) { continue; } }

// Skip over the boilerplate messages such as See also, Further information, Main article,... while (next_sibling_node.nodeName == "DL") { next_sibling_node = next_sibling_node.nextSibling; if (!next_sibling_node) { continue; } }			parent_node = next_sibling_node.parentNode; if (!parent_node) { continue; }

hot_text = document.createTextNode("(+) Add table here.");

hot_typeface = document.createElement("I"); hot_typeface.appendChild(hot_text);

hot_link = document.createElement("A"); hot_link.className = "hottable"; hot_link.title = "Click to add a table here."; hot_link.appendChild(hot_typeface); hot_link.onclick = function { num_rows = window.prompt("Number of table rows:",""); if (!num_rows) {return;} num_columns = window.prompt("Number of table columns:",""); if (!num_columns) {return;} hotTablePopup(num_rows,num_columns); return; }			parent_node.insertBefore(hot_link, next_sibling_node); num_hot_links_added++;

} // closes addition of the HotTable symbol } // closes loop over the anchors

cutoff_anchor_index = anchor_index; cutoff_H2_anchor_index = num_H2_anchors; if (cutoff_anchor_index < num_anchors) { read_entire_article = false; alert_string += "\nHot-table insertion will stop before the \"" + temp_anchor.name.replace(/(_+)/ig, " ") + "\" section.\n"; } else { read_entire_article = true; alert_string += "\nHot-table insertion will cover the entire article.\n"; } //	window.alert(alert_string);

// Acknowledgment alert_string = "Added " + num_hot_links_added + " symbols for new tables."; window.alert(alert_string); } // closes function hotTables $(function {            mw.util.addPortletLink('p-cactions', 'javascript:hotTables', hot_table_string, 'ca-hottables', 'Symbols for editing Tables', , ); }); //