|
| 1 | +import { |
| 2 | + EventEmitter, |
| 3 | + Injectable, |
| 4 | + ModuleWithProviders, |
| 5 | + NgModule, |
| 6 | + Optional, |
| 7 | + SkipSelf |
| 8 | +} from '@angular/core'; |
| 9 | + |
| 10 | +import {Dir} from './dir'; |
| 11 | + |
| 12 | +export type Direction = 'ltr' | 'rtl'; |
| 13 | + |
| 14 | +/** |
| 15 | + * The directionality (LTR / RTL) context for the application (or a subtree of it). |
| 16 | + * Exposes the current direction and a stream of direction changes. |
| 17 | + */ |
| 18 | +@Injectable() |
| 19 | +export class Directionality { |
| 20 | + value: Direction = 'ltr'; |
| 21 | + change: EventEmitter<void> = new EventEmitter<void>(); |
| 22 | + |
| 23 | + constructor() { |
| 24 | + if (document) { |
| 25 | + // TODO: handle auto value - |
| 26 | + // We still need to account for dir="auto". |
| 27 | + // It looks like HTMLElemenet.dir is also "auto" when that's set to the attribute, |
| 28 | + // but getComputedStyle return either "ltr" or "rtl". avoiding getComputedStyle for now |
| 29 | + // though, we're already calling it for the theming check. |
| 30 | + this.value = |
| 31 | + (document.body.getAttribute('dir') || |
| 32 | + document.documentElement.getAttribute('dir') || |
| 33 | + 'ltr') as Direction; |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +@NgModule({ |
| 39 | + exports: [Dir], |
| 40 | + declarations: [Dir], |
| 41 | + providers: [Directionality] |
| 42 | +}) |
| 43 | +export class BidiModule { |
| 44 | + /** @deprecated */ |
| 45 | + static forRoot(): ModuleWithProviders { |
| 46 | + return { |
| 47 | + ngModule: BidiModule, |
| 48 | + providers: [DIRECTIONALITY_PROVIDER] |
| 49 | + }; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +export const DIRECTIONALITY_PROVIDER = { |
| 54 | + // If there is already a Directionality available, use that. Otherwise, provide a new one. |
| 55 | + provide: Directionality, |
| 56 | + deps: [[new Optional(), new SkipSelf(), Directionality]], |
| 57 | + useFactory: function (parentDirectionality) { |
| 58 | + return parentDirectionality || new Directionality(); |
| 59 | + } |
| 60 | +}; |
0 commit comments