Home | History | Annotate | Download | only in chromeos
      1 // Copyright (c) 2012 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 This file contains methods that allow to tweak
      7  * internal page UI based on the status of current user (owner/user/guest).
      8  * It is assumed that required data is passed via i18n strings
      9  * (using loadTimeData dictionary) that are filled with call to
     10  * AddAccountUITweaksLocalizedValues in ui_account_tweaks.cc.
     11  * It is also assumed that tweaked page has chrome://resources/css/widgets.css
     12  * included.
     13  */
     14 
     15 cr.define('uiAccountTweaks', function() {
     16 
     17   /////////////////////////////////////////////////////////////////////////////
     18   // UIAccountTweaks class:
     19 
     20   // String specificators for different types of sessions.
     21   /** @const */ var SESSION_TYPE_GUEST = 'guest';
     22   /** @const */ var SESSION_TYPE_PUBLIC = 'public-account';
     23 
     24   /**
     25    * Encapsulated handling of ChromeOS accounts options page.
     26    * @constructor
     27    */
     28   function UIAccountTweaks() {
     29   }
     30 
     31   /**
     32    * @return {boolean} Whether the current user is owner or not.
     33    */
     34   UIAccountTweaks.currentUserIsOwner = function() {
     35     return loadTimeData.getBoolean('currentUserIsOwner');
     36   };
     37 
     38   /**
     39    * @return {boolean} Whether we're currently in guest session.
     40    */
     41   UIAccountTweaks.loggedInAsGuest = function() {
     42     return loadTimeData.getBoolean('loggedInAsGuest');
     43   };
     44 
     45   /**
     46    * @return {boolean} Whether we're currently in public session.
     47    */
     48   UIAccountTweaks.loggedInAsPublicAccount = function() {
     49     return loadTimeData.getBoolean('loggedInAsPublicAccount');
     50   };
     51 
     52   /**
     53    * @return {boolean} Whether we're currently in supervised user mode.
     54    */
     55   UIAccountTweaks.loggedInAsSupervisedUser = function() {
     56     return loadTimeData.getBoolean('loggedInAsSupervisedUser');
     57   };
     58 
     59   /**
     60    * Disables or hides some elements in specified type of session in ChromeOS.
     61    * All elements within given document with *sessionType*-visibility
     62    * attribute are either hidden (for *sessionType*-visibility="hidden")
     63    * or disabled (for *sessionType*-visibility="disabled").
     64    *
     65    * @param {Document} document Document that should processed.
     66    * @param {string} sessionType name of the session type processed.
     67    * @private
     68    */
     69   UIAccountTweaks.applySessionTypeVisibility_ = function(document,
     70                                                          sessionType) {
     71     var elements = document.querySelectorAll('['+ sessionType +'-visibility]');
     72     for (var i = 0; i < elements.length; i++) {
     73       var element = elements[i];
     74       var visibility = element.getAttribute(sessionType +'-visibility');
     75       if (visibility == 'hidden')
     76         element.hidden = true;
     77       else if (visibility == 'disabled')
     78         UIAccountTweaks.disableElementsForSessionType(element, sessionType);
     79     }
     80   }
     81 
     82   /**
     83    * Updates specific visibility of elements for Guest session in ChromeOS.
     84    * Calls applySessionTypeVisibility_ method.
     85    *
     86    * @param {Document} document Document that should processed.
     87    */
     88   UIAccountTweaks.applyGuestSessionVisibility = function(document) {
     89     if (!cr.isChromeOS || !UIAccountTweaks.loggedInAsGuest())
     90       return;
     91     UIAccountTweaks.applySessionTypeVisibility_(document, SESSION_TYPE_GUEST);
     92   }
     93 
     94   /**
     95    * Updates specific visibility of elements for Public account session in
     96    * ChromeOS. Calls applySessionTypeVisibility_ method.
     97    *
     98    * @param {Document} document Document that should processed.
     99    */
    100   UIAccountTweaks.applyPublicSessionVisibility = function(document) {
    101     if (!cr.isChromeOS || !UIAccountTweaks.loggedInAsPublicAccount())
    102       return;
    103     UIAccountTweaks.applySessionTypeVisibility_(document, SESSION_TYPE_PUBLIC);
    104   }
    105 
    106   /**
    107    * Disables and marks page elements for specified session type.
    108    * Adds #-disabled css class to all elements within given subtree,
    109    * disables interactive elements (input/select/button), and removes href
    110    * attribute from <a> elements.
    111    *
    112    * @param {!Element} element Root element of DOM subtree that should be
    113    *     disabled.
    114    * @param {string} sessionType session type specificator.
    115    */
    116   UIAccountTweaks.disableElementsForSessionType = function(element,
    117                                                            sessionType) {
    118     UIAccountTweaks.disableElementForSessionType_(element, sessionType);
    119 
    120     // Walk the tree, searching each ELEMENT node.
    121     var walker = document.createTreeWalker(element,
    122                                            NodeFilter.SHOW_ELEMENT,
    123                                            null,
    124                                            false);
    125 
    126     var node = walker.nextNode();
    127     while (node) {
    128       UIAccountTweaks.disableElementForSessionType_(
    129           /** @type {!Element} */(node), sessionType);
    130       node = walker.nextNode();
    131     }
    132   };
    133 
    134   /**
    135    * Disables single element for given session type.
    136    * Adds *sessionType*-disabled css class, adds disabled attribute for
    137    * appropriate elements (input/select/button), and removes href attribute from
    138    * <a> element.
    139    *
    140    * @private
    141    * @param {!Element} element Element that should be disabled.
    142    * @param {string} sessionType account session Type specificator.
    143    */
    144   UIAccountTweaks.disableElementForSessionType_ = function(element,
    145                                                            sessionType) {
    146     element.classList.add(sessionType + '-disabled');
    147     if (element.nodeName == 'INPUT' ||
    148         element.nodeName == 'SELECT' ||
    149         element.nodeName == 'BUTTON') {
    150       element.disabled = true;
    151     } else if (element.nodeName == 'A') {
    152       element.onclick = function() {
    153         return false;
    154       };
    155     }
    156   };
    157 
    158   // Export
    159   return {
    160     UIAccountTweaks: UIAccountTweaks
    161   };
    162 
    163 });
    164