forked from angular/components
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(directionality): a provider to get the overall directionality
- Looks at the `html` and `body` elements for `dir` attribute and sets the Directionality service value to it - Whenever someone would try to inject Directionality - if there's a Dir directive up the dom tree it would be provided fixes angular#3600
- Loading branch information
1 parent
90e6d3c
commit ce40d79
Showing
33 changed files
with
331 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { | ||
Directive, | ||
HostBinding, | ||
Output, | ||
Input, | ||
EventEmitter | ||
} from '@angular/core'; | ||
|
||
import {Direction, Directionality} from './directionality'; | ||
|
||
/** | ||
* Directive to listen for changes of direction of part of the DOM. | ||
* | ||
* Would provide itself in case a component looks for the Directionality service | ||
*/ | ||
@Directive({ | ||
selector: '[dir]', | ||
// TODO(hansl): maybe `$implicit` isn't the best option here, but for now that's the best we got. | ||
exportAs: '$implicit', | ||
providers: [ | ||
{provide: Directionality, useExisting: Dir} | ||
] | ||
}) | ||
export class Dir implements Directionality { | ||
/** Layout direction of the element. */ | ||
_dir: Direction = 'ltr'; | ||
|
||
/** Whether the `value` has been set to its initial value. */ | ||
private _isInitialized: boolean = false; | ||
|
||
/** Event emitted when the direction changes. */ | ||
@Output('dirChange') change = new EventEmitter<void>(); | ||
|
||
/** @docs-private */ | ||
@HostBinding('attr.dir') | ||
@Input('dir') | ||
get dir(): Direction { | ||
return this._dir; | ||
} | ||
|
||
set dir(v: Direction) { | ||
let old = this._dir; | ||
this._dir = v; | ||
if (old !== this._dir && this._isInitialized) { | ||
this.change.emit(); | ||
} | ||
} | ||
|
||
/** Current layout direction of the element. */ | ||
get value(): Direction { return this.dir; } | ||
set value(v: Direction) { this.dir = v; } | ||
|
||
/** Initialize once default value has been set. */ | ||
ngAfterContentInit() { | ||
this._isInitialized = true; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import {async, fakeAsync, TestBed, tick} from '@angular/core/testing'; | ||
import {Component, getDebugNode} from '@angular/core'; | ||
import {By} from '@angular/platform-browser'; | ||
import {Directionality, BidiModule} from './index'; | ||
|
||
describe('Directionality', () => { | ||
let documentElementDir, bodyDir; | ||
|
||
beforeAll(() => { | ||
documentElementDir = document.documentElement.dir; | ||
bodyDir = document.body.dir; | ||
}); | ||
|
||
afterAll(() => { | ||
document.documentElement.dir = documentElementDir; | ||
document.body.dir = bodyDir; | ||
}); | ||
|
||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [BidiModule], | ||
declarations: [ElementWithDir, InjectsDirectionality] | ||
}).compileComponents(); | ||
|
||
clearDocumentDirAttributes(); | ||
})); | ||
|
||
describe('Service', () => { | ||
it('should read dir from the html element if not specified on the body', () => { | ||
document.documentElement.dir = 'rtl'; | ||
|
||
let fixture = TestBed.createComponent(InjectsDirectionality); | ||
let testComponent = fixture.debugElement.componentInstance; | ||
|
||
expect(testComponent.dir.value).toBe('rtl'); | ||
}); | ||
|
||
it('should read dir from the body even it is also specified on the html element', () => { | ||
document.documentElement.dir = 'ltr'; | ||
document.body.dir = 'rtl'; | ||
|
||
let fixture = TestBed.createComponent(InjectsDirectionality); | ||
let testComponent = fixture.debugElement.componentInstance; | ||
|
||
expect(testComponent.dir.value).toBe('rtl'); | ||
}); | ||
|
||
it('should default to ltr if nothing is specified on either body or the html element', () => { | ||
let fixture = TestBed.createComponent(InjectsDirectionality); | ||
let testComponent = fixture.debugElement.componentInstance; | ||
|
||
expect(testComponent.dir.value).toBe('ltr'); | ||
}); | ||
}); | ||
|
||
describe('Dir directive', () => { | ||
it('should provide itself as Directionality', () => { | ||
let fixture = TestBed.createComponent(ElementWithDir); | ||
const injectedDirectionality = | ||
fixture.debugElement.query(By.directive(InjectsDirectionality)).componentInstance.dir; | ||
|
||
fixture.detectChanges(); | ||
|
||
expect(injectedDirectionality.value).toBe('rtl'); | ||
}); | ||
|
||
it('should emit a change event when the value changes', fakeAsync(() => { | ||
let fixture = TestBed.createComponent(ElementWithDir); | ||
const injectedDirectionality = | ||
fixture.debugElement.query(By.directive(InjectsDirectionality)).componentInstance.dir; | ||
|
||
fixture.detectChanges(); | ||
|
||
expect(injectedDirectionality.value).toBe('rtl'); | ||
expect(fixture.componentInstance.changeCount).toBe(0); | ||
|
||
fixture.componentInstance.direction = 'ltr'; | ||
|
||
fixture.detectChanges(); | ||
tick(); | ||
|
||
expect(injectedDirectionality.value).toBe('ltr'); | ||
expect(fixture.componentInstance.changeCount).toBe(1); | ||
})); | ||
}); | ||
}); | ||
|
||
|
||
function clearDocumentDirAttributes() { | ||
document.documentElement.dir = ''; | ||
document.body.dir = ''; | ||
} | ||
|
||
@Component({ | ||
template: ` | ||
<div [dir]="direction" (dirChange)="changeCount= changeCount + 1"> | ||
<injects-directionality></injects-directionality> | ||
</div> | ||
` | ||
}) | ||
class ElementWithDir { | ||
direction = 'rtl'; | ||
changeCount = 0; | ||
} | ||
|
||
/** Test component with Dir directive. */ | ||
@Component({ | ||
selector: 'injects-directionality', | ||
template: `<div></div>` | ||
}) | ||
class InjectsDirectionality { | ||
constructor(public dir: Directionality) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { | ||
EventEmitter, | ||
Injectable, | ||
Optional, | ||
SkipSelf | ||
} from '@angular/core'; | ||
|
||
export type Direction = 'ltr' | 'rtl'; | ||
|
||
/** | ||
* The directionality (LTR / RTL) context for the application (or a subtree of it). | ||
* Exposes the current direction and a stream of direction changes. | ||
*/ | ||
@Injectable() | ||
export class Directionality { | ||
value: Direction = 'ltr'; | ||
public change = new EventEmitter<void>(); | ||
|
||
constructor() { | ||
if (typeof document === 'object' && !!document) { | ||
// TODO: handle 'auto' value - | ||
// We still need to account for dir="auto". | ||
// It looks like HTMLElemenet.dir is also "auto" when that's set to the attribute, | ||
// but getComputedStyle return either "ltr" or "rtl". avoiding getComputedStyle for now | ||
// though, we're already calling it for the theming check. | ||
this.value = (document.body.dir || document.documentElement.dir || 'ltr') as Direction; | ||
} | ||
} | ||
} | ||
|
||
export function DIRECTIONALITY_PROVIDER_FACTORY(parentDirectionality) { | ||
return parentDirectionality || new Directionality(); | ||
} | ||
|
||
export const DIRECTIONALITY_PROVIDER = { | ||
// If there is already a Directionality available, use that. Otherwise, provide a new one. | ||
provide: Directionality, | ||
deps: [[new Optional(), new SkipSelf(), Directionality]], | ||
useFactory: DIRECTIONALITY_PROVIDER_FACTORY | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import {ModuleWithProviders, NgModule} from '@angular/core'; | ||
import {Dir} from './dir'; | ||
import {Directionality, DIRECTIONALITY_PROVIDER} from './directionality'; | ||
|
||
export { | ||
Directionality, | ||
DIRECTIONALITY_PROVIDER, | ||
Direction | ||
} from './directionality'; | ||
export {Dir} from './dir'; | ||
|
||
@NgModule({ | ||
exports: [Dir], | ||
declarations: [Dir], | ||
providers: [Directionality] | ||
}) | ||
export class BidiModule { | ||
/** @deprecated */ | ||
static forRoot(): ModuleWithProviders { | ||
return { | ||
ngModule: BidiModule, | ||
providers: [DIRECTIONALITY_PROVIDER] | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.