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 templateData variable) 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   /**
     21    * Encapsulated handling of ChromeOS accounts options page.
     22    * @constructor
     23    */
     24   function UIAccountTweaks() {
     25   }
     26 
     27   /**
     28    * @return {boolean} Whether the current user is owner or not.
     29    */
     30   UIAccountTweaks.currentUserIsOwner = function() {
     31     return templateData['currentUserIsOwner'] == 'true';
     32   };
     33 
     34   /**
     35    * @return {boolean} Whether we're currently in guest mode.
     36    */
     37   UIAccountTweaks.loggedInAsGuest = function() {
     38     return templateData['loggedInAsGuest'] == 'true';
     39   };
     40 
     41   /**
     42    * Disables or hides some elements in Guest mode in ChromeOS.
     43    * All elements within given document with guest-visibility
     44    * attribute are either hidden (for guest-visibility="hidden")
     45    * or disabled (for guest-visibility="disabled").
     46    *
     47    * @param {Document} document Document that should processed.
     48    */
     49   UIAccountTweaks.applyGuestModeVisibility = function(document) {
     50     if (!cr.isChromeOS || !UIAccountTweaks.loggedInAsGuest())
     51       return;
     52     var elements = document.querySelectorAll('[guest-visibility]');
     53     for (var i = 0; i < elements.length; i++) {
     54       var element = elements[i];
     55       var visibility = element.getAttribute('guest-visibility');
     56       if (visibility == 'hidden')
     57         element.hidden = true;
     58       else if (visibility == 'disabled')
     59         UIAccountTweaks.disableElementsForGuest(element);
     60     }
     61   }
     62 
     63   /**
     64    * Disables and marks page elements for Guest mode.
     65    * Adds guest-disabled css class to all elements within given subtree,
     66    * disables interactive elements (input/select/button), and removes href
     67    * attribute from <a> elements.
     68    *
     69    * @param {Element} element Root element of DOM subtree that should be
     70    *     disabled.
     71    */
     72   UIAccountTweaks.disableElementsForGuest = function(element) {
     73     UIAccountTweaks.disableElementForGuest_(element);
     74 
     75     // Walk the tree, searching each ELEMENT node.
     76     var walker = document.createTreeWalker(element,
     77                                            NodeFilter.SHOW_ELEMENT,
     78                                            null,
     79                                            false);
     80 
     81     var node = walker.nextNode();
     82     while (node) {
     83       UIAccountTweaks.disableElementForGuest_(node);
     84       node = walker.nextNode();
     85     }
     86   };
     87 
     88   /**
     89    * Disables single element for Guest mode.
     90    * Adds guest-disabled css class, adds disabled attribute for appropriate
     91    * elements (input/select/button), and removes href attribute from
     92    * <a> element.
     93    *
     94    * @private
     95    * @param {Element} element Element that should be disabled.
     96    */
     97   UIAccountTweaks.disableElementForGuest_ = function(element) {
     98     element.classList.add('guest-disabled');
     99     if (element.nodeName == 'INPUT' ||
    100         element.nodeName == 'SELECT' ||
    101         element.nodeName == 'BUTTON')
    102       element.disabled = true;
    103     if (element.nodeName == 'A') {
    104       element.onclick = function() {
    105         return false;
    106       };
    107     }
    108   };
    109 
    110   // Export
    111   return {
    112     UIAccountTweaks: UIAccountTweaks
    113   };
    114 
    115 });
    116