Skip to content

Commit

Permalink
refactor(edition): reorganize a few things in the edition related stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
cbourget committed May 10, 2019
1 parent 403e748 commit 532b3e6
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 49 deletions.
3 changes: 2 additions & 1 deletion packages/common/src/lib/action/shared/action.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ export interface Action {
title?: string;
icon?: string;
tooltip?: string;
conditions?: Array<() => boolean>;
conditions?: Array<(...args: any[]) => boolean>;
args?: any[];
conditionArgs?: any[];
}

export type ActionHandler = (...args: any[]) => void;
5 changes: 4 additions & 1 deletion packages/common/src/lib/action/shared/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export class ActionStore extends EntityStore<Action> {

this.entities$.value.forEach((action: Action) => {
const conditions = action.conditions || [];
const available = conditions.every((condition: () => boolean) => condition());
const args = action.conditionArgs || [];
const available = conditions.every((condition: (...args: any[]) => boolean) => {
return condition(...args);
});
available ? availables.push(action) : unavailables.push(action);
});

Expand Down
14 changes: 7 additions & 7 deletions packages/common/src/lib/edition/shared/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import { EditorOptions } from './edition.interfaces';
* entities and the actions that consume them. It also defines an
* entity table template that may be used by an entity table component.
*/
export class Editor {
export class Editor<E extends object = object> {

/**
* Observable of the selected entity
*/
public entity$ = new BehaviorSubject<object>(undefined);
public entity$ = new BehaviorSubject<E>(undefined);

/**
* Observable of the selected widget
Expand Down Expand Up @@ -72,7 +72,7 @@ export class Editor {
/**
* Entities store
*/
get entityStore(): EntityStore<object> { return this.options.entityStore; }
get entityStore(): EntityStore<E> { return this.options.entityStore as EntityStore<E>; }

/**
* Actions store (some actions activate a widget)
Expand All @@ -82,7 +82,7 @@ export class Editor {
/**
* Selected entity
*/
get entity(): object { return this.entity$.value; }
get entity(): E { return this.entity$.value; }

/**
* Selected widget
Expand Down Expand Up @@ -114,8 +114,8 @@ export class Editor {

if (this.entityStore !== undefined) {
this.entities$$ = this.entityStore.stateView
.manyBy$((record: EntityRecord<object>) => record.state.selected === true)
.subscribe((records: EntityRecord<object>[]) => {
.manyBy$((record: EntityRecord<E>) => record.state.selected === true)
.subscribe((records: EntityRecord<E>[]) => {
// If more than one entity is selected, consider that no entity at all is selected.
const entity = (records.length === 0 || records.length > 1) ? undefined : records[0].entity;
this.onSelectEntity(entity);
Expand Down Expand Up @@ -176,7 +176,7 @@ export class Editor {
* entity and update the actions availability.
* @param entity Entity
*/
private onSelectEntity(entity: object) {
private onSelectEntity(entity: E) {
if (entity === this.entity$.value) {
return;
}
Expand Down
32 changes: 28 additions & 4 deletions packages/common/src/lib/edition/shared/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,43 @@ import { Editor } from './editor';
*/
export class EditorStore extends EntityStore<Editor> {

/**
* Register an editor and make it available
* @param editor Editor
*/
register(editor: Editor) {
this.insert(editor);
}

/**
* Unregister an editor and make it unavailable
* @param editor Editor
*/
unregister(editor: Editor) {
this.delete(editor);
}

/**
* Activate the an editor editor and deactivate the one currently active
* @param editor Editor
*/
activateEditor(editor: Editor) {
const active = this.view.firstBy((_editor: Editor) => _editor.isActive() === true);
if (active !== undefined) {
active.deactivate();
}
this.deactivateEditor();
if (editor !== undefined) {
this.state.update(editor, {active: true, selected: true}, true);
editor.activate();
}
}

/**
* Deactivate the current editor
* @param editor Editor
*/
deactivateEditor() {
const active = this.view.firstBy((_editor: Editor) => _editor.isActive() === true);
if (active !== undefined) {
active.deactivate();
}
}

}
4 changes: 2 additions & 2 deletions packages/common/src/lib/entity/shared/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ export class EntityStore<E extends object, S extends EntityState = EntityState>
* Set this store's entities
* @param entities Entities
*/
load(entities: E[]) {
load(entities: E[], pristine: boolean = true) {
this._index = this.generateIndex(entities);
this._pristine = true;
this._pristine = pristine;
this.next();
}

Expand Down
37 changes: 3 additions & 34 deletions packages/integration/src/lib/edition/edition.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { EntityRecord, Editor, EditorStore } from '@igo2/common';
providedIn: 'root'
})
export class EditionState {

/**
* Observable of the active editor
*/
Expand All @@ -19,49 +20,17 @@ export class EditionState {
/**
* Store that holds all the available editors
*/
get store(): EditorStore {
return this._store;
}
get store(): EditorStore { return this._store; }
private _store: EditorStore;

constructor() {
this._store = new EditorStore([]);
this._store.stateView
.firstBy$(
(record: EntityRecord<Editor>) => record.state.selected === true
)
.firstBy$((record: EntityRecord<Editor>) => record.state.active === true)
.subscribe((record: EntityRecord<Editor>) => {
const editor = record ? record.entity : undefined;
this.editor$.next(editor);
});
}

/**
* ToolComponent an editor and make it available
* @param editor Editor
*/
register(editor: Editor) {
this.store.insert(editor);
}

/**
* Unregister an editor and make it unavailable
* @param editor Editor
*/
unregister(editor: Editor) {
this.store.delete(editor);
}

/**
* Set the active editor
* @param editor Editor
*/
setEditor(editor: Editor | undefined) {
if (editor === undefined) {
this.store.state.updateAll({ selected: false });
} else {
const entity = this.store.get(editor.id);
this.store.state.update(entity, { selected: true }, true);
}
}
}

0 comments on commit 532b3e6

Please sign in to comment.