add reversibility on category level
This commit is contained in:
@@ -11,6 +11,7 @@ import { Script } from '@/domain/Script';
|
||||
import { IApplication } from '@/domain/IApplication';
|
||||
import { IApplicationCode } from './Code/IApplicationCode';
|
||||
import applicationFile from 'js-yaml-loader!@/application/application.yaml';
|
||||
import { SelectedScript } from '@/application/State/Selection/SelectedScript';
|
||||
|
||||
/** Mutatable singleton application state that's the single source of truth throughout the application */
|
||||
export class ApplicationState implements IApplicationState {
|
||||
@@ -37,7 +38,7 @@ export class ApplicationState implements IApplicationState {
|
||||
public readonly app: IApplication,
|
||||
/** Initially selected scripts */
|
||||
public readonly defaultScripts: Script[]) {
|
||||
this.selection = new UserSelection(app, defaultScripts);
|
||||
this.selection = new UserSelection(app, defaultScripts.map((script) => new SelectedScript(script, false)));
|
||||
this.code = new ApplicationCode(this.selection, app.version);
|
||||
this.filter = new UserFilter(app);
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ export interface IUserSelection {
|
||||
readonly selectedScripts: ReadonlyArray<SelectedScript>;
|
||||
readonly totalSelected: number;
|
||||
removeAllInCategory(categoryId: number): void;
|
||||
addAllInCategory(categoryId: number): void;
|
||||
addOrUpdateAllInCategory(categoryId: number, revert: boolean): void;
|
||||
addSelectedScript(scriptId: string, revert: boolean): void;
|
||||
addOrUpdateSelectedScript(scriptId: string, revert: boolean): void;
|
||||
removeSelectedScript(scriptId: string): void;
|
||||
|
||||
@@ -12,13 +12,11 @@ export class UserSelection implements IUserSelection {
|
||||
|
||||
constructor(
|
||||
private readonly app: IApplication,
|
||||
/** Initially selected scripts */
|
||||
selectedScripts: ReadonlyArray<IScript>) {
|
||||
selectedScripts: ReadonlyArray<SelectedScript>) {
|
||||
this.scripts = new InMemoryRepository<string, SelectedScript>();
|
||||
if (selectedScripts && selectedScripts.length > 0) {
|
||||
for (const script of selectedScripts) {
|
||||
const selected = new SelectedScript(script, false);
|
||||
this.scripts.addItem(selected);
|
||||
this.scripts.addItem(script);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,16 +34,19 @@ export class UserSelection implements IUserSelection {
|
||||
this.changed.notify(this.scripts.getItems());
|
||||
}
|
||||
|
||||
public addAllInCategory(categoryId: number): void {
|
||||
public addOrUpdateAllInCategory(categoryId: number, revert: boolean = false): void {
|
||||
const category = this.app.findCategory(categoryId);
|
||||
const scriptsToAdd = category.getAllScriptsRecursively()
|
||||
.filter((script) => !this.scripts.exists(script.id));
|
||||
if (!scriptsToAdd.length) {
|
||||
const scriptsToAddOrUpdate = category.getAllScriptsRecursively()
|
||||
.filter((script) =>
|
||||
!this.scripts.exists(script.id)
|
||||
|| this.scripts.getById(script.id).revert !== revert,
|
||||
);
|
||||
if (!scriptsToAddOrUpdate.length) {
|
||||
return;
|
||||
}
|
||||
for (const script of scriptsToAdd) {
|
||||
const selectedScript = new SelectedScript(script, false);
|
||||
this.scripts.addItem(selectedScript);
|
||||
for (const script of scriptsToAddOrUpdate) {
|
||||
const selectedScript = new SelectedScript(script, revert);
|
||||
this.scripts.addOrUpdateItem(selectedScript);
|
||||
}
|
||||
this.changed.notify(this.scripts.getItems());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user