Skip to content

Commit

Permalink
Merge pull request #551 from karolina-siemieniuk-morawska/revert-544-…
Browse files Browse the repository at this point in the history
…incorrect_conflicts_handling

Revert "fix index issue in patches modal  when removing patches"
  • Loading branch information
karolina-siemieniuk-morawska authored Jan 25, 2024
2 parents 4b41c38 + be9c549 commit 88cf65c
Show file tree
Hide file tree
Showing 14 changed files with 23 additions and 63 deletions.
2 changes: 1 addition & 1 deletion example/assets/mock-data/patches.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,4 @@
"op": "remove",
"path": "/references/0/reference/label"
}
]
]
15 changes: 1 addition & 14 deletions src/abstract-list-field/abstract-list-field.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export abstract class AbstractListFieldComponent extends AbstractFieldComponent
values: List<any>;
schema: JSONSchema;
path: Array<any>;
supportValues: List<any>;

constructor(public appGlobalsService: AppGlobalsService,
public problemsService: ProblemsService,
Expand All @@ -58,23 +57,11 @@ export abstract class AbstractListFieldComponent extends AbstractFieldComponent
/**
* @param index - Index of the element to be deleted
*/
deleteElement(index: number, value: any) {
deleteElement(index: number) {
const elementPath = this.path.concat(index);
const patchIndex = this.supportValues.indexOf(value);

this.jsonStoreService.removeIn(elementPath);
// empty list as notSetValue, because there can be lists rendered on the UI, while they are absent in json (alwaysShow)
this.values = this.jsonStoreService.getIn(this.path, List());

// remove patches for this element
if (this.jsonStoreService.hasPatch(this.pathString)) {
this.jsonStoreService.deletePatchForElement(this.pathString);
}

// if patch is in child path that will be removed
if (this.jsonStoreService.hasPatchOrChildrenHavePatch(this.getPathStringForChild(patchIndex))) {
this.jsonStoreService.deletePatchForElement(this.getPathStringForChild(patchIndex));
}
}

getPathStringForChild(index: number): string {
Expand Down
6 changes: 3 additions & 3 deletions src/any-type-field/any-type-field.component.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<div [ngSwitch]="schema.componentType">
<div *ngSwitchCase="'table-list'">
<table-list-field [values]="value" [supportValues]="value" [schema]="schema" [path]="path"></table-list-field>
<table-list-field [values]="value" [schema]="schema" [path]="path"></table-list-field>
</div>
<div *ngSwitchCase="'complex-list'">
<complex-list-field [values]="value" [supportValues]="value" [schema]="schema" [path]="path"></complex-list-field>
<complex-list-field [values]="value" [schema]="schema" [path]="path"></complex-list-field>
</div>
<div *ngSwitchCase="'primitive-list'">
<primitive-list-field [values]="value" [supportValues]="value" [schema]="schema" [path]="path"></primitive-list-field>
<primitive-list-field [values]="value" [schema]="schema" [path]="path"></primitive-list-field>
</div>
<div *ngSwitchCase="'object'">
<object-field [value]="value" [schema]="schema" [path]="path"></object-field>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
} from '@angular/core';

import { AbstractSubscriberComponent } from '../../abstract-subscriber';
import { DomUtilService, JsonStoreService, AppGlobalsService } from '../../shared/services';
import { DomUtilService, PathUtilService, JsonStoreService, AppGlobalsService } from '../../shared/services';
import { JsonPatch, JsonPatchesByPath } from '../../shared/interfaces';

@Component({
Expand All @@ -47,6 +47,7 @@ export class PatchesConsoleTabComponent extends AbstractSubscriberComponent impl

constructor(private domUtilService: DomUtilService,
private appGlobalsService: AppGlobalsService,
private pathUtilService: PathUtilService,
private jsonStoreService: JsonStoreService,
private changeDetectorRef: ChangeDetectorRef) {
super();
Expand All @@ -56,9 +57,7 @@ export class PatchesConsoleTabComponent extends AbstractSubscriberComponent impl
this.jsonStoreService.patchesByPath$
.takeUntil(this.isDestroyed)
.subscribe(patchesByPath => {
// filter paths with empty patch arrays to prevent from rendering empty <li> elements
const filtered = Object.entries(patchesByPath).filter(([key, value]) => value.length > 0);
this.patchesByPath = filtered.reduce((o, key) => Object.assign(o, {[key[0]]: key[1]}), {});
this.patchesByPath = patchesByPath;
this.changeDetectorRef.markForCheck();
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/complex-list-field/complex-list-field.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
</object-field>
<div class="row element-button-container">
<div class="col-md-12 text-right">
<list-action-group (onMove)="moveElement(item.index, $event)" (onDelete)="deleteElement(item.index, values.get(item.index))" [canMove]="sortable"
<list-action-group (onMove)="moveElement(item.index, $event)" (onDelete)="deleteElement(item.index)" [canMove]="sortable"
[isDisabled]="disabled || hasPatchOrChildrenHavePatch()">
</list-action-group>
</div>
Expand Down
1 change: 0 additions & 1 deletion src/complex-list-field/complex-list-field.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ import { LongListNavigatorConfig, JSONSchema, PaginatedItem } from '../shared/in
export class ComplexListFieldComponent extends AbstractListFieldComponent implements OnChanges, OnInit {

@Input() values: List<Map<string, any>>;
@Input() supportValues: List<Map<string, any>>;
@Input() schema: JSONSchema;
@Input() path: Array<any>;

Expand Down
4 changes: 2 additions & 2 deletions src/primitive-list-field/primitive-list-field.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<any-type-field [value]="value" [schema]="schema.items" [path]="getPathForChild(i)"></any-type-field>
</td>
<td *ngIf="values.size > 0" class="button-holder" [class.sortable]="schema.sortable">
<list-action-group (onMove)="moveElement(i, $event)" (onDelete)="deleteElement(i, value)" [canMove]="schema.sortable" [isDisabled]="disabled"></list-action-group>
<list-action-group (onMove)="moveElement(i, $event)" (onDelete)="deleteElement(i)" [canMove]="schema.sortable" [isDisabled]="disabled"></list-action-group>
</td>
</tr>
<tr *ngIf="removeJsonPatch">
Expand All @@ -22,4 +22,4 @@
</tr>
</table>
</div>
</div>
</div>
1 change: 0 additions & 1 deletion src/primitive-list-field/primitive-list-field.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import { JSONSchema } from '../shared/interfaces';
})
export class PrimitiveListFieldComponent extends AbstractListFieldComponent {
@Input() values: List<any>;
@Input() supportValues: List<any>;
@Input() schema: JSONSchema;
@Input() path: Array<any>;

Expand Down
1 change: 1 addition & 0 deletions src/shared/pipes/self-or-empty.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { JSONSchema } from '../interfaces';
@Pipe({
name: 'selfOrEmpty',
})

export class SelfOrEmptyPipe implements PipeTransform {

constructor(public emptyValueService: EmptyValueService) {}
Expand Down
34 changes: 6 additions & 28 deletions src/shared/services/json-store.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ export class JsonStoreService {
readonly json$ = new Subject<Map<string, any>>();
readonly jsonPatches$ = new Subject<Array<JsonPatch>>();

private patchesByPath: JsonPatchesByPath = {};
private json: Map<string, any>;
private jsonPatches: Array<JsonPatch>;
patchesByPath: JsonPatchesByPath = {};

// list of reverse patches for changes
private history = new SizedStack<JsonPatch>(10);
Expand Down Expand Up @@ -211,10 +211,6 @@ export class JsonStoreService {
return true;
}

if (Object.keys(this.patchesByPath).some(patch => patch.includes(path))) {
return true;
}

if (this.jsonPatches) {
const childPathPrefix = `${path}${this.pathUtilService.separator}`;
return this.jsonPatches
Expand All @@ -223,23 +219,17 @@ export class JsonStoreService {
return false;
}

removeJsonPatch(patch: JsonPatch) {
private removeJsonPatch(patch: JsonPatch) {
const path = this.getComponentPathForPatch(patch);
// don't do anything if it's UNDO json-patch.
if (this.patchesByPath[path]) {
const patchIndex = this.patchesByPath[path].indexOf(patch);
if (patchIndex > -1) {
// if there is more than one patch in the same path remove all of them when 'remove'
if (patch.op === 'remove' && this.patchesByPath[path].length > 1) {
this.patchesByPath[path] = [];
this.jsonPatches = this.jsonPatches.filter(p => p.path !== path);
} else {
this.patchesByPath[path].splice(patchIndex, 1);
const globalPatchIndex = this.jsonPatches.indexOf(patch);
this.jsonPatches.splice(globalPatchIndex, 1);
}

this.patchesByPath[path].splice(patchIndex, 1);
this.patchesByPath$.next(this.patchesByPath);

const globalPatchIndex = this.jsonPatches.indexOf(patch);
this.jsonPatches.splice(globalPatchIndex, 1);
this.jsonPatches$.next(this.jsonPatches);
}
}
Expand All @@ -258,16 +248,4 @@ export class JsonStoreService {
const parentPathArray = pathArray.slice(0, -1);
return this.pathUtilService.toPathString(parentPathArray);
}

getPatchesIncludingPath(path: string): JsonPatch[] {
const pathsWithPatches = Object.keys(this.patchesByPath).filter(patch => patch.includes(path));
const patches = pathsWithPatches.map(p => this.patchesByPath[p]);
return [].concat(...patches);
}

deletePatchForElement(path: any) {
if (this.getPatchesIncludingPath(path)) {
this.getPatchesIncludingPath(path).forEach(patch => this.removeJsonPatch(patch));
}
}
}
2 changes: 1 addition & 1 deletion src/table-list-field/table-list-field.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<tr *ngFor="let value of values; let i = index; trackBy:trackByIndex" table-item-field [id]="getPathStringForChild(i)" [value]="value"
[schema]="schema.items" [path]="getPathForChild(i)" [keys]="keys$ | async">
<td *ngIf="values.size > 0" class="button-holder" [class.sortable]="schema.sortable">
<list-action-group (onMove)="moveElement(i, $event)" (onDelete)="deleteElement(i, value)" [canMove]="schema.sortable" [isDisabled]="disabled"></list-action-group>
<list-action-group (onMove)="moveElement(i, $event)" (onDelete)="deleteElement(i)" [canMove]="schema.sortable" [isDisabled]="disabled"></list-action-group>
</td>
</tr>
<tr *ngIf="removeJsonPatch">
Expand Down
1 change: 0 additions & 1 deletion src/table-list-field/table-list-field.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import { JSONSchema } from '../shared/interfaces';
export class TableListFieldComponent extends AbstractListFieldComponent {

@Input() values: List<Map<string, any>>;
@Input() supportValues: List<Map<string, any>>;
@Input() schema: JSONSchema;
@Input() path: Array<any>;

Expand Down
5 changes: 2 additions & 3 deletions src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
"emitDecoratorMetadata": true,
"lib": [
"es6",
"dom",
"es2017.object"
"dom"
],
"rootDir": "../",
"module": "es6",
Expand All @@ -26,4 +25,4 @@
"../node_modules/@types"
]
}
}
}
5 changes: 2 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"emitDecoratorMetadata": true,
"lib": [
"es6",
"dom",
"es2017.object"
"dom"
],
"paths": {
"@angular/core": [
Expand Down Expand Up @@ -54,4 +53,4 @@
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true
}
}
}

0 comments on commit 88cf65c

Please sign in to comment.