Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / chromeos / login / screen_password_changed.js
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 /**
6  * @fileoverview Password changed screen implementation.
7  */
8
9 login.createScreen('PasswordChangedScreen', 'password-changed', function() {
10   return {
11     EXTERNAL_API: [
12       'show'
13     ],
14
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     },
46
47     /**
48      * Screen controls.
49      * @type {array} Array of Buttons.
50      */
51     get buttons() {
52       var buttons = [];
53
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);
71
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);
80
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);
92
93       return buttons;
94     },
95
96     /**
97      * Returns a control which should receive an initial focus.
98      */
99     get defaultControl() {
100       return $('old-password');
101     },
102
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     },
121
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     },
129
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     },
141
142     /**
143      * Event handler that is invoked just before the screen is hidden.
144      */
145     onBeforeHide: function() {
146       $('login-header-bar').disabled = false;
147     },
148
149     /**
150      * Starts migration process by removing old cryptohome and re-syncing data.
151      */
152     resync: function() {
153       this.disabled = true;
154       chrome.send('resyncUserData');
155     },
156
157     /**
158      * Show password changed screen.
159      * @param {boolean} showError Whether to show the incorrect password error.
160      */
161     show: function(showError) {
162       // We'll get here after the successful online authentication.
163       // It assumes session is about to start so hides login screen controls.
164       Oobe.getInstance().headerHidden = false;
165       var screen = $('password-changed');
166       screen.classList.toggle('password-error', showError);
167       screen.classList.add('migrate');
168       screen.classList.remove('resync');
169       $('old-password').value = '';
170       $('password-changed').disabled = false;
171
172       Oobe.showScreen({id: SCREEN_PASSWORD_CHANGED});
173       $('password-changed-ok-button').disabled = true;
174     }
175   };
176 });
177