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