1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
|
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is TOCMaker.
*
* The Initial Developer of the Original Code is
* Daniel Glazman.
* Portions created by the Initial Developer are Copyright (C) 2002
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Daniel Glazman <daniel@glazman.org> (Original author)
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
// tocHeadersArray is the array containing the pairs tag/class
// defining TOC entries
var tocHeadersArray = new Array(6);
// a global used when building the TOC
var currentHeaderLevel = 0;
// a global set to true if the TOC is to be readonly
var readonly = false;
// a global set to true if user wants indexes in the TOC
var orderedList = true;
// constants
const kMozToc = "mozToc";
const kMozTocLength = 6;
const kMozTocIdPrefix = "mozTocId";
const kMozTocIdPrefixLength = 8;
const kMozTocClassPrefix = "mozToc";
const kMozTocClassPrefixLength = 6;
// Startup() is called when EdInsertTOC.xul is opened
function Startup()
{
// early way out if if we have no editor
if (!GetCurrentEditor()) {
window.close();
return;
}
var i, j;
// clean the table of tag/class pairs we look for
for (i = 0; i < 6; ++i)
tocHeadersArray[i] = [ "", "" ];
// reset all settings
for (i = 1; i < 7; ++i) {
var menulist = document.getElementById("header" + i + "Menulist");
var menuitem = document.getElementById("header" + i + "none");
var textbox = document.getElementById("header" + i + "Class");
menulist.selectedItem = menuitem;
textbox.setAttribute("disabled", "true");
}
var theDocument = GetCurrentEditor().document;
// do we already have a TOC in the document ? It should have "mozToc" ID
var toc = theDocument.getElementById(kMozToc);
// default TOC definition, use h1-h6 for TOC entry levels 1-6
var headers = "h1 1 h2 2 h3 3 h4 4 h5 5 h6 6";
var orderedListCheckbox = document.getElementById("orderedListCheckbox");
orderedListCheckbox.checked = true;
if (toc) {
// man, there is already a TOC here
if (toc.getAttribute("class") == "readonly") {
// and it's readonly
var checkbox = document.getElementById("readOnlyCheckbox");
checkbox.checked = true;
readonly = true;
}
// let's see if it's an OL or an UL
orderedList = (toc.nodeName.toLowerCase() == "ol");
orderedListCheckbox.checked = orderedList;
var nodeList = toc.childNodes;
// let's look at the children of the TOC ; if we find a comment beginning
// with "mozToc", it contains the TOC definition
for (i = 0; i< nodeList.length; ++i) {
if (nodeList.item(i).nodeType == Node.COMMENT_NODE &&
nodeList.item(i).data.substr(0, kMozTocLength) == kMozToc) {
// yep, there is already a definition here; parse it !
headers = nodeList.item(i).data.substr(kMozTocLength + 1,
nodeList.item(i).length - kMozTocLength - 1);
break;
}
}
}
// let's get an array filled with the (tag.class, index level) pairs
var headersArray = headers.split(" ");
for (i = 0; i < headersArray.length; i += 2) {
var tag = headersArray[i], className = "";
var index = headersArray[i + 1];
menulist = document.getElementById("header" + index + "Menulist");
if (menulist) {
var sep = tag.indexOf(".");
if (sep != -1) {
// the tag variable contains in fact "tag.className", let's parse
// the class and get the real tag name
var tmp = tag.substr(0, sep);
className = tag.substr(sep + 1, tag.length - sep - 1);
tag = tmp;
}
// update the dialog
menuitem = document.getElementById("header" + index +
tag.toUpperCase());
textbox = document.getElementById("header" + index + "Class");
menulist.selectedItem = menuitem;
if (tag != "") {
textbox.removeAttribute("disabled");
}
if (className != "") {
textbox.value = className;
}
tocHeadersArray[index - 1] = [ tag, className ];
}
}
}
function BuildTOC(update)
{
// controlClass() is a node filter that accepts a node if
// (a) we don't look for a class (b) we look for a class and
// node has it
function controlClass(node, index)
{
currentHeaderLevel = index + 1;
if (tocHeadersArray[index][1] == "") {
// we are not looking for a specific class, this node is ok
return NodeFilter.FILTER_ACCEPT;
}
if (node.getAttribute("class")) {
// yep, we look for a class, let's look at all the classes
// the node has
var classArray = node.getAttribute("class").split(" ");
for (var j = 0; j < classArray.length; j++) {
if (classArray[j] == tocHeadersArray[index][1]) {
// hehe, we found it...
return NodeFilter.FILTER_ACCEPT;
}
}
}
return NodeFilter.FILTER_SKIP;
}
// the main node filter for our node iterator
// it selects the tag names as specified in the dialog
// then calls the controlClass filter above
function acceptNode(node)
{
switch (node.nodeName.toLowerCase())
{
case tocHeadersArray[0][0]:
return controlClass(node, 0);
break;
case tocHeadersArray[1][0]:
return controlClass(node, 1);
break;
case tocHeadersArray[2][0]:
return controlClass(node, 2);
break;
case tocHeadersArray[3][0]:
return controlClass(node, 3);
break;
case tocHeadersArray[4][0]:
return controlClass(node, 4);
break;
case tocHeadersArray[5][0]:
return controlClass(node, 5);
break;
default:
return NodeFilter.FILTER_SKIP;
break;
}
return NodeFilter.FILTER_SKIP; // placate the js compiler
}
var editor = GetCurrentEditor();
var theDocument = editor.document;
// let's create a TreeWalker to look for our nodes
var treeWalker = theDocument.createTreeWalker(theDocument.documentElement,
NodeFilter.SHOW_ELEMENT,
acceptNode,
true);
// we need an array to store all TOC entries we find in the document
var tocArray = new Array();
if (treeWalker) {
var tocSourceNode = treeWalker.nextNode();
while (tocSourceNode) {
var headerIndex = currentHeaderLevel;
// we have a node, we need to get all its textual contents
var textTreeWalker = theDocument.createTreeWalker(tocSourceNode,
NodeFilter.SHOW_TEXT,
null,
true);
var textNode = textTreeWalker.nextNode(), headerText = "";
while (textNode) {
headerText += textNode.data;
textNode = textTreeWalker.nextNode();
}
var anchor = tocSourceNode.firstChild, id;
// do we have a named anchor as 1st child of our node ?
if (anchor.nodeName.toLowerCase() == "a" &&
anchor.hasAttribute("name") &&
anchor.getAttribute("name").substr(0, kMozTocIdPrefixLength) == kMozTocIdPrefix) {
// yep, get its name
id = anchor.getAttribute("name");
}
else {
// no we don't and we need to create one
anchor = theDocument.createElement("a");
tocSourceNode.insertBefore(anchor, tocSourceNode.firstChild);
// let's give it a random ID
var c = 1000000 * Math.random();
id = kMozTocIdPrefix + Math.round(c);
anchor.setAttribute("name", id);
anchor.setAttribute("class", kMozTocClassPrefix +
tocSourceNode.nodeName.toUpperCase());
}
// and store that new entry in our array
tocArray.push(headerIndex, headerText, id);
tocSourceNode = treeWalker.nextNode();
}
}
/* generate the TOC itself */
headerIndex = 0;
var item, toc;
for (var i = 0; i < tocArray.length; i += 3) {
if (!headerIndex) {
// do we need to create an ol/ul container for the first entry ?
++headerIndex;
toc = theDocument.getElementById(kMozToc);
if (!toc || !update) {
// we need to create a list container for the table of contents
toc = GetCurrentEditor().createElementWithDefaults(orderedList ? "ol" : "ul");
// grrr, we need to create a LI inside the list otherwise
// Composer will refuse an empty list and will remove it !
var pit = theDocument.createElement("li");
toc.appendChild(pit);
GetCurrentEditor().insertElementAtSelection(toc, true);
// ah, now it's inserted so let's remove the useless list item...
toc.removeChild(pit);
// we need to recognize later that this list is our TOC
toc.setAttribute("id", kMozToc);
}
else {
// we have to update an existing TOC, is the existing TOC of the
// desired type (ordered or not) ?
if (orderedList != (toc.nodeName.toLowerCase() == "ol")) {
// nope, we have to recreate the list
var newToc = GetCurrentEditor().createElementWithDefaults(orderedList ? "ol" : "ul");
toc.parentNode.insertBefore(newToc, toc);
// and remove the old one
toc.parentNode.removeChild(toc);
toc = newToc;
toc.setAttribute("id", kMozToc);
}
else {
// we can keep the list itself but let's get rid of the TOC entries
while (toc.hasChildNodes())
toc.removeChild(toc.lastChild);
}
}
var commentText = "mozToc ";
for (var j = 0; j < 6; j++) {
if (tocHeadersArray[j][0] != "") {
commentText += tocHeadersArray[j][0];
if (tocHeadersArray[j][1] != "") {
commentText += "." + tocHeadersArray[j][1];
}
commentText += " " + (j + 1) + " ";
}
}
// important, we have to remove trailing spaces
commentText = TrimStringRight(commentText);
// forge a comment we'll insert in the TOC ; that comment will hold
// the TOC definition for us
var ct = theDocument.createComment(commentText);
toc.appendChild(ct);
// assign a special class to the TOC top element if the TOC is readonly
// the definition of this class is in EditorOverride.css
if (readonly) {
toc.setAttribute("class", "readonly");
}
else {
toc.removeAttribute("class");
}
// We need a new variable to hold the local ul/ol container
// The toplevel TOC element is not the parent element of a
// TOC entry if its depth is > 1...
var tocList = toc;
// create a list item
var tocItem = theDocument.createElement("li");
// and an anchor in this list item
var tocAnchor = theDocument.createElement("a");
// make it target the source of the TOC entry
tocAnchor.setAttribute("href", "#" + tocArray[i + 2]);
// and put the textual contents of the TOC entry in that anchor
var tocEntry = theDocument.createTextNode(tocArray[i + 1]);
// now, insert everything where it has to be inserted
tocAnchor.appendChild(tocEntry);
tocItem.appendChild(tocAnchor);
tocList.appendChild(tocItem);
item = tocList;
}
else {
if (tocArray[i] < headerIndex) {
// if the depth of the new TOC entry is less than the depth of the
// last entry we created, find the good ul/ol ancestor
for (j = headerIndex - tocArray[i]; j > 0; --j) {
if (item != toc) {
item = item.parentNode.parentNode;
}
}
tocItem = theDocument.createElement("li");
}
else if (tocArray[i] > headerIndex) {
// to the contrary, it's deeper than the last one
// we need to create sub ul/ol's and li's
for (j = tocArray[i] - headerIndex; j > 0; --j) {
tocList = theDocument.createElement(orderedList ? "ol" : "ul");
item.lastChild.appendChild(tocList);
tocItem = theDocument.createElement("li");
tocList.appendChild(tocItem);
item = tocList;
}
}
else {
tocItem = theDocument.createElement("li");
}
tocAnchor = theDocument.createElement("a");
tocAnchor.setAttribute("href", "#" + tocArray[i + 2]);
tocEntry = theDocument.createTextNode(tocArray[i + 1]);
tocAnchor.appendChild(tocEntry);
tocItem.appendChild(tocAnchor);
item.appendChild(tocItem);
headerIndex = tocArray[i];
}
}
SaveWindowLocation();
return true;
}
function selectHeader(elt, index)
{
var tag = elt.value;
tocHeadersArray[index - 1][0] = tag;
var textbox = document.getElementById("header" + index + "Class");
if (tag == "") {
textbox.setAttribute("disabled", "true");
}
else {
textbox.removeAttribute("disabled");
}
}
function changeClass(elt, index)
{
tocHeadersArray[index - 1][1] = elt.value;
}
function ToggleReadOnlyToc(elt)
{
readonly = elt.checked;
}
function ToggleOrderedList(elt)
{
orderedList = elt.checked;
}
|