blob: b9155846875293c057421434af9cd9d3c9f61ca7 [file] [log] [blame]
joe.gregorio258eefb2008-09-06 01:30:25 +00001/// XXX: make it cross browser
2
3/**
4 * make the code below compatible with browsers without
5 * an installed firebug like debugger
6 */
7if (!window.console || !console.firebug) {
8 var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
9 "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
10 window.console = {};
11 for (var i = 0; i < names.length; ++i)
12 window.console[names[i]] = function() {}
13}
14
15/**
16 * small helper function to urldecode strings
17 */
18jQuery.urldecode = function(x) {
19 return decodeURIComponent(x).replace(/\+/g, ' ');
20}
21
22/**
23 * small helper function to urlencode strings
24 */
25jQuery.urlencode = encodeURIComponent;
26
27/**
28 * This function returns the parsed url parameters of the
29 * current request. Multiple values per key are supported,
30 * it will always return arrays of strings for the value parts.
31 */
32jQuery.getQueryParameters = function(s) {
33 if (typeof s == 'undefined')
34 s = document.location.search;
35 var parts = s.substr(s.indexOf('?') + 1).split('&');
36 var result = {};
37 for (var i = 0; i < parts.length; i++) {
38 var tmp = parts[i].split('=', 2);
39 var key = jQuery.urldecode(tmp[0]);
40 var value = jQuery.urldecode(tmp[1]);
41 if (key in result)
42 result[key].push(value);
43 else
44 result[key] = [value];
45 }
46 return result;
47}
48
49/**
50 * small function to check if an array contains
51 * a given item.
52 */
53jQuery.contains = function(arr, item) {
54 for (var i = 0; i < arr.length; i++) {
55 if (arr[i] == item)
56 return true;
57 }
58 return false;
59}
60
61/**
62 * highlight a given string on a jquery object by wrapping it in
63 * span elements with the given class name.
64 */
65jQuery.fn.highlightText = function(text, className) {
66 function highlight(node) {
67 if (node.nodeType == 3) {
68 var val = node.nodeValue;
69 var pos = val.toLowerCase().indexOf(text);
70 if (pos >= 0 && !jQuery.className.has(node.parentNode, className)) {
71 var span = document.createElement("span");
72 span.className = className;
73 span.appendChild(document.createTextNode(val.substr(pos, text.length)));
74 node.parentNode.insertBefore(span, node.parentNode.insertBefore(
75 document.createTextNode(val.substr(pos + text.length)),
76 node.nextSibling));
77 node.nodeValue = val.substr(0, pos);
78 }
79 }
80 else if (!jQuery(node).is("button, select, textarea")) {
81 jQuery.each(node.childNodes, function() {
82 highlight(this)
83 });
84 }
85 }
86 return this.each(function() {
87 highlight(this);
88 });
89}
90
91/**
92 * Small JavaScript module for the documentation.
93 */
94var Documentation = {
95
96 init : function() {
97 /* this.addContextElements(); -- now done statically */
98 this.fixFirefoxAnchorBug();
99 this.highlightSearchWords();
100 this.initModIndex();
101 this.initComments();
102 },
103
104 /**
105 * add context elements like header anchor links
106 */
107 addContextElements : function() {
108 for (var i = 1; i <= 6; i++) {
109 $('h' + i + '[@id]').each(function() {
110 $('<a class="headerlink">\u00B6</a>').
111 attr('href', '#' + this.id).
112 attr('title', 'Permalink to this headline').
113 appendTo(this);
114 });
115 }
116 $('dt[@id]').each(function() {
117 $('<a class="headerlink">\u00B6</a>').
118 attr('href', '#' + this.id).
119 attr('title', 'Permalink to this definition').
120 appendTo(this);
121 });
122 },
123
124 /**
125 * workaround a firefox stupidity
126 */
127 fixFirefoxAnchorBug : function() {
128 if (document.location.hash && $.browser.mozilla)
129 window.setTimeout(function() {
130 document.location.href += '';
131 }, 10);
132 },
133
134 /**
135 * highlight the search words provided in the url in the text
136 */
137 highlightSearchWords : function() {
138 var params = $.getQueryParameters();
139 var terms = (params.highlight) ? params.highlight[0].split(/\s+/) : [];
140 if (terms.length) {
141 var body = $('div.body');
142 window.setTimeout(function() {
143 $.each(terms, function() {
144 body.highlightText(this.toLowerCase(), 'highlight');
145 });
146 }, 10);
147 $('<li class="highlight-link"><a href="javascript:Documentation.' +
148 'hideSearchWords()">Hide Search Matches</a></li>')
149 .appendTo($('.sidebar .this-page-menu'));
150 }
151 },
152
153 /**
154 * init the modindex toggle buttons
155 */
156 initModIndex : function() {
157 var togglers = $('img.toggler').click(function() {
158 var src = $(this).attr('src');
159 var idnum = $(this).attr('id').substr(7);
160 console.log($('tr.cg-' + idnum).toggle());
161 if (src.substr(-9) == 'minus.png')
162 $(this).attr('src', src.substr(0, src.length-9) + 'plus.png');
163 else
164 $(this).attr('src', src.substr(0, src.length-8) + 'minus.png');
165 }).css('display', '');
166 if (DOCUMENTATION_OPTIONS.COLLAPSE_MODINDEX) {
167 togglers.click();
168 }
169 },
170
171 /**
172 * init the inline comments
173 */
174 initComments : function() {
175 $('.inlinecomments div.actions').each(function() {
176 this.innerHTML += ' | ';
177 $(this).append($('<a href="#">hide comments</a>').click(function() {
178 $(this).parent().parent().toggle();
179 return false;
180 }));
181 });
182 $('.inlinecomments .comments').hide();
183 $('.inlinecomments a.bubble').each(function() {
184 $(this).click($(this).is('.emptybubble') ? function() {
185 var params = $.getQueryParameters(this.href);
186 Documentation.newComment(params.target[0]);
187 return false;
188 } : function() {
189 $('.comments', $(this).parent().parent()[0]).toggle();
190 return false;
191 });
192 });
193 $('#comments div.actions a.newcomment').click(function() {
194 Documentation.newComment();
195 return false;
196 });
197 if (document.location.hash.match(/^#comment-/))
198 $('.inlinecomments .comments ' + document.location.hash)
199 .parent().toggle();
200 },
201
202 /**
203 * helper function to hide the search marks again
204 */
205 hideSearchWords : function() {
206 $('.sidebar .this-page-menu li.highlight-link').fadeOut(300);
207 $('span.highlight').removeClass('highlight');
208 },
209
210 /**
211 * show the comment window for a certain id or the whole page.
212 */
213 newComment : function(id) {
214 Documentation.CommentWindow.openFor(id || '');
215 },
216
217 /**
218 * write a new comment from within a comment view box
219 */
220 newCommentFromBox : function(link) {
221 var params = $.getQueryParameters(link.href);
222 $(link).parent().parent().fadeOut('slow');
223 this.newComment(params.target);
224 },
225
226 /**
227 * make the url absolute
228 */
229 makeURL : function(relativeURL) {
230 return DOCUMENTATION_OPTIONS.URL_ROOT + '/' + relativeURL;
231 },
232
233 /**
234 * get the current relative url
235 */
236 getCurrentURL : function() {
237 var path = document.location.pathname;
238 var parts = path.split(/\//);
239 $.each(DOCUMENTATION_OPTIONS.URL_ROOT.split(/\//), function() {
240 if (this == '..')
241 parts.pop();
242 });
243 var url = parts.join('/');
244 return path.substring(url.lastIndexOf('/') + 1, path.length - 1);
245 },
246
247 /**
248 * class that represents the comment window
249 */
250 CommentWindow : (function() {
251 var openWindows = {};
252
253 var Window = function(sectionID) {
254 this.url = Documentation.makeURL('@comments/' + Documentation.getCurrentURL()
255 + '/?target=' + $.urlencode(sectionID) + '&mode=ajax');
256 this.sectionID = sectionID;
257
258 this.root = $('<div class="commentwindow"></div>');
259 this.root.appendTo($('body'));
260 this.title = $('<h3>New Comment</h3>').appendTo(this.root);
261 this.body = $('<div class="form">please wait...</div>').appendTo(this.root);
262 this.resizeHandle = $('<div class="resizehandle"></div>').appendTo(this.root);
263
264 this.root.Draggable({
265 handle: this.title[0]
266 });
267
268 this.root.css({
269 left: window.innerWidth / 2 - $(this.root).width() / 2,
270 top: window.scrollY + (window.innerHeight / 2 - 150)
271 });
272 this.root.fadeIn('slow');
273 this.updateView();
274 };
275
276 Window.prototype.updateView = function(data) {
277 var self = this;
278 function update(data) {
279 if (data.posted) {
280 document.location.hash = '#comment-' + data.commentID;
281 document.location.reload();
282 }
283 else {
284 self.body.html(data.body);
285 $('div.actions', self.body).append($('<input>')
286 .attr('type', 'button')
287 .attr('value', 'Close')
288 .click(function() { self.close(); })
289 );
290 $('div.actions input[@name="preview"]')
291 .attr('type', 'button')
292 .click(function() { self.submitForm($('form', self.body)[0], true); });
293 $('form', self.body).bind("submit", function() {
294 self.submitForm(this);
295 return false;
296 });
297
298 if (data.error) {
299 self.root.Highlight(1000, '#aadee1');
300 $('div.error', self.root).slideDown(500);
301 }
302 }
303 }
304
305 if (typeof data == 'undefined')
306 $.getJSON(this.url, function(json) { update(json); });
307 else
308 $.ajax({
309 url: this.url,
310 type: 'POST',
311 dataType: 'json',
312 data: data,
313 success: function(json) { update(json); }
314 });
315 }
316
317 Window.prototype.getFormValue = function(name) {
318 return $('*[@name="' + name + '"]', this.body)[0].value;
319 }
320
321 Window.prototype.submitForm = function(form, previewMode) {
322 this.updateView({
323 author: form.author.value,
324 author_mail: form.author_mail.value,
325 title: form.title.value,
326 comment_body: form.comment_body.value,
327 preview: previewMode ? 'yes' : ''
328 });
329 }
330
331 Window.prototype.close = function() {
332 var self = this;
333 delete openWindows[this.sectionID];
334 this.root.fadeOut('slow', function() {
335 self.root.remove();
336 });
337 }
338
339 Window.openFor = function(sectionID) {
340 if (sectionID in openWindows)
341 return openWindows[sectionID];
342 return new Window(sectionID);
343 }
344
345 return Window;
346 })()
347};
348
349
350$(document).ready(function() {
351 Documentation.init();
352});