Skip to content
This repository was archived by the owner on Sep 28, 2022. It is now read-only.

Commit 46f8d31

Browse files
committed
attempting to fix jupyterlab version of scriptedforms
1 parent ed77352 commit 46f8d31

9 files changed

Lines changed: 110 additions & 69 deletions

File tree

scriptedforms/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"build": "rimraf lib && webpack --config webpack.dev.js",
2727
"build:prod": "rimraf lib && webpack --config webpack.prod.js",
2828
"build:jlab": "rimraf ./jlab-build && copyfiles -u 1 './src/**/*.{css,html,scss}' ./jlab-build && tsc && yarn package",
29+
"watch:jlab": "tsc -w",
2930
"package": "rimraf simonbiggs-scriptedforms*.tgz && yarn pack",
3031
"selenium": "./scripts/selenium.sh",
3132
"test": "yarn --cwd ./e2e e2e",

scriptedforms/src/app.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,21 @@ document.getElementsByTagName('head').item(0).appendChild(userStyle);
2727
import { Widget } from '@phosphor/widgets';
2828
import { ServiceManager, ContentsManager } from '@jupyterlab/services';
2929
import { ScriptedFormsWidget } from './app/widget';
30+
import { AngularLoader } from './app/phosphor-angular-loader';
31+
import { AppModule } from './app/app.module';
3032

3133
export function loadApp(): void {
3234
const serviceManager = new ServiceManager();
3335
const contentsManager = new ContentsManager();
36+
const angularLoader = new AngularLoader<AppModule>(AppModule);
3437

3538
const formWidget = new ScriptedFormsWidget({
3639
serviceManager,
37-
contentsManager
40+
contentsManager,
41+
angularLoader
3842
});
3943

40-
formWidget.content.initiliseScriptedForms();
44+
// formWidget.content.initiliseScriptedForms();
4145
window.onresize = () => { formWidget.update(); };
4246
Widget.attach(formWidget, document.body);
4347
}

scriptedforms/src/app/app-error-handler.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,19 @@ import { ErrorHandler } from '@angular/core';
4040
export class AppErrorHandler extends ErrorHandler {
4141

4242
handleError(error: any) {
43-
const errorbox = document.getElementsByClassName('errorbox');
44-
if (errorbox.length > 0) {
45-
errorbox[0].innerHTML = `<h2>Javascript Error:</h2>
46-
<p>
47-
A Javascript error has occured. This could be due to an error within your
48-
ScriptedForms template or an issue with ScriptedForms itself.
49-
</p>
50-
<div class="jp-OutputArea-child">
51-
<div class="jp-OutputPrompt"></div>
52-
<div class="jp-RenderedText" data-mime-type="application/vnd.jupyter.stderr">
53-
<pre style="white-space: pre-wrap;">
54-
` + error + '</pre></div></div>';
55-
}
43+
// const errorbox = document.getElementsByClassName('errorbox');
44+
// if (errorbox.length > 0) {
45+
// errorbox[0].innerHTML = `<h2>Javascript Error:</h2>
46+
// <p>
47+
// A Javascript error has occured. This could be due to an error within your
48+
// ScriptedForms template or an issue with ScriptedForms itself.
49+
// </p>
50+
// <div class="jp-OutputArea-child">
51+
// <div class="jp-OutputPrompt"></div>
52+
// <div class="jp-RenderedText" data-mime-type="application/vnd.jupyter.stderr">
53+
// <pre style="white-space: pre-wrap;">
54+
// ` + error + '</pre></div></div>';
55+
// }
5656

5757
// delegate to the default handler
5858
super.handleError(error);

scriptedforms/src/app/form-builder-module/form-builder.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ export class FormBuilderComponent implements OnInit, AfterViewInit {
8181
this.myMarkdownIt = new MarkdownIt({
8282
html: true,
8383
linkify: true,
84-
typographer: true
84+
typographer: true,
8585
});
86+
this.myMarkdownIt.disable('code')
8687
}
8788

8889
ngAfterViewInit() {

scriptedforms/src/app/phosphor-angular-loader.ts

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
import {
2727
ApplicationRef, Type, Injector,
2828
ComponentFactoryResolver, ComponentRef, NgZone,
29-
NgModuleRef
29+
// NgModuleRef
3030
} from '@angular/core';
3131

3232
import {
@@ -67,43 +67,57 @@ export class AngularLoader<M> {
6767
private componentFactoryResolver: ComponentFactoryResolver;
6868
ngZone: NgZone;
6969
private injector: Injector;
70+
loaderReady = new PromiseDelegate<void>();
7071

71-
constructor( ngModuleRef: NgModuleRef<M>) {
72-
this.injector = ngModuleRef.injector;
73-
this.applicationRef = this.injector.get(ApplicationRef);
74-
this.ngZone = this.injector.get(NgZone);
75-
this.componentFactoryResolver = this.injector.get(ComponentFactoryResolver);
72+
constructor(ngModule: Type<M>) {
73+
platformBrowserDynamic().bootstrapModule(ngModule)
74+
.then(ngModuleRef => {
75+
this.injector = ngModuleRef.injector;
76+
this.applicationRef = this.injector.get(ApplicationRef);
77+
this.ngZone = this.injector.get(NgZone);
78+
this.componentFactoryResolver = this.injector.get(ComponentFactoryResolver);
79+
80+
this.loaderReady.resolve(null)
81+
})
7682
}
7783

78-
attachComponent<T>(ngComponent: Type<T>, dom: Element): ComponentRef<T> {
79-
let componentRef: ComponentRef<T>;
80-
this.ngZone.run(() => {
81-
const componentFactory = this.componentFactoryResolver.resolveComponentFactory(ngComponent);
82-
componentRef = componentFactory.create(this.injector, [], dom);
83-
this.applicationRef.attachView(componentRef.hostView);
84-
});
85-
return componentRef;
84+
attachComponent<T>(ngComponent: Type<T>, dom: Element): Promise<ComponentRef<T>> {
85+
const attachedComponent = new PromiseDelegate<ComponentRef<T>>();
86+
this.loaderReady.promise
87+
.then(() => {
88+
let componentRef: ComponentRef<T>;
89+
this.ngZone.run(() => {
90+
const componentFactory = this.componentFactoryResolver.resolveComponentFactory(ngComponent);
91+
componentRef = componentFactory.create(this.injector, [], dom);
92+
this.applicationRef.attachView(componentRef.hostView);
93+
94+
attachedComponent.resolve(componentRef)
95+
});
96+
})
97+
98+
return attachedComponent.promise;
8699
}
87100
}
88101

89102
export class AngularWidget<C, M> extends Widget {
90-
angularLoader: AngularLoader<M>;
91103
ngZone: NgZone;
92104
componentRef: ComponentRef<C>;
93105
componentInstance: C;
94106
componentReady = new PromiseDelegate<void>();
95107

96-
constructor(ngComponent: Type<C>, ngModule: Type<M>, options?: Widget.IOptions) {
108+
constructor(ngComponent: Type<C>, angularLoader: AngularLoader<M>, options?: Widget.IOptions) {
97109
super(options);
98-
platformBrowserDynamic().bootstrapModule(ngModule)
99-
.then(ngModuleRef => {
100-
this.angularLoader = new AngularLoader(ngModuleRef);
101-
this.ngZone = this.angularLoader.ngZone;
102-
this.componentRef = this.angularLoader.attachComponent(
110+
angularLoader.loaderReady.promise
111+
.then(() => {
112+
this.ngZone = angularLoader.ngZone;
113+
return angularLoader.attachComponent(
103114
ngComponent, this.node);
115+
})
116+
.then(componentRef => {
117+
this.componentRef = componentRef;
104118
this.componentInstance = this.componentRef.instance;
105119
this.componentReady.resolve(undefined);
106-
});
120+
})
107121
}
108122

109123
run(func: () => void): void {

scriptedforms/src/app/services/file.service.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,20 @@ export class FileService {
6464
) { }
6565

6666
getApplicationBaseUrl() {
67-
const jupyterBaseUrl = JSON.parse(document.getElementById('jupyter-config-data').textContent).baseUrl;
68-
const application = JSON.parse(document.getElementById('scriptedforms-config-data').textContent).applicationToRun;
69-
70-
const baseUrl = `${window.location.protocol}//${window.location.host}${jupyterBaseUrl}scriptedforms/${application}/`;
71-
console.log(baseUrl);
67+
const SF_CONFIG = document.getElementById('scriptedforms-config-data');
68+
const JLAB_CONFIG = document.getElementById('jupyter-config-data');
69+
70+
let config: {baseUrl: string};
71+
72+
if (SF_CONFIG) {
73+
config = JSON.parse(SF_CONFIG.textContent);
74+
} else {
75+
config = JSON.parse(JLAB_CONFIG.textContent);
76+
}
7277

78+
const baseUrl = `${window.location.protocol}//${window.location.host}${config.baseUrl}`;
79+
// console.log(baseUrl);
80+
7381
return baseUrl;
7482
}
7583

scriptedforms/src/app/widget.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { Toolbar } from '@jupyterlab/apputils';
2121
import { DocumentRegistry } from '@jupyterlab/docregistry';
2222
import { PathExt } from '@jupyterlab/coreutils';
2323

24-
import { AngularWidget } from './phosphor-angular-loader';
24+
import { AngularWidget, AngularLoader } from './phosphor-angular-loader';
2525
import { AppComponent } from './app.component';
2626
import { AppModule } from './app.module';
2727

@@ -43,6 +43,7 @@ export namespace IScriptedFormsWidget {
4343
export interface IOptions {
4444
serviceManager: ServiceManager;
4545
contentsManager: ContentsManager;
46+
angularLoader: AngularLoader<AppModule>;
4647
context?: DocumentRegistry.Context;
4748
}
4849
}
@@ -52,6 +53,7 @@ export namespace IAngularWrapperWidget {
5253
toolbar: Toolbar<Widget>;
5354
serviceManager: ServiceManager;
5455
contentsManager: ContentsManager;
56+
angularLoader: AngularLoader<AppModule>;
5557
context?: DocumentRegistry.Context;
5658
}
5759
}
@@ -64,7 +66,8 @@ export class AngularWrapperWidget extends AngularWidget<
6466
scriptedFormsOptions: IScriptedForms.IOptions;
6567

6668
constructor(options: IAngularWrapperWidget.IOptions) {
67-
super(AppComponent, AppModule);
69+
70+
super(AppComponent, options.angularLoader);
6871

6972
this.scriptedFormsOptions = Object.assign(
7073
{
@@ -95,8 +98,8 @@ export class AngularWrapperWidget extends AngularWidget<
9598

9699
export class ScriptedFormsWidget extends Widget {
97100
_context: DocumentRegistry.Context;
98-
content: AngularWrapperWidget;
99-
toolbar: Toolbar
101+
private _content: AngularWrapperWidget;
102+
toolbar = new Toolbar();
100103
id: 'ScriptedForms';
101104

102105
constructor(options: IScriptedFormsWidget.IOptions) {
@@ -109,20 +112,24 @@ export class ScriptedFormsWidget extends Widget {
109112
this.addClass('scripted-form-widget');
110113

111114
const layout = (this.layout = new BoxLayout());
112-
const toolbar = new Toolbar();
113-
this.toolbar = toolbar;
114-
toolbar.addClass('jp-NotebookPanel-toolbar');
115-
toolbar.addClass('custom-toolbar');
116-
layout.addWidget(toolbar);
117-
BoxLayout.setStretch(toolbar, 0);
115+
this.toolbar.addClass('jp-NotebookPanel-toolbar');
116+
this.toolbar.addClass('custom-toolbar');
117+
layout.addWidget(this.toolbar);
118+
BoxLayout.setStretch(this.toolbar, 0);
119+
120+
const angularWrapperWidgetOptions = Object.assign({ toolbar: this.toolbar }, options);
121+
122+
this._content = new AngularWrapperWidget(angularWrapperWidgetOptions);
123+
this._content.addClass('form-container');
118124

119-
const angularWrapperWidgetOptions = Object.assign({ toolbar }, options);
125+
layout.addWidget(this._content);
126+
BoxLayout.setStretch(this._content, 1);
120127

121-
this.content = new AngularWrapperWidget(angularWrapperWidgetOptions);
122-
this.content.addClass('form-container');
128+
this._content.initiliseScriptedForms();
129+
}
123130

124-
layout.addWidget(this.content);
125-
BoxLayout.setStretch(this.content, 1);
131+
get content() {
132+
return this._content;
126133
}
127134

128135
get revealed() {
@@ -136,9 +143,4 @@ export class ScriptedFormsWidget extends Widget {
136143
onPathChanged(): void {
137144
this.title.label = PathExt.basename(this._context.path);
138145
}
139-
140-
dispose() {
141-
this.content.dispose();
142-
super.dispose();
143-
}
144146
}

scriptedforms/src/docs/landing-page/landing-page.component.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { Widget } from '@phosphor/widgets';
44

55
import { ServiceManager, ContentsManager } from '@jupyterlab/services';
66
import { ScriptedFormsWidget } from '../../app/widget';
7+
import { AngularLoader } from '../../app/phosphor-angular-loader';
8+
import { AppModule } from '../../app/app.module';
79

810
import * as htmlTemplate from 'html-loader!./landing-page.component.html';
911
const template = '' + htmlTemplate;
@@ -55,13 +57,15 @@ export class LandingPageComponent implements AfterViewInit {
5557
ngAfterViewInit() {
5658
const serviceManager = new ServiceManager();
5759
const contentsManager = new ContentsManager();
60+
const angularLoader = new AngularLoader<AppModule>(AppModule);
5861

5962
const formWidget = new ScriptedFormsWidget({
6063
serviceManager,
61-
contentsManager
64+
contentsManager,
65+
angularLoader
6266
});
6367

64-
formWidget.content.initiliseScriptedForms();
68+
// formWidget.content.initiliseScriptedForms();
6569

6670
window.onresize = () => { formWidget.update(); };
6771
Widget.attach(formWidget, this.formWrapper.nativeElement);

scriptedforms/src/jupyterlab-extension/jupyterlab-plugin.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import { ABCWidgetFactory, DocumentRegistry } from '@jupyterlab/docregistry';
77
import { ServiceManager, ContentsManager } from '@jupyterlab/services';
88
import { InstanceTracker } from '@jupyterlab/apputils';
99

10+
import { AngularLoader } from '../app/phosphor-angular-loader';
11+
12+
import { AppModule } from '../app/app.module';
1013
import { ScriptedFormsWidget } from './../app/widget';
1114

1215

@@ -41,30 +44,31 @@ namespace IScriptedFormsWidgetFactory {
4144
interface IOptions extends DocumentRegistry.IWidgetFactoryOptions {
4245
serviceManager: ServiceManager;
4346
contentsManager: ContentsManager;
47+
angularLoader: AngularLoader<AppModule>;
4448
}
4549
}
4650

4751
export
4852
class ScriptedFormsWidgetFactory extends ABCWidgetFactory<ScriptedFormsWidget, DocumentRegistry.IModel> {
4953
serviceManager: ServiceManager;
5054
contentsManager: ContentsManager;
55+
angularLoader: AngularLoader<AppModule>;
5156

5257
constructor(options: IScriptedFormsWidgetFactory.IOptions) {
5358
super(options);
5459
this.serviceManager = options.serviceManager;
5560
this.contentsManager = options.contentsManager;
61+
this.angularLoader = options.angularLoader;
5662
}
5763

5864
protected createNewWidget(context: DocumentRegistry.Context): ScriptedFormsWidget {
5965
const formWidget = new ScriptedFormsWidget({
6066
serviceManager: this.serviceManager,
6167
contentsManager: this.contentsManager,
68+
angularLoader: this.angularLoader,
6269
context: context
6370
});
64-
65-
formWidget.context.ready.then(() => {
66-
formWidget.content.initiliseScriptedForms();
67-
});
71+
// formWidget.content.initiliseScriptedForms();
6872

6973
return formWidget;
7074
}
@@ -73,6 +77,8 @@ class ScriptedFormsWidgetFactory extends ABCWidgetFactory<ScriptedFormsWidget, D
7377
function activate(
7478
app: JupyterLab, restorer: ILayoutRestorer, settingRegistry: ISettingRegistry
7579
) {
80+
const angularLoader = new AngularLoader<AppModule>(AppModule);
81+
7682
app.docRegistry.addFileType({
7783
name: 'scripted-form',
7884
mimeTypes: ['text/markdown'],
@@ -88,6 +94,7 @@ function activate(
8894
readOnly: true,
8995
serviceManager: app.serviceManager,
9096
contentsManager: app.serviceManager.contents,
97+
angularLoader
9198
});
9299

93100
app.docRegistry.addWidgetFactory(factory);

0 commit comments

Comments
 (0)