From 3b4b73c85e66657fd6fafee19ae400b621f6d5fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Pior?= Date: Tue, 6 Feb 2018 17:43:25 +0100 Subject: [PATCH] #6214 extract model --- js/notebook/src/tree/TreeWidget.ts | 172 +++++------------------------ 1 file changed, 29 insertions(+), 143 deletions(-) diff --git a/js/notebook/src/tree/TreeWidget.ts b/js/notebook/src/tree/TreeWidget.ts index f398bbeea3..8cc4cee873 100644 --- a/js/notebook/src/tree/TreeWidget.ts +++ b/js/notebook/src/tree/TreeWidget.ts @@ -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 { @@ -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); } @@ -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; -} \ No newline at end of file