1/**
2 *
3 * Darkfish Page Functions
4 * $Id: darkfish.js 53 2009-01-07 02:52:03Z deveiant $
5 *
6 * Author: Michael Granger <mgranger@laika.com>
7 *
8 */
9
10/* Provide console simulation for firebug-less environments */
11if (!("console" in window) || !("firebug" in console)) {
12  var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
13    "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
14
15  window.console = {};
16  for (var i = 0; i < names.length; ++i)
17    window.console[names[i]] = function() {};
18};
19
20
21/**
22 * Unwrap the first element that matches the given @expr@ from the targets and return them.
23 */
24$.fn.unwrap = function( expr ) {
25  return this.each( function() {
26    $(this).parents( expr ).eq( 0 ).after( this ).remove();
27  });
28};
29
30
31function showSource( e ) {
32  var target = e.target;
33  var codeSections = $(target).
34    parents('.method-detail').
35    find('.method-source-code');
36
37  $(target).
38    parents('.method-detail').
39    find('.method-source-code').
40    slideToggle();
41};
42
43function hookSourceViews() {
44  $('.method-heading').click( showSource );
45};
46
47function toggleDebuggingSection() {
48  $('.debugging-section').slideToggle();
49};
50
51function hookDebuggingToggle() {
52  $('#debugging-toggle img').click( toggleDebuggingSection );
53};
54
55function hookTableOfContentsToggle() {
56  $('.indexpage li .toc-toggle').each( function() {
57    $(this).click( function() {
58      $(this).toggleClass('open');
59    });
60
61    var section = $(this).next();
62
63    $(this).click( function() {
64      section.slideToggle();
65    });
66  });
67}
68
69function hookSearch() {
70  var input  = $('#search-field').eq(0);
71  var result = $('#search-results').eq(0);
72  $(result).show();
73
74  var search_section = $('#search-section').get(0);
75  $(search_section).show();
76
77  var search = new Search(search_data, input, result);
78
79  search.renderItem = function(result) {
80    var li = document.createElement('li');
81    var html = '';
82
83    // TODO add relative path to <script> per-page
84    html += '<p class="search-match"><a href="' + rdoc_rel_prefix + result.path + '">' + this.hlt(result.title);
85    if (result.params)
86      html += '<span class="params">' + result.params + '</span>';
87    html += '</a>';
88
89
90    if (result.namespace)
91      html += '<p class="search-namespace">' + this.hlt(result.namespace);
92
93    if (result.snippet)
94      html += '<div class="search-snippet">' + result.snippet + '</div>';
95
96    li.innerHTML = html;
97
98    return li;
99  }
100
101  search.select = function(result) {
102    var result_element = result.get(0);
103    window.location.href = result_element.firstChild.firstChild.href;
104  }
105
106  search.scrollIntoView = search.scrollInWindow;
107};
108
109function highlightTarget( anchor ) {
110  console.debug( "Highlighting target '%s'.", anchor );
111
112  $("a[name]").each( function() {
113    if ( $(this).attr("name") == anchor ) {
114      if ( !$(this).parent().parent().hasClass('target-section') ) {
115        console.debug( "Wrapping the target-section" );
116        $('div.method-detail').unwrap( 'div.target-section' );
117        $(this).parent().wrap( '<div class="target-section"></div>' );
118      } else {
119        console.debug( "Already wrapped." );
120      }
121    }
122  });
123};
124
125function highlightLocationTarget() {
126  console.debug( "Location hash: %s", window.location.hash );
127  if ( ! window.location.hash || window.location.hash.length == 0 ) return;
128
129  var anchor = window.location.hash.substring(1);
130  console.debug( "Found anchor: %s; matching %s", anchor, "a[name=" + anchor + "]" );
131
132  highlightTarget( anchor );
133};
134
135function highlightClickTarget( event ) {
136  console.debug( "Highlighting click target for event %o", event.target );
137  try {
138    var anchor = $(event.target).attr( 'href' ).substring(1);
139    console.debug( "Found target anchor: %s", anchor );
140    highlightTarget( anchor );
141  } catch ( err ) {
142    console.error( "Exception while highlighting: %o", err );
143  };
144};
145
146
147$(document).ready( function() {
148  hookSourceViews();
149  hookDebuggingToggle();
150  hookSearch();
151  highlightLocationTarget();
152  hookTableOfContentsToggle();
153
154  $('ul.link-list a').bind( "click", highlightClickTarget );
155});
156