This repository was 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
Expand file tree
/
Copy pathdocs-parameters-behavior.html
More file actions
294 lines (290 loc) · 8.47 KB
/
docs-parameters-behavior.html
File metadata and controls
294 lines (290 loc) · 8.47 KB
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
284
285
286
287
288
289
290
291
292
293
294
<!--
@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="../arc-polyfills/arc-polyfills.html">
<script>
(function() {
window.RamlBehaviors = window.RamlBehaviors || {};
/**
* Use `RamlBehaviors.DocsParametersBehavior` to implement a custom element
* that is rendering a parameters table in the RAML documentation view.
* @polymerBehavior RamlBehaviors.DocsParametersBehavior
*/
window.RamlBehaviors.DocsParametersBehavior = {
properties: {
// If set automatically hides the element which doesn't have parameteres set
autoHide: {
type: Boolean,
value: false
},
// RAML base types
baseTypes: {
type: Array,
value: function() {
return ['object', 'array','integer', 'string','number','boolean',
'datetime','file', 'null','any'];
}
},
/**
* A list of examples in the definition.
* This is a computed value for both `example` and `examples` properties.
*
* This is not computed automatically. Set the type object change observer. See the
* `_computeExamples()` function for more information.
*
* If you see this documentation it means that the element that implements this behavior is
* not using examples and it's not relevant for this element.
*/
examples: Array,
// Computed value if current type has examples.
hasExamples: {
type: Boolean,
value: false,
notify: true,
computed: '_computeHasExamples(examples.*)'
},
// If true then the narrow layout is applied.
narrow: {
type: Boolean,
reflectToAttribute: true
}
},
/**
* Check if passed object is set.
* 0 (zero) returns true while empty string returns false.
*
* @return {Boolean} True/False depending if passed object has a value.
*/
hasValue: function(obj) {
var type = typeof obj;
if (type === 'number' && obj === 0) {
return true;
}
if (type === 'boolean') {
return true;
}
if (type === 'string' && obj === '') {
return true;
}
return !!obj;
},
/**
* Gets a value of an object.
* It returns the same object except if the value is empty string. Then it
* return `(empty string)` label.
*
* @param {any} obj Object to evaludate
* @return {any} the same object
*/
getValue: function(obj) {
var type = typeof obj;
if (type === 'string' && obj === '') {
return '(empty string)';
}
return obj;
},
/**
* in RAML `properties` property of the type object is an object.
* To make it useful while itereing using `dom-repeat` this function
* produces an array of objects. Object content is the same as
* RAML parser's. Object's key is the same as the `name` property in the
* object.
*
* ## Example
* Source object:
* ```javascript
* properties: {
* available: {
* description: 'description',
* displayName: 'available',
* name: 'available',
* required: true
* }
* }
* ```
* becomes:
* ```javascript
* properties: [
* {
* description: 'description',
* displayName: 'available',
* name: 'available',
* required: true
* }
* ]
* ```
* @param {Object} obj The original RAML parser properties value.
* @return {Array}
*/
readProperties: function(obj) {
if (!obj) {
return [];
}
if (obj instanceof Array) {
return obj;
}
var names = Object.keys(obj);
if (!names) {
return [];
}
names.sort();
return names.map(function(i) {
var _obj = obj[i];
_obj.key = i;
return _obj;
});
},
_computeEnumValue: function(enumValue) {
if (!enumValue || !enumValue.length) {
return '';
}
if (typeof enumValue === 'string') {
return enumValue;
}
enumValue = enumValue.map(function(item) {
if (typeof item === 'string') {
if (item === '') {
return '""';
}
return item;
}
return item.displayName || item.name || item.key;
});
return enumValue.join(', ');
},
/**
* Computes list of examples.
* This function to be used with object sub-properties observer:
* ```
* examples: {
* type: Array,
* computed: '_computeExamples(type.*)'
* }
* ```
* or in the observers list.
*/
_computeExamples: function(record) {
var object = record.base;
if (!object) {
return undefined;
}
if (object.examples && object.examples.length) {
return object.examples;
}
if (object.example) {
if (typeof object.example === 'string') {
return [object.example];
} else {
return [JSON.stringify(object.example, null, 2)];
}
}
return undefined;
},
// Computes boolean value when the `examples` property change.
_computeHasExamples: function(record) {
var object = record.base;
if (!object) {
return false;
}
return !!object.length;
},
// Checks if the item has a maximum or max property.
_hasMaximum: function(item) {
return !!(item && (item.max || item.maximum || item.max === 0 ||
item.maximum === 0));
},
// returns item's maximum or max property.
_getMaximum: function(item) {
if (!item) {
return;
}
if (!!item.max || item.max === 0) {
return item.max;
}
if (!!item.maximum || item.maximum === 0) {
return item.maximum;
}
},
// Checks if the item has a maximum or max property.
_hasMinimum: function(item) {
return !!(item && (item.min || item.minimum || item.min === 0 ||
item.minimum === 0));
},
// returns item's maximum or max property.
_getMinimum: function(item) {
if (!item) {
return;
}
if (!!item.min || item.min === 0) {
return item.min;
}
if (!!item.minimum || item.minimum === 0) {
return item.minimum;
}
return item && (item.min || item.minimum);
},
// Checks if the item has a maximum or max property.
_hasExamples: function(item) {
return !!(item && item.examples && item.examples.length);
},
// returns item's maximum or max property.
_getExamples: function(item) {
if (!this._hasExamples(item)) {
return;
}
return item.examples.map(function(item) {
if (typeof item === 'string') {
return item;
}
return item.value;
}).join('\n');
},
// Returns true if `displayName` is set and it's different that `name`
_hasDisplayName: function(item) {
return !!(item && item.displayName && item.displayName !== item.name);
},
/**
* Used by schema display elements.
* Returns a value for 'hidden' attribute to hide the schema title label
* when schema is RAML 0.8 definition.
*
* @param {Object} type RAML type definition.
* @return {Boolean} True when not schema with label to display.
*/
_computeSchemaTypeHidden: function(type) {
if (!type || !type.schema) {
return true;
}
if (type.schema && type.schema === type.schemaContent) {
return true;
}
return false;
},
/**
* Computes schema's label to display in schema viewer.
* @param {Object} type RAML type definition.
* @return {String|undefined} Label to display in schema viewer or undefined.
*/
_computeSchemaLabel: function(type) {
if (!type || !type.schema) {
return;
}
if (type.schema && type.schema === type.schemaContent) {
return;
}
return type.schema;
}
};
})();
</script>