bidi.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * @license
  3. * Copyright Google LLC All Rights Reserved.
  4. *
  5. * Use of this source code is governed by an MIT-style license that can be
  6. * found in the LICENSE file at https://angular.io/license
  7. */
  8. import { DOCUMENT } from '@angular/common';
  9. import { inject, InjectionToken, EventEmitter, Inject, Injectable, Optional, Directive, Output, Input, NgModule, ɵɵdefineInjectable, ɵɵinject } from '@angular/core';
  10. /**
  11. * @fileoverview added by tsickle
  12. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  13. */
  14. /**
  15. * Injection token used to inject the document into Directionality.
  16. * This is used so that the value can be faked in tests.
  17. *
  18. * We can't use the real document in tests because changing the real `dir` causes geometry-based
  19. * tests in Safari to fail.
  20. *
  21. * We also can't re-provide the DOCUMENT token from platform-brower because the unit tests
  22. * themselves use things like `querySelector` in test code.
  23. *
  24. * This token is defined in a separate file from Directionality as a workaround for
  25. * https://github.com/angular/angular/issues/22559
  26. *
  27. * \@docs-private
  28. * @type {?}
  29. */
  30. const DIR_DOCUMENT = new InjectionToken('cdk-dir-doc', {
  31. providedIn: 'root',
  32. factory: DIR_DOCUMENT_FACTORY,
  33. });
  34. /**
  35. * \@docs-private
  36. * @return {?}
  37. */
  38. function DIR_DOCUMENT_FACTORY() {
  39. return inject(DOCUMENT);
  40. }
  41. /**
  42. * @fileoverview added by tsickle
  43. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  44. */
  45. /**
  46. * The directionality (LTR / RTL) context for the application (or a subtree of it).
  47. * Exposes the current direction and a stream of direction changes.
  48. */
  49. class Directionality {
  50. /**
  51. * @param {?=} _document
  52. */
  53. constructor(_document) {
  54. /**
  55. * The current 'ltr' or 'rtl' value.
  56. */
  57. this.value = 'ltr';
  58. /**
  59. * Stream that emits whenever the 'ltr' / 'rtl' state changes.
  60. */
  61. this.change = new EventEmitter();
  62. if (_document) {
  63. // TODO: handle 'auto' value -
  64. // We still need to account for dir="auto".
  65. // It looks like HTMLElemenet.dir is also "auto" when that's set to the attribute,
  66. // but getComputedStyle return either "ltr" or "rtl". avoiding getComputedStyle for now
  67. /** @type {?} */
  68. const bodyDir = _document.body ? _document.body.dir : null;
  69. /** @type {?} */
  70. const htmlDir = _document.documentElement ? _document.documentElement.dir : null;
  71. /** @type {?} */
  72. const value = bodyDir || htmlDir;
  73. this.value = (value === 'ltr' || value === 'rtl') ? value : 'ltr';
  74. }
  75. }
  76. /**
  77. * @return {?}
  78. */
  79. ngOnDestroy() {
  80. this.change.complete();
  81. }
  82. }
  83. Directionality.decorators = [
  84. { type: Injectable, args: [{ providedIn: 'root' },] },
  85. ];
  86. /** @nocollapse */
  87. Directionality.ctorParameters = () => [
  88. { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [DIR_DOCUMENT,] }] }
  89. ];
  90. /** @nocollapse */ Directionality.ngInjectableDef = ɵɵdefineInjectable({ factory: function Directionality_Factory() { return new Directionality(ɵɵinject(DIR_DOCUMENT, 8)); }, token: Directionality, providedIn: "root" });
  91. /**
  92. * @fileoverview added by tsickle
  93. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  94. */
  95. /**
  96. * Directive to listen for changes of direction of part of the DOM.
  97. *
  98. * Provides itself as Directionality such that descendant directives only need to ever inject
  99. * Directionality to get the closest direction.
  100. */
  101. class Dir {
  102. constructor() {
  103. /**
  104. * Normalized direction that accounts for invalid/unsupported values.
  105. */
  106. this._dir = 'ltr';
  107. /**
  108. * Whether the `value` has been set to its initial value.
  109. */
  110. this._isInitialized = false;
  111. /**
  112. * Event emitted when the direction changes.
  113. */
  114. this.change = new EventEmitter();
  115. }
  116. /**
  117. * \@docs-private
  118. * @return {?}
  119. */
  120. get dir() { return this._dir; }
  121. /**
  122. * @param {?} value
  123. * @return {?}
  124. */
  125. set dir(value) {
  126. /** @type {?} */
  127. const old = this._dir;
  128. /** @type {?} */
  129. const normalizedValue = value ? value.toLowerCase() : value;
  130. this._rawDir = value;
  131. this._dir = (normalizedValue === 'ltr' || normalizedValue === 'rtl') ? normalizedValue : 'ltr';
  132. if (old !== this._dir && this._isInitialized) {
  133. this.change.emit(this._dir);
  134. }
  135. }
  136. /**
  137. * Current layout direction of the element.
  138. * @return {?}
  139. */
  140. get value() { return this.dir; }
  141. /**
  142. * Initialize once default value has been set.
  143. * @return {?}
  144. */
  145. ngAfterContentInit() {
  146. this._isInitialized = true;
  147. }
  148. /**
  149. * @return {?}
  150. */
  151. ngOnDestroy() {
  152. this.change.complete();
  153. }
  154. }
  155. Dir.decorators = [
  156. { type: Directive, args: [{
  157. selector: '[dir]',
  158. providers: [{ provide: Directionality, useExisting: Dir }],
  159. host: { '[attr.dir]': '_rawDir' },
  160. exportAs: 'dir',
  161. },] },
  162. ];
  163. Dir.propDecorators = {
  164. change: [{ type: Output, args: ['dirChange',] }],
  165. dir: [{ type: Input }]
  166. };
  167. /**
  168. * @fileoverview added by tsickle
  169. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  170. */
  171. class BidiModule {
  172. }
  173. BidiModule.decorators = [
  174. { type: NgModule, args: [{
  175. exports: [Dir],
  176. declarations: [Dir],
  177. },] },
  178. ];
  179. /**
  180. * @fileoverview added by tsickle
  181. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  182. */
  183. /**
  184. * @fileoverview added by tsickle
  185. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  186. */
  187. export { Directionality, DIR_DOCUMENT, Dir, BidiModule, DIR_DOCUMENT_FACTORY as ɵa };
  188. //# sourceMappingURL=bidi.js.map