Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(layout-direction): a service to accompany the Dir directive #2286

Closed

Conversation

EladBezalel
Copy link
Member

  • Finds the application root element and traverses upwards and looking for dir attribute
  • If Dir directive is available throughout the application it will be injected instead the activeLayoutDirection service

fixes #1719

@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Dec 19, 2016
@@ -1,6 +1,6 @@
import {NgModule, ModuleWithProviders} from '@angular/core';
import {MdLineModule} from './line/line';
import {RtlModule} from './rtl/dir';
import {RtlModule} from './rtl/index';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't need index in there


constructor(private _appRef: ApplicationRef) {}

getActiveDirection(injector: Injector): Promise<LayoutDirection> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs JsDoc description (here and for other method)

})
.then(rootComponent => {
return this._getParentDirection(rootComponent);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return Promise.resolve(null)
    .then(() => this._getRootElement(injector))
    .then(rootComponent => this._getParentDirection(rootComponent));

Also add comment explaining why the Promise.resolve is necessary


for (let i = 0; i < this._appRef.componentTypes.length; i++) {
if (injector.get(this._appRef.componentTypes[i])) {
rootComponent = this._appRef.components[i].location;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just return directly here


@Injectable()
export class ActiveLayoutDirection {
dirChange = new EventEmitter<void>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can do someting like

dirChange = Observable.of([])

With a comment explaining that this won't ever fire anything

} from '@angular/core';

import {LayoutDirection} from './dir';
import {EventEmitter} from '@angular/common/src/facade/async';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unnecessary since it will be changed to an Observable, but FYI never deep import from anything in @angular/xxx. This would normally be

import {EventEmitter} from '@angular/core';

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Webstorm auto import 😅

return rootComponent.nativeElement;
}

_getParentDirection(element: HTMLElement): string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_getClosestAncestorDirValue

constructor(
private _renderer: Renderer,
private _element: ElementRef,
@Optional() private _dir: Dir) {}
@Optional() _activeLayoutDirection: ActiveLayoutDirection,
@Optional() _injector: Injector) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Injector doesn't have to be @Optional. You can always inject an Injector.

import {EventEmitter} from '@angular/common/src/facade/async';

@Injectable()
export class ActiveLayoutDirection {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename this class to something like GlobalDirAccessor and then add another class called ActiveLayoutDirection that just looks like

export class ActiveLayoutDirection {
  dirChange: Observable<void>;
  getActiveDirection(injector: Injector): Promise<LayoutDirection> { return null; }
}

The both Dir and GlobalDirAccessor will have implements ActiveLayoutDirection (TypeScript lets you implement a class just like an interface. Keeping it as a class lets us use it as an injection token).

- Finds the application root element and traverses upwards and looking for dir attribute
- If Dir directive is available throughout the application it will be injected instead the activeLayoutDirection service

fixes angular#1719
@jelbourn
Copy link
Member

@EladBezalel was this ready for another round of review?

@EladBezalel
Copy link
Member Author

nope 😅

@EladBezalel
Copy link
Member Author

A better solution was suggested on #3600

@EladBezalel EladBezalel deleted the feat/layout-direction branch March 14, 2017 18:19
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes PR author has agreed to Google's Contributor License Agreement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Introduce [ltr, rtl] layout direction service
4 participants