home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / AvantBrowser / asetup.exe / _data / webkit / chrome.dll / 0 / BINDATA / 605 < prev    next >
Encoding:
Text File  |  2013-04-03  |  10.8 KB  |  363 lines

  1. // Copyright (c) 2012 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.
  6. /** @const */ var FEEDBACK_LANDING_PAGE =
  7.     'https://www.google.com/support/chrome/go/feedback_confirmation';
  8. /** @const */ var MAX_ATTACH_FILE_SIZE = 3 * 1024 * 1024;
  9.  
  10. var selectedThumbnailDivId = '';
  11. var selectedThumbnailId = '';
  12. var selectedImageUrl;
  13.  
  14. var savedThumbnailIds = [];
  15. savedThumbnailIds['current-screenshots'] = '';
  16. savedThumbnailIds['saved-screenshots'] = '';
  17.  
  18. var categoryTag = '';
  19. var forceDisableScreenshots = false;
  20.  
  21.  
  22. // Globals to manage reading data from the attach a file option.
  23. var attachFileBinaryData = '';
  24. var lastReader = null;
  25.  
  26. /**
  27.  * Selects an image thumbnail in the specified div.
  28.  * @param {string} divId The id of the div to search in.
  29.  * @param {string} thumbnailId The id of the thumbnail to search for.
  30.  */
  31. function selectImage(divId, thumbnailId) {
  32.   var thumbnailDivs = $(divId).children;
  33.   selectedThumbnailDivId = divId;
  34.   if (thumbnailDivs.length == 0) {
  35.     $(divId).hidden = true;
  36.     return;
  37.   }
  38.  
  39.   for (var i = 0; i < thumbnailDivs.length; i++) {
  40.     thumbnailDivs[i].className = 'image-thumbnail-container';
  41.  
  42.     // If the the current div matches the thumbnail id provided,
  43.     // or there is no thumbnail id given, and we're at the first thumbnail.
  44.     if (thumbnailDivs[i].id == thumbnailId || (!thumbnailId && !i)) {
  45.       thumbnailDivs[i].classList.add('image-thumbnail-container-selected');
  46.       selectedThumbnailId = thumbnailDivs[i].id;
  47.       savedThumbnailIds[divId] = thumbnailId;
  48.     }
  49.   }
  50. }
  51.  
  52. /**
  53.  * Adds an image thumbnail to the specified div.
  54.  * @param {string} divId The id of the div to add a screenshot to.
  55.  * @param {string} screenshot The URL of the screenshot being added.
  56.  */
  57. function addScreenshot(divId, screenshot) {
  58.   var thumbnailDiv = document.createElement('div');
  59.   thumbnailDiv.className = 'image-thumbnail-container';
  60.  
  61.   thumbnailDiv.id = divId + '-thumbnailDiv-' + $(divId).children.length;
  62.   thumbnailDiv.onclick = function() {
  63.     selectImage(divId, thumbnailDiv.id);
  64.   };
  65.  
  66.   var innerDiv = document.createElement('div');
  67.   innerDiv.className = 'image-thumbnail';
  68.  
  69.   var thumbnail = document.createElement('img');
  70.   thumbnail.id = thumbnailDiv.id + '-image';
  71.   // We add the ?+timestamp to make sure the image URLs are unique
  72.   // and Chrome does not load the image from cache.
  73.   thumbnail.src = screenshot + '?' + Date.now();
  74.   innerDiv.appendChild(thumbnail);
  75.  
  76.   thumbnailDiv.appendChild(innerDiv);
  77.   $(divId).appendChild(thumbnailDiv);
  78.  
  79.   if (!selectedThumbnailId)
  80.     selectImage(divId, thumbnailDiv.id);
  81. }
  82.  
  83. /**
  84.  * Disables screenshots completely.
  85.  */
  86. function enableScreenshots() {
  87.   if (forceDisableScreenshots)
  88.     return;
  89.   $('screenshot-row').hidden = false;
  90. }
  91.  
  92. /**
  93.  * Reads the selected file when the user selects a file.
  94.  * @param {event} evtFileSelected The on changed event for the file input box.
  95.  */
  96. function onFileSelected(evtFileSelected) {
  97.   var file = evtFileSelected.target.files[0];
  98.   if (!file) {
  99.     // User canceled file selection.
  100.     $('attach-file-checkbox').checked = false;
  101.     attachFileBinaryData = null;
  102.     return;
  103.   }
  104.  
  105.   if (file.size > MAX_ATTACH_FILE_SIZE) {
  106.     $('attach-error').hidden = false;
  107.  
  108.     // Clear our selected file.
  109.     $('attach-file').value = '';
  110.     attachFileBinaryData = null;
  111.     $('attach-file-checkbox').checked = false;
  112.  
  113.     return;
  114.   }
  115.  
  116.   $('attach-error').hidden = true;
  117.  
  118.   // Abort an existing file read operation if one exists.
  119.   if (lastReader) {
  120.     lastReader.abort();
  121.     lastReader = null;
  122.   }
  123.  
  124.   var reader = new FileReader();
  125.   reader.onloadend = function(evtLoadEnd) {
  126.     if (evtLoadEnd.target.readyState == FileReader.DONE) {
  127.       attachFileBinaryData = evtLoadEnd.target.result;
  128.       lastReader = null;
  129.       // Check the checkbox so we do send this file. Users can uncheck the
  130.       // box if they don't want to send the file.
  131.       $('attach-file-checkbox').checked = true;
  132.       $('reading-file').hidden = true;
  133.       $('send-report-button').disabled = false;
  134.     }
  135.   };
  136.  
  137.   lastReader = reader;
  138.   reader.readAsBinaryString(file);
  139.   $('reading-file').hidden = false;
  140.   $('send-report-button').disabled = true;
  141. }
  142.  
  143. /**
  144.  * Sends the report; after the report is sent, we need to be redirected to
  145.  * the landing page, but we shouldn't be able to navigate back, hence
  146.  * we open the landing page in a new tab and sendReport closes this tab.
  147.  * @return {boolean} True if the report was sent.
  148.  */
  149. function sendReport() {
  150.   if ($('description-text').value.length == 0) {
  151.     alert(loadTimeData.getString('no-description'));
  152.     return false;
  153.   }
  154.  
  155.   var imagePath = '';
  156.   if ($('screenshot-checkbox').checked && selectedThumbnailId)
  157.     imagePath = $(selectedThumbnailId + '-image').src;
  158.   var pageUrl = $('page-url-text').value;
  159.   if (!$('page-url-checkbox').checked)
  160.     pageUrl = '';
  161.   var userEmail = $('user-email-text').value;
  162.   if (!$('user-email-checkbox').checked)
  163.     userEmail = '';
  164.  
  165.   var reportArray = [pageUrl,
  166.                      categoryTag,
  167.                      $('description-text').value,
  168.                      userEmail,
  169.                      imagePath];
  170.  
  171.   // Add chromeos data if it exists.
  172.   if ($('sys-info-checkbox')) {
  173.     reportArray = reportArray.concat([String($('sys-info-checkbox').checked)]);
  174.   }
  175.  
  176.   if ($('attach-file-checkbox') &&
  177.       $('attach-file-checkbox').checked &&
  178.       attachFileBinaryData) {
  179.     reportArray = reportArray.concat(
  180.         [$('attach-file').files[0].name, btoa(attachFileBinaryData)]);
  181.   }
  182.  
  183.   // open the landing page in a new tab, sendReport will close this one.
  184.   window.open(FEEDBACK_LANDING_PAGE, '_blank');
  185.   chrome.send('sendReport', reportArray);
  186.   return true;
  187. }
  188.  
  189. /**
  190.  * Click listener for the cancel button.
  191.  * @param {Event} e The click event being handled.
  192.  */
  193. function cancel(e) {
  194.   chrome.send('cancel');
  195.   e.preventDefault();
  196. }
  197.  
  198. /**
  199.  * Select the current screenshots div, restoring the image that was
  200.  * selected when we had this div open previously.
  201.  */
  202. function currentSelected() {
  203.   // TODO(rkc): Change this to use a class instead.
  204.   $('current-screenshots').hidden = false;
  205.   if ($('saved-screenshots'))
  206.     $('saved-screenshots').hidden = true;
  207.  
  208.   if (selectedThumbnailDivId != 'current-screenshots')
  209.     selectImage('current-screenshots',
  210.                 savedThumbnailIds['current-screenshots']);
  211. }
  212.  
  213. /**
  214.  * Select the saved screenshots div, restoring the image that was
  215.  * selected when we had this div open previously.
  216.  */
  217. function savedSelected() {
  218.   if ($('saved-screenshots').childElementCount == 0) {
  219.     // setupSavedScreenshots will take care of changing visibility
  220.     chrome.send('refreshSavedScreenshots');
  221.   } else {
  222.     $('current-screenshots').hidden = true;
  223.     $('saved-screenshots').hidden = false;
  224.     if (selectedThumbnailDivId != 'saved-screenshots')
  225.       selectImage('saved-screenshots', savedThumbnailIds['saved-screenshots']);
  226.   }
  227. }
  228.  
  229. /**
  230.  * Change the type of screenshot we're showing to the user from
  231.  * the current screenshot to saved screenshots
  232.  */
  233. function changeToSaved() {
  234.   $('screenshot-label-current').hidden = true;
  235.   $('screenshot-label-saved').hidden = false;
  236.  
  237.   // Change the link to say "go to original"
  238.   $('screenshot-link-tosaved').hidden = true;
  239.   $('screenshot-link-tocurrent').hidden = false;
  240.  
  241.   savedSelected();
  242. }
  243.  
  244. /**
  245.  * Change the type of screenshot we're showing to the user from
  246.  * the saved screenshots to the current screenshots
  247.  */
  248. function changeToCurrent() {
  249.   $('screenshot-label-current').hidden = false;
  250.   $('screenshot-label-saved').hidden = true;
  251.  
  252.   // Change the link to say "go to saved"
  253.   $('screenshot-link-tosaved').hidden = false;
  254.   $('screenshot-link-tocurrent').hidden = true;
  255.  
  256.   currentSelected();
  257. }
  258.  
  259. ///////////////////////////////////////////////////////////////////////////////
  260. // Document Functions:
  261. /**
  262.  * Window onload handler, sets up the page.
  263.  */
  264. function load() {
  265.   if ($('attach-file'))
  266.     $('attach-file').addEventListener('change', onFileSelected);
  267.  
  268.   if ($('sysinfo-url')) {
  269.     $('sysinfo-url').onclick = function(event) {
  270.       chrome.send('openSystemTab');
  271.     };
  272.   }
  273.  
  274.  
  275.   $('send-report-button').onclick = sendReport;
  276.   $('cancel-button').onclick = cancel;
  277.  
  278.   // Set default values for the possible parameters, and then parse the actual
  279.   // values from the URL hash.
  280.   var parameters = {
  281.     'description': '',
  282.     'categoryTag': '',
  283.     'customPageUrl': '',
  284.   };
  285.   var queryPos = window.location.hash.indexOf('?');
  286.   if (queryPos !== -1) {
  287.     // Get an array of parameters in 'name=value' form.
  288.     var query = window.location.hash.substring(queryPos + 1).split('&');
  289.     for (var i = 0; i < query.length; i++) {
  290.       // Decode and store each parameter value.
  291.       parameter = query[i].split('=');
  292.       parameters[parameter[0]] = decodeURIComponent(parameter[1]);
  293.     }
  294.  
  295.     // For a clean URL, trim the parameters from the hash.
  296.     window.location.hash = window.location.hash.substring(0, queryPos);
  297.   }
  298.  
  299.   // Set the initial description text.
  300.   $('description-text').textContent = parameters['description'];
  301.   // If a page url is spcified in the parameters, override the default page url.
  302.   if (parameters['customPageUrl'] != '') {
  303.     $('page-url-text').value = parameters['customPageUrl'];
  304.     // and disable the page image, since it doesn't make sense on a custum url.
  305.     $('screenshot-checkbox').checked = false;
  306.     forceDisableScreenshots = true;
  307.   }
  308.  
  309.   // Pick up the category tag (for most cases this will be an empty string)
  310.   categoryTag = parameters['categoryTag'];
  311.  
  312.   chrome.send('getDialogDefaults');
  313.   chrome.send('refreshCurrentScreenshot');
  314. }
  315.  
  316. function setupCurrentScreenshot(screenshot) {
  317.   addScreenshot('current-screenshots', screenshot);
  318. }
  319.  
  320. function setupSavedScreenshots(screenshots) {
  321.   if (screenshots.length == 0) {
  322.     $('saved-screenshots').textContent =
  323.         loadTimeData.getString('no-saved-screenshots');
  324.  
  325.     // Make sure we make the display the message.
  326.     $('current-screenshots').hidden = true;
  327.     $('saved-screenshots').hidden = false;
  328.  
  329.     // In case the user tries to send now; fail safe, do not send a screenshot
  330.     // at all versus sending the current screenshot.
  331.     selectedThumbnailDivId = '';
  332.     selectedThumbnailId = '';
  333.   } else {
  334.     for (i = 0; i < screenshots.length; ++i)
  335.       addScreenshot('saved-screenshots', screenshots[i]);
  336.  
  337.     // Now that we have our screenshots, try selecting the saved screenshots
  338.     // again.
  339.     savedSelected();
  340.   }
  341. }
  342.  
  343. function setupDialogDefaults(defaults) {
  344.   // Current url.
  345.   if ($('page-url-text').value == '')
  346.     $('page-url-text').value = defaults.currentUrl;
  347.   if (defaults.currentUrl == '')
  348.     $('page-url-checkbox').checked = false;
  349.   // User e-mail.
  350.   $('user-email-text').value = defaults.userEmail;
  351.   $('user-email-checkbox').checked = defaults.emailCheckboxDefault;
  352.  
  353.   // Are screenshots disabled?
  354.   if (!defaults.disableScreenshots)
  355.     enableScreenshots();
  356.  
  357.   if (defaults.useSaved) {
  358.     $('screenshot-link-tosaved').hidden = false;
  359.   }
  360. }
  361.  
  362. window.addEventListener('DOMContentLoaded', load);
  363.