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
/
raml-annotations-display.html
215 lines (203 loc) · 6.87 KB
/
raml-annotations-display.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
<!--
@license
Copyright 2016 The Advanced REST client authors <[email protected]>
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="../marked-element/marked-element.html">
<link rel="import" href="../prism-element/prism-theme-default.html">
<link rel="import" href="../markdown-styles/markdown-styles.html">
<link rel="import" href="../arc-polyfills/arc-polyfills.html">
<link rel="import" href="../iron-flex-layout/iron-flex-layout.html">
<!--
The `<annotations-display>` element displays list of annotations added to a
property.
It creates its own model out of RAML definition.
Currently this element support annotations that it's values are either string,
nil or a properties list that have primitive values.
### Example
```
<raml-annotations-display annotations='[...]'></raml-annotations-display>
```
### Styling
`<raml-annotations-display>` provides the following custom properties and mixins for styling:
Custom property | Description | Default
----------------|-------------|----------
`--annotations-display` | Mixin applied to the element | `{}`
`--annotations-display-name` | Mixin applied to annotation name element | `{}`
`--annotations-display-value` | Mixin applied to the value container. Note that it may contain `<marked-element>`, `ul` for properties list or be empty | `{}`
`--annotations-display-list` | Mixin applied to the list of annotation properties. The `ul` element in value container. | `{}`
`--annotations-display-list-name` | Mixin applied to the property list names | `{}`
`--annotations-display-list-value` | Mixin applied to the property list values | `{}`
`--annotations-display-list-value-font-weight` | Font weight of the annotation list value | `500`
@group RAML Elements
@element raml-annotations-display
@demo demo/index.html
-->
<dom-module id="raml-annotations-display">
<template strip-whitespaces>
<style include="markdown-styles"></style>
<style>
:host {
font-family: inherit;
font-size: inherit;
color: inherit;
display: block;
@apply --annotations-display;
}
.annotation {
@apply --layout-horizontal;
}
.annotation-name {
margin-right: 8px;
text-transform: capitalize;
@apply --annotations-display-name;
}
.annotation-value {
@apply --annotations-display-value;
}
ul {
margin: 0;
padding-left: 24px;
@apply --annotations-display-list;
}
li>.value-name {
margin-right: 8px;
@apply --annotations-display-list-name;
}
li>.value-content {
font-weight: var(--annotations-display-list-value-font-weight, 500);
@apply --annotations-display-list-value;
}
.markdown-html p {
margin: 0;
}
</style>
<template is="dom-repeat" items="[[_model]]">
<div class="annotation">
<div class="annotation-name">[[item.name]]<template is="dom-if" if="[[!item.isNil]]">:</template></div>
<div class="annotation-value">
<template is="dom-if" if="[[item.isString]]" restamp>
<marked-element markdown="[[item.value]]">
<div class="markdown-html markdown-body"></div>
</marked-element>
</template>
<template is="dom-if" if="[[item.isArray]]" restamp>
<ul class="array">
<template is="dom-repeat" items="[[item.list]]" as="list">
<li><span class="value-name">[[list.name]]</span><span class="value-content">[[list.value]]</span></li>
</template>
</ul>
</template>
</div>
</div>
</template>
</template>
<script>
Polymer({
is: 'raml-annotations-display',
properties: {
/**
* Annotation property of an RAML object.
*/
annotations: Array,
// Model generated for the annotations.
_model: {
type: Array,
computed: '_computeModel(annotations.*)'
}
},
/**
* Computes if valid annotations has been passed to the element.
*/
_computeHasAnnotations: function(record) {
return !!(record && record.base && record.base.length);
},
// Computes model for the annotations to display.
_computeModel: function(record) {
if (!this._computeHasAnnotations(record)) {
return;
}
var result = record.base.map(function(item) {
item = Object.assign({}, item);
delete item.key;
item = this._createModelItem(item);
return item;
}, this);
return result.filter(function(item) {
return !!item;
});
},
/**
* Creates a model item from RAML definition.
*
* @param {Object} item RAML definition of the annotation.
* @return {Object} A model for the view.
*/
_createModelItem: function(item) {
var structureType = typeof item.structuredValue;
item.isString = !!~['string', 'number', 'boolean'].indexOf(structureType);
item.isNil = item.structuredValue === null || item.structuredValue === undefined;
if (!item.isString && !item.isNil) {
try {
var list = this._computeAnnotationList(item);
if (list) {
item.isArray = true;
item.list = list;
} else {
// Not supported annotation.
console.info('Annotation type not supported', item);
return;
}
} catch (e) {
console.warn('Issue with annotations computations.', e, item);
}
} else if (item.isString) {
item.value = String(item.structuredValue);
}
if (!item.isArray) {
item.isArray = false;
}
delete item.structuredValue;
return item;
},
// Computes annotation model for an annotation with list of values.
_computeAnnotationList: function(item) {
if (!item.structuredValue) {
return;
}
var keys = Object.keys(item.structuredValue);
var size = keys.length;
if (!size) {
return;
}
var list = keys.map(function(key) {
var value = item.structuredValue[key];
var _typeof = typeof value;
if (!~['string', 'number', 'boolean'].indexOf(_typeof)) {
return;
}
return {
name: key,
value: value
};
});
list = list.filter(function(item) {
return !!item;
});
if (!list.length) {
return;
}
return list;
}
});
</script>
</dom-module>