1// Copyright (c) 2010 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
5// Constants.
6var FEEDBACK_LANDING_PAGE =
7    'http://www.google.com/support/chrome/go/feedback_confirmation'
8
9var selectedThumbnailDivId = '';
10var selectedThumbnailId = '';
11var selectedImageUrl;
12
13var savedThumbnailIds = [];
14savedThumbnailIds['current-screenshots'] = '';
15savedThumbnailIds['saved-screenshots'] = '';
16
17var localStrings = new LocalStrings();
18
19/**
20 * Selects an image thumbnail in the specified div.
21 */
22function selectImage(divId, thumbnailId) {
23  var thumbnailDivs = $(divId).children;
24  selectedThumbnailDivId = divId;
25  if (thumbnailDivs.length == 0) {
26    $(divId).style.display = 'none';
27    return;
28  }
29  for (var i = 0; i < thumbnailDivs.length; i++) {
30    // If the the current div matches the thumbnail id provided,
31    // or there is no thumbnail id given, and we're at the first thumbnail.
32    if ((thumbnailDivs[i].id == thumbnailId) || (!thumbnailId && !i)) {
33      thumbnailDivs[i].className = 'image-thumbnail-container-selected';
34      selectedThumbnailId = thumbnailId;
35      savedThumbnailIds[divId] = thumbnailId;
36    } else {
37      thumbnailDivs[i].className = 'image-thumbnail-container';
38    }
39  }
40}
41
42/**
43 * Adds an image thumbnail to the specified div.
44 */
45function addScreenshot(divId, screenshot) {
46  var thumbnailDiv = document.createElement('div');
47  thumbnailDiv.className = 'image-thumbnail-container';
48
49  thumbnailDiv.id = divId + '-thumbnailDiv-' + $(divId).children.length;
50  thumbnailDiv.onclick = function() {
51    selectImage(divId, thumbnailDiv.id);
52  };
53
54  var innerDiv = document.createElement('div');
55  if (divId == 'current-screenshots')
56    innerDiv.className = 'image-thumbnail-current';
57  else
58    innerDiv.className = 'image-thumbnail';
59
60  var thumbnail = document.createElement('img');
61  thumbnail.id = thumbnailDiv.id + '-image';
62  // We add the ?+timestamp to make sure the image URLs are unique
63  // and Chrome does not load the image from cache.
64  thumbnail.src = screenshot + '?' + Date.now();
65  innerDiv.appendChild(thumbnail);
66
67  thumbnailDiv.appendChild(innerDiv);
68  $(divId).appendChild(thumbnailDiv);
69
70  if (!selectedThumbnailId)
71    selectImage(divId, thumbnailDiv.id);
72}
73
74/**
75 * Send's the report; after the report is sent, we need to be redirected to
76 * the landing page, but we shouldn't be able to navigate back, hence
77 * we open the landing page in a new tab and sendReport closes this tab.
78 */
79function sendReport() {
80  if (!$('issue-with-combo').selectedIndex) {
81    alert(localStrings.getString('no-issue-selected'));
82    return false;
83  } else if ($('description-text').value.length == 0) {
84    alert(localStrings.getString('no-description'));
85    return false;
86  }
87
88  var imagePath = '';
89  if ($('screenshot-checkbox').checked && selectedThumbnailId)
90    imagePath = $(selectedThumbnailId + '-image').src;
91  var pageUrl = $('page-url-text').value;
92  if (!$('page-url-checkbox').checked)
93    pageUrl = '';
94
95  // Note, categories are based from 1 in our protocol buffers, so no
96  // adjustment is needed on selectedIndex.
97  var reportArray = [String($('issue-with-combo').selectedIndex),
98                     pageUrl,
99                     $('description-text').value,
100                     imagePath];
101
102  // Add chromeos data if it exists.
103  if ($('user-email-text') && $('sys-info-checkbox')) {
104    var userEmail= $('user-email-text').textContent;
105    if (!$('user-email-checkbox').checked)
106      userEmail = '';
107    reportArray = reportArray.concat([userEmail,
108                                      String($('sys-info-checkbox').checked)]);
109  }
110
111  // open the landing page in a new tab, sendReport will close this one.
112  window.open(FEEDBACK_LANDING_PAGE, '_blank');
113  chrome.send('sendReport', reportArray);
114  return true;
115}
116
117function cancel() {
118  chrome.send('cancel', []);
119  return true;
120}
121
122/**
123 * Select the current screenshots div, restoring the image that was
124 * selected when we had this div open previously.
125 */
126function currentSelected() {
127  // TODO(rkc): Change this to use a class instead.
128  $('current-screenshots').style.display = 'block';
129  if ($('saved-screenshots'))
130    $('saved-screenshots').style.display = 'none';
131
132  if (selectedThumbnailDivId != 'current-screenshots')
133    selectImage('current-screenshots',
134                savedThumbnailIds['current-screenshots']);
135
136  return true;
137}
138
139/**
140 * Select the saved screenshots div, restoring the image that was
141 * selected when we had this div open previously.
142 */
143function savedSelected() {
144  $('current-screenshots').style.display = 'none';
145
146  if ($('saved-screenshots').childElementCount == 0) {
147    // setupSavedScreenshots will take care of changing visibility
148    chrome.send('refreshSavedScreenshots', []);
149  } else {
150    $('saved-screenshots').style.display = 'block';
151    if (selectedThumbnailDivId != 'saved-screenshots')
152      selectImage('saved-screenshots', savedThumbnailIds['saved-screenshots']);
153  }
154
155  return true;
156}
157
158
159/**
160 * Change the type of screenshot we're showing to the user from
161 * the current screenshot to saved screenshots
162 */
163function changeToSaved() {
164  $('screenshot-label-current').style.display = 'none';
165  $('screenshot-label-saved').style.display = 'inline';
166
167  // Change the link to say "go to original"
168  $('screenshot-link-tosaved').style.display = 'none';
169  $('screenshot-link-tocurrent').style.display = 'inline';
170
171  savedSelected();
172}
173
174/**
175 * Change the type of screenshot we're showing to the user from
176 * the saved screenshots to the current screenshots
177 */
178function changeToCurrent() {
179  $('screenshot-label-current').style.display = 'inline';
180  $('screenshot-label-saved').style.display = 'none';
181
182  // Change the link to say "go to original"
183  $('screenshot-link-tosaved').style.display = 'inline';
184  $('screenshot-link-tocurrent').style.display = 'none';
185
186  currentSelected();
187}
188