~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to doc/godocs.js

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Except as noted, this content is licensed under Creative Commons
2
 
// Attribution 3.0
 
1
// Copyright 2012 The Go Authors. All rights reserved.
 
2
// Use of this source code is governed by a BSD-style
 
3
// license that can be found in the LICENSE file.
3
4
 
4
5
/* A little code to ease navigation of these documents.
5
6
 *
6
7
 * On window load we:
7
 
 *  + Generate a table of contents (godocs_generateTOC)
8
 
 *  + Add links up to the top of the doc from each section (godocs_addTopLinks)
9
 
 */
10
 
 
11
 
/* We want to do some stuff on page load (after the HTML is rendered).
12
 
   So listen for that:
13
 
 */
14
 
function bindEvent(el, e, fn) {
15
 
  if (el.addEventListener){
16
 
    el.addEventListener(e, fn, false);
17
 
  } else if (el.attachEvent){
18
 
    el.attachEvent('on'+e, fn);
19
 
  }
20
 
}
21
 
 
22
 
function godocs_bindSearchEvents() {
23
 
  var search = document.getElementById('search');
24
 
  if (!search) {
25
 
    // no search box (index disabled)
26
 
    return;
27
 
  }
 
8
 *  + Bind search box hint placeholder show/hide events (bindSearchEvents)
 
9
 *  + Generate a table of contents (generateTOC)
 
10
 *  + Bind foldable sections (bindToggles)
 
11
 *  + Bind links to foldable sections (bindToggleLinks)
 
12
 */
 
13
 
 
14
(function() {
 
15
'use strict';
 
16
 
 
17
function bindSearchEvents() {
 
18
 
 
19
  var search = $('#search');
 
20
  if (search.length === 0) {
 
21
    return; // no search box
 
22
  }
 
23
 
28
24
  function clearInactive() {
29
 
    if (search.className == "inactive") {
30
 
      search.value = "";
31
 
      search.className = "";
 
25
    if (search.is('.inactive')) {
 
26
      search.val('');
 
27
      search.removeClass('inactive');
32
28
    }
33
29
  }
 
30
 
34
31
  function restoreInactive() {
35
 
    if (search.value !== "") {
 
32
    if (search.val() !== '') {
36
33
      return;
37
34
    }
38
 
    if (search.type != "search") {
39
 
      search.value = search.getAttribute("placeholder");
40
 
    }
41
 
    search.className = "inactive";
 
35
    search.val(search.attr('placeholder'));
 
36
    search.addClass('inactive');
42
37
  }
 
38
 
 
39
  search.on('focus', clearInactive);
 
40
  search.on('blur', restoreInactive);
 
41
 
43
42
  restoreInactive();
44
 
  bindEvent(search, 'focus', clearInactive);
45
 
  bindEvent(search, 'blur', restoreInactive);
46
 
}
47
 
 
48
 
/* Returns the "This sweet header" from <h2>This <i>sweet</i> header</h2>.
49
 
 * Takes a node, returns a string.
50
 
 */
51
 
function godocs_nodeToText(node) {
52
 
  var TEXT_NODE = 3; // Defined in Mozilla but not MSIE :(
53
 
 
54
 
  var text = '';
55
 
  for (var j = 0; j != node.childNodes.length; j++) {
56
 
    var child = node.childNodes[j];
57
 
    if (child.nodeType == TEXT_NODE) {
58
 
      if (child.nodeValue != '[Top]') { //ok, that's a hack, but it works.
59
 
        text = text + child.nodeValue;
60
 
      }
61
 
    } else {
62
 
      text = text + godocs_nodeToText(child);
63
 
    }
64
 
  }
65
 
  return text;
66
43
}
67
44
 
68
45
/* Generates a table of contents: looks for h2 and h3 elements and generates
69
 
 * links.  "Decorates" the element with id=="nav" with this table of contents.
 
46
 * links. "Decorates" the element with id=="nav" with this table of contents.
70
47
 */
71
 
function godocs_generateTOC() {
72
 
  if (document.getElementById('manual-nav')) { return; }
73
 
  var navbar = document.getElementById('nav');
74
 
  if (!navbar) { return; }
 
48
function generateTOC() {
 
49
  if ($('#manual-nav').length > 0) {
 
50
    return;
 
51
  }
 
52
 
 
53
  var nav = $('#nav');
 
54
  if (nav.length === 0) {
 
55
    return;
 
56
  }
75
57
 
76
58
  var toc_items = [];
77
 
 
78
 
  var i;
79
 
  var seenNav = false;
80
 
  for (i = 0; i < navbar.parentNode.childNodes.length; i++) {
81
 
    var node = navbar.parentNode.childNodes[i];
82
 
    if (!seenNav) { 
83
 
      if (node.id == 'nav') {
84
 
        seenNav = true;
85
 
      }
86
 
      continue;
87
 
    }
88
 
    if ((node.tagName != 'h2') && (node.tagName != 'H2') &&
89
 
        (node.tagName != 'h3') && (node.tagName != 'H3')) {
90
 
      continue;
91
 
    }
92
 
    if (!node.id) {
93
 
      node.id = 'tmp_' + i;
94
 
    }
95
 
    var text = godocs_nodeToText(node);
96
 
    if (!text) { continue; }
97
 
 
98
 
    var textNode = document.createTextNode(text);
99
 
 
100
 
    var link = document.createElement('a');
101
 
    link.href = '#' + node.id;
102
 
    link.appendChild(textNode);
103
 
 
104
 
    // Then create the item itself
 
59
  $(nav).nextAll('h2, h3').each(function() {
 
60
    var node = this;
 
61
    if (node.id == '')
 
62
      node.id = 'tmp_' + toc_items.length;
 
63
    var link = $('<a/>').attr('href', '#' + node.id).text($(node).text());
105
64
    var item;
106
 
    if ((node.tagName == 'h2') || (node.tagName == 'H2')) {
107
 
      item = document.createElement('dt');
 
65
    if ($(node).is('h2')) {
 
66
      item = $('<dt/>');
108
67
    } else { // h3
109
 
      item = document.createElement('dd');
 
68
      item = $('<dd/>');
110
69
    }
111
 
 
112
 
    item.appendChild(link);
 
70
    item.append(link);
113
71
    toc_items.push(item);
 
72
  });
 
73
  if (toc_items.length <= 1) {
 
74
    return;
114
75
  }
115
76
 
116
 
  if (toc_items.length <= 1) { return; }
117
 
 
118
 
  var dl1 = document.createElement('dl');
119
 
  var dl2 = document.createElement('dl');
 
77
  var dl1 = $('<dl/>');
 
78
  var dl2 = $('<dl/>');
120
79
 
121
80
  var split_index = (toc_items.length / 2) + 1;
122
81
  if (split_index < 8) {
123
82
    split_index = toc_items.length;
124
83
  }
125
 
 
126
 
  for (i = 0; i < split_index; i++) {
127
 
    dl1.appendChild(toc_items[i]);
 
84
  for (var i = 0; i < split_index; i++) {
 
85
    dl1.append(toc_items[i]);
128
86
  }
129
87
  for (/* keep using i */; i < toc_items.length; i++) {
130
 
    dl2.appendChild(toc_items[i]);
 
88
    dl2.append(toc_items[i]);
131
89
  }
132
90
 
133
 
  var tocTable = document.createElement('table');
134
 
  navbar.appendChild(tocTable);
135
 
  tocTable.className = 'unruled';
136
 
  var tocBody = document.createElement('tbody');
137
 
  tocTable.appendChild(tocBody);
138
 
 
139
 
  var tocRow = document.createElement('tr');
140
 
  tocBody.appendChild(tocRow);
 
91
  var tocTable = $('<table class="unruled"/>').appendTo(nav);
 
92
  var tocBody = $('<tbody/>').appendTo(tocTable);
 
93
  var tocRow = $('<tr/>').appendTo(tocBody);
141
94
 
142
95
  // 1st column
143
 
  var tocCell = document.createElement('td');
144
 
  tocCell.className = 'first';
145
 
  tocRow.appendChild(tocCell);
146
 
  tocCell.appendChild(dl1);
147
 
 
 
96
  $('<td class="first"/>').appendTo(tocRow).append(dl1);
148
97
  // 2nd column
149
 
  tocCell = document.createElement('td');
150
 
  tocRow.appendChild(tocCell);
151
 
  tocCell.appendChild(dl2);
152
 
}
153
 
 
154
 
function getElementsByClassName(base, clazz) {
155
 
  if (base.getElementsByClassName) {
156
 
    return base.getElementsByClassName(clazz);
157
 
  }
158
 
  var elements = base.getElementsByTagName('*'), foundElements = [];
159
 
  for (var n in elements) {
160
 
    if (clazz == elements[n].className) {
161
 
      foundElements.push(elements[n]);
162
 
    }
163
 
  }
164
 
  return foundElements;
165
 
}
166
 
 
167
 
function godocs_bindToggle(el) {
168
 
  var button = getElementsByClassName(el, "toggleButton");
169
 
  var callback = function() {
170
 
    if (el.className == "toggle") {
171
 
      el.className = "toggleVisible";
 
98
  $('<td/>').appendTo(tocRow).append(dl2);
 
99
}
 
100
 
 
101
function bindToggle(el) {
 
102
  $('.toggleButton', el).click(function() {
 
103
    if ($(el).is('.toggle')) {
 
104
      $(el).addClass('toggleVisible').removeClass('toggle');
172
105
    } else {
173
 
      el.className = "toggle";
 
106
      $(el).addClass('toggle').removeClass('toggleVisible');
174
107
    }
175
 
  };
176
 
  for (var i = 0; i < button.length; i++) {
177
 
    bindEvent(button[i], "click", callback);
178
 
  }
179
 
}
180
 
function godocs_bindToggles(className) {
181
 
  var els = getElementsByClassName(document, className);
182
 
  for (var i = 0; i < els.length; i++) {
183
 
    godocs_bindToggle(els[i]);
184
 
  }
185
 
}
186
 
function godocs_bindToggleLink(l, prefix) {
187
 
  bindEvent(l, "click", function() {
188
 
    var i = l.href.indexOf("#"+prefix);
 
108
  });
 
109
}
 
110
function bindToggles(selector) {
 
111
  $(selector).each(function(i, el) {
 
112
    bindToggle(el);
 
113
  });
 
114
}
 
115
 
 
116
function bindToggleLink(el, prefix) {
 
117
  $(el).click(function() {
 
118
    var href = $(el).attr('href');
 
119
    var i = href.indexOf('#'+prefix);
189
120
    if (i < 0) {
190
121
      return;
191
122
    }
192
 
    var id = prefix + l.href.slice(i+1+prefix.length);
193
 
    var eg = document.getElementById(id);
194
 
    eg.className = "toggleVisible";
195
 
  });
196
 
}
197
 
function godocs_bindToggleLinks(className, prefix) {
198
 
  var links = getElementsByClassName(document, className);
199
 
  for (i = 0; i < links.length; i++) {
200
 
    godocs_bindToggleLink(links[i], prefix);
 
123
    var id = '#' + prefix + href.slice(i+1+prefix.length);
 
124
    if ($(id).is('.toggle')) {
 
125
      $(id).find('.toggleButton').first().click();
 
126
    }
 
127
  });
 
128
}
 
129
function bindToggleLinks(selector, prefix) {
 
130
  $(selector).each(function(i, el) {
 
131
    bindToggleLink(el, prefix);
 
132
  });
 
133
}
 
134
 
 
135
function setupDropdownPlayground() {
 
136
  if (!$('#page').is('.wide')) {
 
137
    return; // don't show on front page
201
138
  }
202
 
}
203
 
 
204
 
function godocs_onload() {
205
 
  godocs_bindSearchEvents();
206
 
  godocs_generateTOC();
207
 
  godocs_bindToggles("toggle");
208
 
  godocs_bindToggles("toggleVisible");
209
 
  godocs_bindToggleLinks("exampleLink", "example_");
210
 
  godocs_bindToggleLinks("overviewLink", "");
211
 
}
212
 
 
213
 
bindEvent(window, 'load', godocs_onload);
 
139
  var button = $('#playgroundButton');
 
140
  var div = $('#playground');
 
141
  var setup = false;
 
142
  button.toggle(function() {
 
143
    button.addClass('active');
 
144
    div.show();
 
145
    if (setup) {
 
146
      return;
 
147
    }
 
148
    setup = true;
 
149
    playground({
 
150
      'codeEl': $('.code', div),
 
151
      'outputEl': $('.output', div),
 
152
      'runEl': $('.run', div),
 
153
      'fmtEl': $('.fmt', div),
 
154
      'shareEl': $('.share', div),
 
155
      'shareRedirect': 'http://play.golang.org/p/'
 
156
    });
 
157
  },
 
158
  function() {
 
159
    button.removeClass('active');
 
160
    div.hide();
 
161
  });
 
162
  button.show();
 
163
  $('#menu').css('min-width', '+=60');
 
164
}
 
165
 
 
166
// fixFocus tries to put focus to div#page so that keyboard navigation works.
 
167
function fixFocus() {
 
168
  var page = $('div#page');
 
169
  var topbar = $('div#topbar');
 
170
  page.css('outline', 0); // disable outline when focused
 
171
  page.attr('tabindex', -1); // and set tabindex so that it is focusable
 
172
  $(window).resize(function (evt) {
 
173
    // only focus page when the topbar is at fixed position (that is, it's in
 
174
    // front of page, and keyboard event will go to the former by default.)
 
175
    // by focusing page, keyboard event will go to page so that up/down arrow,
 
176
    // space, etc. will work as expected.
 
177
    if (topbar.css('position') == "fixed")
 
178
      page.focus();
 
179
  }).resize();
 
180
}
 
181
 
 
182
function toggleHash() {
 
183
    var hash = $(window.location.hash);
 
184
    if (hash.is('.toggle')) {
 
185
      hash.addClass('toggleVisible').removeClass('toggle');
 
186
    }
 
187
}
 
188
 
 
189
$(document).ready(function() {
 
190
  bindSearchEvents();
 
191
  generateTOC();
 
192
  bindToggles(".toggle");
 
193
  bindToggles(".toggleVisible");
 
194
  bindToggleLinks(".exampleLink", "example_");
 
195
  bindToggleLinks(".overviewLink", "");
 
196
  bindToggleLinks(".examplesLink", "");
 
197
  bindToggleLinks(".indexLink", "");
 
198
  setupDropdownPlayground();
 
199
  fixFocus();
 
200
  toggleHash();
 
201
});
 
202
 
 
203
})();