dojo.provide("dojox.toc"); dojox.toc.create=function(/*Object*/args){ // summary: // Create a table of content // description: // Parse dom and find some elements. Convert them into anchor and create an ul/li with all anchor // For two or more arguments, acts as a setter. // args: // Arguments to method (all are optional) : // { // rootNode: root ofr the parsing. Default : dojo.body(), // classToIgnore: elements with a class in this array will be ignored. Default : [], // position: Position where ul will be append into appendNode. Default : "before", // appendNode: The ul will be append into this node. Default : dojo.body(), // "class": Class to add on ul. Default : "toc", // query: Element to search for. Default : "h1,h2", // stripHtml: If true, innerHTML of query elements will be stripped. Default : true // rootAnchor: text added before anchor index. Default : "dojox.toc." // } // example: // | dojox.toc({query:"h1,h2,h3",rootNode:dojo.byId("content")}); var def = { rootNode : dojo.body(), classToIgnore : [], position : "before", appendNode : dojo.body(), "class" : "toc", query : "h1,h2", stripHtml : true, rootAnchor : "dojox.toc." }; dojo.mixin(def,args); var hasTitle = false; var toc = dojo.create("ul", {"class" : def["class"]}); dojo.query(def.query, def.rootNode).sort(function(a,b){ if(a.sourceIndex){ return a.sourceIndex - b.sourceIndex; }else if(a.compareDocumentPosition){ return 3 - (a.compareDocumentPosition(b) & 6); }else{ return 0; } }).forEach(function(node,index){ for(var i in def.classToIgnore){ if(dojo.hasClass(node,def.classToIgnore[i])){ return; } } var content = node.innerHTML; hasTitle = true; node.innerHTML = ""; (toc.appendChild(dojo.create("li",{ "class" : node.tagName.toLowerCase() }))).appendChild( dojo.create("a",{ innerHTML : (!def.stripHtml ? content : content.replace(/(<([^>]+)>)/ig,"")), href : "#" + def.rootAnchor + index, "class" : node.tagName.toLowerCase() }) ); node.appendChild( dojo.create("a",{ innerHTML : content, name : def.rootAnchor + index }) ); }); if(!hasTitle){ dojo.destroy(toc); return false; } if(def.appendNode !== null){ dojo.place(toc,def.appendNode,def.position); } return toc; };