Skip to content

Commit

Permalink
feat(event-display): create an active observable variable
Browse files Browse the repository at this point in the history
  • Loading branch information
9inpachi committed Apr 10, 2021
1 parent c6ed1a2 commit 854e543
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 19 deletions.
3 changes: 2 additions & 1 deletion packages/phoenix-event-display/src/event-display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Configuration } from './extras/configuration';
import { StateManager } from './managers/state-manager';
import { LoadingManager } from './managers/loading-manager';
import { URLOptionsManager } from './managers/url-options-manager';
import { ActiveVariable } from './helpers/active-variable';

declare global {
/**
Expand Down Expand Up @@ -580,7 +581,7 @@ export class EventDisplay {
* Get the uuid of the currently selected object.
* @returns uuid of the currently selected object.
*/
public getActiveObjectId(): any {
public getActiveObjectId(): ActiveVariable<string> {
return this.graphicsLibrary.getActiveObjectId();
}

Expand Down
35 changes: 35 additions & 0 deletions packages/phoenix-event-display/src/helpers/active-variable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/** Callback function type. */
export type CallbackFunction<T = any> = (updatedValue: T) => void;

/**
* An active variable whose value can be changed and the change can be observed.
*/
export class ActiveVariable<T = any> {
/**
* Create the observable active variable.
* @param value Initial value.
*/
constructor(public value?: T) {}

/**
* Callbacks to call on update.
*/
private callbacks: CallbackFunction<T>[] = [];

/**
* Update the value of variable.
* @param updatedValue New updated value.
*/
public update(updatedValue: T) {
this.value = updatedValue;
this.callbacks.forEach((callback) => callback(updatedValue));
}

/**
* Call a function on updating the value of variable.
* @param callback Callback to call with updated value when the variable is updated.
*/
public onUpdate(callback: CallbackFunction<T>) {
this.callbacks.push(callback);
}
}
1 change: 1 addition & 0 deletions packages/phoenix-event-display/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export * from './extras/preset-view.model';
export * from './helpers/rk-helper';
export * from './helpers/runge-kutta';
export * from './helpers/pretty-symbols';
export * from './helpers/active-variable';

// Loaders
export * from './loaders/event-data-loader';
Expand Down
3 changes: 2 additions & 1 deletion packages/phoenix-event-display/src/three/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { EffectsManager } from './effects-manager';
import { VRManager } from './vr-manager';
import { StateManager } from '../managers/state-manager';
import { LoadingManager } from '../managers/loading-manager';
import { ActiveVariable } from '../helpers/active-variable';

/**
* Manager for all three.js related functions.
Expand Down Expand Up @@ -544,7 +545,7 @@ export class ThreeManager {
* Get the uuid of the currently selected object.
* @returns uuid of the currently selected object.
*/
public getActiveObjectId(): any {
public getActiveObjectId(): ActiveVariable<string> {
return this.getSelectionManager().getActiveObjectId();
}

Expand Down
17 changes: 3 additions & 14 deletions packages/phoenix-event-display/src/three/selection-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { OutlinePass } from 'three/examples/jsm/postprocessing/OutlinePass.js';
import { InfoLogger } from '../info-logger';
import { EffectsManager } from './effects-manager';
import { PrettySymbols } from '../helpers/pretty-symbols';
import { ActiveVariable } from '../helpers/active-variable';

/**
* Manager for managing event display's selection related functions.
Expand All @@ -26,19 +27,7 @@ export class SelectionManager {
/** Object used to display the information of the selected 3D object. */
private selectedObject: { name: string; attributes: any[] };
/** The currently selected object which is observable for changes. */
private activeObject = {
uuid: '',
callbacks: [],
update: function (uuid: string) {
this.uuid = uuid;
for (const callback of this.callbacks) {
callback(uuid);
}
},
onUpdate: function (callback: (uuid: string) => void) {
this.callbacks.push(callback);
},
};
private activeObject = new ActiveVariable<string>('');
/** Objects to be ignored on hovering over the scene. */
private ignoreList: string[];

Expand Down Expand Up @@ -102,7 +91,7 @@ export class SelectionManager {
* Get the uuid of the currently selected object.
* @returns uuid of the currently selected object.
*/
public getActiveObjectId(): any {
public getActiveObjectId(): ActiveVariable<string> {
return this.activeObject;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
*ngFor="let object of showingCollection; index as i"
[attr.id]="object.uuid"
[ngClass]="{
'active-object': activeObject && activeObject.uuid === object.uuid
'active-object': activeObject && activeObject.value === object.uuid
}"
>
<td>#{{ i }}</td>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, OnInit, Input, ElementRef } from '@angular/core';
import { PrettySymbols } from 'phoenix-event-display';
import { PrettySymbols, ActiveVariable } from 'phoenix-event-display';
import { EventDisplayService } from '../../../../services/event-display.service';

@Component({
Expand All @@ -13,7 +13,7 @@ export class CollectionsInfoOverlayComponent implements OnInit {
selectedCollection: string;
showingCollection: any;
collectionColumns: string[];
activeObject: any;
activeObject: ActiveVariable<string>;

constructor(
private elementRef: ElementRef,
Expand Down

0 comments on commit 854e543

Please sign in to comment.