OSDN Git Service

Version 0.6.5
[pettanr/clientJs.git] / 0.3.0 / wiki / wiki.js
1 /**
2  * @author Administrator
3  */
4 ( function(){
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'),
9                 titleArray = [],
10                 elmBuilder = document.createElement( 'div'),
11                 wikiPageClass = function( id, title, elmNavi, elmAnchor, elmWrapper, elmBody){
12                         var visible = false;
13                         titleArray.push( title);
14                         
15                         elmAnchor.onclick = jumpPage;
16                         
17                         return {
18                                 fixCommentToHtml: function(){
19                                         elmBuilder.innerHTML = elmBody.childNodes[0].nodeValue.replace( /\[\[BR\]\]/g, '');
20                                         cleanElement( elmBuilder);
21                                         moveElement( elmBody);
22                                         delete this.fixCommentToHtml;
23                                 },
24                                 buildInnerLink: function(){
25                                         var jumpArray = elmBody.getElementsByTagName( 'a'),
26                                                 l = jumpArray.length,
27                                                 _jump, _index;
28                                         for(var i=0; i<l; ++i){
29                                                 _jump = jumpArray[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';
35                                         }
36                                         delete this.buildInnerLink;
37                                 },
38                                 show: function( _id){
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);
44                                                 visible = true;
45                                         }
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);
51                                                 visible = false;
52                                         }
53                                 }
54                         }
55                 },
56                 wikiPageArray = ( function(){
57                         var ret = [],
58                                 _body = document.body,
59                                 _children = navi.getElementsByTagName( 'div'),
60                                 l = _children.length,
61                                 _child, _a, _title, _id, _wrap,
62                                 _page;
63                                 
64                         _body.appendChild( elmBuilder);
65
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();
75                                         ret.push( _page);
76                                 }
77                         }
78                         _body.removeChild( elmBuilder);
79                         
80                         return ret;
81                 })(),
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],
87                 _jump, i;
88
89         // 
90         if( !document.all){
91                 cleanElement( root);
92         }
93         root = navi = elmBuilder = null;
94
95         // build inner link
96         for( i=0; i<NUM_PAGE; ++i){
97                 wikiPageArray[i].buildInnerLink();
98         }
99         
100         // externalLink
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';
106                 }
107         }
108         jumpArray = _jump = null;
109         
110         // show current
111         setTimeout(
112                 function(){
113                         jumpPage( hash && hash.match( /page\d/) ? hash : 'page' +FRONT_INDEX);
114                         hash = null;
115                 }
116         );
117         
118         function moveElement( _targetElm){
119                 var _elms = _targetElm.childNodes;
120                 while( _elms.length > 0){
121                         _targetElm.removeChild( _elms[ 0]);
122                 }               
123                 
124                 _elms = elmBuilder.childNodes;
125                 while( _elms.length > 0){
126                         _targetElm.appendChild( _elms[0]);
127                 }
128                 cleanElement( elmBuilder);
129         }
130         function cleanElement( _targetElm){
131                 var l = CLEAN_TARGET_ELEMENT.length,
132                         elms, elm, i;
133                 for( i=0; i<l; ++i){
134                         elms = _targetElm.getElementsByTagName( CLEAN_TARGET_ELEMENT[ i]);
135                         while( elms.length > 0){
136                                 elm = elms[ 0];
137                                 elm.parentNode && elm.parentNode.removeChild( elm);
138                         }
139                 }
140                 if( !document.all) return;
141                 elms = _targetElm.getElementsByName( '*');
142                 l = elms.length;
143                 for(i=0; i<l; ++i){
144                         elm = elms[ i];
145                         elm.style.filter = elm.style.behavior = '';
146                 }
147         }
148         function getPageIndexByTitle( _title){
149                 for(var i=0; i<NUM_PAGE; ++i){
150                         if( titleArray[ i] === _title) return i;
151                 }
152                 return -1;
153         }
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);
158                 }
159                 return false;
160         }
161 })();
162
163
164
165 /* Smooth scrolling
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.
169    
170    sil, http://www.kryogenix.org/
171    
172    v1.0 2003-11-11
173    v1.1 2005-06-16 wrap it up in an object
174 */
175
176 var ss = {
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
189         // event handler
190         ss.addEvent(lnk,'click',ss.smoothScroll);
191       }
192     }
193   },
194
195   smoothScroll: function(e) {
196     // This is an event handler; get the clicked on element,
197     // in a cross-browser fashion
198     if (window.event) {
199       target = window.event.srcElement;
200     } else if (e) {
201       target = e.target;
202     } else return;
203
204     // Make sure that the target is an element, not a text node
205     // within an element
206     if (target.nodeName.toLowerCase() != 'a') {
207       target = target.parentNode;
208     }
209   
210     // Paranoia; check this is an A tag
211     if (target.nodeName.toLowerCase() != 'a') return;
212   
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;
223         break;
224       }
225     }
226     if (!destinationLink) destinationLink = document.getElementById(anchor);
227
228     // If we didn't find a destination, give up and let the browser do
229     // its thing
230     if (!destinationLink) return true;
231   
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;
241     }
242   
243     // Stop any current scrolling
244     clearInterval(ss.INTERVAL);
245   
246     cypos = ss.getCurrentYPos();
247   
248     ss_stepsize = parseInt((desty-cypos)/ss.STEPS);
249     ss.INTERVAL = setInterval('ss.scrollWindow('+ss_stepsize+','+desty+',"'+anchor+'")',10);
250
251     // And stop the actual click happening
252     if (window.event) {
253       window.event.cancelBubble = true;
254       window.event.returnValue = false;
255     }
256     if (e && e.preventDefault && e.stopPropagation) {
257       e.preventDefault();
258       e.stopPropagation();
259     }
260   },
261
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;
277     }
278   },
279
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;
287     return 0;
288   },
289
290   addEvent: function(elm, evType, fn, useCapture) {
291     // addEvent and removeEvent
292     // cross-browser event handling for IE5+,  NS6 and Mozilla
293     // By Scott Andrew
294     if (elm.addEventListener){
295       elm.addEventListener(evType, fn, useCapture);
296       return true;
297     } else if (elm.attachEvent){
298       var r = elm.attachEvent("on"+evType, fn);
299       return r;
300     } else {
301       alert("Handler could not be removed");
302     }
303   }
304 }
305
306 ss.STEPS = 25;
307
308 ss.addEvent(window,"load",ss.fixAllLinks);