OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 Polymer('oobe-screen', (function() { |
| 6 /** @const */ var CALLBACK_USER_ACTED = 'userActed'; |
| 7 |
| 8 function doNothing() {}; |
| 9 |
| 10 return { |
| 11 /** |
| 12 * The login.Screen which is hosting |this|. |
| 13 */ |
| 14 screen_: null, |
| 15 |
| 16 /** |
| 17 * Dictionary of context observers that are methods of |this| bound to |
| 18 * |this|. |
| 19 */ |
| 20 contextObservers_: null, |
| 21 |
| 22 /** |
| 23 * login.ScreenContext used for sharing data with native backend. |
| 24 */ |
| 25 context: null, |
| 26 |
| 27 /** |
| 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 automticaly. Reverse is not true, |
| 33 * 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. |
| 38 */ |
| 39 C: null, |
| 40 |
| 41 /** |
| 42 * Called when the screen is beeing registered. |
| 43 */ |
| 44 initialize: doNothing, |
| 45 |
| 46 ready: function() { |
| 47 if (this.decorate_) { |
| 48 this.initialize(); |
| 49 } else { |
| 50 this.ready_ = true; |
| 51 } |
| 52 }, |
| 53 |
| 54 i18n: function(args) { |
| 55 if (!(args instanceof Array)) |
| 56 args = [args]; |
| 57 args[0] = 'login_' + this.name + '_' + args[0]; |
| 58 return loadTimeData.getStringF.apply(loadTimeData, args); |
| 59 }, |
| 60 |
| 61 /** |
| 62 * Called by login.Screen when the screen is beeing registered. |
| 63 */ |
| 64 decorate: function(screen) { |
| 65 this.screen_ = screen; |
| 66 screen.initialize(); |
| 67 this.context = screen.screenContext_; |
| 68 this.C = this.context.storage_; |
| 69 this.contextObservers_ = {}; |
| 70 var self = this; |
| 71 this.querySelectorAllImpl_('button[action]').forEach(function(button) { |
| 72 button.addEventListener('click', function(e) { |
| 73 var action = this.getAttribute('action'); |
| 74 self.send(CALLBACK_USER_ACTED, action); |
| 75 e.stopPropagation(); |
| 76 }); |
| 77 }); |
| 78 if (this.ready_) { |
| 79 this.initialize(); |
| 80 } else { |
| 81 this.decorate_ = true; |
| 82 } |
| 83 }, |
| 84 |
| 85 /** |
| 86 * @final |
| 87 */ |
| 88 send: function() { |
| 89 return this.sendImpl_.apply(this, arguments); |
| 90 }, |
| 91 |
| 92 /** |
| 93 * @final |
| 94 */ |
| 95 addContextObserver: function() { |
| 96 return this.addContextObserverImpl_.apply(this, arguments); |
| 97 }, |
| 98 |
| 99 /** |
| 100 * @final |
| 101 */ |
| 102 removeContextObserver: function() { |
| 103 return this.removeContextObserverImpl_.apply(this, arguments); |
| 104 }, |
| 105 |
| 106 /** |
| 107 * @final |
| 108 */ |
| 109 commitContextChanges: function() { |
| 110 return this.commitContextChangesImpl_.apply(this, arguments); |
| 111 }, |
| 112 |
| 113 /** |
| 114 * @override |
| 115 * @final |
| 116 */ |
| 117 querySelector: function() { |
| 118 return this.querySelectorImpl_.apply(this, arguments); |
| 119 }, |
| 120 |
| 121 /** |
| 122 * @override |
| 123 * @final |
| 124 */ |
| 125 querySelectorAll: function() { |
| 126 return this.querySelectorAllImpl_.apply(this, arguments); |
| 127 }, |
| 128 |
| 129 /** |
| 130 * See login.Screen.send. |
| 131 * @private |
| 132 */ |
| 133 sendImpl_: function() { |
| 134 return this.screen_.send.apply(this.screen_, arguments); |
| 135 }, |
| 136 |
| 137 /** |
| 138 * Starts observation of property with |key| of the context attached to |
| 139 * current screen. This method differs from "login.ScreenContext" in that |
| 140 * it automatically detects if observer is method of |this| and make |
| 141 * all needed actions to make it work correctly. So it's no need for client |
| 142 * to bind methods to |this| and keep resulting callback for |
| 143 * |removeObserver| call: |
| 144 * |
| 145 * this.addContextObserver('key', this.onKeyChanged_); |
| 146 * ... |
| 147 * this.removeContextObserver('key', this.onKeyChanged_); |
| 148 * @private |
| 149 */ |
| 150 addContextObserverImpl_: function(key, observer) { |
| 151 var realObserver = observer; |
| 152 var propertyName = this.getPropertyNameOf_(observer); |
| 153 if (propertyName) { |
| 154 if (!this.contextObservers_.hasOwnProperty(propertyName)) |
| 155 this.contextObservers_[propertyName] = observer.bind(this); |
| 156 realObserver = this.contextObservers_[propertyName]; |
| 157 } |
| 158 this.context.addObserver(key, realObserver); |
| 159 }, |
| 160 |
| 161 /** |
| 162 * Removes |observer| from the list of context observers. Supports not only |
| 163 * regular functions but also screen methods (see comment to |
| 164 * |addContextObserver|). |
| 165 * @private |
| 166 */ |
| 167 removeContextObserverImpl_: function(observer) { |
| 168 var realObserver = observer; |
| 169 var propertyName = this.getPropertyNameOf_(observer); |
| 170 if (propertyName) { |
| 171 if (!this.contextObservers_.hasOwnProperty(propertyName)) |
| 172 return; |
| 173 realObserver = this.contextObservers_[propertyName]; |
| 174 delete this.contextObservers_[propertyName]; |
| 175 } |
| 176 this.context.removeObserver(realObserver); |
| 177 }, |
| 178 |
| 179 /** |
| 180 * See login.Screen.commitContextChanges. |
| 181 * @private |
| 182 */ |
| 183 commitContextChangesImpl_: function() { |
| 184 return this.screen_.commitContextChanges.apply(this.screen_, arguments); |
| 185 }, |
| 186 |
| 187 /** |
| 188 * Calls |querySelector| method of the shadow dom and returns the result. |
| 189 * @private |
| 190 */ |
| 191 querySelectorImpl_: function(selector) { |
| 192 return this.shadowRoot.querySelector(selector); |
| 193 }, |
| 194 |
| 195 |
| 196 /** |
| 197 * Calls standart |querySelectorAll| method of the shadow dom and returns |
| 198 * the result converted to Array. |
| 199 * @private |
| 200 */ |
| 201 querySelectorAllImpl_: function(selector) { |
| 202 var list = this.shadowRoot.querySelectorAll(selector); |
| 203 return Array.prototype.slice.call(list); |
| 204 }, |
| 205 |
| 206 /** |
| 207 * If |value| is the value of some property of |this| returns property's |
| 208 * name. Otherwise returns empty string. |
| 209 * @private |
| 210 */ |
| 211 getPropertyNameOf_: function(value) { |
| 212 for (var key in this) |
| 213 if (this[key] === value) |
| 214 return key; |
| 215 return ''; |
| 216 } |
| 217 }; |
| 218 })()); |
| 219 |
OLD | NEW |