1 // Copyright (c) 2013 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 Base class for all login WebUI screens. 7 */ 8 cr.define('login', function() { 9 var Screen = cr.ui.define('div'); 10 11 Screen.prototype = { 12 __proto__: HTMLDivElement.prototype, 13 14 decorate: function() { 15 } 16 }; 17 18 return { 19 Screen: Screen 20 }; 21 }); 22 23 cr.define('login', function() { 24 return { 25 /** 26 * Creates class and object for screen. 27 * Methods specified in EXTERNAL_API array of prototype 28 * will be available from C++ part. 29 * Example: 30 * login.createScreen('ScreenName', 'screen-id', { 31 * foo: function() { console.log('foo'); }, 32 * bar: function() { console.log('bar'); } 33 * EXTERNAL_API: ['foo']; 34 * }); 35 * login.ScreenName.register(); 36 * var screen = $('screen-id'); 37 * screen.foo(); // valid 38 * login.ScreenName.foo(); // valid 39 * screen.bar(); // valid 40 * login.ScreenName.bar(); // invalid 41 * 42 * @param {string} name Name of created class. 43 * @param {string} id Id of div representing screen. 44 * @param {(function()|Object)} proto Prototype of object or function that 45 * returns prototype. 46 */ 47 createScreen: function(name, id, proto) { 48 if (typeof proto == 'function') 49 proto = proto(); 50 cr.define('login', function() { 51 var api = proto.EXTERNAL_API || []; 52 for (var i = 0; i < api.length; ++i) { 53 var methodName = api[i]; 54 if (typeof proto[methodName] !== 'function') 55 throw Error('External method "' + methodName + '" for screen "' + 56 name + '" not a function or undefined.'); 57 } 58 59 var constructor = cr.ui.define(login.Screen); 60 constructor.prototype = Object.create(login.Screen.prototype); 61 Object.getOwnPropertyNames(proto).forEach(function(propertyName) { 62 var descriptor = 63 Object.getOwnPropertyDescriptor(proto, propertyName); 64 Object.defineProperty(constructor.prototype, 65 propertyName, descriptor); 66 if (api.indexOf(propertyName) >= 0) { 67 constructor[propertyName] = (function(x) { 68 return function() { 69 var screen = $(id); 70 return screen[x].apply(screen, arguments); 71 }; 72 })(propertyName); 73 } 74 }); 75 constructor.prototype.name = function() { return id; }; 76 77 constructor.register = function() { 78 var screen = $(id); 79 constructor.decorate(screen); 80 Oobe.getInstance().registerScreen(screen); 81 }; 82 83 var result = {}; 84 result[name] = constructor; 85 return result; 86 }); 87 } 88 }; 89 }); 90 91