Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / resources / chromeos / login / screen_password_changed.js
blob4c8f1d18de7c45ff5b99da6ef4b85b79bad886be
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.
5 /**
6  * @fileoverview Password changed screen implementation.
7  */
9 login.createScreen('PasswordChangedScreen', 'password-changed', function() {
10   return {
11     EXTERNAL_API: [
12       'show'
13     ],
15     /** @override */
16     decorate: function() {
17       $('old-password').addEventListener(
18         'keydown', function(e) {
19           $('password-changed').classList.remove('password-error');
20           if (e.keyIdentifier == 'Enter') {
21             $('password-changed').migrate();
22             e.stopPropagation();
23           }
24         });
25       $('old-password').addEventListener(
26         'keyup', function(e) {
27           if ($('password-changed').disabled)
28             return;
29           $('password-changed-ok-button').disabled = this.value.length == 0;
30         });
31       $('password-changed-cant-remember-link').addEventListener(
32         'click', function(e) {
33           if (this.classList.contains('disabled'))
34             return;
35           var screen = $('password-changed');
36           if (screen.classList.contains('migrate')) {
37             screen.classList.remove('migrate');
38             screen.classList.add('resync');
39             $('password-changed-proceed-button').focus();
40             $('password-changed').classList.remove('password-error');
41             $('old-password').value = '';
42             $('password-changed-ok-button').disabled = true;
43           }
44         });
45     },
47     /**
48      * Screen controls.
49      * @type {array} Array of Buttons.
50      */
51     get buttons() {
52       var buttons = [];
54       var backButton = this.ownerDocument.createElement('button');
55       backButton.id = 'password-changed-back-button';
56       backButton.textContent =
57           loadTimeData.getString('passwordChangedBackButton');
58       backButton.addEventListener('click', function(e) {
59         var screen = $('password-changed');
60         if (screen.classList.contains('migrate')) {
61           screen.cancel();
62         } else {
63           // Resync all data UI step.
64           screen.classList.remove('resync');
65           screen.classList.add('migrate');
66           $('old-password').focus();
67         }
68         e.stopPropagation();
69       });
70       buttons.push(backButton);
72       var okButton = this.ownerDocument.createElement('button');
73       okButton.id = 'password-changed-ok-button';
74       okButton.textContent = loadTimeData.getString('passwordChangedsOkButton');
75       okButton.addEventListener('click', function(e) {
76         $('password-changed').migrate();
77         e.stopPropagation();
78       });
79       buttons.push(okButton);
81       var proceedAnywayButton = this.ownerDocument.createElement('button');
82       proceedAnywayButton.id = 'password-changed-proceed-button';
83       proceedAnywayButton.textContent =
84           loadTimeData.getString('proceedAnywayButton');
85       proceedAnywayButton.addEventListener('click', function(e) {
86         var screen = $('password-changed');
87         if (screen.classList.contains('resync'))
88           $('password-changed').resync();
89         e.stopPropagation();
90       });
91       buttons.push(proceedAnywayButton);
93       return buttons;
94     },
96     /**
97      * Returns a control which should receive an initial focus.
98      */
99     get defaultControl() {
100       return $('old-password');
101     },
103     /**
104      * True if the the screen is disabled (handles no user interaction).
105      * @type {boolean}
106      */
107     disabled_: false,
108     get disabled() {
109       return this.disabled_;
110     },
111     set disabled(value) {
112       this.disabled_ = value;
113       var controls = this.querySelectorAll('button,input');
114       for (var i = 0, control; control = controls[i]; ++i) {
115         control.disabled = value;
116       }
117       $('login-header-bar').disabled = value;
118       $('password-changed-cant-remember-link').classList[
119           value ? 'add' : 'remove']('disabled');
120     },
122     /**
123      * Cancels password migration and drops the user back to the login screen.
124      */
125     cancel: function() {
126       this.disabled = true;
127       chrome.send('cancelPasswordChangedFlow');
128     },
130     /**
131      * Starts migration process using old password that user provided.
132      */
133     migrate: function() {
134       if (!$('old-password').value) {
135         $('old-password').focus();
136         return;
137       }
138       this.disabled = true;
139       chrome.send('migrateUserData', [$('old-password').value]);
140     },
142     /**
143      * Starts migration process by removing old cryptohome and re-syncing data.
144      */
145     resync: function() {
146       this.disabled = true;
147       chrome.send('resyncUserData');
148     },
150     /**
151      * Show password changed screen.
152      * @param {boolean} showError Whether to show the incorrect password error.
153      */
154     show: function(showError) {
155       // We'll get here after the successful online authentication.
156       // It assumes session is about to start so hides login screen controls.
157       Oobe.getInstance().headerHidden = false;
158       var screen = $('password-changed');
159       screen.classList.toggle('password-error', showError);
160       screen.classList.add('migrate');
161       screen.classList.remove('resync');
162       $('old-password').value = '';
163       $('password-changed').disabled = false;
165       Oobe.showScreen({id: SCREEN_PASSWORD_CHANGED});
166       $('password-changed-ok-button').disabled = true;
167     }
168   };