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.
5 cr.define('login', function() {
6 /** @const */ var CALLBACK_USER_ACTED = 'userActed';
8 var OobeScreenBehavior = {
11 * Internal storage of |this.context|. Short name has been choosen for
12 * reason: such name doesn't take much space in HTML data bindings, which
13 * are used very often.
14 * C binded to the native part of the context, that means that all the
15 * changes in the native part appear in C automatically. Reverse is not
16 * true, you should use:
17 * this.context.set(...);
18 * this.context.commitContextChanges();
19 * to send updates to the native part.
20 * TODO(dzhioev): make binding two-way.
28 * The login.Screen which is hosting |this|.
33 * Dictionary of context observers that are methods of |this| bound to
36 contextObservers_: null,
39 * login.ScreenContext used for sharing data with native backend.
44 * Called when the screen is being registered.
46 initialize: function() {},
56 userActed: function(e) {
57 this.send(CALLBACK_USER_ACTED,
58 e.detail.sourceEvent.target.getAttribute('action'));
61 i18n: function(args) {
62 if (!(args instanceof Array))
64 args[0] = 'login_' + this.name + '_' + args[0];
65 return loadTimeData.getStringF.apply(loadTimeData, args);
69 * Called by login.Screen when the screen is beeing registered.
71 decorate: function(screen) {
72 this.screen_ = screen;
73 this.context = screen.screenContext_;
74 this.C = this.context.storage_;
75 this.contextObservers_ = {};
80 this.decorate_ = true;
85 * Should be called for every context field which is used in Polymer
86 * declarative data bindings (e.g. {{C.fieldName}}).
88 registerBoundContextField: function(fieldName) {
89 this.addContextObserver(fieldName, this.onContextFieldChanged_);
92 onContextFieldChanged_: function(_, _, fieldName) {
93 this.notifyPath('C.' + fieldName, this.C[fieldName]);
100 return this.sendImpl_.apply(this, arguments);
106 addContextObserver: function() {
107 return this.addContextObserverImpl_.apply(this, arguments);
113 removeContextObserver: function() {
114 return this.removeContextObserverImpl_.apply(this, arguments);
120 commitContextChanges: function() {
121 return this.commitContextChangesImpl_.apply(this, arguments);
128 querySelector: function() {
129 return this.querySelectorImpl_.apply(this, arguments);
136 querySelectorAll: function() {
137 return this.querySelectorAllImpl_.apply(this, arguments);
141 * See login.Screen.send.
144 sendImpl_: function() {
145 return this.screen_.send.apply(this.screen_, arguments);
149 * Starts observation of property with |key| of the context attached to
150 * current screen. This method differs from "login.ScreenContext" in that
151 * it automatically detects if observer is method of |this| and make
152 * all needed actions to make it work correctly. So it's no need for client
153 * to bind methods to |this| and keep resulting callback for
154 * |removeObserver| call:
156 * this.addContextObserver('key', this.onKeyChanged_);
158 * this.removeContextObserver('key', this.onKeyChanged_);
161 addContextObserverImpl_: function(key, observer) {
162 var realObserver = observer;
163 var propertyName = this.getPropertyNameOf_(observer);
165 if (!this.contextObservers_.hasOwnProperty(propertyName))
166 this.contextObservers_[propertyName] = observer.bind(this);
167 realObserver = this.contextObservers_[propertyName];
169 this.context.addObserver(key, realObserver);
173 * Removes |observer| from the list of context observers. Supports not only
174 * regular functions but also screen methods (see comment to
175 * |addContextObserver|).
178 removeContextObserverImpl_: function(observer) {
179 var realObserver = observer;
180 var propertyName = this.getPropertyNameOf_(observer);
182 if (!this.contextObservers_.hasOwnProperty(propertyName))
184 realObserver = this.contextObservers_[propertyName];
185 delete this.contextObservers_[propertyName];
187 this.context.removeObserver(realObserver);
191 * See login.Screen.commitContextChanges.
194 commitContextChangesImpl_: function() {
195 return this.screen_.commitContextChanges.apply(this.screen_, arguments);
199 * Calls |querySelector| method of the shadow dom and returns the result.
202 querySelectorImpl_: function(selector) {
203 return this.shadowRoot.querySelector(selector);
208 * Calls standart |querySelectorAll| method of the shadow dom and returns
209 * the result converted to Array.
212 querySelectorAllImpl_: function(selector) {
213 var list = this.shadowRoot.querySelectorAll(selector);
214 return Array.prototype.slice.call(list);
218 * If |value| is the value of some property of |this| returns property's
219 * name. Otherwise returns empty string.
222 getPropertyNameOf_: function(value) {
223 for (var key in this)
224 if (this[key] === value)
231 OobeScreenBehavior: OobeScreenBehavior