| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 cr.define('accessibility', function() { | 5 cr.define('accessibility', function() { |
| 6 'use strict'; | 6 'use strict'; |
| 7 | 7 |
| 8 // Note: keep these values in sync with the values in |
| 9 // content/common/accessibility_mode_enums.h |
| 10 const MODE_FLAG_NATIVE_APIS = 1 << 0; |
| 11 const MODE_FLAG_WEB_CONTENTS = 1 << 1; |
| 12 const MODE_FLAG_INLINE_TEXT_BOXES = 1 << 2; |
| 13 const MODE_FLAG_SCREEN_READER = 1 << 3; |
| 14 const MODE_FLAG_HTML = 1 << 4; |
| 15 |
| 8 function requestData() { | 16 function requestData() { |
| 9 var xhr = new XMLHttpRequest(); | 17 var xhr = new XMLHttpRequest(); |
| 10 xhr.open('GET', 'targets-data.json', false); | 18 xhr.open('GET', 'targets-data.json', false); |
| 11 xhr.send(null); | 19 xhr.send(null); |
| 12 if (xhr.status === 200) { | 20 if (xhr.status === 200) { |
| 13 console.log(xhr.responseText); | 21 console.log(xhr.responseText); |
| 14 return JSON.parse(xhr.responseText); | 22 return JSON.parse(xhr.responseText); |
| 15 } | 23 } |
| 16 return []; | 24 return []; |
| 17 } | 25 } |
| 18 | 26 |
| 19 // TODO(aboxhall): add a mechanism to request individual and global a11y | 27 function toggleAccessibility(data, element, mode) { |
| 20 // mode, xhr them on toggle... or just re-requestData and be smarter about | |
| 21 // ID-ing rows? | |
| 22 | |
| 23 function toggleAccessibility(data, element) { | |
| 24 chrome.send('toggleAccessibility', | 28 chrome.send('toggleAccessibility', |
| 25 [String(data.processId), String(data.routeId)]); | 29 [String(data.processId), String(data.routeId), mode]); |
| 26 var a11y_was_on = (element.textContent.match(/on/) != null); | 30 document.location.reload(); |
| 27 element.textContent = ' accessibility ' + (a11y_was_on ? ' off' : ' on'); | |
| 28 var row = element.parentElement; | |
| 29 if (a11y_was_on) { | |
| 30 while (row.lastChild != element) | |
| 31 row.removeChild(row.lastChild); | |
| 32 } else { | |
| 33 row.appendChild(document.createTextNode(' | ')); | |
| 34 row.appendChild(createShowAccessibilityTreeElement(data, row, false)); | |
| 35 } | |
| 36 } | 31 } |
| 37 | 32 |
| 38 function requestAccessibilityTree(data, element) { | 33 function requestAccessibilityTree(data, element) { |
| 39 chrome.send('requestAccessibilityTree', | 34 chrome.send('requestAccessibilityTree', |
| 40 [String(data.processId), String(data.routeId)]); | 35 [String(data.processId), String(data.routeId)]); |
| 41 } | 36 } |
| 42 | 37 |
| 43 function initialize() { | 38 function initialize() { |
| 44 console.log('initialize'); | 39 console.log('initialize'); |
| 45 var data = requestData(); | 40 var data = requestData(); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 61 | 56 |
| 62 function bindCheckbox(name, value) { | 57 function bindCheckbox(name, value) { |
| 63 if (value == 'on') | 58 if (value == 'on') |
| 64 $(name).checked = true; | 59 $(name).checked = true; |
| 65 if (value == 'disabled') { | 60 if (value == 'disabled') { |
| 66 $(name).disabled = true; | 61 $(name).disabled = true; |
| 67 $(name).labels[0].classList.add('disabled'); | 62 $(name).labels[0].classList.add('disabled'); |
| 68 } | 63 } |
| 69 $(name).addEventListener('change', function() { | 64 $(name).addEventListener('change', function() { |
| 70 chrome.send('setGlobalFlag', [name, $(name).checked]); | 65 chrome.send('setGlobalFlag', [name, $(name).checked]); |
| 71 document.location.reload(); // FIXME see TODO above | 66 document.location.reload(); |
| 72 }); | 67 }); |
| 73 } | 68 } |
| 74 | 69 |
| 75 function addToPagesList(data) { | 70 function addToPagesList(data) { |
| 76 // TODO: iterate through data and pages rows instead | 71 // TODO: iterate through data and pages rows instead |
| 77 var id = data['processId'] + '.' + data['routeId']; | 72 var id = data['processId'] + '.' + data['routeId']; |
| 78 var row = document.createElement('div'); | 73 var row = document.createElement('div'); |
| 79 row.className = 'row'; | 74 row.className = 'row'; |
| 80 row.id = id; | 75 row.id = id; |
| 81 formatRow(row, data); | 76 formatRow(row, data); |
| 82 | 77 |
| 83 row.processId = data.processId; | 78 row.processId = data.processId; |
| 84 row.routeId = data.routeId; | 79 row.routeId = data.routeId; |
| 85 | 80 |
| 86 var list = $('pages'); | 81 var list = $('pages'); |
| 87 list.appendChild(row); | 82 list.appendChild(row); |
| 88 } | 83 } |
| 89 | 84 |
| 90 function formatRow(row, data) { | 85 function formatRow(row, data) { |
| 91 if (!('url' in data)) { | 86 if (!('url' in data)) { |
| 92 if ('error' in data) { | 87 if ('error' in data) { |
| 93 row.appendChild(createErrorMessageElement(data, row)); | 88 row.appendChild(createErrorMessageElement(data, row)); |
| 94 return; | 89 return; |
| 95 } | 90 } |
| 96 } | 91 } |
| 92 |
| 93 var siteInfo = document.createElement('div'); |
| 97 var properties = ['favicon_url', 'name', 'url']; | 94 var properties = ['favicon_url', 'name', 'url']; |
| 98 for (var j = 0; j < properties.length; j++) | 95 for (var j = 0; j < properties.length; j++) |
| 99 row.appendChild(formatValue(data, properties[j])); | 96 siteInfo.appendChild(formatValue(data, properties[j])); |
| 97 row.appendChild(siteInfo); |
| 100 | 98 |
| 101 row.appendChild(createToggleAccessibilityElement(data)); | 99 row.appendChild(createModeElement(MODE_FLAG_NATIVE_APIS, data)) |
| 102 if (data['a11y_mode']) { | 100 row.appendChild(createModeElement(MODE_FLAG_WEB_CONTENTS, data)) |
| 103 row.appendChild(document.createTextNode(' | ')); | 101 row.appendChild(createModeElement(MODE_FLAG_INLINE_TEXT_BOXES, data)) |
| 104 if ('tree' in data) { | 102 row.appendChild(createModeElement(MODE_FLAG_SCREEN_READER, data)) |
| 105 row.appendChild(createShowAccessibilityTreeElement(data, row, true)); | 103 row.appendChild(createModeElement(MODE_FLAG_HTML, data)) |
| 106 row.appendChild(document.createTextNode(' | ')); | 104 |
| 107 row.appendChild(createHideAccessibilityTreeElement(row.id)); | 105 row.appendChild(document.createTextNode(' | ')); |
| 108 row.appendChild(createAccessibilityTreeElement(data)); | 106 |
| 109 } | 107 if ('tree' in data) { |
| 110 else { | 108 row.appendChild(createShowAccessibilityTreeElement(data, row, true)); |
| 111 row.appendChild(createShowAccessibilityTreeElement(data, row, false)); | 109 row.appendChild(createHideAccessibilityTreeElement(row.id)); |
| 112 if ('error' in data) | 110 row.appendChild(createAccessibilityTreeElement(data)); |
| 113 row.appendChild(createErrorMessageElement(data, row)); | 111 } |
| 114 } | 112 else { |
| 113 row.appendChild(createShowAccessibilityTreeElement(data, row, false)); |
| 114 if ('error' in data) |
| 115 row.appendChild(createErrorMessageElement(data, row)); |
| 115 } | 116 } |
| 116 } | 117 } |
| 117 | 118 |
| 118 function formatValue(data, property) { | 119 function formatValue(data, property) { |
| 119 var value = data[property]; | 120 var value = data[property]; |
| 120 | 121 |
| 121 if (property == 'favicon_url') { | 122 if (property == 'favicon_url') { |
| 122 var faviconElement = document.createElement('img'); | 123 var faviconElement = document.createElement('img'); |
| 123 if (value) | 124 if (value) |
| 124 faviconElement.src = value; | 125 faviconElement.src = value; |
| 125 faviconElement.alt = ""; | 126 faviconElement.alt = ""; |
| 126 return faviconElement; | 127 return faviconElement; |
| 127 } | 128 } |
| 128 | 129 |
| 129 var text = value ? String(value) : ''; | 130 var text = value ? String(value) : ''; |
| 130 if (text.length > 100) | 131 if (text.length > 100) |
| 131 text = text.substring(0, 100) + '\u2026'; // ellipsis | 132 text = text.substring(0, 100) + '\u2026'; // ellipsis |
| 132 | 133 |
| 133 var span = document.createElement('span'); | 134 var span = document.createElement('span'); |
| 134 span.textContent = ' ' + text + ' '; | 135 span.textContent = ' ' + text + ' '; |
| 135 span.className = property; | 136 span.className = property; |
| 136 return span; | 137 return span; |
| 137 } | 138 } |
| 138 | 139 |
| 139 function createToggleAccessibilityElement(data) { | 140 function getNameForAccessibilityMode(mode) { |
| 141 switch (mode) { |
| 142 case MODE_FLAG_NATIVE_APIS: |
| 143 return "native" |
| 144 case MODE_FLAG_WEB_CONTENTS: |
| 145 return "web" |
| 146 case MODE_FLAG_INLINE_TEXT_BOXES: |
| 147 return "inline text" |
| 148 case MODE_FLAG_SCREEN_READER: |
| 149 return "screen reader" |
| 150 case MODE_FLAG_HTML: |
| 151 return "html" |
| 152 } |
| 153 return "unknown" |
| 154 } |
| 155 |
| 156 function createModeElement(mode, data) { |
| 157 var currentMode = data['a11y_mode']; |
| 140 var link = document.createElement('a', 'action-link'); | 158 var link = document.createElement('a', 'action-link'); |
| 141 link.setAttribute('role', 'button'); | 159 link.setAttribute('role', 'button'); |
| 142 var full_a11y_on = data['a11y_mode']; | 160 |
| 143 link.textContent = 'accessibility ' + (full_a11y_on ? 'on' : 'off'); | 161 var stateText = ((currentMode & mode) != 0) ? 'true' : 'false'; |
| 144 link.setAttribute('aria-pressed', (full_a11y_on ? 'true' : 'false')); | 162 link.textContent = getNameForAccessibilityMode(mode) + ": " + stateText; |
| 163 link.setAttribute('aria-pressed', stateText); |
| 145 link.addEventListener('click', | 164 link.addEventListener('click', |
| 146 toggleAccessibility.bind(this, data, link)); | 165 toggleAccessibility.bind(this, data, link, mode)); |
| 147 return link; | 166 return link; |
| 148 } | 167 } |
| 149 | 168 |
| 150 function createShowAccessibilityTreeElement(data, row, opt_refresh) { | 169 function createShowAccessibilityTreeElement(data, row, opt_refresh) { |
| 151 var link = document.createElement('a', 'action-link'); | 170 var link = document.createElement('a', 'action-link'); |
| 152 link.setAttribute('role', 'button'); | 171 link.setAttribute('role', 'button'); |
| 153 if (opt_refresh) | 172 if (opt_refresh) |
| 154 link.textContent = 'refresh accessibility tree'; | 173 link.textContent = 'refresh accessibility tree'; |
| 155 else | 174 else |
| 156 link.textContent = 'show accessibility tree'; | 175 link.textContent = 'show accessibility tree'; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 return treeElement; | 230 return treeElement; |
| 212 } | 231 } |
| 213 | 232 |
| 214 return { | 233 return { |
| 215 initialize: initialize, | 234 initialize: initialize, |
| 216 showTree: showTree | 235 showTree: showTree |
| 217 }; | 236 }; |
| 218 }); | 237 }); |
| 219 | 238 |
| 220 document.addEventListener('DOMContentLoaded', accessibility.initialize); | 239 document.addEventListener('DOMContentLoaded', accessibility.initialize); |
| OLD | NEW |