- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / help / channel_change_page.js
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.
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__: help.HelpBasePage.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       help.HelpBasePage.prototype.initialize.call(this, 'channel-change-page');
54
55       var self = this;
56
57       $('channel-change-page-cancel-button').onclick = function() {
58         help.HelpPage.cancelOverlay();
59       };
60
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);
66         };
67       }
68
69       $('channel-change-page-powerwash-button').onclick = function() {
70         self.setChannel_(self.getSelectedOption_(), true);
71         help.HelpPage.cancelOverlay();
72       };
73
74       $('channel-change-page-change-button').onclick = function() {
75         self.setChannel_(self.getSelectedOption_(), false);
76         help.HelpPage.cancelOverlay();
77       };
78     },
79
80     /**
81      * Returns the list of all radio buttons responsible for channel selection.
82      * @return {Array.<HTMLInputElement>} Array of radio buttons
83      * @private
84      */
85     getAllChannelOptions_: function() {
86       return $('channel-change-page').querySelectorAll('input[type="radio"]');
87     },
88
89     /**
90      * Returns value of the selected option.
91      * @return {string} Selected channel name or null, if neither
92      *     option is selected.
93      * @private
94      */
95     getSelectedOption_: function() {
96       var options = this.getAllChannelOptions_();
97       for (var i = 0; i < options.length; i++) {
98         var option = options[i];
99         if (option.checked)
100           return option.value;
101       }
102       return null;
103     },
104
105     /**
106      * Updates UI according to selected channel.
107      * @param {string} selectedChannel Selected channel
108      * @private
109      */
110     updateUI_: function(selectedChannel) {
111       var currentStability = this.channelList_.indexOf(this.currentChannel_);
112       var newStability = this.channelList_.indexOf(selectedChannel);
113
114       var newOverlayClass = null;
115
116       if (selectedChannel == this.currentChannel_) {
117         if (this.currentChannel_ != this.targetChannel_) {
118           // Allow user to switch back to the current channel.
119           newOverlayClass = 'selected-channel-good';
120         }
121       } else if (selectedChannel != this.targetChannel_) {
122         // Selected channel isn't equal to the current and target channel.
123         if (newStability > currentStability) {
124           // More stable channel is selected. For customer devices
125           // notify user about powerwash.
126           if (this.isEnterpriseManaged_)
127             newOverlayClass = 'selected-channel-requires-delayed-update';
128           else
129             newOverlayClass = 'selected-channel-requires-powerwash';
130         } else if (selectedChannel == 'dev-channel') {
131           // Warn user about unstable channel.
132           newOverlayClass = 'selected-channel-unstable';
133         } else {
134           // Switching to the less stable channel.
135           newOverlayClass = 'selected-channel-good';
136         }
137       }
138
139       // Switch to the new UI state.
140       for (var i = 0; i < this.uiClassTable_.length; i++)
141           $('channel-change-page').classList.remove(this.uiClassTable_[i]);
142
143       if (newOverlayClass)
144         $('channel-change-page').classList.add(newOverlayClass);
145     },
146
147     /**
148      * Sets the device target channel.
149      * @param {string} channel The name of the target channel
150      * @param {boolean} isPowerwashAllowed True iff powerwash is allowed
151      * @private
152      */
153     setChannel_: function(channel, isPowerwashAllowed) {
154       this.targetChannel_ = channel;
155       this.updateUI_(channel);
156       help.HelpPage.setChannel(channel, isPowerwashAllowed);
157     },
158
159     /**
160      * Updates page UI according to device owhership policy.
161      * @param {boolean} isEnterpriseManaged True if the device is
162      *     enterprise managed
163      * @private
164      */
165     updateIsEnterpriseManaged_: function(isEnterpriseManaged) {
166       this.isEnterpriseManaged_ = isEnterpriseManaged;
167       help.HelpPage.updateChannelChangePageContainerVisibility();
168     },
169
170     /**
171      * Updates name of the current channel, i.e. the name of the
172      * channel the device is currently on.
173      * @param {string} channel The name of the current channel
174      * @private
175      */
176     updateCurrentChannel_: function(channel) {
177      if (this.channelList_.indexOf(channel) < 0)
178         return;
179       this.currentChannel_ = channel;
180
181       var options = this.getAllChannelOptions_();
182       for (var i = 0; i < options.length; i++) {
183         var option = options[i];
184         if (option.value == channel)
185           option.checked = true;
186       }
187       help.HelpPage.updateChannelChangePageContainerVisibility();
188     },
189
190     /**
191      * Updates name of the target channel, i.e. the name of the
192      * channel the device is supposed to be in case of a pending
193      * channel change.
194      * @param {string} channel The name of the target channel
195      * @private
196      */
197     updateTargetChannel_: function(channel) {
198       if (this.channelList_.indexOf(channel) < 0)
199         return;
200       this.targetChannel_ = channel;
201       help.HelpPage.updateChannelChangePageContainerVisibility();
202     },
203
204     /**
205      * @return {boolean} True if the page is ready and can be
206      *     displayed, false otherwise
207      * @private
208      */
209     isPageReady_: function() {
210       if (typeof this.isEnterpriseManaged_ == 'undefined')
211         return false;
212       if (!this.currentChannel_ || !this.targetChannel_)
213         return false;
214       return true;
215     },
216   };
217
218   ChannelChangePage.updateIsEnterpriseManaged = function(isEnterpriseManaged) {
219     ChannelChangePage.getInstance().updateIsEnterpriseManaged_(
220         isEnterpriseManaged);
221   };
222
223   ChannelChangePage.updateCurrentChannel = function(channel) {
224     ChannelChangePage.getInstance().updateCurrentChannel_(channel);
225   };
226
227   ChannelChangePage.updateTargetChannel = function(channel) {
228     ChannelChangePage.getInstance().updateTargetChannel_(channel);
229   };
230
231   ChannelChangePage.isPageReady = function() {
232     return ChannelChangePage.getInstance().isPageReady_();
233   };
234
235   // Export
236   return {
237     ChannelChangePage: ChannelChangePage
238   };
239 });