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