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

Universal support for DimensionsHelper and ScrollbarHelper #1178

Merged
merged 28 commits into from
Dec 13, 2017

Conversation

earlyster
Copy link
Contributor

What kind of change does this PR introduce? (check one with "x")

  • [x ] Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior? (You can also link to an open issue here)

Error when you load ngx-datagrid scrollbar width is calculated and grid width is calculated based on window size. With universal SSR we can't calculate it that same way so we need to replace implementation of DimensionHelper with a universal helpful version

What is the new behavior?

No error and you must implement your own DimensionsHelper. Here is a snippet of my version of SSR DimensionsHelper. I had to export this service in order to inject a different implementation.

import { ScrollbarHelper } from '@swimlane/ngx-datatable';
import { Injectable, Inject } from '@angular/core';
import { Request } from 'express';
import { REQUEST } from '@nguniversal/express-engine/tokens';
import { DimensionsHelper } from '@swimlane/ngx-datatable';

@Injectable()
export class ServerDimensionsHelper extends DimensionsHelper {

    constructor(@Inject(REQUEST) private request: Request) {
        super();
    }


    getDimensions(element: Element): ClientRect {
        const width = parseInt(this.request.cookies['CH-DW'], 10) || 1000;
        const height = parseInt(this.request.cookies['CH-DH'], 10) || 800;

        const adjustedWidth = width;
        const adjustedHeight = height;


        return {
            height: adjustedHeight,
            bottom: 0,
            top: 0,
            width: adjustedWidth,
            left: 0,
            right: 0
        };
    }
}

Also had to create a ScrollbarHelper service implementation for server. Which returns my custom css style width of all scrollbars of 16.

import { ScrollbarHelper } from '@swimlane/ngx-datatable';
import { Injectable, Inject } from '@angular/core';
import { DOCUMENT } from '@angular/common';

@Injectable()
export class ServerScrollBarHelper extends ScrollbarHelper {
    width: number;

    constructor(@Inject(DOCUMENT) document) {
        super(document);
        this.width = 16;
    }

    getWidth(): number {
        return this.width;
    }
}

I then added these two services using @NgModule providers

  providers:  [  {
    provide: ScrollbarHelper,
    useClass: ServerScrollBarHelper
  },
  {
    provide: DimensionsHelper,
    useClass: ServerDimensionsHelper
  }]

Does this PR introduce a breaking change? (check one with "x")

  • Yes
  • No

If this PR contains a breaking change, please describe the impact and migration path for existing applications: ...

Other information:

I did not include these sample implementations in my pull request since this strategy may not work for everyone.

@amcdnl
Copy link
Contributor

amcdnl commented Dec 7, 2017

Thanks for the PR, this looks awesome. Can you remove the built files?

@@ -13,7 +14,7 @@ export declare class ScrollerComponent implements OnInit, OnDestroy {
element: any;
parentElement: any;
onScrollListener: any;
constructor(ngZone: NgZone, element: ElementRef);
constructor(ngZone: NgZone, element: ElementRef, renderer: Renderer2);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did you add Renderer here?

this.ngZone.runOutsideAngular(() => {
this.parentElement.addEventListener('scroll', this.onScrolled.bind(this));
renderer.listen(
this.parentElement, 'scroll', this.onScrolled.bind(this));
Copy link
Contributor

Choose a reason for hiding this comment

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

You actually shouldn't have to use the renderer to do this in v5.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I originally started with v4

node.classList.add('resize-handle');
this.element.appendChild(node);
const node = renderer2.createElement('span');
// node.classList.add('resize-handle');
Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove dead code.

export class DimensionsHelper {

// tslint:disable-next-line:no-empty
constructor() { }
Copy link
Contributor

Choose a reason for hiding this comment

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

Why empty constructor?

@@ -808,7 +813,8 @@ export class DatatableComponent implements OnInit, DoCheck, AfterViewInit {
*
*/
recalculateDims(): void {
const dims = this.element.getBoundingClientRect();
// const dims = this.element.getBoundingClientRect();
Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove dead code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@earlyster
Copy link
Contributor Author

PR should have all clean up now

@amcdnl
Copy link
Contributor

amcdnl commented Dec 10, 2017

One last request, can you add your example config to the docs?

@earlyster
Copy link
Contributor Author

Added universal doc

@amcdnl amcdnl merged commit d9d0c0b into swimlane:master Dec 13, 2017
@amcdnl
Copy link
Contributor

amcdnl commented Dec 13, 2017

Thanks for your contribution!!!

@earlyster
Copy link
Contributor Author

Your welcome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants