Skip to content

Commit

Permalink
#6214 extract model
Browse files Browse the repository at this point in the history
  • Loading branch information
piorek committed Feb 6, 2018
1 parent 96d92bb commit 3b4b73c
Showing 1 changed file with 29 additions and 143 deletions.
172 changes: 29 additions & 143 deletions js/notebook/src/tree/TreeWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@
* limitations under the License.
*/

import { Panel, Widget } from "@phosphor/widgets";
import BannerWidget from "./BannerWidget";
import JVMOptionsWidget, {JVMOptionsModel} from "./JVMOptionsWidget";
import * as $ from "jquery";
import {Message} from "@phosphor/messaging";
import BeakerXApi, {IApiSettingsResponse, IJVMOptions, IUIOptions} from "./BeakerXApi";
import {UIOptionsModel, UIOptionsWidget} from "./JVMOptions/UIOptionsWidget";
import {SyncIndicatorWidget} from "./JVMOptions/SyncIndicatorWidget";
import {Messages} from "./JVMOptions/Messages";
import {DefaultOptionsModel} from "./JVMOptions/DefaultOptionsWidget";

import { Panel, Widget } from "@phosphor/widgets";
import { Message } from "@phosphor/messaging";

import BeakerXApi from "./Utils/BeakerXApi";
import BannerWidget from "./Widgets/BannerWidget";
import { Messages } from "./Messages";
import ITreeWidgetOptions from "./Types/ITreeWidgetOptions";
import TreeWidgetModel from "./Models/TreeWidgetModel";
import SyncIndicatorWidget from "./Widgets/SyncIndicatorWidget";
import JVMOptionsWidget from "./Widgets/JVMOptionsWidget";
import {UIOptionsWidget} from "./Widgets/UIOptions/UIOptionsWidget";
import UIOptionsModel from "./Models/UIOptionsModel";

export default class TreeWidget extends Panel {

Expand Down Expand Up @@ -56,16 +60,29 @@ export default class TreeWidget extends Panel {
private createWidgetContent(api: BeakerXApi) {

let syncIndicatorWidget = new SyncIndicatorWidget();
this._model = new TreeWidgetModel(api, syncIndicatorWidget);
let jvmOptionsWidget = new JVMOptionsWidget();
let uiOptionsWidget;
let uiOptionsModel;
if (false === this.options.isLab) {
uiOptionsWidget = new UIOptionsWidget();
uiOptionsModel = new UIOptionsModel(uiOptionsWidget);
}

this._model = new TreeWidgetModel(
api,
jvmOptionsWidget.model,
uiOptionsModel,
syncIndicatorWidget
);

this.addWidget(new BannerWidget(api));
this.addWidget(new Widget({ node: document.createElement('br') }));

if (false === this.options.isLab) {
this.addWidget(new UIOptionsWidget());
this.addWidget(uiOptionsWidget);
}

this.addWidget(new JVMOptionsWidget());
this.addWidget(jvmOptionsWidget);
this.addWidget(syncIndicatorWidget);
}

Expand Down Expand Up @@ -101,134 +118,3 @@ export default class TreeWidget extends Panel {
.insertBefore(this.node);
}
}

export class TreeWidgetModel {

private _options: IApiSettingsResponse;

private jvmOptionsModel: JVMOptionsModel;
private uiOptionsModel: UIOptionsModel;

constructor(
private api: BeakerXApi,
private syncWidget: SyncIndicatorWidget
) {
}

public load() {
this.syncStart();
this.api
.loadSettings()
.then((data: IApiSettingsResponse) => {
if (!data.ui_options) {
data.ui_options = {
auto_close: false,
improve_fonts: true,
wide_cells: true,
show_publication: false,
}
}

this._options = data;

this.jvmOptionsModel
.update(data.jvm_options);
this.uiOptionsModel
.update(data.ui_options);

this.showResult(data.jvm_options);

setTimeout(() => {
this.syncEnd()
},1000);
});

}

public save() {
this.syncStart();

let payload: IApiSettingsResponse = {
"jvm_options": {
"heap_GB": null,
"other": [],
"properties": []
},
"ui_options": {
"auto_close": false,
"improve_fonts": true,
"wide_cells": true,
"show_publication": false,
},
"version": 2
};

payload.jvm_options.heap_GB = this._options.jvm_options.heap_GB;
payload.jvm_options.other = this._options.jvm_options.other;
payload.jvm_options.properties = this._options.jvm_options.properties;

payload.ui_options.auto_close = this._options.ui_options.auto_close;
payload.ui_options.improve_fonts = this._options.ui_options.improve_fonts;
payload.ui_options.wide_cells = this._options.ui_options.wide_cells;
payload.ui_options.show_publication = this._options.ui_options.show_publication;

this.showResult(payload.jvm_options);

this.api.saveSettings({ beakerx: payload })
.then(() => {
setTimeout(() => {
this.syncEnd()
},1000);
});
}

public clearErrors() {
this.syncWidget.clearErrors();
}

public showError(error: Error) {
this.syncWidget.onError(error);
}

private syncStart(): void {
this.syncWidget.onSyncStart();
}

private syncEnd(): void {
this.syncWidget.onSyncEnd();
}

private showResult(options: IJVMOptions) {
this.syncWidget.showResult(this.buildResult(options));
}

private buildResult(options: IJVMOptions): string {
let result: string = '';
if (options.heap_GB !== null) {
result += `-Xmx${ DefaultOptionsModel.normaliseHeapSize(options.heap_GB) } `;
}

for (let other of options.other) {
result += `${other} `
}

for (let property of options.properties) {
result += `-D${property.name}=${property.value} `;
}
return result;
};

setUIOptions(options: IUIOptions) {
this._options.ui_options = options;
}

setJVMOptions(options: IJVMOptions) {
this._options.jvm_options = options;
}
}

export interface ITreeWidgetOptions {
isLab: boolean;
baseUrl: string;
CodeCell: any;
}

0 comments on commit 3b4b73c

Please sign in to comment.