UNPKG

6.95 kBJavaScriptView Raw
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 */
8import { EventEmitter, Inject, Injectable, Optional } from '@angular/core';
9import { DIR_DOCUMENT } from './dir-document-token';
10import * as i0 from "@angular/core";
11/** Regex that matches locales with an RTL script. Taken from `goog.i18n.bidi.isRtlLanguage`. */
12const RTL_LOCALE_PATTERN = /^(ar|ckb|dv|he|iw|fa|nqo|ps|sd|ug|ur|yi|.*[-_](Adlm|Arab|Hebr|Nkoo|Rohg|Thaa))(?!.*[-_](Latn|Cyrl)($|-|_))($|-|_)/i;
13/** Resolves a string value to a specific direction. */
14export function _resolveDirectionality(rawValue) {
15 const value = rawValue?.toLowerCase() || '';
16 if (value === 'auto' && typeof navigator !== 'undefined' && navigator?.language) {
17 return RTL_LOCALE_PATTERN.test(navigator.language) ? 'rtl' : 'ltr';
18 }
19 return value === 'rtl' ? 'rtl' : 'ltr';
20}
21/**
22 * The directionality (LTR / RTL) context for the application (or a subtree of it).
23 * Exposes the current direction and a stream of direction changes.
24 */
25class Directionality {
26 constructor(_document) {
27 /** The current 'ltr' or 'rtl' value. */
28 this.value = 'ltr';
29 /** Stream that emits whenever the 'ltr' / 'rtl' state changes. */
30 this.change = new EventEmitter();
31 if (_document) {
32 const bodyDir = _document.body ? _document.body.dir : null;
33 const htmlDir = _document.documentElement ? _document.documentElement.dir : null;
34 this.value = _resolveDirectionality(bodyDir || htmlDir || 'ltr');
35 }
36 }
37 ngOnDestroy() {
38 this.change.complete();
39 }
40 static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: Directionality, deps: [{ token: DIR_DOCUMENT, optional: true }], target: i0.ɵɵFactoryTarget.Injectable }); }
41 static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: Directionality, providedIn: 'root' }); }
42}
43export { Directionality };
44i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: Directionality, decorators: [{
45 type: Injectable,
46 args: [{ providedIn: 'root' }]
47 }], ctorParameters: function () { return [{ type: undefined, decorators: [{
48 type: Optional
49 }, {
50 type: Inject,
51 args: [DIR_DOCUMENT]
52 }] }]; } });
53//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZGlyZWN0aW9uYWxpdHkuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi8uLi9zcmMvY2RrL2JpZGkvZGlyZWN0aW9uYWxpdHkudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7Ozs7OztHQU1HO0FBRUgsT0FBTyxFQUFDLFlBQVksRUFBRSxNQUFNLEVBQUUsVUFBVSxFQUFFLFFBQVEsRUFBWSxNQUFNLGVBQWUsQ0FBQztBQUNwRixPQUFPLEVBQUMsWUFBWSxFQUFDLE1BQU0sc0JBQXNCLENBQUM7O0FBSWxELGdHQUFnRztBQUNoRyxNQUFNLGtCQUFrQixHQUN0QixvSEFBb0gsQ0FBQztBQUV2SCx1REFBdUQ7QUFDdkQsTUFBTSxVQUFVLHNCQUFzQixDQUFDLFFBQWdCO0lBQ3JELE1BQU0sS0FBSyxHQUFHLFFBQVEsRUFBRSxXQUFXLEVBQUUsSUFBSSxFQUFFLENBQUM7SUFFNUMsSUFBSSxLQUFLLEtBQUssTUFBTSxJQUFJLE9BQU8sU0FBUyxLQUFLLFdBQVcsSUFBSSxTQUFTLEVBQUUsUUFBUSxFQUFFO1FBQy9FLE9BQU8sa0JBQWtCLENBQUMsSUFBSSxDQUFDLFNBQVMsQ0FBQyxRQUFRLENBQUMsQ0FBQyxDQUFDLENBQUMsS0FBSyxDQUFDLENBQUMsQ0FBQyxLQUFLLENBQUM7S0FDcEU7SUFFRCxPQUFPLEtBQUssS0FBSyx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