This repository has been archived by the owner on Feb 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docs-json-structure-view.html
283 lines (268 loc) · 9.86 KB
/
docs-json-structure-view.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<!--
@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>