Skip to content

Commit a4aeb72

Browse files
authored
Merge pull request #51 from plotly/separated-bundle
Separated bundle
2 parents a57c9b4 + e892381 commit a4aeb72

File tree

7 files changed

+90
-22
lines changed

7 files changed

+90
-22
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ Add the `PlotlyModule` into the main app module of your project
4343
import { NgModule } from '@angular/core';
4444
import { CommonModule } from '@angular/common';
4545

46+
import * as PlotlyJS from 'plotly.js/dist/plotly.js';
4647
import { PlotlyModule } from 'angular-plotly.js';
4748

49+
PlotlyModule.plotlyjs = PlotlyJS;
50+
4851
@NgModule({
4952
imports: [CommonModule, PlotlyModule],
5053
...

package-lock.json

Lines changed: 30 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app/demo/demo.module.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { CommonModule } from '@angular/common';
33
import { HttpClientModule } from '@angular/common/http';
44
import { RouterModule, Routes } from '@angular/router';
55

6+
import * as PlotlyJS from 'plotly.js/dist/plotly.js';
7+
68
import { HomeComponent } from './home/home.component';
79
import { DemoComponent } from './demo.component';
810

@@ -33,12 +35,16 @@ const demoRoutes: Routes = [
3335
{ path: '', redirectTo: '/home', pathMatch: 'full' },
3436
];
3537

38+
39+
PlotlyModule.plotlyjs = PlotlyJS;
40+
// PlotlyViaCDNModule.plotlyVersion = 'latest';
41+
3642
@NgModule({
3743
imports: [
3844
CommonModule,
3945
HttpClientModule,
4046
PlotlyModule,
41-
// PlotlyViaCDNModule.forRoot({version: 'latest'}),
47+
// PlotlyViaCDNModule,
4248
// PlotlyViaWindowModule,
4349
RouterModule.forRoot(demoRoutes, { enableTracing: true }),
4450
],

src/app/plotly-via-cdn/plotly-via-cdn.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class PlotlyViaCDNModule {
5151
fn();
5252
}
5353

54-
static forRoot(config: Partial<{version: string}>): ModuleWithProviders {
54+
static forRoot(config: Partial<{version: string}>): ModuleWithProviders<PlotlyViaCDNModule> {
5555
if (config.version === undefined) {
5656
console.warn(`It's strongly recommended that you set a plotly version when using via CDN.`);
5757
config.version = 'latest';

src/app/plotly/plotly.module.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { PlotlyModule } from "./plotly.module";
2+
3+
4+
5+
describe('PlotlyModule', () => {
6+
7+
it('should create', () => {
8+
const fn = () => {
9+
const mod = new PlotlyModule();
10+
};
11+
12+
const msg = "Invalid PlotlyJS object. Please check https://github.com/plotly/angular-plotly.js#quick-start"
13+
+ " to see how to add PlotlyJS to your project.";
14+
expect(fn).toThrowError(msg);
15+
16+
17+
const fn2 = () => {
18+
PlotlyModule.plotlyjs = {plot: function() { }};
19+
const mod = new PlotlyModule();
20+
};
21+
22+
expect(fn2).not.toThrowError(msg);
23+
});
24+
25+
});

src/app/plotly/plotly.module.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { NgModule, ModuleWithProviders } from '@angular/core';
1+
import { NgModule } from '@angular/core';
22
import { CommonModule } from '@angular/common';
33

4-
import * as PlotlyJS from 'plotly.js/dist/plotly.js';
5-
64
import { PlotComponent } from '../shared/plot/plot.component';
75
import { PlotlyService } from '../shared/plotly.service';
86
import { SharedModule } from '../shared/shared.module';
@@ -14,14 +12,20 @@ import { SharedModule } from '../shared/shared.module';
1412
exports: [PlotComponent]
1513
})
1614
export class PlotlyModule {
15+
public static plotlyjs: any = {};
16+
1717
constructor() {
18-
PlotlyService.setPlotly(PlotlyJS);
18+
if (!this.isValid()) {
19+
const msg = "Invalid PlotlyJS object. Please check https://github.com/plotly/angular-plotly.js#quick-start"
20+
+ " to see how to add PlotlyJS to your project.";
21+
throw new Error(msg);
22+
}
23+
24+
PlotlyService.setPlotly(PlotlyModule.plotlyjs);
1925
}
2026

21-
static forRoot(): ModuleWithProviders {
22-
return {
23-
ngModule: PlotlyModule,
24-
providers: [PlotlyService]
25-
};
27+
private isValid(): boolean {
28+
return PlotlyModule.plotlyjs !== undefined
29+
&& typeof PlotlyModule.plotlyjs.plot === 'function';
2630
}
2731
}

src/app/shared/plot/plot.component.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,17 @@ describe('PlotComponent', () => {
141141
}, 13);
142142
});
143143

144+
it('should fail when plotlyInstance is undefined', () => {
145+
component.plotlyInstance = undefined;
146+
147+
const error = new Error(`Plotly component wasn't initialized`);
148+
const fn = () => {
149+
component.updatePlot();
150+
};
151+
152+
expect(fn).toThrow(error);
153+
});
154+
144155
it('should add handler into window.resize when useResizeHandler=true', () => {
145156
spyOn(component, 'getWindow').and.callFake(() => windowSpy);
146157

0 commit comments

Comments
 (0)