1 // Copyright 2014 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.
6 * @fileoverview Base class for all login WebUI screens.
8 cr
.define('login', function() {
9 /** @const */ var CALLBACK_USER_ACTED
= 'userActed';
10 /** @const */ var CALLBACK_CONTEXT_CHANGED
= 'contextChanged';
12 function doNothing() {};
14 var querySelectorAll
= HTMLDivElement
.prototype.querySelectorAll
;
16 var Screen = function(sendPrefix
) {
17 this.sendPrefix_
= sendPrefix
;
18 this.screenContext_
= null;
19 this.contextObservers_
= {};
23 __proto__
: HTMLDivElement
.prototype,
26 * Prefix added to sent to Chrome messages' names.
31 * Context used by this screen.
36 return this.screenContext_
;
40 * Dictionary of context observers that are methods of |this| bound to
43 contextObservers_
: null,
46 * Called during screen registration.
51 * Returns minimal size that screen prefers to have. Default implementation
52 * returns current screen size.
53 * @return {{width: number, height: number}}
55 getPreferredSize: function() {
56 return {width
: this.offsetWidth
, height
: this.offsetHeight
};
60 * Called for currently active screen when screen size changed.
62 onWindowResize
: doNothing
,
67 initialize: function() {
68 return this.initializeImpl_
.apply(this, arguments
);
75 return this.sendImpl_
.apply(this, arguments
);
81 addContextObserver: function() {
82 return this.addContextObserverImpl_
.apply(this, arguments
);
88 removeContextObserver: function() {
89 return this.removeContextObserverImpl_
.apply(this, arguments
);
95 commitContextChanges: function() {
96 return this.commitContextChangesImpl_
.apply(this, arguments
);
103 querySelectorAll: function() {
104 return this.querySelectorAllImpl_
.apply(this, arguments
);
108 * Does the following things:
109 * * Creates screen context.
110 * * Looks for elements having "alias" property and adds them as the
111 * proprties of the screen with name equal to value of "alias", i.e. HTML
112 * element <div alias="myDiv"></div> will be stored in this.myDiv.
113 * * Looks for buttons having "action" properties and adds click handlers
114 * to them. These handlers send |CALLBACK_USER_ACTED| messages to
115 * C++ with "action" property's value as payload.
118 initializeImpl_: function() {
119 this.screenContext_
= new login
.ScreenContext();
120 this.querySelectorAllImpl_('[alias]').forEach(function(element
) {
121 var alias
= element
.getAttribute('alias');
123 throw Error('Alias "' + alias
+ '" of "' + this.name() + '" screen ' +
124 'shadows or redefines property that is already defined.');
125 this[alias
] = element
;
126 this[element
.getAttribute('alias')] = element
;
129 this.querySelectorAllImpl_('button[action]').forEach(function(button
) {
130 button
.addEventListener('click', function(e
) {
131 var action
= this.getAttribute('action');
132 self
.send(CALLBACK_USER_ACTED
, action
);
139 * Sends message to Chrome, adding needed prefix to message name. All
140 * arguments after |messageName| are packed into message parameters list.
142 * @param {string} messageName Name of message without a prefix.
143 * @param {...*} varArgs parameters for message.
146 sendImpl_: function(messageName
, varArgs
) {
147 if (arguments
.length
== 0)
148 throw Error('Message name is not provided.');
149 var fullMessageName
= this.sendPrefix_
+ messageName
;
150 var payload
= Array
.prototype.slice
.call(arguments
, 1);
151 chrome
.send(fullMessageName
, payload
);
155 * Starts observation of property with |key| of the context attached to
156 * current screen. This method differs from "login.ScreenContext" in that
157 * it automatically detects if observer is method of |this| and make
158 * all needed actions to make it work correctly. So it's no need for client
159 * to bind methods to |this| and keep resulting callback for
160 * |removeObserver| call:
162 * this.addContextObserver('key', this.onKeyChanged_);
164 * this.removeContextObserver('key', this.onKeyChanged_);
167 addContextObserverImpl_: function(key
, observer
) {
168 var realObserver
= observer
;
169 var propertyName
= this.getPropertyNameOf_(observer
);
171 if (!this.contextObservers_
.hasOwnProperty(propertyName
))
172 this.contextObservers_
[propertyName
] = observer
.bind(this);
173 realObserver
= this.contextObservers_
[propertyName
];
175 this.screenContext_
.addObserver(key
, realObserver
);
179 * Removes |observer| from the list of context observers. Supports not only
180 * regular functions but also screen methods (see comment to
181 * |addContextObserver|).
184 removeContextObserverImpl_: function(observer
) {
185 var realObserver
= observer
;
186 var propertyName
= this.getPropertyNameOf_(observer
);
188 if (!this.contextObservers_
.hasOwnProperty(propertyName
))
190 realObserver
= this.contextObservers_
[propertyName
];
191 delete this.contextObservers_
[propertyName
];
193 this.screenContext_
.removeObserver(realObserver
);
197 * Sends recent context changes to C++ handler.
200 commitContextChangesImpl_: function() {
201 if (!this.screenContext_
.hasChanges())
203 this.sendImpl_(CALLBACK_CONTEXT_CHANGED
,
204 this.screenContext_
.getChangesAndReset());
208 * Calls standart |querySelectorAll| method and returns its result converted
212 querySelectorAllImpl_: function(selector
) {
213 var list
= querySelectorAll
.call(this, selector
);
214 return Array
.prototype.slice
.call(list
);
218 * Called when context changes are recieved from C++.
221 contextChanged_: function(diff
) {
222 this.screenContext_
.applyChanges(diff
);
226 * If |value| is the value of some property of |this| returns property's
227 * name. Otherwise returns empty string.
230 getPropertyNameOf_: function(value
) {
231 for (var key
in this)
232 if (this[key
] === value
)
238 Screen
.CALLBACK_USER_ACTED
= CALLBACK_USER_ACTED
;
245 cr
.define('login', function() {
248 * Creates class and object for screen.
249 * Methods specified in EXTERNAL_API array of prototype
250 * will be available from C++ part.
252 * login.createScreen('ScreenName', 'screen-id', {
253 * foo: function() { console.log('foo'); },
254 * bar: function() { console.log('bar'); }
255 * EXTERNAL_API: ['foo'];
257 * login.ScreenName.register();
258 * var screen = $('screen-id');
259 * screen.foo(); // valid
260 * login.ScreenName.foo(); // valid
261 * screen.bar(); // valid
262 * login.ScreenName.bar(); // invalid
264 * @param {string} name Name of created class.
265 * @param {string} id Id of div representing screen.
266 * @param {(function()|Object)} proto Prototype of object or function that
269 createScreen: function(name
, id
, template
) {
270 if (typeof template
== 'function')
271 template
= template();
273 var apiNames
= template
.EXTERNAL_API
|| [];
274 for (var i
= 0; i
< apiNames
.length
; ++i
) {
275 var methodName
= apiNames
[i
];
276 if (typeof template
[methodName
] !== 'function')
277 throw Error('External method "' + methodName
+ '" for screen "' +
278 name
+ '" not a function or undefined.');
281 function checkPropertyAllowed(propertyName
) {
282 if (propertyName
.charAt(propertyName
.length
- 1) === '_' &&
283 (propertyName
in login
.Screen
.prototype)) {
284 throw Error('Property "' + propertyName
+ '" of "' + id
+ '" ' +
285 'shadows private property of login.Screen prototype.');
289 var Constructor = function() {
290 login
.Screen
.call(this, 'login.' + name
+ '.');
292 Constructor
.prototype = Object
.create(login
.Screen
.prototype);
295 Object
.getOwnPropertyNames(template
).forEach(function(propertyName
) {
296 if (propertyName
=== 'EXTERNAL_API')
299 checkPropertyAllowed(propertyName
);
302 Object
.getOwnPropertyDescriptor(template
, propertyName
);
303 Object
.defineProperty(Constructor
.prototype, propertyName
, descriptor
);
305 if (apiNames
.indexOf(propertyName
) >= 0) {
306 api
[propertyName
] = function() {
308 return screen
[propertyName
].apply(screen
, arguments
);
313 Constructor
.prototype.name = function() { return id
; };
315 api
.contextChanged = function() {
317 screen
.contextChanged_
.apply(screen
, arguments
);
320 api
.register = function() {
322 screen
.__proto__
= new Constructor();
324 Oobe
.getInstance().registerScreen(screen
);
327 cr
.define('login', function() {