Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 /** | |
| 6 * Constructor for the tab UI governing playback selection and running. | |
| 7 * All HTML controls under tag #plaback-tab, plus the tab label | |
| 8 * #playback-tab-label are controlled by this class. | |
| 9 * @param {!Object} cyclerUI The master UI class, needed for global state | |
| 10 * such as current capture name. | |
| 11 * @param {!Object} cyclerData The local FileSystem-based database of | |
| 12 * available captures to play back. | |
| 13 */ | |
| 14 var PlaybackTab = function (cyclerUI, cyclerData) { | |
| 15 /** | |
| 16 * Enum for different capture tab states. | |
|
Jeffrey Yasskin
2012/08/16 20:19:31
s/capture/playback/?
clintstaley
2012/08/17 04:12:25
Done.
clintstaley
2012/08/17 04:12:25
Done.
| |
| 17 * @enum {number} | |
| 18 * @private | |
| 19 */ | |
| 20 var EnableState_ = { | |
| 21 choosePlayback: 1, // Choose a playback, if none already chosen. | |
| 22 showPlayback: 2, // Show currently chosen playback, and offer options. | |
| 23 showNoCaptures: 3 // Show message indicating no captures are available. | |
| 24 }; | |
| 25 | |
| 26 this.cyclerUI_ = cyclerUI; | |
| 27 this.cyclerData_ = cyclerData; | |
| 28 | |
| 29 /** | |
| 30 * Create members for all UI elements with which we'll interact. | |
| 31 */ | |
| 32 this.tabLabel_ = $('#playback-tab-label'); | |
| 33 this.playbackTab_ = $('#playback-tab'); | |
| 34 this.noCaptures_ = $('#no-captures'); | |
| 35 this.yesCaptures_ = $('#yes-captures'); | |
| 36 | |
| 37 this.playbackName_ = $('#playback-name'); | |
| 38 this.playbackDetails_ = $('#playback-details'); | |
| 39 this.playbackURLs_ = $('#playback-urls'); | |
| 40 this.playbackRepeats_ = $('#playback-repeats'); | |
| 41 this.playbackExtension_ = $('#playback-extension'); | |
| 42 this.doPlaybackButton_ = $('#do-playback'); | |
| 43 this.doDeleteButton_ = $('#do-delete'); | |
| 44 | |
| 45 /* | |
| 46 * Enable the playback tab, showing no current playback choice, by | |
| 47 * hiding the playbackDetails_ div. | |
| 48 * @private | |
| 49 */ | |
| 50 this.enableChoosePlayback_ = function() { | |
| 51 if (this.enableState != EnableState_.choosePlayback) { | |
| 52 this.enableState = EnableState_.choosePlayback; | |
| 53 this.yesCaptures_.hidden = false; | |
| 54 this.noCaptures_.hidden = true; | |
| 55 this.playbackDetails_.hidden = true; | |
| 56 } | |
| 57 }; | |
| 58 | |
| 59 /* | |
| 60 * Enable the playback tab, showing a current playback choice by showing | |
| 61 * the playbackDetails_ div. | |
| 62 * @private | |
| 63 */ | |
| 64 this.enableShowPlayback_ = function() { | |
| 65 if (this.enableState != EnableState_.showPlayback) { | |
| 66 this.enableState = EnableState_.showPlayback; | |
| 67 this.yesCaptures_.hidden = false; | |
| 68 this.noCaptures_.hidden = true; | |
| 69 this.playbackDetails_.hidden = false; | |
| 70 } | |
| 71 }; | |
| 72 | |
| 73 /* | |
| 74 * Enable the playback tab and adjust tab labels appropriately. Show | |
| 75 * no available captures by hiding the yesCaptures_ div and showing the | |
| 76 * noCaptures_ div instead. | |
| 77 * @private | |
| 78 */ | |
| 79 this.enableShowNoCaptures_ = function() { | |
| 80 if (this.enableState != EnableState_.showNoCaptures) { | |
| 81 this.enableState = EnableState_.showNoCaptures; | |
| 82 this.noCaptures_.hidden = false; | |
| 83 this.yesCaptures_.hidden = true; | |
| 84 } | |
| 85 }; | |
| 86 | |
| 87 /** | |
| 88 * Enable the playback tab, showing either its "no captures", "choose | |
| 89 * a capture" or "display chosen capture" form, depending on the state | |
| 90 * of existing captures and |currentCaptureName_|. | |
| 91 */ | |
| 92 this.enable = function() { | |
| 93 this.tabLabel_.classList.add('selected'); | |
| 94 | |
| 95 this.playbackTab_.hidden = false; | |
| 96 if (this.cyclerData_.getCaptures().length == 0) { | |
| 97 this.enableShowNoCaptures_(); | |
| 98 } else if (this.cyclerUI_.currentCaptureName == null) { | |
| 99 this.enableChoosePlayback_(); | |
| 100 } else { | |
| 101 this.enableShowPlayback_(); | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 /** | |
| 106 * Disable the playback tab altogether, presumably in favor of some | |
| 107 * other tab. | |
| 108 */ | |
| 109 this.disable = function() { | |
| 110 this.tabLabel_.classList.remove('selected'); | |
| 111 this.playbackTab_.hidden = true; | |
| 112 } | |
| 113 | |
| 114 /** | |
| 115 * Utility function to refresh the selection list of captures that may | |
| 116 * be chosen for playback. Show all current captures, and also a | |
| 117 * "Choose a capture" default text if no capture is currently selected. | |
| 118 * @private | |
| 119 */ | |
| 120 this.updatePlaybackChoices_ = function() { | |
| 121 var captureNames = this.cyclerData_.getCaptures(); | |
| 122 var options = this.playbackName_.options; | |
| 123 var nextIndex = 0; | |
| 124 | |
| 125 options.length = 0; | |
| 126 | |
| 127 if (this.cyclerUI_.currentCaptureName == null) { | |
| 128 options.add(new Option('Choose a capture', null)); | |
| 129 options.selectedIndex = nextIndex++; | |
| 130 } | |
| 131 for (var i = 0; i < captureNames.length; i++) { | |
| 132 options.add(new Option(captureNames[i], captureNames[i])); | |
| 133 if (captureNames[i] == this.cyclerUI_.currentCaptureName) { | |
| 134 options.selectedIndex = nextIndex; | |
| 135 } | |
| 136 nextIndex++; | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 /** | |
| 141 * Event callback for selection of a capture to play back. Save the | |
| 142 * choice in |currentCaptureName_|. Update the selection list because the | |
| 143 * default reminder message is no longer needed when a capture is chosen. | |
| 144 * Change playback tab to show-chosen-capture mode. | |
| 145 */ | |
| 146 this.selectPlaybackName = function() { | |
| 147 this.cyclerUI_.currentCaptureName = this.playbackName_.value; | |
| 148 this.updatePlaybackChoices_(); | |
| 149 this.enableShowPlayback_(); | |
| 150 } | |
| 151 | |
| 152 /** | |
| 153 * Event callback for pressing the playback button, which button is only | |
| 154 * enabled if a capture has been chosen for playback. Check for errors | |
| 155 * on playback page, and either display a message with such errors, or | |
| 156 * call the extenion api to initiate a playback. | |
| 157 * @private | |
| 158 */ | |
| 159 this.doPlayback_ = function() { | |
| 160 var extensionPath = this.playbackExtension_.value; | |
| 161 var repeatCount = parseInt(this.playbackRepeats_.value); | |
| 162 var errors = []; | |
| 163 | |
| 164 // Check local errors | |
| 165 if (isNaN(repeatCount)) { | |
| 166 errors.push('Enter a number for repeat count'); | |
| 167 } else if (repeatCount < 1 || repeatCount > 100) { | |
| 168 errors.push('Repeat count must be between 1 and 100'); | |
| 169 } | |
| 170 | |
| 171 if (errors.length > 0) { | |
| 172 this.cyclerUI_.showMessage(errors.join('\n'), 'Ok'); | |
| 173 } else { | |
| 174 this.doPlaybackButton_.disabled = true; | |
| 175 chrome.experimental.record.replayURLs( | |
| 176 this.cyclerUI_.currentCaptureName, | |
| 177 repeatCount, | |
| 178 {'extensionPath': extensionPath}, | |
| 179 this.onPlaybackDone.bind(this)); | |
| 180 } | |
| 181 } | |
| 182 | |
| 183 /** | |
| 184 * Extension API calls this back when a playback is done. | |
| 185 * @param {!number} runtime Number of ms it took to run the playback. | |
| 186 * @param {!string} stats Statistics from the playback. | |
| 187 * @param {!Array.<!string>} errors List of error messages, if any, | |
| 188 * resuting from the playback. | |
| 189 */ | |
| 190 this.onPlaybackDone = function(runTime, stats, errors) { | |
| 191 this.doPlaybackButton_.disabled = false; | |
| 192 | |
| 193 if (errors.length > 0) { | |
| 194 this.cyclerUI_.showMessage(errors.join('\n'), 'Ok'); | |
| 195 } else { | |
| 196 this.cyclerUI_.showMessage('Test took ' + runTime + 'mS :\n' + | |
| 197 stats, 'Ok'); | |
| 198 } | |
| 199 } | |
| 200 | |
| 201 /** | |
| 202 * Delete the capture with name |currentCaptureName_|. For this method | |
| 203 * to be callable, there must be a selected currentCaptureName_. Change | |
| 204 * the displayed HTML to show no captures if none exist now, or to show | |
| 205 * none selected (since we just deleted the selected one). | |
| 206 * @private | |
| 207 */ | |
| 208 this.doDelete_ = function() { | |
| 209 this.cyclerData_.deleteCapture(this.cyclerUI_.currentCaptureName); | |
| 210 this.cyclerUI_.currentCaptureName = null; | |
| 211 this.updatePlaybackChoices_(); | |
| 212 if (this.cyclerData_.getCaptures().length == 0) { | |
| 213 this.enableShowNoCaptures_(); | |
| 214 } else { | |
| 215 this.enableChoosePlayback_(); | |
| 216 } | |
| 217 } | |
| 218 | |
| 219 // Set up listeners on buttons. | |
| 220 this.doPlaybackButton_.addEventListener('click', this.doPlayback_.bind(this)); | |
| 221 this.doDeleteButton_.addEventListener('click', this.doDelete_.bind(this)); | |
| 222 | |
| 223 // Set up initial selection list for existing captures, and selection | |
| 224 // event listener. | |
| 225 this.updatePlaybackChoices_(); | |
| 226 this.playbackName_.addEventListener('change', | |
| 227 this.selectPlaybackName.bind(this)); | |
| 228 }; | |
| OLD | NEW |