Skip to content

Commit

Permalink
complex list addition of adding an element in a given position
Browse files Browse the repository at this point in the history
  • Loading branch information
drjova committed Jan 7, 2022
1 parent d1f08df commit e53daeb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/complex-list-field/complex-list-field.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,16 @@
</div>
<div [ngClass]="redLeftBorderClass">
<!-- Elements -->
<div *ngFor="let item of paginatedItems; trackBy:trackByElement">
<div *ngFor="let item of paginatedItems; index as i; trackBy:trackByElement">

<div class="complex-list-field-wrapper">

<div class="add-new-element">
<a href="javascript:void(0)" (click)="onAddNewElementAtPosition(i)">
Add new element here
</a>
</div>

<span *ngIf="shouldDisplayViewTemplate && values.get(item.index).keySeq().size != 0">
<ng-template [ngTemplateOutlet]="customTemplate" [ngTemplateOutletContext]="{item: values.get(item.index)}"></ng-template>
<a href="javascript:void(0)" (click)="item.editFormDisplayedByUser = !shouldDisplayEditableFieldsForItem(item)">
Expand Down
8 changes: 6 additions & 2 deletions src/complex-list-field/complex-list-field.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
align-items: center;
justify-content: space-between;
padding: 0 15px;
background-color: white;
background-color: white;
box-shadow: 0 0.5px 0 0 #ffffff inset, 0 1px 2px 0 #B3B3B3;
}

Expand All @@ -32,7 +32,7 @@

.find-button {
color: darkslategray;

.fa-search {
opacity: 0.83;
}
Expand All @@ -49,3 +49,7 @@
label.btn {
color: white !important;
}

.add-new-element {
text-align: right;
}
27 changes: 26 additions & 1 deletion src/complex-list-field/complex-list-field.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,20 @@ import {
import { List, Map, Set, Iterable } from 'immutable';

import { AbstractListFieldComponent } from '../abstract-list-field';

import {
AppGlobalsService,
JsonStoreService,
DomUtilService,
PathUtilService,
ListPageChangerService,
ProblemsService
ProblemsService,
EmptyValueService,
} from '../shared/services';
import { LongListNavigatorConfig, JSONSchema, PaginatedItem } from '../shared/interfaces';



@Component({
selector: 'complex-list-field',
styleUrls: [
Expand Down Expand Up @@ -74,6 +78,7 @@ export class ComplexListFieldComponent extends AbstractListFieldComponent implem
public domUtilService: DomUtilService,
public pathUtilService: PathUtilService,
public changeDetectorRef: ChangeDetectorRef,
public emptyValueService: EmptyValueService,
public listPageChangerService: ListPageChangerService) {
super(appGlobalsService, problemsService, jsonStoreService, pathUtilService, changeDetectorRef);
}
Expand Down Expand Up @@ -156,6 +161,26 @@ export class ComplexListFieldComponent extends AbstractListFieldComponent implem
}
}

onAddNewElementAtPosition(index: number) {
const itemSchema = this.schema.items;
const emptyValue = this.emptyValueService.generateEmptyValue(itemSchema);
const values: List<any> = this.jsonStoreService.getIn(this.path);

let insertIndex;
if (this.navigator) {
insertIndex = index + (this.navigator.itemsPerPage * (this.currentPage - 1));
} else {
insertIndex = index;
}
const insertPath = this.path.concat(insertIndex);
this.jsonStoreService.addIn(insertPath, emptyValue);
// focus on the new added element
const insertPathString = this.pathUtilService.toPathString(insertPath);
setTimeout(() => {
this.domUtilService.focusAndSelectFirstEditableChildById(insertPathString);
});
}

onFindInputKeypress(key: string) {
if (key === 'Enter') {
this.onFindClick();
Expand Down

0 comments on commit e53daeb

Please sign in to comment.