1 // Copyright 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.
5 cr.define('help', function() {
7 * Encapsulated handling of the channel change overlay.
9 function ChannelChangePage() {}
11 cr.addSingletonGetter(ChannelChangePage);
13 ChannelChangePage.prototype = {
14 __proto__: help.HelpBasePage.prototype,
17 * Name of the channel the device is currently on.
20 currentChannel_: null,
23 * Name of the channel the device is supposed to be on.
29 * True iff the device is enterprise-managed.
32 isEnterpriseManaged_: undefined,
35 * List of the channels names, from the least stable to the most stable.
38 channelList_: ['dev-channel', 'beta-channel', 'stable-channel'],
41 * List of the possible ui states.
44 uiClassTable_: ['selected-channel-requires-powerwash',
45 'selected-channel-requires-delayed-update',
46 'selected-channel-good',
47 'selected-channel-unstable'],
50 * Perform initial setup.
52 initialize: function() {
53 help.HelpBasePage.prototype.initialize.call(this, 'channel-change-page');
57 $('channel-change-page-cancel-button').onclick = function() {
58 help.HelpPage.cancelOverlay();
61 var options = this.getAllChannelOptions_();
62 for (var i = 0; i < options.length; i++) {
63 var option = options[i];
64 option.onclick = function() {
65 self.updateUI_(this.value);
69 $('channel-change-page-powerwash-button').onclick = function() {
70 self.setChannel_(self.getSelectedOption_(), true);
71 help.HelpPage.cancelOverlay();
74 $('channel-change-page-change-button').onclick = function() {
75 self.setChannel_(self.getSelectedOption_(), false);
76 help.HelpPage.cancelOverlay();
80 onBeforeShow: function() {
81 help.HelpBasePage.prototype.onBeforeShow.call(this);
82 if (this.targetChannel_ != null)
83 this.selectOption_(this.targetChannel_);
84 else if (this.currentChannel_ != null)
85 this.selectOption_(this.currentChannel_);
86 var options = this.getAllChannelOptions_();
87 for (var i = 0; i < options.length; i++) {
88 var option = options[i];
95 * Returns the list of all radio buttons responsible for channel selection.
96 * @return {Array.<HTMLInputElement>} Array of radio buttons
99 getAllChannelOptions_: function() {
100 return $('channel-change-page').querySelectorAll('input[type="radio"]');
104 * Returns value of the selected option.
105 * @return {string} Selected channel name or null, if neither
106 * option is selected.
109 getSelectedOption_: function() {
110 var options = this.getAllChannelOptions_();
111 for (var i = 0; i < options.length; i++) {
112 var option = options[i];
120 * Selects option for a given channel.
121 * @param {string} channel Name of channel option that should be selected.
124 selectOption_: function(channel) {
125 var options = this.getAllChannelOptions_();
126 for (var i = 0; i < options.length; i++) {
127 var option = options[i];
128 if (option.value == channel) {
129 option.checked = true;
132 this.updateUI_(channel);
136 * Updates UI according to selected channel.
137 * @param {string} selectedChannel Selected channel
140 updateUI_: function(selectedChannel) {
141 var currentStability = this.channelList_.indexOf(this.currentChannel_);
142 var newStability = this.channelList_.indexOf(selectedChannel);
144 var newOverlayClass = null;
146 if (selectedChannel == this.currentChannel_) {
147 if (this.currentChannel_ != this.targetChannel_) {
148 // Allow user to switch back to the current channel.
149 newOverlayClass = 'selected-channel-good';
151 } else if (selectedChannel != this.targetChannel_) {
152 // Selected channel isn't equal to the current and target channel.
153 if (newStability > currentStability) {
154 // More stable channel is selected. For customer devices
155 // notify user about powerwash.
156 if (this.isEnterpriseManaged_)
157 newOverlayClass = 'selected-channel-requires-delayed-update';
159 newOverlayClass = 'selected-channel-requires-powerwash';
160 } else if (selectedChannel == 'dev-channel') {
161 // Warn user about unstable channel.
162 newOverlayClass = 'selected-channel-unstable';
164 // Switching to the less stable channel.
165 newOverlayClass = 'selected-channel-good';
169 // Switch to the new UI state.
170 for (var i = 0; i < this.uiClassTable_.length; i++)
171 $('channel-change-page').classList.remove(this.uiClassTable_[i]);
174 $('channel-change-page').classList.add(newOverlayClass);
178 * Sets the device target channel.
179 * @param {string} channel The name of the target channel
180 * @param {boolean} isPowerwashAllowed True iff powerwash is allowed
183 setChannel_: function(channel, isPowerwashAllowed) {
184 this.targetChannel_ = channel;
185 this.updateUI_(channel);
186 help.HelpPage.setChannel(channel, isPowerwashAllowed);
190 * Updates page UI according to device owhership policy.
191 * @param {boolean} isEnterpriseManaged True if the device is
195 updateIsEnterpriseManaged_: function(isEnterpriseManaged) {
196 this.isEnterpriseManaged_ = isEnterpriseManaged;
197 help.HelpPage.updateChannelChangePageContainerVisibility();
201 * Updates name of the current channel, i.e. the name of the
202 * channel the device is currently on.
203 * @param {string} channel The name of the current channel
206 updateCurrentChannel_: function(channel) {
207 if (this.channelList_.indexOf(channel) < 0)
209 this.currentChannel_ = channel;
210 this.selectOption_(channel);
211 help.HelpPage.updateChannelChangePageContainerVisibility();
215 * Updates name of the target channel, i.e. the name of the
216 * channel the device is supposed to be in case of a pending
218 * @param {string} channel The name of the target channel
221 updateTargetChannel_: function(channel) {
222 if (this.channelList_.indexOf(channel) < 0)
224 this.targetChannel_ = channel;
225 help.HelpPage.updateChannelChangePageContainerVisibility();
229 * @return {boolean} True if the page is ready and can be
230 * displayed, false otherwise
233 isPageReady_: function() {
234 if (typeof this.isEnterpriseManaged_ == 'undefined')
236 if (!this.currentChannel_ || !this.targetChannel_)
242 ChannelChangePage.updateIsEnterpriseManaged = function(isEnterpriseManaged) {
243 ChannelChangePage.getInstance().updateIsEnterpriseManaged_(
244 isEnterpriseManaged);
247 ChannelChangePage.updateCurrentChannel = function(channel) {
248 ChannelChangePage.getInstance().updateCurrentChannel_(channel);
251 ChannelChangePage.updateTargetChannel = function(channel) {
252 ChannelChangePage.getInstance().updateTargetChannel_(channel);
255 ChannelChangePage.isPageReady = function() {
256 return ChannelChangePage.getInstance().isPageReady_();
261 ChannelChangePage: ChannelChangePage