2 * @author Administrator
5 var CLEAN_TARGET_ELEMENT = 'script,style,object,applet,embed,iframe,frame,base,bgsound,frameset,listing'.split( ','),
6 root = document.getElementById('wiki-container'),
7 navi = document.getElementById('page-navi-container'),
8 ELM_BODY_CONTAINER = document.getElementById('page-body-container'),
10 elmBuilder = document.createElement( 'div'),
11 wikiPageClass = function( id, title, elmNavi, elmAnchor, elmWrapper, elmBody){
13 titleArray.push( title);
15 elmAnchor.onclick = jumpPage;
18 fixCommentToHtml: function(){
19 elmBuilder.innerHTML = elmBody.childNodes[0].nodeValue.replace( /\[\[BR\]\]/g, '');
20 cleanElement( elmBuilder);
21 moveElement( elmBody);
22 delete this.fixCommentToHtml;
24 buildInnerLink: function(){
25 var jumpArray = elmBody.getElementsByTagName( 'a'),
28 for(var i=0; i<l; ++i){
30 _index = getPageIndexByTitle( _jump.childNodes[0].nodeValue);
31 if( _index === -1) continue;
32 _jump.href = '#page' +( _index +1);
33 _jump.onclick = jumpPage;
34 _jump.className += ' internalLink';
36 delete this.buildInnerLink;
39 if( id === _id && visible === false){
40 elmWrapper.className = 'page-wrapper-current';
41 elmWrapper.style.display = 'block';
42 elmNavi.className = 'page-navi-current';
43 ELM_BODY_CONTAINER.appendChild( elmWrapper);
46 if( id !== _id && visible === true){
47 elmWrapper.className = 'page-wrapper';
48 elmWrapper.style.display = '';
49 elmNavi.className = 'page-navi';
50 ELM_BODY_CONTAINER.removeChild( elmWrapper);
56 wikiPageArray = ( function(){
58 _body = document.body,
59 _children = navi.getElementsByTagName( 'div'),
61 _child, _a, _title, _id, _wrap,
64 _body.appendChild( elmBuilder);
66 for(var i=0; i<l; ++i){
67 _child = _children[i];
68 if( _child.className.indexOf( 'page-navi') !== -1){
69 _a = _child.getElementsByTagName( 'a')[0];
70 _title = _a ? _a.childNodes[0].nodeValue : null;
71 _id = _a ? _a.href.split('#')[1] : null;
72 _wrap = document.getElementById( _id);
73 _page = new wikiPageClass( _id, _title, _child, _a, _wrap, _wrap.getElementsByTagName( 'div')[1]);
74 !document.all && _page.fixCommentToHtml();
78 _body.removeChild( elmBuilder);
82 NUM_PAGE = wikiPageArray.length,
83 FRONT_ID = 'FrontPage',
84 FRONT_INDEX = getPageIndexByTitle( FRONT_ID) +1,
85 jumpArray = root.getElementsByTagName('a'),
86 hash = document.location.href.split('#')[1],
93 root = navi = elmBuilder = null;
96 for( i=0; i<NUM_PAGE; ++i){
97 wikiPageArray[i].buildInnerLink();
101 for( i=0, l = jumpArray.length; i<l; ++i){
102 _jump = jumpArray[i];
103 if( _jump.href.indexOf('#page') === -1 ){
104 _jump.target = '_blank';
105 _jump.className += ' externalLink';
108 jumpArray = _jump = null;
113 jumpPage( hash && hash.match( /page\d/) ? hash : 'page' +FRONT_INDEX);
118 function moveElement( _targetElm){
119 var _elms = _targetElm.childNodes;
120 while( _elms.length > 0){
121 _targetElm.removeChild( _elms[ 0]);
124 _elms = elmBuilder.childNodes;
125 while( _elms.length > 0){
126 _targetElm.appendChild( _elms[0]);
128 cleanElement( elmBuilder);
130 function cleanElement( _targetElm){
131 var l = CLEAN_TARGET_ELEMENT.length,
134 elms = _targetElm.getElementsByTagName( CLEAN_TARGET_ELEMENT[ i]);
135 while( elms.length > 0){
137 elm.parentNode && elm.parentNode.removeChild( elm);
140 if( !document.all) return;
141 elms = _targetElm.getElementsByName( '*');
145 elm.style.filter = elm.style.behavior = '';
148 function getPageIndexByTitle( _title){
149 for(var i=0; i<NUM_PAGE; ++i){
150 if( titleArray[ i] === _title) return i;
154 function jumpPage( id){
155 var id = typeof id === 'string' ? id : this.href.split('#')[1];
156 for(var i=0; i<NUM_PAGE; ++i){
157 wikiPageArray[i].show( id);
166 Changes links that link to other parts of this page to scroll
167 smoothly to those links rather than jump to them directly, which
168 can be a little disorienting.
170 sil, http://www.kryogenix.org/
173 v1.1 2005-06-16 wrap it up in an object
177 fixAllLinks: function() {
178 // Get a list of all links in the page
179 var allLinks = document.getElementsByTagName('a');
180 // Walk through the list
181 for (var i=0;i<allLinks.length;i++) {
182 var lnk = allLinks[i];
183 if ((lnk.href && lnk.href.indexOf('#') != -1) &&
184 ( (lnk.pathname == location.pathname) ||
185 ('/'+lnk.pathname == location.pathname) ) &&
186 (lnk.search == location.search)) {
187 // If the link is internal to the page (begins in #)
188 // then attach the smoothScroll function as an onclick
190 ss.addEvent(lnk,'click',ss.smoothScroll);
195 smoothScroll: function(e) {
196 // This is an event handler; get the clicked on element,
197 // in a cross-browser fashion
199 target = window.event.srcElement;
204 // Make sure that the target is an element, not a text node
206 if (target.nodeName.toLowerCase() != 'a') {
207 target = target.parentNode;
210 // Paranoia; check this is an A tag
211 if (target.nodeName.toLowerCase() != 'a') return;
213 // Find the <a name> tag corresponding to this href
214 // First strip off the hash (first character)
215 anchor = target.hash.substr(1);
216 // Now loop all A tags until we find one with that name
217 var allLinks = document.getElementsByTagName('a');
218 var destinationLink = null;
219 for (var i=0;i<allLinks.length;i++) {
220 var lnk = allLinks[i];
221 if (lnk.name && (lnk.name == anchor)) {
222 destinationLink = lnk;
226 if (!destinationLink) destinationLink = document.getElementById(anchor);
228 // If we didn't find a destination, give up and let the browser do
230 if (!destinationLink) return true;
232 // Find the destination's position
233 var destx = destinationLink.offsetLeft;
234 var desty = destinationLink.offsetTop;
235 var thisNode = destinationLink;
236 while (thisNode.offsetParent &&
237 (thisNode.offsetParent != document.body)) {
238 thisNode = thisNode.offsetParent;
239 destx += thisNode.offsetLeft;
240 desty += thisNode.offsetTop;
243 // Stop any current scrolling
244 clearInterval(ss.INTERVAL);
246 cypos = ss.getCurrentYPos();
248 ss_stepsize = parseInt((desty-cypos)/ss.STEPS);
249 ss.INTERVAL = setInterval('ss.scrollWindow('+ss_stepsize+','+desty+',"'+anchor+'")',10);
251 // And stop the actual click happening
253 window.event.cancelBubble = true;
254 window.event.returnValue = false;
256 if (e && e.preventDefault && e.stopPropagation) {
262 scrollWindow: function(scramount,dest,anchor) {
263 wascypos = ss.getCurrentYPos();
264 isAbove = (wascypos < dest);
265 window.scrollTo(0,wascypos + scramount);
266 iscypos = ss.getCurrentYPos();
267 isAboveNow = (iscypos < dest);
268 if ((isAbove != isAboveNow) || (wascypos == iscypos)) {
269 // if we've just scrolled past the destination, or
270 // we haven't moved from the lpageWrapperArraycroll (i.e., we're at the
271 // bottom of the page) then scroll exactly to the link
272 window.scrollTo(0,dest);
273 // cancel the repeating timer
274 clearInterval(ss.INTERVAL);
275 // and jump to the link directly so the URL's right
276 location.hash = anchor;
280 getCurrentYPos: function() {
281 if (document.body && document.body.scrollTop)
282 return document.body.scrollTop;
283 if (document.documentElement && document.documentElement.scrollTop)
284 return document.documentElement.scrollTop;
285 if (window.pageYOffset)
286 return window.pageYOffset;
290 addEvent: function(elm, evType, fn, useCapture) {
291 // addEvent and removeEvent
292 // cross-browser event handling for IE5+, NS6 and Mozilla
294 if (elm.addEventListener){
295 elm.addEventListener(evType, fn, useCapture);
297 } else if (elm.attachEvent){
298 var r = elm.attachEvent("on"+evType, fn);
301 alert("Handler could not be removed");
308 ss.addEvent(window,"load",ss.fixAllLinks);