|
1 |
| -import { Component, Input } from '@angular/core'; |
| 1 | +import { Component, Input, OnInit } from '@angular/core'; |
| 2 | +import * as _ from 'lodash'; |
| 3 | +import { JsonSchemaFormService } from '../../json-schema-form.service'; |
2 | 4 |
|
3 | 5 | @Component({
|
4 | 6 | selector: 'no-framework',
|
5 | 7 | template: `
|
| 8 | + <button style='float:right;clear: right;' *ngIf="showRemoveButton" |
| 9 | + type="button" |
| 10 | + (click)="removeItem()"> |
| 11 | + <span aria-hidden="true">×</span> |
| 12 | +</button> |
6 | 13 | <select-widget-widget
|
7 | 14 | [dataIndex]="dataIndex"
|
8 | 15 | [layoutIndex]="layoutIndex"
|
9 |
| - [layoutNode]="layoutNode"></select-widget-widget>`, |
| 16 | + [layoutNode]="layoutNode"></select-widget-widget> |
| 17 | + `, |
10 | 18 | })
|
11 |
| -export class NoFrameworkComponent { |
| 19 | +export class NoFrameworkComponent implements OnInit { |
| 20 | + |
| 21 | + options: any; // Options used in this framework |
| 22 | + parentArray: any = null; |
| 23 | + isOrderable = false; |
| 24 | + |
12 | 25 | @Input() layoutNode: any;
|
13 | 26 | @Input() layoutIndex: number[];
|
14 | 27 | @Input() dataIndex: number[];
|
| 28 | + |
| 29 | + constructor( |
| 30 | + public jsf: JsonSchemaFormService |
| 31 | + ) { } |
| 32 | + |
| 33 | + ngOnInit() { |
| 34 | + this.options = _.cloneDeep(this.layoutNode.options); |
| 35 | + if (this.layoutNode.arrayItem && this.layoutNode.type !== '$ref') { |
| 36 | + this.parentArray = this.jsf.getParentNode(this); |
| 37 | + if (this.parentArray) { |
| 38 | + this.isOrderable = this.layoutNode.arrayItemType === 'list' && |
| 39 | + !this.options.readonly && this.parentArray.options.orderable; |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + get showRemoveButton(): boolean { |
| 46 | + if (!this.options.removable || this.options.readonly || |
| 47 | + this.layoutNode.type === '$ref' |
| 48 | + ) { return false; } |
| 49 | + if (this.layoutNode.recursiveReference) { return true; } |
| 50 | + if (!this.layoutNode.arrayItem || !this.parentArray) { return false; } |
| 51 | + // If array length <= minItems, don't allow removing any items |
| 52 | + return this.parentArray.items.length - 1 <= this.parentArray.options.minItems ? false : |
| 53 | + // For removable list items, allow removing any item |
| 54 | + this.layoutNode.arrayItemType === 'list' ? true : |
| 55 | + // For removable tuple items, only allow removing last item in list |
| 56 | + this.layoutIndex[this.layoutIndex.length - 1] === this.parentArray.items.length - 2; |
| 57 | + } |
| 58 | + |
| 59 | + removeItem() { |
| 60 | + this.jsf.removeItem(this); |
| 61 | + } |
15 | 62 | }
|
0 commit comments