Mercurial > hg > vamp-website
comparison forum/Themes/default/scripts/stats.js @ 76:e3e11437ecea website
Add forum code
author | Chris Cannam |
---|---|
date | Sun, 07 Jul 2013 11:25:48 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
75:72f59aa7e503 | 76:e3e11437ecea |
---|---|
1 function smf_StatsCenter(oOptions) | |
2 { | |
3 this.opt = oOptions; | |
4 | |
5 this.oTable = null; | |
6 this.oYears = {}; | |
7 | |
8 this.bIsLoading = false; | |
9 | |
10 this.init(); | |
11 } | |
12 | |
13 smf_StatsCenter.prototype.init = function () | |
14 { | |
15 this.oTable = document.getElementById(this.opt.sTableId); | |
16 | |
17 // Is the table actually present? | |
18 if (typeof(this.oTable) != 'object') | |
19 return; | |
20 | |
21 // Find all months and years defined in the table. | |
22 var aRows = this.oTable.getElementsByTagName('tr'); | |
23 var aResults = []; | |
24 | |
25 var sYearId = null; | |
26 var oCurYear = null; | |
27 | |
28 var sMonthId = null; | |
29 var oCurMonth = null; | |
30 for (var i = 0, n = aRows.length; i < n; i++) | |
31 { | |
32 // Check if the current row represents a year. | |
33 if ((aResults = this.opt.reYearPattern.exec(aRows[i].id)) != null) | |
34 { | |
35 // The id is part of the pattern match. | |
36 sYearId = aResults[1]; | |
37 | |
38 // Setup the object that'll have the state information of the year. | |
39 this.oYears[sYearId] = { | |
40 oCollapseImage: document.getElementById(this.opt.sYearImageIdPrefix + sYearId), | |
41 oMonths: {} | |
42 }; | |
43 | |
44 // Create a shortcut, makes things more readable. | |
45 oCurYear = this.oYears[sYearId]; | |
46 | |
47 // Use the collapse image to determine the current state. | |
48 oCurYear.bIsCollapsed = oCurYear.oCollapseImage.src.indexOf(this.opt.sYearImageCollapsed) >= 0; | |
49 | |
50 // Setup the toggle element for the year. | |
51 oCurYear.oToggle = new smc_Toggle({ | |
52 bToggleEnabled: true, | |
53 bCurrentlyCollapsed: oCurYear.bIsCollapsed, | |
54 instanceRef: this, | |
55 sYearId: sYearId, | |
56 funcOnBeforeCollapse: function () { | |
57 this.opt.instanceRef.onBeforeCollapseYear(this); | |
58 }, | |
59 aSwappableContainers: [ | |
60 ], | |
61 aSwapImages: [ | |
62 { | |
63 sId: this.opt.sYearImageIdPrefix + sYearId, | |
64 srcExpanded: smf_images_url + '/' + this.opt.sYearImageExpanded, | |
65 altExpanded: '-', | |
66 srcCollapsed: smf_images_url + '/' + this.opt.sYearImageCollapsed, | |
67 altCollapsed: '+' | |
68 } | |
69 ], | |
70 aSwapLinks: [ | |
71 { | |
72 sId: this.opt.sYearLinkIdPrefix + sYearId, | |
73 msgExpanded: sYearId, | |
74 msgCollapsed: sYearId | |
75 } | |
76 ] | |
77 }); | |
78 } | |
79 | |
80 // Or maybe the current row represents a month. | |
81 else if ((aResults = this.opt.reMonthPattern.exec(aRows[i].id)) != null) | |
82 { | |
83 // Set the id to the matched pattern. | |
84 sMonthId = aResults[1]; | |
85 | |
86 // Initialize the month as a child object of the year. | |
87 oCurYear.oMonths[sMonthId] = { | |
88 oCollapseImage: document.getElementById(this.opt.sMonthImageIdPrefix + sMonthId) | |
89 }; | |
90 | |
91 // Create a shortcut to the current month. | |
92 oCurMonth = oCurYear.oMonths[sMonthId]; | |
93 | |
94 // Determine whether the month is currently collapsed or expanded.. | |
95 oCurMonth.bIsCollapsed = oCurMonth.oCollapseImage.src.indexOf(this.opt.sMonthImageCollapsed) >= 0; | |
96 | |
97 var sLinkText = getInnerHTML(document.getElementById(this.opt.sMonthLinkIdPrefix + sMonthId)); | |
98 | |
99 // Setup the toggle element for the month. | |
100 oCurMonth.oToggle = new smc_Toggle({ | |
101 bToggleEnabled: true, | |
102 bCurrentlyCollapsed: oCurMonth.bIsCollapsed, | |
103 instanceRef: this, | |
104 sMonthId: sMonthId, | |
105 funcOnBeforeCollapse: function () { | |
106 this.opt.instanceRef.onBeforeCollapseMonth(this); | |
107 }, | |
108 funcOnBeforeExpand: function () { | |
109 this.opt.instanceRef.onBeforeExpandMonth(this); | |
110 }, | |
111 aSwappableContainers: [ | |
112 ], | |
113 aSwapImages: [ | |
114 { | |
115 sId: this.opt.sMonthImageIdPrefix + sMonthId, | |
116 srcExpanded: smf_images_url + '/' + this.opt.sMonthImageExpanded, | |
117 altExpanded: '-', | |
118 srcCollapsed: smf_images_url + '/' + this.opt.sMonthImageCollapsed, | |
119 altCollapsed: '+' | |
120 } | |
121 ], | |
122 aSwapLinks: [ | |
123 { | |
124 sId: this.opt.sMonthLinkIdPrefix + sMonthId, | |
125 msgExpanded: sLinkText, | |
126 msgCollapsed: sLinkText | |
127 } | |
128 ] | |
129 }); | |
130 | |
131 oCurYear.oToggle.opt.aSwappableContainers[oCurYear.oToggle.opt.aSwappableContainers.length] = aRows[i].id; | |
132 } | |
133 | |
134 else if((aResults = this.opt.reDayPattern.exec(aRows[i].id)) != null) | |
135 { | |
136 oCurMonth.oToggle.opt.aSwappableContainers[oCurMonth.oToggle.opt.aSwappableContainers.length] = aRows[i].id; | |
137 oCurYear.oToggle.opt.aSwappableContainers[oCurYear.oToggle.opt.aSwappableContainers.length] = aRows[i].id; | |
138 } | |
139 } | |
140 | |
141 // Collapse all collapsed years! | |
142 for (i = 0; i < this.opt.aCollapsedYears.length; i++) | |
143 this.oYears[this.opt.aCollapsedYears[i]].oToggle.toggle(); | |
144 } | |
145 | |
146 smf_StatsCenter.prototype.onBeforeCollapseYear = function (oToggle) | |
147 { | |
148 // Tell SMF that all underlying months have disappeared. | |
149 for (var sMonth in this.oYears[oToggle.opt.sYearId].oMonths) | |
150 if (this.oYears[oToggle.opt.sYearId].oMonths[sMonth].oToggle.opt.aSwappableContainers.length > 0) | |
151 this.oYears[oToggle.opt.sYearId].oMonths[sMonth].oToggle.changeState(true); | |
152 } | |
153 | |
154 | |
155 smf_StatsCenter.prototype.onBeforeCollapseMonth = function (oToggle) | |
156 { | |
157 if (!oToggle.bCollapsed) | |
158 { | |
159 // Tell SMF that it the state has changed. | |
160 getXMLDocument(smf_prepareScriptUrl(smf_scripturl) + 'action=stats;collapse=' + oToggle.opt.sMonthId + ';xml'); | |
161 | |
162 // Remove the month rows from the year toggle. | |
163 var aNewContainers = []; | |
164 var oYearToggle = this.oYears[oToggle.opt.sMonthId.substr(0, 4)].oToggle; | |
165 | |
166 for (var i = 0, n = oYearToggle.opt.aSwappableContainers.length; i < n; i++) | |
167 if (!in_array(oYearToggle.opt.aSwappableContainers[i], oToggle.opt.aSwappableContainers)) | |
168 aNewContainers[aNewContainers.length] = oYearToggle.opt.aSwappableContainers[i]; | |
169 | |
170 oYearToggle.opt.aSwappableContainers = aNewContainers; | |
171 } | |
172 } | |
173 | |
174 | |
175 smf_StatsCenter.prototype.onBeforeExpandMonth = function (oToggle) | |
176 { | |
177 // Ignore if we're still loading the previous batch. | |
178 if (this.bIsLoading) | |
179 return; | |
180 | |
181 if (oToggle.opt.aSwappableContainers.length == 0) | |
182 { | |
183 // A complicated way to call getXMLDocument, but stay in scope. | |
184 this.tmpMethod = getXMLDocument; | |
185 this.oXmlRequestHandle = this.tmpMethod(smf_prepareScriptUrl(smf_scripturl) + 'action=stats;expand=' + oToggle.opt.sMonthId + ';xml', this.onDocReceived); | |
186 delete this.tmpMethod; | |
187 | |
188 if ('ajax_indicator' in window) | |
189 ajax_indicator(true); | |
190 | |
191 this.bIsLoading = true; | |
192 } | |
193 | |
194 // Silently let SMF know this one is expanded. | |
195 else | |
196 getXMLDocument(smf_prepareScriptUrl(smf_scripturl) + 'action=stats;expand=' + oToggle.opt.sMonthId + ';xml'); | |
197 } | |
198 | |
199 smf_StatsCenter.prototype.onDocReceived = function (oXMLDoc) | |
200 { | |
201 // Loop through all the months we got from the XML. | |
202 var aMonthNodes = oXMLDoc.getElementsByTagName('month'); | |
203 for (var iMonthIndex = 0, iNumMonths = aMonthNodes.length; iMonthIndex < iNumMonths; iMonthIndex++) | |
204 { | |
205 var sMonthId = aMonthNodes[iMonthIndex].getAttribute('id'); | |
206 var iStart = document.getElementById('tr_month_' + sMonthId).rowIndex + 1; | |
207 var sYearId = sMonthId.substr(0, 4); | |
208 | |
209 // Within the current months, check out all the days. | |
210 var aDayNodes = aMonthNodes[iMonthIndex].getElementsByTagName('day'); | |
211 for (var iDayIndex = 0, iNumDays = aDayNodes.length; iDayIndex < iNumDays; iDayIndex++) | |
212 { | |
213 var oCurRow = this.oTable.insertRow(iStart + iDayIndex); | |
214 oCurRow.className = this.opt.sDayRowClassname; | |
215 oCurRow.id = this.opt.sDayRowIdPrefix + aDayNodes[iDayIndex].getAttribute('date'); | |
216 | |
217 for (var iCellIndex = 0, iNumCells = this.opt.aDataCells.length; iCellIndex < iNumCells; iCellIndex++) | |
218 { | |
219 var oCurCell = oCurRow.insertCell(-1); | |
220 | |
221 if (this.opt.aDataCells[iCellIndex] == 'date') | |
222 oCurCell.style.paddingLeft = '6ex'; | |
223 else | |
224 oCurCell.style.textAlign = 'center'; | |
225 | |
226 var sCurData = aDayNodes[iDayIndex].getAttribute(this.opt.aDataCells[iCellIndex]); | |
227 oCurCell.appendChild(document.createTextNode(sCurData)); | |
228 } | |
229 | |
230 // Add these day rows to the toggle objects in case of collapse. | |
231 this.oYears[sYearId].oMonths[sMonthId].oToggle.opt.aSwappableContainers[this.oYears[sYearId].oMonths[sMonthId].oToggle.opt.aSwappableContainers.length] = oCurRow.id; | |
232 this.oYears[sYearId].oToggle.opt.aSwappableContainers[this.oYears[sYearId].oToggle.opt.aSwappableContainers.length] = oCurRow.id; | |
233 } | |
234 } | |
235 | |
236 this.bIsLoading = false; | |
237 if (typeof(window.ajax_indicator) == 'function') | |
238 ajax_indicator(false); | |
239 } |