1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5function toggleHelpBox() {
6  var helpBoxOuter = document.getElementById('help-box-outer');
7  helpBoxOuter.classList.toggle('hidden');
8  var detailsButton = document.getElementById('details-button');
9  if (helpBoxOuter.classList.contains('hidden'))
10    detailsButton.innerText = detailsButton.detailsText;
11  else
12    detailsButton.innerText = detailsButton.hideDetailsText;
13}
14
15function diagnoseErrors() {
16  var extensionId = 'idddmepepmjcgiedknnmlbadcokidhoa';
17  var diagnoseFrame = document.getElementById('diagnose-frame');
18  diagnoseFrame.innerHTML =
19      '<iframe src="chrome-extension://' + extensionId +
20      '/index.html"></iframe>';
21}
22
23// Subframes use a different layout but the same html file.  This is to make it
24// easier to support platforms that load the error page via different
25// mechanisms (Currently just iOS).
26if (window.top.location != window.location)
27  document.documentElement.setAttribute('subframe', '');
28
29// Re-renders the error page using |strings| as the dictionary of values.
30// Used by NetErrorTabHelper to update DNS error pages with probe results.
31function updateForDnsProbe(strings) {
32  i18nTemplate.process(document, strings);
33  var context = new JsEvalContext(strings);
34  jstProcess(context, document.getElementById('t'));
35}
36
37// Given the classList property of an element, adds an icon class to the list
38// and removes the previously-
39function updateIconClass(classList, newClass) {
40  var oldClass;
41
42  if (classList.hasOwnProperty('last_icon_class')) {
43    oldClass = classList['last_icon_class'];
44    if (oldClass == newClass)
45      return;
46  }
47
48  classList.add(newClass);
49  if (oldClass !== undefined)
50    classList.remove(oldClass);
51
52  classList['last_icon_class'] = newClass;
53
54  if (newClass == 'icon-offline') {
55    document.body.classList.add('offline');
56    new Runner('.interstitial-wrapper');
57  }
58}
59
60// Does a search using |baseSearchUrl| and the text in the search box.
61function search(baseSearchUrl) {
62  var searchTextNode = document.getElementById('search-box');
63  document.location = baseSearchUrl + searchTextNode.value;
64  return false;
65}
66
67// Use to track clicks on elements generated by the navigation correction
68// service.  If |trackingId| is negative, the element does not come from the
69// correction service.
70function trackClick(trackingId) {
71  // This can't be done with XHRs because XHRs are cancelled on navigation
72  // start, and because these are cross-site requests.
73  if (trackingId >= 0 && errorPageController)
74    errorPageController.trackClick(trackingId);
75}
76
77// Called when an <a> tag generated by the navigation correction service is
78// clicked.  Separate function from trackClick so the resources don't have to
79// be updated if new data is added to jstdata.
80function linkClicked(jstdata) {
81  trackClick(jstdata.trackingId);
82}
83
84// Implements button clicks.  This function is needed during the transition
85// between implementing these in trunk chromium and implementing them in
86// iOS.
87function reloadButtonClick(url) {
88  if (window.errorPageController) {
89    errorPageController.reloadButtonClick();
90  } else {
91    location = url;
92  }
93}
94
95function loadStaleButtonClick() {
96  if (window.errorPageController) {
97    errorPageController.loadStaleButtonClick();
98  }
99}
100
101function detailsButtonClick() {
102  if (window.errorPageController)
103    errorPageController.detailsButtonClick();
104}
105
106var primaryControlOnLeft = true;
107<if expr="is_macosx or is_ios or is_linux or is_android">
108primaryControlOnLeft = false;
109</if>
110
111// Sets up the proper button layout for the current platform.
112function setButtonLayout() {
113  var buttonsDiv = document.getElementById('buttons');
114  var controlButtonDiv = document.getElementById('control-buttons');
115  var reloadButton = document.getElementById('reload-button');
116  var detailsButton = document.getElementById('details-button');
117  var staleLoadButton = document.getElementById('stale-load-button');
118
119  var primaryButton = reloadButton;
120  var secondaryButton = staleLoadButton;
121
122  if (primaryControlOnLeft) {
123    buttons.classList.add('suggested-left');
124    controlButtonDiv.insertBefore(primaryButton, secondaryButton);
125  } else {
126    buttons.classList.add('suggested-right');
127    controlButtonDiv.insertBefore(secondaryButton, primaryButton);
128  }
129
130  if (reloadButton.style.display == 'none' &&
131      staleLoadButton.style.display == 'none') {
132    detailsButton.classList.add('singular');
133  }
134
135  if (templateData) {
136    // Hide the details button if there are no details to show.
137    if (templateData.summary && !templateData.summary.msg) {
138      detailsButton.style.display = 'none';
139      document.getElementById('help-box-outer').style.display = 'block';
140    }
141
142    // Show control buttons.
143    if (templateData.reloadButton && templateData.reloadButton.msg ||
144        templateData.staleLoadButton && templateData.staleLoadButton.msg) {
145      controlButtonDiv.hidden = false;
146    }
147  }
148}
149
150document.addEventListener('DOMContentLoaded', setButtonLayout);
151