| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
(function(window, angular) {'use strict'; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
var ARIA_DISABLE_ATTR = 'ngAriaDisable'; |
| 64 |
|
| 65 |
var ngAriaModule = angular.module('ngAria', ['ng']). |
| 66 |
info({ angularVersion: '1.8.1' }). |
| 67 |
provider('$aria', $AriaProvider); |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
var nativeAriaNodeNames = ['BUTTON', 'A', 'INPUT', 'TEXTAREA', 'SELECT', 'DETAILS', 'SUMMARY']; |
| 73 |
|
| 74 |
var isNodeOneOf = function(elem, nodeTypeArray) { |
| 75 |
if (nodeTypeArray.indexOf(elem[0].nodeName) !== -1) { |
| 76 |
return true; |
| 77 |
} |
| 78 |
}; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
function $AriaProvider() { |
| 102 |
var config = { |
| 103 |
ariaHidden: true, |
| 104 |
ariaChecked: true, |
| 105 |
ariaReadonly: true, |
| 106 |
ariaDisabled: true, |
| 107 |
ariaRequired: true, |
| 108 |
ariaInvalid: true, |
| 109 |
ariaValue: true, |
| 110 |
tabindex: true, |
| 111 |
bindKeydown: true, |
| 112 |
bindRoleForClick: true |
| 113 |
}; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
tags |
| 122 |
tags |
| 123 |
tags |
| 124 |
tags |
| 125 |
tags |
| 126 |
tags |
| 127 |
and |
| 128 |
|
| 129 |
tags |
| 130 |
tive |
| 131 |
|
| 132 |
|
| 133 |
h as |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
this.config = function(newConfig) { |
| 141 |
config = angular.extend(config, newConfig); |
| 142 |
}; |
| 143 |
|
| 144 |
function watchExpr(attrName, ariaAttr, nativeAriaNodeNames, negate) { |
| 145 |
return function(scope, elem, attr) { |
| 146 |
if (attr.hasOwnProperty(ARIA_DISABLE_ATTR)) return; |
| 147 |
|
| 148 |
var ariaCamelName = attr.$normalize(ariaAttr); |
| 149 |
if (config[ariaCamelName] && !isNodeOneOf(elem, nativeAriaNodeNames) && !attr[ariaCamelName]) { |
| 150 |
scope.$watch(attr[attrName], function(boolVal) { |
| 151 |
|
| 152 |
boolVal = negate ? !boolVal : !!boolVal; |
| 153 |
elem.attr(ariaAttr, boolVal); |
| 154 |
}); |
| 155 |
} |
| 156 |
}; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
this.$get = function() { |
| 208 |
return { |
| 209 |
config: function(key) { |
| 210 |
return config[key]; |
| 211 |
}, |
| 212 |
$$watchExpr: watchExpr |
| 213 |
}; |
| 214 |
}; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
ngAriaModule.directive('ngShow', ['$aria', function($aria) { |
| 219 |
return $aria.$$watchExpr('ngShow', 'aria-hidden', [], true); |
| 220 |
}]) |
| 221 |
.directive('ngHide', ['$aria', function($aria) { |
| 222 |
return $aria.$$watchExpr('ngHide', 'aria-hidden', [], false); |
| 223 |
}]) |
| 224 |
.directive('ngValue', ['$aria', function($aria) { |
| 225 |
return $aria.$$watchExpr('ngValue', 'aria-checked', nativeAriaNodeNames, false); |
| 226 |
}]) |
| 227 |
.directive('ngChecked', ['$aria', function($aria) { |
| 228 |
return $aria.$$watchExpr('ngChecked', 'aria-checked', nativeAriaNodeNames, false); |
| 229 |
}]) |
| 230 |
.directive('ngReadonly', ['$aria', function($aria) { |
| 231 |
return $aria.$$watchExpr('ngReadonly', 'aria-readonly', nativeAriaNodeNames, false); |
| 232 |
}]) |
| 233 |
.directive('ngRequired', ['$aria', function($aria) { |
| 234 |
return $aria.$$watchExpr('ngRequired', 'aria-required', nativeAriaNodeNames, false); |
| 235 |
}]) |
| 236 |
.directive('ngModel', ['$aria', function($aria) { |
| 237 |
|
| 238 |
function shouldAttachAttr(attr, normalizedAttr, elem, allowNonAriaNodes) { |
| 239 |
return $aria.config(normalizedAttr) && |
| 240 |
!elem.attr(attr) && |
| 241 |
(allowNonAriaNodes || !isNodeOneOf(elem, nativeAriaNodeNames)) && |
| 242 |
(elem.attr('type') !== 'hidden' || elem[0].nodeName !== 'INPUT'); |
| 243 |
} |
| 244 |
|
| 245 |
function shouldAttachRole(role, elem) { |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
return !elem.attr('role') && (elem.attr('type') === role) && !isNodeOneOf(elem, nativeAriaNodeNames); |
| 250 |
} |
| 251 |
|
| 252 |
function getShape(attr, elem) { |
| 253 |
var type = attr.type, |
| 254 |
role = attr.role; |
| 255 |
|
| 256 |
return ((type || role) === 'checkbox' || role === 'menuitemcheckbox') ? 'checkbox' : |
| 257 |
((type || role) === 'radio' || role === 'menuitemradio') ? 'radio' : |
| 258 |
(type === 'range' || role === 'progressbar' || role === 'slider') ? 'range' : ''; |
| 259 |
} |
| 260 |
|
| 261 |
return { |
| 262 |
restrict: 'A', |
| 263 |
require: 'ngModel', |
| 264 |
priority: 200, |
| 265 |
compile: function(elem, attr) { |
| 266 |
if (attr.hasOwnProperty(ARIA_DISABLE_ATTR)) return; |
| 267 |
|
| 268 |
var shape = getShape(attr, elem); |
| 269 |
|
| 270 |
return { |
| 271 |
post: function(scope, elem, attr, ngModel) { |
| 272 |
var needsTabIndex = shouldAttachAttr('tabindex', 'tabindex', elem, false); |
| 273 |
|
| 274 |
function ngAriaWatchModelValue() { |
| 275 |
return ngModel.$modelValue; |
| 276 |
} |
| 277 |
|
| 278 |
function getRadioReaction(newVal) { |
| 279 |
|
| 280 |
|
| 281 |
var boolVal = (attr.value == ngModel.$viewValue); |
| 282 |
elem.attr('aria-checked', boolVal); |
| 283 |
} |
| 284 |
|
| 285 |
function getCheckboxReaction() { |
| 286 |
elem.attr('aria-checked', !ngModel.$isEmpty(ngModel.$viewValue)); |
| 287 |
} |
| 288 |
|
| 289 |
switch (shape) { |
| 290 |
case 'radio': |
| 291 |
case 'checkbox': |
| 292 |
if (shouldAttachRole(shape, elem)) { |
| 293 |
elem.attr('role', shape); |
| 294 |
} |
| 295 |
if (shouldAttachAttr('aria-checked', 'ariaChecked', elem, false)) { |
| 296 |
scope.$watch(ngAriaWatchModelValue, shape === 'radio' ? |
| 297 |
getRadioReaction : getCheckboxReaction); |
| 298 |
} |
| 299 |
if (needsTabIndex) { |
| 300 |
elem.attr('tabindex', 0); |
| 301 |
} |
| 302 |
break; |
| 303 |
case 'range': |
| 304 |
if (shouldAttachRole(shape, elem)) { |
| 305 |
elem.attr('role', 'slider'); |
| 306 |
} |
| 307 |
if ($aria.config('ariaValue')) { |
| 308 |
var needsAriaValuemin = !elem.attr('aria-valuemin') && |
| 309 |
(attr.hasOwnProperty('min') || attr.hasOwnProperty('ngMin')); |
| 310 |
var needsAriaValuemax = !elem.attr('aria-valuemax') && |
| 311 |
(attr.hasOwnProperty('max') || attr.hasOwnProperty('ngMax')); |
| 312 |
var needsAriaValuenow = !elem.attr('aria-valuenow'); |
| 313 |
|
| 314 |
if (needsAriaValuemin) { |
| 315 |
attr.$observe('min', function ngAriaValueMinReaction(newVal) { |
| 316 |
elem.attr('aria-valuemin', newVal); |
| 317 |
}); |
| 318 |
} |
| 319 |
if (needsAriaValuemax) { |
| 320 |
attr.$observe('max', function ngAriaValueMinReaction(newVal) { |
| 321 |
elem.attr('aria-valuemax', newVal); |
| 322 |
}); |
| 323 |
} |
| 324 |
if (needsAriaValuenow) { |
| 325 |
scope.$watch(ngAriaWatchModelValue, function ngAriaValueNowReaction(newVal) { |
| 326 |
elem.attr('aria-valuenow', newVal); |
| 327 |
}); |
| 328 |
} |
| 329 |
} |
| 330 |
if (needsTabIndex) { |
| 331 |
elem.attr('tabindex', 0); |
| 332 |
} |
| 333 |
break; |
| 334 |
} |
| 335 |
|
| 336 |
if (!attr.hasOwnProperty('ngRequired') && ngModel.$validators.required |
| 337 |
&& shouldAttachAttr('aria-required', 'ariaRequired', elem, false)) { |
| 338 |
|
| 339 |
attr.$observe('required', function() { |
| 340 |
elem.attr('aria-required', !!attr['required']); |
| 341 |
}); |
| 342 |
} |
| 343 |
|
| 344 |
if (shouldAttachAttr('aria-invalid', 'ariaInvalid', elem, true)) { |
| 345 |
scope.$watch(function ngAriaInvalidWatch() { |
| 346 |
return ngModel.$invalid; |
| 347 |
}, function ngAriaInvalidReaction(newVal) { |
| 348 |
elem.attr('aria-invalid', !!newVal); |
| 349 |
}); |
| 350 |
} |
| 351 |
} |
| 352 |
}; |
| 353 |
} |
| 354 |
}; |
| 355 |
}]) |
| 356 |
.directive('ngDisabled', ['$aria', function($aria) { |
| 357 |
return $aria.$$watchExpr('ngDisabled', 'aria-disabled', nativeAriaNodeNames, false); |
| 358 |
}]) |
| 359 |
.directive('ngMessages', function() { |
| 360 |
return { |
| 361 |
restrict: 'A', |
| 362 |
require: '?ngMessages', |
| 363 |
link: function(scope, elem, attr, ngMessages) { |
| 364 |
if (attr.hasOwnProperty(ARIA_DISABLE_ATTR)) return; |
| 365 |
|
| 366 |
if (!elem.attr('aria-live')) { |
| 367 |
elem.attr('aria-live', 'assertive'); |
| 368 |
} |
| 369 |
} |
| 370 |
}; |
| 371 |
}) |
| 372 |
.directive('ngClick',['$aria', '$parse', function($aria, $parse) { |
| 373 |
return { |
| 374 |
restrict: 'A', |
| 375 |
compile: function(elem, attr) { |
| 376 |
if (attr.hasOwnProperty(ARIA_DISABLE_ATTR)) return; |
| 377 |
|
| 378 |
var fn = $parse(attr.ngClick); |
| 379 |
return function(scope, elem, attr) { |
| 380 |
|
| 381 |
if (!isNodeOneOf(elem, nativeAriaNodeNames)) { |
| 382 |
|
| 383 |
if ($aria.config('bindRoleForClick') && !elem.attr('role')) { |
| 384 |
elem.attr('role', 'button'); |
| 385 |
} |
| 386 |
|
| 387 |
if ($aria.config('tabindex') && !elem.attr('tabindex')) { |
| 388 |
elem.attr('tabindex', 0); |
| 389 |
} |
| 390 |
|
| 391 |
if ($aria.config('bindKeydown') && !attr.ngKeydown && !attr.ngKeypress && !attr.ngKeyup) { |
| 392 |
elem.on('keydown', function(event) { |
| 393 |
var keyCode = event.which || event.keyCode; |
| 394 |
|
| 395 |
if (keyCode === 13 || keyCode === 32) { |
| 396 |
|
| 397 |
if (nativeAriaNodeNames.indexOf(event.target.nodeName) === -1 && !event.target.isContentEditable) { |
| 398 |
|
| 399 |
|
| 400 |
event.preventDefault(); |
| 401 |
} |
| 402 |
scope.$apply(callback); |
| 403 |
} |
| 404 |
|
| 405 |
function callback() { |
| 406 |
fn(scope, { $event: event }); |
| 407 |
} |
| 408 |
}); |
| 409 |
} |
| 410 |
} |
| 411 |
}; |
| 412 |
} |
| 413 |
}; |
| 414 |
}]) |
| 415 |
.directive('ngDblclick', ['$aria', function($aria) { |
| 416 |
return function(scope, elem, attr) { |
| 417 |
if (attr.hasOwnProperty(ARIA_DISABLE_ATTR)) return; |
| 418 |
|
| 419 |
if ($aria.config('tabindex') && !elem.attr('tabindex') && !isNodeOneOf(elem, nativeAriaNodeNames)) { |
| 420 |
elem.attr('tabindex', 0); |
| 421 |
} |
| 422 |
}; |
| 423 |
}]); |
| 424 |
|
| 425 |
|
| 426 |
})(window, window.angular); |