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.
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
15 cr.define('uiAccountTweaks', function() {
17 /////////////////////////////////////////////////////////////////////////////
18 // UIAccountTweaks class:
20 // String specificators for different types of sessions.
21 /** @const */ var SESSION_TYPE_GUEST = 'guest';
22 /** @const */ var SESSION_TYPE_PUBLIC = 'public-account';
25 * Encapsulated handling of ChromeOS accounts options page.
28 function UIAccountTweaks() {
32 * @return {boolean} Whether the current user is owner or not.
34 UIAccountTweaks.currentUserIsOwner = function() {
35 return loadTimeData.getBoolean('currentUserIsOwner');
39 * @return {boolean} Whether we're currently in guest session.
41 UIAccountTweaks.loggedInAsGuest = function() {
42 return loadTimeData.getBoolean('loggedInAsGuest');
46 * @return {boolean} Whether we're currently in public session.
48 UIAccountTweaks.loggedInAsPublicAccount = function() {
49 return loadTimeData.getBoolean('loggedInAsPublicAccount');
53 * @return {boolean} Whether we're currently in supervised user mode.
55 UIAccountTweaks.loggedInAsSupervisedUser = function() {
56 return loadTimeData.getBoolean('loggedInAsSupervisedUser');
60 * Enables an element unless it should be disabled for the session type.
62 * @param {!Element} element Element that should be enabled.
64 UIAccountTweaks.enableElementIfPossible = function(element) {
66 if (UIAccountTweaks.loggedInAsGuest())
67 sessionType = SESSION_TYPE_GUEST;
68 else if (UIAccountTweaks.loggedInAsPublicAccount())
69 sessionType = SESSION_TYPE_PUBLIC;
72 element.getAttribute(sessionType + '-visibility') == 'disabled') {
76 element.disabled = false;
80 * Disables or hides some elements in specified type of session in ChromeOS.
81 * All elements within given document with *sessionType*-visibility
82 * attribute are either hidden (for *sessionType*-visibility="hidden")
83 * or disabled (for *sessionType*-visibility="disabled").
85 * @param {Document} document Document that should processed.
86 * @param {string} sessionType name of the session type processed.
89 UIAccountTweaks.applySessionTypeVisibility_ = function(document,
91 var elements = document.querySelectorAll('['+ sessionType + '-visibility]');
92 for (var i = 0; i < elements.length; i++) {
93 var element = elements[i];
94 var visibility = element.getAttribute(sessionType + '-visibility');
95 if (visibility == 'hidden')
96 element.hidden = true;
97 else if (visibility == 'disabled')
98 UIAccountTweaks.disableElementsForSessionType(element, sessionType);
103 * Updates specific visibility of elements for Guest session in ChromeOS.
104 * Calls applySessionTypeVisibility_ method.
106 * @param {Document} document Document that should processed.
108 UIAccountTweaks.applyGuestSessionVisibility = function(document) {
109 if (!UIAccountTweaks.loggedInAsGuest())
111 UIAccountTweaks.applySessionTypeVisibility_(document, SESSION_TYPE_GUEST);
115 * Updates specific visibility of elements for Public account session in
116 * ChromeOS. Calls applySessionTypeVisibility_ method.
118 * @param {Document} document Document that should processed.
120 UIAccountTweaks.applyPublicSessionVisibility = function(document) {
121 if (!UIAccountTweaks.loggedInAsPublicAccount())
123 UIAccountTweaks.applySessionTypeVisibility_(document, SESSION_TYPE_PUBLIC);
127 * Disables and marks page elements for specified session type.
128 * Adds #-disabled css class to all elements within given subtree,
129 * disables interactive elements (input/select/button), and removes href
130 * attribute from <a> elements.
132 * @param {!Element} element Root element of DOM subtree that should be
134 * @param {string} sessionType session type specificator.
136 UIAccountTweaks.disableElementsForSessionType = function(element,
138 UIAccountTweaks.disableElementForSessionType_(element, sessionType);
140 // Walk the tree, searching each ELEMENT node.
141 var walker = document.createTreeWalker(element,
142 NodeFilter.SHOW_ELEMENT,
146 var node = walker.nextNode();
148 UIAccountTweaks.disableElementForSessionType_(
149 /** @type {!Element} */(node), sessionType);
150 node = walker.nextNode();
155 * Disables single element for given session type.
156 * Adds *sessionType*-disabled css class, adds disabled attribute for
157 * appropriate elements (input/select/button), and removes href attribute from
161 * @param {!Element} element Element that should be disabled.
162 * @param {string} sessionType account session Type specificator.
164 UIAccountTweaks.disableElementForSessionType_ = function(element,
166 element.classList.add(sessionType + '-disabled');
167 if (element.nodeName == 'INPUT' ||
168 element.nodeName == 'SELECT' ||
169 element.nodeName == 'BUTTON') {
170 element.disabled = true;
171 } else if (element.nodeName == 'A') {
172 element.onclick = function() {
180 UIAccountTweaks: UIAccountTweaks