Skip to content

Commit

Permalink
display error message, dirty indicator and title icon
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmetanakol authored and edgarmueller committed Aug 29, 2018
1 parent eaf4478 commit a16ef93
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 13 deletions.
34 changes: 34 additions & 0 deletions src/browser/DirtyResourceSavable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { ResourceSaveable } from './ResourceSaveable';
import { unmanaged } from 'inversify';
import { WidgetManager} from "@theia/core/lib/browser";
import { MessageService, Resource } from '@theia/core/lib/common';
import { DIRTY_CLASS, TreeEditorWidget } from './theia-tree-editor-widget';
import * as _ from 'lodash';

export class DirtyResourceSavable extends ResourceSaveable {
private _widgetManager: WidgetManager;
private _messageService: MessageService;

constructor(
resource: Resource,
getData: () => any,
@unmanaged() widgetManager: WidgetManager,
@unmanaged() messageService: MessageService
) {
super(resource, getData);

this._widgetManager = widgetManager;
this._messageService = messageService;
}

onSaveSuccess = () => {
const widget = _.head(this._widgetManager.getWidgets('theia-tree-editor')) as TreeEditorWidget;
const dirtyClass = ` ${DIRTY_CLASS}`;
widget.title.className = widget.title.className.replace(dirtyClass, '');

}

onSaveFailure = () => {
this._messageService.error('Cannot Save: Undefined Resource');
}
}
28 changes: 15 additions & 13 deletions src/browser/ResourceSaveable.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Disposable, Event, MaybePromise, Resource } from '@theia/core/lib/common';
import {Saveable, WidgetManager} from "@theia/core/lib/browser";
import { DIRTY_CLASS, TreeEditorWidget } from './theia-tree-editor-widget';
import * as _ from 'lodash';
import { unmanaged } from 'inversify';
import {
Disposable,
Event,
MaybePromise,
Resource
} from '@theia/core/lib/common';
import { Saveable } from "@theia/core/lib/browser";

export class ResourceSaveable implements Saveable {
autoSave;
Expand All @@ -20,30 +22,30 @@ export class ResourceSaveable implements Saveable {
);

constructor(private resource: Resource,
private getData: () => any,
@unmanaged() protected readonly widgetManager: WidgetManager) {}
private getData: () => any) {}

save(): MaybePromise<void> {
return this.onSave(this.getData()).then(this.doSave)
}

doSave = (content: string): MaybePromise<void> => {
if ( this.resource.saveContents !== undefined ) {
if ( this.resource !== undefined && this.resource.saveContents !== undefined ) {
return this.resource.saveContents(content, { encoding: 'UTF-8' })
.then(() => {
this.dirty = false;
const widget = _.head(this.widgetManager.getWidgets('theia-tree-editor')) as TreeEditorWidget;
const dirtyClass = ` ${DIRTY_CLASS}`;
widget.title.className = widget.title.className.replace(dirtyClass, '');
this.onSaveSuccess();
});
} else {
console.warn('resource cannot save');
return undefined;
this.onSaveFailure();
}
}

onSave(data: any): Promise<string> {
const content = JSON.stringify(data, null, 2);
return Promise.resolve(content);
}

onSaveSuccess = () => {}

onSaveFailure = () => {}
}
1 change: 1 addition & 0 deletions src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './theia-tree-editor-widget';
export * from './tree-editor-utils';
export * from './theia-tree-editor-contribution';
export * from './ResourceSaveable';
export * from './DirtyResourceSavable';
3 changes: 3 additions & 0 deletions src/browser/theia-tree-editor-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const defaultResourceParser = (content: string): Promise<any> => {

let widgetCounter = 0;
export const DIRTY_CLASS = 'theia-mod-dirty';
const JSON_ICON_CLASS = 'database-icon medium-yellow file-icon';
@injectable()
export class TreeEditorWidget extends BaseWidget implements SaveableSource {
saveable: Saveable;
Expand All @@ -48,6 +49,8 @@ export class TreeEditorWidget extends BaseWidget implements SaveableSource {
this.title.closable = true;
this.title.label = this.options.fileName;
this.title.caption = this.title.label;
this.title.iconClass =
this.title.iconClass.replace('no-icon', '') + `${JSON_ICON_CLASS}`;
this.resource.readContents()
.then(content => {
if(this.options.onResourceLoad === undefined) {
Expand Down

0 comments on commit a16ef93

Please sign in to comment.