Home | History | Annotate | Download | only in login
      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   function doNothing() {};
     12 
     13   Screen.prototype = {
     14     __proto__: HTMLDivElement.prototype,
     15 
     16     decorate: doNothing,
     17 
     18     /**
     19      * Returns minimal size that screen prefers to have. Default implementation
     20      * returns current screen size.
     21      * @return {{width: number, height: number}}
     22      */
     23     getPreferredSize: function() {
     24       return {width: this.offsetWidth, height: this.offsetHeight};
     25     },
     26 
     27     /**
     28      * Called for currently active screen when screen size changed.
     29      */
     30     onWindowResize: doNothing
     31   };
     32 
     33   return {
     34     Screen: Screen
     35   };
     36 });
     37 
     38 cr.define('login', function() {
     39   return {
     40     /**
     41      * Creates class and object for screen.
     42      * Methods specified in EXTERNAL_API array of prototype
     43      * will be available from C++ part.
     44      * Example:
     45      *     login.createScreen('ScreenName', 'screen-id', {
     46      *       foo: function() { console.log('foo'); },
     47      *       bar: function() { console.log('bar'); }
     48      *       EXTERNAL_API: ['foo'];
     49      *     });
     50      *     login.ScreenName.register();
     51      *     var screen = $('screen-id');
     52      *     screen.foo(); // valid
     53      *     login.ScreenName.foo(); // valid
     54      *     screen.bar(); // valid
     55      *     login.ScreenName.bar(); // invalid
     56      *
     57      * @param {string} name Name of created class.
     58      * @param {string} id Id of div representing screen.
     59      * @param {(function()|Object)} proto Prototype of object or function that
     60      *     returns prototype.
     61      */
     62     createScreen: function(name, id, proto) {
     63       if (typeof proto == 'function')
     64         proto = proto();
     65       cr.define('login', function() {
     66         var api = proto.EXTERNAL_API || [];
     67         for (var i = 0; i < api.length; ++i) {
     68           var methodName = api[i];
     69           if (typeof proto[methodName] !== 'function')
     70             throw Error('External method "' + methodName + '" for screen "' +
     71                 name + '" not a function or undefined.');
     72         }
     73 
     74         var constructor = cr.ui.define(login.Screen);
     75         constructor.prototype = Object.create(login.Screen.prototype);
     76         Object.getOwnPropertyNames(proto).forEach(function(propertyName) {
     77           var descriptor =
     78               Object.getOwnPropertyDescriptor(proto, propertyName);
     79           Object.defineProperty(constructor.prototype,
     80               propertyName, descriptor);
     81           if (api.indexOf(propertyName) >= 0) {
     82             constructor[propertyName] = (function(x) {
     83               return function() {
     84                 var screen = $(id);
     85                 return screen[x].apply(screen, arguments);
     86               };
     87             })(propertyName);
     88           }
     89         });
     90         constructor.prototype.name = function() { return id; };
     91 
     92         constructor.register = function() {
     93           var screen = $(id);
     94           constructor.decorate(screen);
     95           Oobe.getInstance().registerScreen(screen);
     96         };
     97 
     98         var result = {};
     99         result[name] = constructor;
    100         return result;
    101       });
    102     }
    103   };
    104 });
    105 
    106