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