angular-aria.js 19 KB

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