1 // Copyright (c) 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.
5 Polymer('oobe-screen', (function() {
6 /** @const */ var CALLBACK_USER_ACTED
= 'userActed';
8 function doNothing() {};
12 * The login.Screen which is hosting |this|.
17 * Dictionary of context observers that are methods of |this| bound to
20 contextObservers_
: null,
23 * login.ScreenContext used for sharing data with native backend.
28 * Internal storage of |this.context|. Short name has been choosen for
29 * reason: such name doesn't take much space in HTML data bindings, which
30 * are used very often.
31 * C binded to the native part of the context, that means that all the
32 * changes in the native part appear in C automatically. Reverse is not
33 * true, you should use:
34 * this.context.set(...);
35 * this.context.commitContextChanges();
36 * to send updates to the native part.
37 * TODO(dzhioev): make binding two-way.
42 * Called when the screen is being registered.
44 initialize
: doNothing
,
54 userActed: function(_
, _
, source
) {
55 this.send(CALLBACK_USER_ACTED
, source
.getAttribute('action'));
58 i18n: function(args
) {
59 if (!(args
instanceof Array
))
61 args
[0] = 'login_' + this.name
+ '_' + args
[0];
62 return loadTimeData
.getStringF
.apply(loadTimeData
, args
);
66 * Called by login.Screen when the screen is beeing registered.
68 decorate: function(screen
) {
69 this.screen_
= screen
;
70 this.context
= screen
.screenContext_
;
71 this.C
= this.context
.storage_
;
72 this.contextObservers_
= {};
77 this.decorate_
= true;
85 return this.sendImpl_
.apply(this, arguments
);
91 addContextObserver: function() {
92 return this.addContextObserverImpl_
.apply(this, arguments
);
98 removeContextObserver: function() {
99 return this.removeContextObserverImpl_
.apply(this, arguments
);
105 commitContextChanges: function() {
106 return this.commitContextChangesImpl_
.apply(this, arguments
);
113 querySelector: function() {
114 return this.querySelectorImpl_
.apply(this, arguments
);
121 querySelectorAll: function() {
122 return this.querySelectorAllImpl_
.apply(this, arguments
);
126 * See login.Screen.send.
129 sendImpl_: function() {
130 return this.screen_
.send
.apply(this.screen_
, arguments
);
134 * Starts observation of property with |key| of the context attached to
135 * current screen. This method differs from "login.ScreenContext" in that
136 * it automatically detects if observer is method of |this| and make
137 * all needed actions to make it work correctly. So it's no need for client
138 * to bind methods to |this| and keep resulting callback for
139 * |removeObserver| call:
141 * this.addContextObserver('key', this.onKeyChanged_);
143 * this.removeContextObserver('key', this.onKeyChanged_);
146 addContextObserverImpl_: function(key
, observer
) {
147 var realObserver
= observer
;
148 var propertyName
= this.getPropertyNameOf_(observer
);
150 if (!this.contextObservers_
.hasOwnProperty(propertyName
))
151 this.contextObservers_
[propertyName
] = observer
.bind(this);
152 realObserver
= this.contextObservers_
[propertyName
];
154 this.context
.addObserver(key
, realObserver
);
158 * Removes |observer| from the list of context observers. Supports not only
159 * regular functions but also screen methods (see comment to
160 * |addContextObserver|).
163 removeContextObserverImpl_: function(observer
) {
164 var realObserver
= observer
;
165 var propertyName
= this.getPropertyNameOf_(observer
);
167 if (!this.contextObservers_
.hasOwnProperty(propertyName
))
169 realObserver
= this.contextObservers_
[propertyName
];
170 delete this.contextObservers_
[propertyName
];
172 this.context
.removeObserver(realObserver
);
176 * See login.Screen.commitContextChanges.
179 commitContextChangesImpl_: function() {
180 return this.screen_
.commitContextChanges
.apply(this.screen_
, arguments
);
184 * Calls |querySelector| method of the shadow dom and returns the result.
187 querySelectorImpl_: function(selector
) {
188 return this.shadowRoot
.querySelector(selector
);
193 * Calls standart |querySelectorAll| method of the shadow dom and returns
194 * the result converted to Array.
197 querySelectorAllImpl_: function(selector
) {
198 var list
= this.shadowRoot
.querySelectorAll(selector
);
199 return Array
.prototype.slice
.call(list
);
203 * If |value| is the value of some property of |this| returns property's
204 * name. Otherwise returns empty string.
207 getPropertyNameOf_: function(value
) {
208 for (var key
in this)
209 if (this[key
] === value
)