|
1 gToc = null ; |
|
2 |
|
3 instanciateToc = function() { |
|
4 gToc = { |
|
5 'position':[CY.WidgetPositionExt.TL, CY.WidgetPositionExt.TL], |
|
6 'tocId':CY.guid(), |
|
7 'tocTitleId':CY.guid(), |
|
8 'closeBtnId':CY.guid() |
|
9 } ; |
|
10 |
|
11 var overlayHtml = {}; |
|
12 overlayHtml['headerContent'] = '<div id="' + gToc['tocId'] + '"><h3 id="' + gToc['tocTitleId'] + '"></h3>'; |
|
13 |
|
14 var toBeTOCced = getElementsByTagNames('h2,h3,h4,h5,h6', document.getElementById('maincontainer')); |
|
15 if (toBeTOCced.length < 2) return false; |
|
16 |
|
17 var content = document.createElement('div'); |
|
18 for (var i=0;i<toBeTOCced.length;i++) { |
|
19 var tmp = document.createElement('a'); |
|
20 tmp.innerHTML = toBeTOCced[i].innerHTML; |
|
21 tmp.className = 'page indent' + toBeTOCced[i].nodeName; |
|
22 content.appendChild(tmp); |
|
23 var headerId = toBeTOCced[i].id || 'link' + i; |
|
24 tmp.href = '#' + headerId; |
|
25 toBeTOCced[i].id = headerId; |
|
26 } |
|
27 overlayHtml['bodyContent'] = content.innerHTML; |
|
28 |
|
29 var width = gLayout.getTopICommentsWidth() ; |
|
30 |
|
31 var overlay = new CY.Overlay( { |
|
32 zIndex :3, |
|
33 shim :false, // until we really need it, no shim |
|
34 visible :false, |
|
35 headerContent :overlayHtml['headerContent'], |
|
36 bodyContent :overlayHtml['bodyContent'], |
|
37 xy :[10,10], |
|
38 width : width |
|
39 }); |
|
40 overlay.get('contentBox').addClass("c-toc") ; |
|
41 |
|
42 // attach to DOM |
|
43 overlay.render('#leftcolumn'); |
|
44 |
|
45 CY.get("#"+gToc['tocTitleId']).set('innerHTML', gettext('Table of contents')) ; |
|
46 |
|
47 gToc['overlay'] = overlay ; |
|
48 |
|
49 var animationHide = null ; |
|
50 animationHide = new CY.Anim({ |
|
51 node: overlay.get('boundingBox'), |
|
52 duration: .3, //gPrefs['general']['animduration'], |
|
53 easing: CY.Easing.easeOut |
|
54 }); |
|
55 gToc['animationHide'] = animationHide ; |
|
56 animationHide.set('to', { opacity: 0});// height : 0 doesn't work |
|
57 gToc['animationHide-handle'] = animationHide.on('end', onTocHideAnimEnd, gToc); |
|
58 |
|
59 var animationShow = null ; |
|
60 animationShow = new CY.Anim({ |
|
61 node: overlay.get('boundingBox'), |
|
62 duration: .3, //gPrefs['general']['animduration'], |
|
63 easing: CY.Easing.easeOut |
|
64 }); |
|
65 gToc['animationShow'] = animationShow ; |
|
66 animationShow.set('to', { opacity: 1}); |
|
67 gToc['animationShow-handle'] = animationShow.on('end', onTocShowAnimEnd, gToc); |
|
68 document.getElementsByClassName('c-toc')[0].style.width = width + 'px'; |
|
69 } |
|
70 |
|
71 toggleTocFn = function() { |
|
72 if (isTocVisible()) |
|
73 hideToc(); |
|
74 else |
|
75 showToc(); |
|
76 } |
|
77 |
|
78 hideToc = function() { |
|
79 gToc['overlay'].hide(); |
|
80 } |
|
81 |
|
82 onTocHideAnimEnd = function() { |
|
83 // iComment['overlay'].blur() ; |
|
84 this.overlay.hide() ; |
|
85 gSync.resume() ; |
|
86 } |
|
87 |
|
88 onTocShowAnimEnd = function() { |
|
89 gSync.resume() ; |
|
90 } |
|
91 |
|
92 showToc= function () { |
|
93 removeFormErrMsg(gToc['tocId']) ; |
|
94 gIComments.hide() ; |
|
95 positionToc() ; |
|
96 gToc['overlay'].show(); |
|
97 } |
|
98 |
|
99 isTocVisible = function () { |
|
100 if (gToc != null) |
|
101 return gToc['overlay'].get('visible') ; |
|
102 return false ; |
|
103 } |
|
104 |
|
105 positionToc = function () { |
|
106 if (gToc != null) { |
|
107 var overlay = gToc['overlay'] ; |
|
108 var boundingBox = overlay.get('boundingBox') ; |
|
109 |
|
110 var commentFormHeight = boundingBox.get('offsetHeight') ; |
|
111 var windowHeight = boundingBox.get('winHeight') ; |
|
112 |
|
113 var pos = gToc['position'] ; |
|
114 if (commentFormHeight > windowHeight) // trying to have save comment visible ... : |
|
115 pos = [CY.WidgetPositionExt.BL, CY.WidgetPositionExt.BL] ; |
|
116 |
|
117 overlay.set("align", {points:pos}); |
|
118 if (commentFormHeight <= windowHeight) |
|
119 overlay.set("y", overlay.get("y") + 30); |
|
120 boundingBox.setX(boundingBox.getX() + gConf['iCommentLeftPadding']); |
|
121 } |
|
122 } |
|
123 |
|
124 function getElementsByTagNames(list,obj) { |
|
125 if (!obj) var obj = document; |
|
126 var tagNames = list.split(','); |
|
127 var resultArray = new Array(); |
|
128 for (var i=0;i<tagNames.length;i++) { |
|
129 var tags = obj.getElementsByTagName(tagNames[i]); |
|
130 for (var j=0;j<tags.length;j++) { |
|
131 resultArray.push(tags[j]); |
|
132 } |
|
133 } |
|
134 var testNode = resultArray[0]; |
|
135 if (!testNode) return []; |
|
136 if (testNode.sourceIndex) { |
|
137 resultArray.sort(function (a,b) { |
|
138 return a.sourceIndex - b.sourceIndex; |
|
139 }); |
|
140 } |
|
141 else if (testNode.compareDocumentPosition) { |
|
142 resultArray.sort(function (a,b) { |
|
143 return 3 - (a.compareDocumentPosition(b) & 6); |
|
144 }); |
|
145 } |
|
146 return resultArray; |
|
147 } |