OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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 |
| 5 cr.define('help', function() { |
| 6 /** |
| 7 * Encapsulated handling of the channel change overlay. |
| 8 */ |
| 9 function ChannelChangePage() {} |
| 10 |
| 11 cr.addSingletonGetter(ChannelChangePage); |
| 12 |
| 13 ChannelChangePage.prototype = { |
| 14 __proto__: HTMLDivElement.prototype, |
| 15 |
| 16 /** |
| 17 * Name of the channel the device is currently on. |
| 18 * @private |
| 19 */ |
| 20 currentChannel_: null, |
| 21 |
| 22 /** |
| 23 * Name of the channel the device is supposed to be on. |
| 24 * @private |
| 25 */ |
| 26 targetChannel_: null, |
| 27 |
| 28 /** |
| 29 * True iff the device is enterprise-managed. |
| 30 * @private |
| 31 */ |
| 32 isEnterpriseManaged_: undefined, |
| 33 |
| 34 /** |
| 35 * List of the channels names, from the least stable to the most stable. |
| 36 * @private |
| 37 */ |
| 38 channelList_: ['dev-channel', 'beta-channel', 'stable-channel'], |
| 39 |
| 40 /** |
| 41 * List of the possible ui states. |
| 42 * @private |
| 43 */ |
| 44 uiClassTable_: ['selected-channel-requires-powerwash', |
| 45 'selected-channel-requires-delayed-update', |
| 46 'selected-channel-good', |
| 47 'selected-channel-unstable'], |
| 48 |
| 49 /** |
| 50 * Perform initial setup. |
| 51 */ |
| 52 initialize: function() { |
| 53 var self = this; |
| 54 |
| 55 $('channel-change-page-cancel-button').onclick = function() { |
| 56 help.HelpPage.cancelOverlay(); |
| 57 }; |
| 58 |
| 59 var options = this.getAllChannelOptions_(); |
| 60 for (var i = 0; i < options.length; i++) { |
| 61 var option = options[i]; |
| 62 option.onclick = function() { |
| 63 self.updateUI_(this.value); |
| 64 }; |
| 65 } |
| 66 |
| 67 $('channel-change-page-powerwash-button').onclick = function() { |
| 68 self.setChannel_(self.getSelectedOption_(), true); |
| 69 help.HelpPage.cancelOverlay(); |
| 70 }; |
| 71 |
| 72 $('channel-change-page-change-button').onclick = function() { |
| 73 self.setChannel_(self.getSelectedOption_(), false); |
| 74 help.HelpPage.cancelOverlay(); |
| 75 }; |
| 76 }, |
| 77 |
| 78 /** |
| 79 * Returns the list of all radio buttons responsible for channel selection. |
| 80 * @return {Array.<HTMLInputElement>} Array of radio buttons |
| 81 * @private |
| 82 */ |
| 83 getAllChannelOptions_: function() { |
| 84 return $('channel-change-page').querySelectorAll('input[type="radio"]'); |
| 85 }, |
| 86 |
| 87 /** |
| 88 * Returns value of the selected option. |
| 89 * @return {string} Selected channel name or null, if neither |
| 90 * option is selected. |
| 91 * @private |
| 92 */ |
| 93 getSelectedOption_: function() { |
| 94 var options = this.getAllChannelOptions_(); |
| 95 for (var i = 0; i < options.length; i++) { |
| 96 var option = options[i]; |
| 97 if (option.checked) |
| 98 return option.value; |
| 99 } |
| 100 return null; |
| 101 }, |
| 102 |
| 103 /** |
| 104 * Updates UI according to selected channel. |
| 105 * @param {string} selectedChannel Selected channel |
| 106 * @private |
| 107 */ |
| 108 updateUI_: function(selectedChannel) { |
| 109 var currentStability = this.channelList_.indexOf(this.currentChannel_); |
| 110 var newStability = this.channelList_.indexOf(selectedChannel); |
| 111 |
| 112 var newOverlayClass = null; |
| 113 |
| 114 if (selectedChannel == this.currentChannel_) { |
| 115 if (this.currentChannel_ != this.targetChannel_) { |
| 116 // Allow user to switch back to the current channel. |
| 117 newOverlayClass = 'selected-channel-good'; |
| 118 } |
| 119 } else if (selectedChannel != this.targetChannel_) { |
| 120 // Selected channel isn't equal to the current and target channel. |
| 121 if (newStability > currentStability) { |
| 122 // More stable channel is selected. For customer devices |
| 123 // notify user about powerwash. |
| 124 if (this.isEnterpriseManaged_) |
| 125 newOverlayClass = 'selected-channel-requires-delayed-update'; |
| 126 else |
| 127 newOverlayClass = 'selected-channel-requires-powerwash'; |
| 128 } else if (selectedChannel == 'dev-channel') { |
| 129 // Warn user about unstable channel. |
| 130 newOverlayClass = 'selected-channel-unstable'; |
| 131 } else { |
| 132 // Switching to the less stable channel. |
| 133 newOverlayClass = 'selected-channel-good'; |
| 134 } |
| 135 } |
| 136 |
| 137 // Switch to the new UI state. |
| 138 for (var i = 0; i < this.uiClassTable_.length; i++) |
| 139 $('channel-change-page').classList.remove(this.uiClassTable_[i]); |
| 140 |
| 141 if (newOverlayClass) |
| 142 $('channel-change-page').classList.add(newOverlayClass); |
| 143 }, |
| 144 |
| 145 /** |
| 146 * Sets the device target channel. |
| 147 * @param {string} channel The name of the target channel |
| 148 * @param {boolean} isPowerwashAllowed True iff powerwash is allowed |
| 149 * @private |
| 150 */ |
| 151 setChannel_: function(channel, isPowerwashAllowed) { |
| 152 this.targetChannel_ = channel; |
| 153 this.updateUI_(channel); |
| 154 help.HelpPage.setChannel(channel, isPowerwashAllowed); |
| 155 }, |
| 156 |
| 157 /** |
| 158 * Updates page UI according to device owhership policy. |
| 159 * @param {boolean} isEnterpriseManaged True if the device is |
| 160 * enterprise managed |
| 161 * @private |
| 162 */ |
| 163 updateIsEnterpriseManaged_: function(isEnterpriseManaged) { |
| 164 this.isEnterpriseManaged_ = isEnterpriseManaged; |
| 165 help.HelpPage.updateChannelChangePageContainerVisibility(); |
| 166 }, |
| 167 |
| 168 /** |
| 169 * Updates name of the current channel, i.e. the name of the |
| 170 * channel the device is currently on. |
| 171 * @param {string} channel The name of the current channel |
| 172 * @private |
| 173 */ |
| 174 updateCurrentChannel_: function(channel) { |
| 175 if (this.channelList_.indexOf(channel) < 0) |
| 176 return; |
| 177 this.currentChannel_ = channel; |
| 178 |
| 179 var options = this.getAllChannelOptions_(); |
| 180 for (var i = 0; i < options.length; i++) { |
| 181 var option = options[i]; |
| 182 if (option.value == channel) |
| 183 option.checked = true; |
| 184 } |
| 185 help.HelpPage.updateChannelChangePageContainerVisibility(); |
| 186 }, |
| 187 |
| 188 /** |
| 189 * Updates name of the target channel, i.e. the name of the |
| 190 * channel the device is supposed to be in case of a pending |
| 191 * channel change. |
| 192 * @param {string} channel The name of the target channel |
| 193 * @private |
| 194 */ |
| 195 updateTargetChannel_: function(channel) { |
| 196 if (this.channelList_.indexOf(channel) < 0) |
| 197 return; |
| 198 this.targetChannel_ = channel; |
| 199 help.HelpPage.updateChannelChangePageContainerVisibility(); |
| 200 }, |
| 201 |
| 202 /** |
| 203 * @return {boolean} True if the page is ready and can be |
| 204 * displayed, false otherwise |
| 205 * @private |
| 206 */ |
| 207 isPageReady_: function() { |
| 208 if (typeof this.isEnterpriseManaged_ == 'undefined') |
| 209 return false; |
| 210 if (!this.currentChannel_ || !this.targetChannel_) |
| 211 return false; |
| 212 return true; |
| 213 }, |
| 214 }; |
| 215 |
| 216 ChannelChangePage.updateIsEnterpriseManaged = function(isEnterpriseManaged) { |
| 217 ChannelChangePage.getInstance().updateIsEnterpriseManaged_( |
| 218 isEnterpriseManaged); |
| 219 }; |
| 220 |
| 221 ChannelChangePage.updateCurrentChannel = function(channel) { |
| 222 ChannelChangePage.getInstance().updateCurrentChannel_(channel); |
| 223 }; |
| 224 |
| 225 ChannelChangePage.updateTargetChannel = function(channel) { |
| 226 ChannelChangePage.getInstance().updateTargetChannel_(channel); |
| 227 }; |
| 228 |
| 229 ChannelChangePage.isPageReady = function() { |
| 230 return ChannelChangePage.getInstance().isPageReady_(); |
| 231 }; |
| 232 |
| 233 // Export |
| 234 return { |
| 235 ChannelChangePage: ChannelChangePage |
| 236 }; |
| 237 }); |
OLD | NEW |