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

fix(tabs): observing tab header label changes to recalculate width #2186

Merged
merged 1 commit into from
Dec 20, 2016

Conversation

EladBezalel
Copy link
Member

  • Uses observe-changes directive that emit an event when the mutation observer notifies a change

fixes #2155

@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Dec 12, 2016
constructor(private elementRef: ElementRef) {}

ngAfterViewInit() {
this.observer = new MutationObserver(mutations => mutations.forEach(() => this.event.emit()));
Copy link
Member

@crisbeto crisbeto Dec 12, 2016

Choose a reason for hiding this comment

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

Might be a good idea to clear out the mutation observer on destroy.

selector: '[cdk-observe-content]'
})
export class MdMutationObserver {
private observer: MutationObserver;
Copy link
Member

Choose a reason for hiding this comment

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

We prefix private members with underscore (_observer)

export class MdMutationObserver {
private observer: MutationObserver;

@Output('cdk-observe-content') event = new EventEmitter();
Copy link
Member

Choose a reason for hiding this comment

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

I think it should be EventEmitter<void>


@Output('cdk-observe-content') event = new EventEmitter();

constructor(private elementRef: ElementRef) {}
Copy link
Member

Choose a reason for hiding this comment

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

_elementRef


constructor(private elementRef: ElementRef) {}

ngAfterViewInit() {
Copy link
Member

Choose a reason for hiding this comment

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

Should be ngAfterContentInit, since technically a @Directive doesn't have a view, only content.

ngAfterViewInit() {
this.observer = new MutationObserver(mutations => mutations.forEach(() => this.event.emit()));

this.observer.observe(this.elementRef.nativeElement, {characterData: true, subtree: true});
Copy link
Member

Choose a reason for hiding this comment

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

Don't need childList?

@Directive({
selector: '[cdk-observe-content]'
})
export class MdMutationObserver {
Copy link
Member

Choose a reason for hiding this comment

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

Call this class just ObserveContent

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops..

@@ -150,6 +148,12 @@ export class MdTabHeader {
}
}

_checkPagination() {
Copy link
Member

Choose a reason for hiding this comment

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

_updatePagination? Also needs a method description


fixture.componentInstance.text = 'text';
fixture.detectChanges();
});
Copy link
Member

Choose a reason for hiding this comment

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

Why not have below

class ComponentWithTextContent {
  contentUpdated = jasmine.createSpy('contentUpdated');
}

And then in the test

let fixture = TestBed.createComponent(ComponentWithTextContent);
fixture.detectChanges();
fixture.componentInstance.contentUpdated.reset();

fixture.componentInstance.text = 'Taco';
fixture.detectChanges();

expect(fixture.componentInstance.contentUpdated.calls.count())
    .toBe(1, 'Expected exactly one content change event');


fixture.componentInstance.text = 'Chocolate';
fixture.detectChanges();

expect(fixture.componentInstance.contentUpdated.calls.count())
    .toBe(2, 'Expected exactly two content change events');

(similar for other test)

Copy link
Member

Choose a reason for hiding this comment

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

Just tried this locally and I saw that the async stuff doesn't really work. Seems like zones doesn't play well with MutationObserver...

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, Zones is supposed to understand MutationObserver, I'll have to check with Misko on this tomorrow.

@EladBezalel EladBezalel force-pushed the feat/observe-changes branch 3 times, most recently from 650d09b to 9b2404b Compare December 14, 2016 18:50
@EladBezalel
Copy link
Member Author

@jelbourn @crisbeto please re-review :)

@Directive({
selector: '[cdk-observe-content]'
})
export class ObserveContent {
Copy link
Member

Choose a reason for hiding this comment

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

You should add an annotation to the class: export class ObserveContent implements OnDestroy, AfterContentInit.

Copy link
Member Author

Choose a reason for hiding this comment

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

When you're right, you're right!

@EladBezalel EladBezalel force-pushed the feat/observe-changes branch 2 times, most recently from 5fa3c58 to f839831 Compare December 16, 2016 14:48
Copy link
Member

@jelbourn jelbourn left a comment

Choose a reason for hiding this comment

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

Can you fix the lint error?

@@ -150,6 +148,16 @@ export class MdTabHeader {
}
}

/**
* Updating the view whether pagination should be enabled or not
* @private
Copy link
Member

Choose a reason for hiding this comment

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

Don't use @private JsDoc, it messes up stuff when we sync into google

@EladBezalel EladBezalel force-pushed the feat/observe-changes branch 2 times, most recently from 56d06b1 to d0b4fc8 Compare December 19, 2016 22:22
@jelbourn
Copy link
Member

@andrewseguin can you review as well?

Copy link
Contributor

@andrewseguin andrewseguin left a comment

Choose a reason for hiding this comment

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

LGTM, small thing about the output

export class ObserveContent implements AfterContentInit, OnDestroy {
private _observer: MutationObserver;

@Output('cdk-observe-content') event = new EventEmitter<void>();
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be camelcased?

- Uses observe-changes directive that emit an event when the mutation observer notifies a change

fixes angular#2155
@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.

5 participants