You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

402 lines
15 KiB

7 years ago
  1. /**
  2. * @license AngularJS v1.6.1
  3. * (c) 2010-2016 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. * <div doc-module-components="ngAria"></div>
  18. *
  19. * ## Usage
  20. *
  21. * For ngAria to do its magic, simply include the module `ngAria` as a dependency. The following
  22. * directives are supported:
  23. * `ngModel`, `ngChecked`, `ngReadonly`, `ngRequired`, `ngValue`, `ngDisabled`, `ngShow`, `ngHide`, `ngClick`,
  24. * `ngDblClick`, and `ngMessages`.
  25. *
  26. * Below is a more detailed breakdown of the attributes handled by ngAria:
  27. *
  28. * | Directive | Supported Attributes |
  29. * |---------------------------------------------|-----------------------------------------------------------------------------------------------------|
  30. * | {@link ng.directive:ngModel ngModel} | aria-checked, aria-valuemin, aria-valuemax, aria-valuenow, aria-invalid, aria-required, input roles |
  31. * | {@link ng.directive:ngDisabled ngDisabled} | aria-disabled |
  32. * | {@link ng.directive:ngRequired ngRequired} | aria-required |
  33. * | {@link ng.directive:ngChecked ngChecked} | aria-checked |
  34. * | {@link ng.directive:ngReadonly ngReadonly} | aria-readonly |
  35. * | {@link ng.directive:ngValue ngValue} | aria-checked |
  36. * | {@link ng.directive:ngShow ngShow} | aria-hidden |
  37. * | {@link ng.directive:ngHide ngHide} | aria-hidden |
  38. * | {@link ng.directive:ngDblclick ngDblclick} | tabindex |
  39. * | {@link module:ngMessages ngMessages} | aria-live |
  40. * | {@link ng.directive:ngClick ngClick} | tabindex, keydown event, button role |
  41. *
  42. * Find out more information about each directive by reading the
  43. * {@link guide/accessibility ngAria Developer Guide}.
  44. *
  45. * ## Example
  46. * Using ngDisabled with ngAria:
  47. * ```html
  48. * <md-checkbox ng-disabled="disabled">
  49. * ```
  50. * Becomes:
  51. * ```html
  52. * <md-checkbox ng-disabled="disabled" aria-disabled="true">
  53. * ```
  54. *
  55. * ## Disabling Attributes
  56. * It's possible to disable individual attributes added by ngAria with the
  57. * {@link ngAria.$ariaProvider#config config} method. For more details, see the
  58. * {@link guide/accessibility Developer Guide}.
  59. */
  60. var ngAriaModule = angular.module('ngAria', ['ng']).
  61. 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, /* interceptorFn */ null, /* expensiveChecks */ true);
  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);