angular-aria.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /**
  2. * @license AngularJS v1.6.9
  3. * (c) 2010-2018 Google, Inc. http://angularjs.org
  4. * License: MIT
  5. */
  6. (function(window, angular) {'use strict';
  7. /**
  8. * @ngdoc module
  9. * @name ngAria
  10. * @description
  11. *
  12. * The `ngAria` module provides support for common
  13. * [<abbr title="Accessible Rich Internet Applications">ARIA</abbr>](http://www.w3.org/TR/wai-aria/)
  14. * attributes that convey state or semantic information about the application for users
  15. * of assistive technologies, such as screen readers.
  16. *
  17. * ## Usage
  18. *
  19. * For ngAria to do its magic, simply include the module `ngAria` as a dependency. The following
  20. * directives are supported:
  21. * `ngModel`, `ngChecked`, `ngReadonly`, `ngRequired`, `ngValue`, `ngDisabled`, `ngShow`, `ngHide`, `ngClick`,
  22. * `ngDblClick`, and `ngMessages`.
  23. *
  24. * Below is a more detailed breakdown of the attributes handled by ngAria:
  25. *
  26. * | Directive | Supported Attributes |
  27. * |---------------------------------------------|-----------------------------------------------------------------------------------------------------|
  28. * | {@link ng.directive:ngModel ngModel} | aria-checked, aria-valuemin, aria-valuemax, aria-valuenow, aria-invalid, aria-required, input roles |
  29. * | {@link ng.directive:ngDisabled ngDisabled} | aria-disabled |
  30. * | {@link ng.directive:ngRequired ngRequired} | aria-required |
  31. * | {@link ng.directive:ngChecked ngChecked} | aria-checked |
  32. * | {@link ng.directive:ngReadonly ngReadonly} | aria-readonly |
  33. * | {@link ng.directive:ngValue ngValue} | aria-checked |
  34. * | {@link ng.directive:ngShow ngShow} | aria-hidden |
  35. * | {@link ng.directive:ngHide ngHide} | aria-hidden |
  36. * | {@link ng.directive:ngDblclick ngDblclick} | tabindex |
  37. * | {@link module:ngMessages ngMessages} | aria-live |
  38. * | {@link ng.directive:ngClick ngClick} | tabindex, keydown event, button role |
  39. *
  40. * Find out more information about each directive by reading the
  41. * {@link guide/accessibility ngAria Developer Guide}.
  42. *
  43. * ## Example
  44. * Using ngDisabled with ngAria:
  45. * ```html
  46. * <md-checkbox ng-disabled="disabled">
  47. * ```
  48. * Becomes:
  49. * ```html
  50. * <md-checkbox ng-disabled="disabled" aria-disabled="true">
  51. * ```
  52. *
  53. * ## Disabling Attributes
  54. * It's possible to disable individual attributes added by ngAria with the
  55. * {@link ngAria.$ariaProvider#config config} method. For more details, see the
  56. * {@link guide/accessibility Developer Guide}.
  57. */
  58. var ngAriaModule = angular.module('ngAria', ['ng']).
  59. info({ angularVersion: '1.6.9' }).
  60. provider('$aria', $AriaProvider);
  61. /**
  62. * Internal Utilities
  63. */
  64. var nodeBlackList = ['BUTTON', 'A', 'INPUT', 'TEXTAREA', 'SELECT', 'DETAILS', 'SUMMARY'];
  65. var isNodeOneOf = function(elem, nodeTypeArray) {
  66. if (nodeTypeArray.indexOf(elem[0].nodeName) !== -1) {
  67. return true;
  68. }
  69. };
  70. /**
  71. * @ngdoc provider
  72. * @name $ariaProvider
  73. * @this
  74. *
  75. * @description
  76. *
  77. * Used for configuring the ARIA attributes injected and managed by ngAria.
  78. *
  79. * ```js
  80. * angular.module('myApp', ['ngAria'], function config($ariaProvider) {
  81. * $ariaProvider.config({
  82. * ariaValue: true,
  83. * tabindex: false
  84. * });
  85. * });
  86. *```
  87. *
  88. * ## Dependencies
  89. * Requires the {@link ngAria} module to be installed.
  90. *
  91. */
  92. function $AriaProvider() {
  93. var config = {
  94. ariaHidden: true,
  95. ariaChecked: true,
  96. ariaReadonly: true,
  97. ariaDisabled: true,
  98. ariaRequired: true,
  99. ariaInvalid: true,
  100. ariaValue: true,
  101. tabindex: true,
  102. bindKeydown: true,
  103. bindRoleForClick: true
  104. };
  105. /**
  106. * @ngdoc method
  107. * @name $ariaProvider#config
  108. *
  109. * @param {object} config object to enable/disable specific ARIA attributes
  110. *
  111. * - **ariaHidden** – `{boolean}` – Enables/disables aria-hidden tags
  112. * - **ariaChecked** – `{boolean}` – Enables/disables aria-checked tags
  113. * - **ariaReadonly** – `{boolean}` – Enables/disables aria-readonly tags
  114. * - **ariaDisabled** – `{boolean}` – Enables/disables aria-disabled tags
  115. * - **ariaRequired** – `{boolean}` – Enables/disables aria-required tags
  116. * - **ariaInvalid** – `{boolean}` – Enables/disables aria-invalid tags
  117. * - **ariaValue** – `{boolean}` – Enables/disables aria-valuemin, aria-valuemax and
  118. * aria-valuenow tags
  119. * - **tabindex** – `{boolean}` – Enables/disables tabindex tags
  120. * - **bindKeydown** – `{boolean}` – Enables/disables keyboard event binding on non-interactive
  121. * elements (such as `div` or `li`) using ng-click, making them more accessible to users of
  122. * assistive technologies
  123. * - **bindRoleForClick** – `{boolean}` – Adds role=button to non-interactive elements (such as
  124. * `div` or `li`) using ng-click, making them more accessible to users of assistive
  125. * technologies
  126. *
  127. * @description
  128. * Enables/disables various ARIA attributes
  129. */
  130. this.config = function(newConfig) {
  131. config = angular.extend(config, newConfig);
  132. };
  133. function watchExpr(attrName, ariaAttr, nodeBlackList, negate) {
  134. return function(scope, elem, attr) {
  135. var ariaCamelName = attr.$normalize(ariaAttr);
  136. if (config[ariaCamelName] && !isNodeOneOf(elem, nodeBlackList) && !attr[ariaCamelName]) {
  137. scope.$watch(attr[attrName], function(boolVal) {
  138. // ensure boolean value
  139. boolVal = negate ? !boolVal : !!boolVal;
  140. elem.attr(ariaAttr, boolVal);
  141. });
  142. }
  143. };
  144. }
  145. /**
  146. * @ngdoc service
  147. * @name $aria
  148. *
  149. * @description
  150. * @priority 200
  151. *
  152. * The $aria service contains helper methods for applying common
  153. * [ARIA](http://www.w3.org/TR/wai-aria/) attributes to HTML directives.
  154. *
  155. * ngAria injects common accessibility attributes that tell assistive technologies when HTML
  156. * elements are enabled, selected, hidden, and more. To see how this is performed with ngAria,
  157. * let's review a code snippet from ngAria itself:
  158. *
  159. *```js
  160. * ngAriaModule.directive('ngDisabled', ['$aria', function($aria) {
  161. * return $aria.$$watchExpr('ngDisabled', 'aria-disabled', nodeBlackList, false);
  162. * }])
  163. *```
  164. * Shown above, the ngAria module creates a directive with the same signature as the
  165. * traditional `ng-disabled` directive. But this ngAria version is dedicated to
  166. * solely managing accessibility attributes on custom elements. The internal `$aria` service is
  167. * used to watch the boolean attribute `ngDisabled`. If it has not been explicitly set by the
  168. * developer, `aria-disabled` is injected as an attribute with its value synchronized to the
  169. * value in `ngDisabled`.
  170. *
  171. * Because ngAria hooks into the `ng-disabled` directive, developers do not have to do
  172. * anything to enable this feature. The `aria-disabled` attribute is automatically managed
  173. * simply as a silent side-effect of using `ng-disabled` with the ngAria module.
  174. *
  175. * The full list of directives that interface with ngAria:
  176. * * **ngModel**
  177. * * **ngChecked**
  178. * * **ngReadonly**
  179. * * **ngRequired**
  180. * * **ngDisabled**
  181. * * **ngValue**
  182. * * **ngShow**
  183. * * **ngHide**
  184. * * **ngClick**
  185. * * **ngDblclick**
  186. * * **ngMessages**
  187. *
  188. * Read the {@link guide/accessibility ngAria Developer Guide} for a thorough explanation of each
  189. * directive.
  190. *
  191. *
  192. * ## Dependencies
  193. * Requires the {@link ngAria} module to be installed.
  194. */
  195. this.$get = function() {
  196. return {
  197. config: function(key) {
  198. return config[key];
  199. },
  200. $$watchExpr: watchExpr
  201. };
  202. };
  203. }
  204. ngAriaModule.directive('ngShow', ['$aria', function($aria) {
  205. return $aria.$$watchExpr('ngShow', 'aria-hidden', [], true);
  206. }])
  207. .directive('ngHide', ['$aria', function($aria) {
  208. return $aria.$$watchExpr('ngHide', 'aria-hidden', [], false);
  209. }])
  210. .directive('ngValue', ['$aria', function($aria) {
  211. return $aria.$$watchExpr('ngValue', 'aria-checked', nodeBlackList, false);
  212. }])
  213. .directive('ngChecked', ['$aria', function($aria) {
  214. return $aria.$$watchExpr('ngChecked', 'aria-checked', nodeBlackList, false);
  215. }])
  216. .directive('ngReadonly', ['$aria', function($aria) {
  217. return $aria.$$watchExpr('ngReadonly', 'aria-readonly', nodeBlackList, false);
  218. }])
  219. .directive('ngRequired', ['$aria', function($aria) {
  220. return $aria.$$watchExpr('ngRequired', 'aria-required', nodeBlackList, false);
  221. }])
  222. .directive('ngModel', ['$aria', function($aria) {
  223. function shouldAttachAttr(attr, normalizedAttr, elem, allowBlacklistEls) {
  224. return $aria.config(normalizedAttr) && !elem.attr(attr) && (allowBlacklistEls || !isNodeOneOf(elem, nodeBlackList));
  225. }
  226. function shouldAttachRole(role, elem) {
  227. // if element does not have role attribute
  228. // AND element type is equal to role (if custom element has a type equaling shape) <-- remove?
  229. // AND element is not in nodeBlackList
  230. return !elem.attr('role') && (elem.attr('type') === role) && !isNodeOneOf(elem, nodeBlackList);
  231. }
  232. function getShape(attr, elem) {
  233. var type = attr.type,
  234. role = attr.role;
  235. return ((type || role) === 'checkbox' || role === 'menuitemcheckbox') ? 'checkbox' :
  236. ((type || role) === 'radio' || role === 'menuitemradio') ? 'radio' :
  237. (type === 'range' || role === 'progressbar' || role === 'slider') ? 'range' : '';
  238. }
  239. return {
  240. restrict: 'A',
  241. require: 'ngModel',
  242. priority: 200, //Make sure watches are fired after any other directives that affect the ngModel value
  243. compile: function(elem, attr) {
  244. var shape = getShape(attr, elem);
  245. return {
  246. post: function(scope, elem, attr, ngModel) {
  247. var needsTabIndex = shouldAttachAttr('tabindex', 'tabindex', elem, false);
  248. function ngAriaWatchModelValue() {
  249. return ngModel.$modelValue;
  250. }
  251. function getRadioReaction(newVal) {
  252. // Strict comparison would cause a BC
  253. // eslint-disable-next-line eqeqeq
  254. var boolVal = (attr.value == ngModel.$viewValue);
  255. elem.attr('aria-checked', boolVal);
  256. }
  257. function getCheckboxReaction() {
  258. elem.attr('aria-checked', !ngModel.$isEmpty(ngModel.$viewValue));
  259. }
  260. switch (shape) {
  261. case 'radio':
  262. case 'checkbox':
  263. if (shouldAttachRole(shape, elem)) {
  264. elem.attr('role', shape);
  265. }
  266. if (shouldAttachAttr('aria-checked', 'ariaChecked', elem, false)) {
  267. scope.$watch(ngAriaWatchModelValue, shape === 'radio' ?
  268. getRadioReaction : getCheckboxReaction);
  269. }
  270. if (needsTabIndex) {
  271. elem.attr('tabindex', 0);
  272. }
  273. break;
  274. case 'range':
  275. if (shouldAttachRole(shape, elem)) {
  276. elem.attr('role', 'slider');
  277. }
  278. if ($aria.config('ariaValue')) {
  279. var needsAriaValuemin = !elem.attr('aria-valuemin') &&
  280. (attr.hasOwnProperty('min') || attr.hasOwnProperty('ngMin'));
  281. var needsAriaValuemax = !elem.attr('aria-valuemax') &&
  282. (attr.hasOwnProperty('max') || attr.hasOwnProperty('ngMax'));
  283. var needsAriaValuenow = !elem.attr('aria-valuenow');
  284. if (needsAriaValuemin) {
  285. attr.$observe('min', function ngAriaValueMinReaction(newVal) {
  286. elem.attr('aria-valuemin', newVal);
  287. });
  288. }
  289. if (needsAriaValuemax) {
  290. attr.$observe('max', function ngAriaValueMinReaction(newVal) {
  291. elem.attr('aria-valuemax', newVal);
  292. });
  293. }
  294. if (needsAriaValuenow) {
  295. scope.$watch(ngAriaWatchModelValue, function ngAriaValueNowReaction(newVal) {
  296. elem.attr('aria-valuenow', newVal);
  297. });
  298. }
  299. }
  300. if (needsTabIndex) {
  301. elem.attr('tabindex', 0);
  302. }
  303. break;
  304. }
  305. if (!attr.hasOwnProperty('ngRequired') && ngModel.$validators.required
  306. && shouldAttachAttr('aria-required', 'ariaRequired', elem, false)) {
  307. // ngModel.$error.required is undefined on custom controls
  308. attr.$observe('required', function() {
  309. elem.attr('aria-required', !!attr['required']);
  310. });
  311. }
  312. if (shouldAttachAttr('aria-invalid', 'ariaInvalid', elem, true)) {
  313. scope.$watch(function ngAriaInvalidWatch() {
  314. return ngModel.$invalid;
  315. }, function ngAriaInvalidReaction(newVal) {
  316. elem.attr('aria-invalid', !!newVal);
  317. });
  318. }
  319. }
  320. };
  321. }
  322. };
  323. }])
  324. .directive('ngDisabled', ['$aria', function($aria) {
  325. return $aria.$$watchExpr('ngDisabled', 'aria-disabled', nodeBlackList, false);
  326. }])
  327. .directive('ngMessages', function() {
  328. return {
  329. restrict: 'A',
  330. require: '?ngMessages',
  331. link: function(scope, elem, attr, ngMessages) {
  332. if (!elem.attr('aria-live')) {
  333. elem.attr('aria-live', 'assertive');
  334. }
  335. }
  336. };
  337. })
  338. .directive('ngClick',['$aria', '$parse', function($aria, $parse) {
  339. return {
  340. restrict: 'A',
  341. compile: function(elem, attr) {
  342. var fn = $parse(attr.ngClick);
  343. return function(scope, elem, attr) {
  344. if (!isNodeOneOf(elem, nodeBlackList)) {
  345. if ($aria.config('bindRoleForClick') && !elem.attr('role')) {
  346. elem.attr('role', 'button');
  347. }
  348. if ($aria.config('tabindex') && !elem.attr('tabindex')) {
  349. elem.attr('tabindex', 0);
  350. }
  351. if ($aria.config('bindKeydown') && !attr.ngKeydown && !attr.ngKeypress && !attr.ngKeyup) {
  352. elem.on('keydown', function(event) {
  353. var keyCode = event.which || event.keyCode;
  354. if (keyCode === 32 || keyCode === 13) {
  355. scope.$apply(callback);
  356. }
  357. function callback() {
  358. fn(scope, { $event: event });
  359. }
  360. });
  361. }
  362. }
  363. };
  364. }
  365. };
  366. }])
  367. .directive('ngDblclick', ['$aria', function($aria) {
  368. return function(scope, elem, attr) {
  369. if ($aria.config('tabindex') && !elem.attr('tabindex') && !isNodeOneOf(elem, nodeBlackList)) {
  370. elem.attr('tabindex', 0);
  371. }
  372. };
  373. }]);
  374. })(window, window.angular);