Refactor schema template

This commit is contained in:
Roman Hotsiy 2016-08-10 15:22:42 +03:00
parent 92f745d10f
commit d3d223a344
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
4 changed files with 112 additions and 95 deletions

View File

@ -1,4 +1,6 @@
<span *ngIf="schema.isFile" class="param-wrap"> <ng-container [ngSwitch]="schema._widgetType">
<template ngSwitchCase="file">
<span class="param-wrap">
<span class="param-type-file">file</span> <span class="param-type-file">file</span>
<div *ngIf="schema._produces && !isRequestSchema" class="file produces"> <div *ngIf="schema._produces && !isRequestSchema" class="file produces">
<ul> <ul>
@ -10,8 +12,10 @@
<li *ngFor="let type of schema._consumes">{{type}}</li> <li *ngFor="let type of schema._consumes">{{type}}</li>
</ul> </ul>
</div> </div>
</span> </span>
<span *ngIf="schema.isTrivial && !schema.isFile" class="param-wrap"> </template>
<template ngSwitchCase="trivial">
<span class="param-wrap">
<span class="param-type param-type-trivial {{schema.type}}" <span class="param-type param-type-trivial {{schema.type}}"
[ngClass]="{'with-hint': schema._displayTypeHint}" [ngClass]="{'with-hint': schema._displayTypeHint}"
title="{{schema._displayTypeHint}}">{{schema._displayType}} {{schema._displayFormat}} title="{{schema._displayTypeHint}}">{{schema._displayType}} {{schema._displayFormat}}
@ -20,18 +24,26 @@
<div *ngIf="schema.enum" class="param-enum"> <div *ngIf="schema.enum" class="param-enum">
<span *ngFor="let enumItem of schema.enum" class="enum-value {{enumItem.type}}"> {{enumItem.val | json}} </span> <span *ngFor="let enumItem of schema.enum" class="enum-value {{enumItem.type}}"> {{enumItem.val | json}} </span>
</div> </div>
</span> </span>
<div *ngIf="schema._isTuple" class="params-wrap params-array array-tuple"> </template>
<template ngSwitchCase="tuple">
<div class="params-wrap params-array array-tuple">
<template ngFor [ngForOf]="schema.items" let-item="$implicit" let-idx="index" [ngForTrackBy]="trackByIdx"> <template ngFor [ngForOf]="schema.items" let-item="$implicit" let-idx="index" [ngForTrackBy]="trackByIdx">
<div class="tuple-item"> <div class="tuple-item">
<span class="tuple-item-index"> [{{idx}}]: </span> <span class="tuple-item-index"> [{{idx}}]: </span>
<json-schema class="nested-schema" [pointer]="item._pointer" [isArray]='item._isArray' <json-schema class="nested-schema" [pointer]="item._pointer"
[nestOdd]="!nestOdd" [isRequestSchema]="isRequestSchema"> [nestOdd]="!nestOdd" [isRequestSchema]="isRequestSchema">
</json-schema> </json-schema>
</div> </div>
</template> </template>
</div> </div>
<table *ngIf="!schema.isTrivial && !schema.isTuple" class="params-wrap" [ngClass]="{'params-array': _isArray}"> </template>
<template ngSwitchCase="array">
<json-schema class="nested-schema" [pointer]="schema._pointer" [isArray]='true'
[nestOdd]="!nestOdd" [isRequestSchema]="isRequestSchema"> </json-schema>
</template>
<template ngSwitchCase="object">
<table class="params-wrap" [ngClass]="{'params-array': schema._isArray}">
<!-- <caption> {{_displayType}} </caption> --> <!-- <caption> {{_displayType}} </caption> -->
<template ngFor [ngForOf]="properties" let-prop="$implicit" let-last="last" [ngForTrackBy]="trackByIdx"> <template ngFor [ngForOf]="properties" let-prop="$implicit" let-last="last" [ngForTrackBy]="trackByIdx">
<tr class="param" [ngClass]="{'last': last, <tr class="param" [ngClass]="{'last': last,
@ -73,7 +85,7 @@
<tr class="param-schema" [ngClass]="{'last': last}" [hidden]="!prop._pointer"> <tr class="param-schema" [ngClass]="{'last': last}" [hidden]="!prop._pointer">
<td colspan="2"> <td colspan="2">
<zippy #subSchema title="Expand" [headless]="true" (open)="lazySchema.load()" [visible]="autoExpand"> <zippy #subSchema title="Expand" [headless]="true" (open)="lazySchema.load()" [visible]="autoExpand">
<json-schema-lazy #lazySchema [auto]="autoExpand" class="nested-schema" [pointer]="prop._pointer" [isArray]='prop._isArray' <json-schema-lazy #lazySchema [auto]="autoExpand" class="nested-schema" [pointer]="prop._pointer"
[nestOdd]="!nestOdd" [isRequestSchema]="isRequestSchema"> [nestOdd]="!nestOdd" [isRequestSchema]="isRequestSchema">
</json-schema-lazy> </json-schema-lazy>
</zippy> </zippy>
@ -89,4 +101,7 @@
</div> </div>
</td> </td>
</tr> </tr>
</table> </table>
</template>
</ng-container>

View File

@ -212,7 +212,7 @@ li:before {
> span { > span {
flex: 0; flex: 0;
padding: 10px 15px; padding: 10px 15px 10px 0;
font-family: monospace; font-family: monospace;
} }

View File

@ -22,7 +22,6 @@ export class JsonSchema extends BaseComponent {
_hasSubSchemas: boolean = false; _hasSubSchemas: boolean = false;
properties: any; properties: any;
_isArray: boolean; _isArray: boolean;
@Input() isArray: boolean;
@Input() final: boolean = false; @Input() final: boolean = false;
@Input() nestOdd: boolean; @Input() nestOdd: boolean;
@Input() childFor: string; @Input() childFor: string;
@ -89,7 +88,6 @@ export class JsonSchema extends BaseComponent {
} }
this.properties = this.schema._properties; this.properties = this.schema._properties;
this._isArray = this.isArray || this.schema._isArray;
if (this.isRequestSchema) { if (this.isRequestSchema) {
this.properties = this.properties && this.properties.filter(prop => !prop.readOnly); this.properties = this.properties && this.properties.filter(prop => !prop.readOnly);
} }

View File

@ -69,6 +69,7 @@ const injectors = {
|| JsonPointer.join(propertySchema._pointer || propPointer, ['items']); || JsonPointer.join(propertySchema._pointer || propPointer, ['items']);
SchemaHelper.runInjectors(injectTo, propertySchema.items, propPointer); SchemaHelper.runInjectors(injectTo, propertySchema.items, propPointer);
injectTo._widgetType = 'array';
} }
}, },
tuple: { tuple: {
@ -83,7 +84,7 @@ const injectors = {
let itemSchema = propertySchema.items[i]; let itemSchema = propertySchema.items[i];
itemSchema._pointer = itemSchema._pointer || JsonPointer.join(itemsPtr, [i.toString()]); itemSchema._pointer = itemSchema._pointer || JsonPointer.join(itemsPtr, [i.toString()]);
} }
injectTo._widgetType = 'tuple';
// SchemaHelper.runInjectors(injectTo, propertySchema.items, propPointer); // SchemaHelper.runInjectors(injectTo, propertySchema.items, propPointer);
} }
}, },
@ -94,14 +95,16 @@ const injectors = {
inject: (injectTo, propertySchema = injectTo) => { inject: (injectTo, propertySchema = injectTo) => {
let baseName = propertySchema._pointer && JsonPointer.baseName(propertySchema._pointer); let baseName = propertySchema._pointer && JsonPointer.baseName(propertySchema._pointer);
injectTo._displayType = propertySchema.title || baseName || 'object'; injectTo._displayType = propertySchema.title || baseName || 'object';
injectTo._widgetType = 'object';
} }
}, },
noType: { noType: {
check: (propertySchema) => !propertySchema.type, check: (propertySchema) => !propertySchema.type,
inject: (injectTo) => { inject: (injectTo) => {
injectTo._displayType = '< * >'; injectTo._displayType = '< anything >';
injectTo._displayTypeHint = 'This field may contain data of any type'; injectTo._displayTypeHint = 'This field may contain data of any type';
injectTo.isTrivial = true; injectTo.isTrivial = true;
injectTo._widgetType = 'trivial';
} }
}, },
simpleType: { simpleType: {
@ -119,6 +122,7 @@ const injectors = {
injectTo._displayType = propertySchema.title ? injectTo._displayType = propertySchema.title ?
`${propertySchema.title} (${propertySchema.type})` : propertySchema.type; `${propertySchema.title} (${propertySchema.type})` : propertySchema.type;
} }
injectTo._widgetType = 'trivial';
} }
}, },
integer: { integer: {
@ -176,7 +180,7 @@ const injectors = {
let root = SpecManager.instance().schema; let root = SpecManager.instance().schema;
injectTo._produces = parentParam && parentParam.produces || root.produces; injectTo._produces = parentParam && parentParam.produces || root.produces;
injectTo._consumes = parentParam && parentParam.consumes || root.consumes; injectTo._consumes = parentParam && parentParam.consumes || root.consumes;
injectTo._widgetType = 'file';
} }
} }
}; };