Skip to content

Commit

Permalink
feat: view tab activation history
Browse files Browse the repository at this point in the history
Closes #74
  • Loading branch information
mofogasy committed Dec 21, 2018
1 parent 33b88ac commit aab67b5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export class InternalWorkbenchRouter implements WorkbenchRouter {
case 'blank': {
const newViewRef = this._viewRegistry.computeNextViewOutletIdentity();
const viewPartRef = extras.blankViewPartRef || this._workbench.activeViewPartService.viewPartRef;
this._workbench.activeViewPartService.addActiveViewToHistory();
const grid = this._viewPartGridUrlObserver.snapshot.addView(viewPartRef, newViewRef).serialize();
return routeFn(newViewRef, grid);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class ViewPartGrid {
* Removes a view and activates the preceding view.
* In case the view was the last view of the viewpart, the viewpart is removed as well.
*/
public removeView(viewRef: string): this {
public removeView(viewRef: string, viewToActivateRef: string): this {
const viewPartRef = this.findContainingViewPartElseThrow(viewRef);
const viewPartInfoArray = this.getViewPartElseThrow(viewPartRef).viewPartInfoArray;

Expand All @@ -64,7 +64,13 @@ export class ViewPartGrid {
if (viewPartInfoArray[ACTIVE_VIEW_REF_INDEX] === viewRef) {
viewPartInfoArray[ACTIVE_VIEW_REF_INDEX] = null;

if (viewPartInfoArray.length > VIEW_REFS_START_INDEX) {
if (viewToActivateRef) {
const viewToActivateIndex = viewPartInfoArray.indexOf(viewToActivateRef, VIEW_REFS_START_INDEX);
if (viewToActivateIndex === -1) {
throw Error(`Illegal argument. View to activate not found in viewpart [viewPartRef=${viewPartRef}, viewRef=${viewToActivateRef}]`);
}
viewPartInfoArray[ACTIVE_VIEW_REF_INDEX] = viewPartInfoArray[viewToActivateIndex];
} else if (viewPartInfoArray.length > VIEW_REFS_START_INDEX) {
viewPartInfoArray[ACTIVE_VIEW_REF_INDEX] = viewPartInfoArray[viewIndex] || viewPartInfoArray[viewIndex - 1];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class WorkbenchViewPartService implements OnDestroy {
private _destroy$ = new Subject<void>();
private _hiddenViewTabs = new Set<string>();
private _hiddenViewTabs$ = new BehaviorSubject<string[]>([]);
private _history: string[] = [];

constructor(private _workbench: InternalWorkbenchService,
private _viewRegistry: WorkbenchViewRegistry,
Expand Down Expand Up @@ -106,6 +107,7 @@ export class WorkbenchViewPartService implements OnDestroy {
return Promise.resolve(true);
}

this.updateHistory(viewRef);
const serializedGrid = this._viewPartGridUrlObserver.snapshot
.activateView(this._viewPart.viewPartRef, viewRef)
.serialize();
Expand All @@ -120,9 +122,11 @@ export class WorkbenchViewPartService implements OnDestroy {
*/
public moveViewToThisViewPart(viewRef: string): Promise<boolean> {
const grid = this._viewPartGridUrlObserver.snapshot;
const viewToActivate = this._workbench.activeViewPartService.getViewToActivateElseNull(viewRef);
this.updateHistory(viewRef);

const serializedGrid = grid
.removeView(viewRef)
.removeView(viewRef, viewToActivate)
.addView(this._viewPart.viewPartRef, viewRef)
.serialize();

Expand All @@ -138,10 +142,11 @@ export class WorkbenchViewPartService implements OnDestroy {
public moveViewToNewViewPart(viewRef: string, region: Region): Promise<boolean> {
const grid = this._viewPartGridUrlObserver.snapshot;
const newViewPartRef = grid.computeNextViewPartIdentity();
const viewToActivate = this._workbench.activeViewPartService.getViewToActivateElseNull(viewRef);

const serializedGrid = grid
.addSiblingViewPart(region, this._viewPart.viewPartRef, newViewPartRef)
.removeView(viewRef)
.removeView(viewRef, viewToActivate)
.addView(newViewPartRef, viewRef)
.serialize();

Expand Down Expand Up @@ -174,6 +179,33 @@ export class WorkbenchViewPartService implements OnDestroy {
return this._hiddenViewTabs$.asObservable();
}

/**
* Returns the view to activate or null if the given view is not active
*/
public getViewToActivateElseNull(viewRef: string): string {
if (this.activeViewRef === viewRef) {
return this._history.pop();
}
this.removeFromHistory(viewRef);
return null;
}

public addActiveViewToHistory(): void {
this._history.push(this.activeViewRef);
}

private updateHistory(viewRef: string): void {
this.removeFromHistory(viewRef);
this.addActiveViewToHistory();
}

private removeFromHistory(viewRef: string): void {
const index = this._history.indexOf(viewRef);
if (index !== -1) {
this._history.splice(index, 1);
}
}

/**
* Navigate based on the provided array of commands with the view grid set as query parameter.
*
Expand Down
3 changes: 2 additions & 1 deletion projects/scion/workbench/src/lib/workbench.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ export class InternalWorkbenchService implements WorkbenchService {

public destroyView(...viewRefs: string[]): Promise<boolean> {
const destroyViewFn = (viewRef: string): Promise<boolean> => {
const viewToActivate = this.resolveViewPartServiceElseThrow(viewRef).getViewToActivateElseNull(viewRef);
const serializedGrid = this._viewPartGridUrlObserver.snapshot
.removeView(viewRef)
.removeView(viewRef, viewToActivate)
.serialize();
return this._router.navigate([{outlets: {[viewRef]: null}}], {
queryParams: {[VIEW_GRID_QUERY_PARAM]: serializedGrid},
Expand Down

0 comments on commit aab67b5

Please sign in to comment.