api-console-assets
Version:
This repo only exists to publish api console components to npm
284 lines (268 loc) • 10.1 kB
HTML
<!--
@license
Copyright 2016 The Advanced REST client authors
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-pages/iron-pages.html">
<link rel="import" href="../paper-tabs/paper-tabs.html">
<link rel="import" href="../paper-tabs/paper-tab.html">
<link rel="import" href="docs-parameters-behavior.html">
<link rel="import" href="docs-parameters-table-shared-styles.html">
<link rel="import" href="structure-display.html">
<link rel="import" href="example-display.html">
<!--
`<docs-json-structure-view>` displays a type structure in code view.
It renders structure (or schema) and examples if available.
The element expects `hasType` and `isSchema` that can be computed by parent
element or application. See `docs-json-parameters-table` to see how this
properties are computed.
### Example
```
<docs-json-structure-view type="[[ramlJsonType]]" has-type></docs-json-structure-view>
```
### Styling
`<docs-json-parameters-table>` provides the following custom properties and mixins for styling:
Custom property | Description | Default
----------------|-------------|----------
`--docs-parameters-table` | Mixin applied to all parameter table elements | `{}`
`--docs-parameters-url-table` | Mixin applied to this elements | `{}`
`--params-table-header-background-color` | Background color of table header | `#00A1DF`
`--params-table-header-color` | Font color of table header | `rgba(255, 255, 255, 0.87)`
`--params-table-subheader-background-color` | Background color of table subheader | `rgba(0, 161, 223, 0.24)`
`--params-table-subheader-color` | Font color of table subheader | `rgba(0, 0, 0, 0.87)`
`--params-table-row-border-color` | Border color of table rows | `rgba(0, 161, 223, 1)`
`--params-table-row-subproperty-border-color` | Border color of table row which is a description of child property. | `rgba(0, 161, 223, 0.24)`
`--params-table-row-background-color` | Background color of table's each row | `#fff`
`--params-table-row-color` | Font color of table's each row | `#fff`
`--docs-parameters-table-cell` | Mixin applied to each cell | `{}`
`--docs-parameters-table-meta` | Mixin applied to property's metadata (example, pattern, etc) | `{}`
`--docs-body-parameters-table-type-name` | Mixin applied to the name of the type | `{}`
`--params-table-subproperty-prefix-color` | Color of the parent property name in the subproperty list | `rgba(0, 0, 0, 0.54)`
`--no-info-message` | A mixin applied to the "missing type" paragraph. | `{}`
Use `paper-tabs`, `paper-tab` and `structure-display` mixins to style this element.
@element docs-json-structure-view
@demo demo/docs-body-parameters-table.html
-->
<dom-module id="docs-json-structure-view">
<template>
<style include="docs-parameters-table-shared-styles"></style>
<style>
:host {
--paper-tabs-selection-bar-color: var(--params-table-header-background-color, #00A1DF);
--paper-tab-ink: var(--params-table-header-background-color, #00A1DF);
}
</style>
<template is="dom-if" if="[[typeOnly]]">
<div>
<pre is="structure-display" display="[[displayJson]]" class="language-javascript" auto-hide></pre>
</div>
</template>
<template is="dom-if" if="[[exampleOnly]]" restamp>
<div class="examples">
<template is="dom-repeat" items="[[examples]]">
<example-display is-json content="[[item]]"></example-display>
</template>
</div>
</template>
<template is="dom-if" if="[[typeAndExample]]" restamp>
<div>
<paper-tabs class="schemas" selected="{{selectedSchemaPage}}">
<paper-tab>[[_getTypeTabName(isSchema)]]</paper-tab>
<paper-tab>Examples</paper-tab>
</paper-tabs>
<iron-pages selected="{{selectedSchemaPage}}">
<div class="schema">
<h5 hidden$="[[_computeSchemaTypeHidden(type)]]">Schema: [[_computeSchemaLabel(type)]]</h5>
<pre is="structure-display" display="[[displayJson]]" class="language-javascript" auto-hide></pre>
</div>
<div class="examples">
<template is="dom-repeat" items="[[examples]]">
<example-display is-json content="[[item]]"></example-display>
</template>
</div>
</iron-pages>
</div>
</template>
</template>
<script>
(function() {
Polymer({
is: 'docs-json-structure-view',
behaviors: [RamlBehaviors.DocsParametersBehavior],
properties: {
/**
* A RAML's type definition for the body payload.
*/
type: Object,
// Set to true if the type has been detected
hasType: Boolean,
// Set to true if the type is a schema.
isSchema: {
type: Boolean,
computed: '_computeIsSchema(type)'
},
// JSON created from the type definition
displayJson: {
type: String,
computed: '_computeDisplayJson(type.*)'
},
/**
* A list of examples in the definition.
* This is a computed value for both `example` and `examples` properties.
*/
examples: {
type: Array,
computed: '_computeExamples(type.*)'
},
// Selected page in the tabs (where examples exists for this page)
selectedSchemaPage: {
type: Number,
value: 0
},
/**
* Computed value when type change.
* If true then only type is rendered.
*/
typeOnly: {
type: Boolean,
readOnly: true
},
/**
* Computed value when type change.
* If true then only examples is rendered.
*/
exampleOnly: {
type: Boolean,
readOnly: true
},
/**
* Computed value when type change.
* If true then both type and example is rendered.
*/
typeAndExample: {
type: Boolean,
readOnly: true
}
},
observers: [
'_updateUiVariables(hasType, hasExamples)'
],
/**
* Compytes main flow variables that controll what is visible.
*/
_updateUiVariables: function(hasType, hasExamples) {
this._setTypeOnly(hasType && !hasExamples);
this._setExampleOnly(!hasType && hasExamples);
this._setTypeAndExample(hasType && hasExamples);
},
// Computes a JSON object to display in the type preview.
_computeDisplayJson: function(record) {
if (!record || !record.base) {
return;
}
if (record.base.schema) {
return record.base.schemaContent;
}
if (record.base.type instanceof Array) {
record.base.type = record.base.type[0];
}
if (record.base.type === 'json') {
return record.base.content;
}
var properties = record.base.properties;
var isArray = false;
var hasProperties = !!(properties && Object.keys(properties).length);
if (!hasProperties) {
if (record.base.type === 'array') {
properties = record.base.items.properties;
isArray = true;
hasProperties = !!(properties && Object.keys(properties).length);
} else if (record.base.type === 'union') {
// just pass to the next block.
} else {
return undefined;
}
}
var data = hasProperties ? this.__transformObject({}, properties) : {};
var self = this;
if (record.base.type === 'union') {
var ut = [];
record.base.anyOf.forEach(function(item) {
var _props = self.__transformObject({}, item.properties, true);
ut[ut.length] = _props;
});
data = Object.assign(data, ut[0]);
}
if (isArray) {
data = [data];
}
return JSON.stringify(data, null, 2);
},
__transformObject: function(base, obj) {
var bt = this.baseTypes;
for (var i = 0, len = obj.length; i < len; i++) {
var item = obj[i];
var type = item.type;
var structure;
if (type instanceof Array) {
type = type[0];
obj.type = type;
}
if (bt.indexOf(type) === -1) {
// other type defined in RAML
if (item.properties) {
structure = this.__transformObject({}, this.readProperties(item.properties));
} else {
structure = type;
}
} else if (type === 'object' && item.properties) {
structure = this.__transformObject({}, this.readProperties(item.properties));
} else if (type === 'array') {
if (item.items.type instanceof Array) {
item.items.type = item.items.type[0];
}
if (typeof item.items === 'string') {
structure = [item.items];
} else if (item.items.type === 'object') {
structure = [this.__transformObject({},
this.readProperties(item.items.properties))];
} else {
structure = [item.items.type];
}
} else {
structure = type; //item.example || type;
}
base[item.key || item.name] = structure;
}
return base;
},
// Computes tab name, `Type` if not schema and `Schema` otherise.
_getTypeTabName: function(isSchema) {
return isSchema ? 'Schema' : 'Type';
},
// Computes `isSchema` value for given type.
_computeIsSchema: function(object) {
if (!object) {
return false;
}
if (object.schema) {
return true;
}
if (object.type === 'json') {
return true;
}
return false;
}
});
})();
</script>
</dom-module>