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-body-media-type-selector.html
189 lines (178 loc) · 5.81 KB
/
docs-body-media-type-selector.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
<!--
@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="../paper-dropdown-menu/paper-dropdown-menu.html">
<link rel="import" href="../paper-listbox/paper-listbox.html">
<link rel="import" href="../paper-item/paper-item.html">
<!--
A dropdown with media types selector for RAML body declaration.
It extracts media types for provided `body` and displays a dropdown. When value
of the dropdown changes then it sets `selectedType` property to type declaration
for selected body.
By default first media type is selected.
### Styling
`<docs-body-media-type-selector>` provides the following custom properties and mixins for styling:
Custom property | Description | Default
----------------|-------------|----------
`--docs-body-media-type-selector` | Mixin applied to this elements | `{}`
`--docs-body-media-type-selector` | Mixin applied to the dropdown element | `{}`
@group RAML Elements
@element docs-body-media-type-selector
@demo demo/docs-body-parameters-table.html
-->
<dom-module id="docs-body-media-type-selector">
<template>
<style>
:host {
display: block;
@apply --docs-body-media-type-selector;
}
paper-listbox paper-item {
cursor: pointer;
}
.body-selector {
@apply --docs-body-media-type-selector-dropdown;
}
</style>
<paper-dropdown-menu label="Media type" class="body-selector">
<paper-listbox class="dropdown-content" attr-for-selected="data-type" selected="{{mediaType}}">
<template is="dom-repeat" items="[[availableMediaTypes]]">
<paper-item data-type$="[[item]]">[[item]]</paper-item>
</template>
</paper-listbox>
</paper-dropdown-menu>
</template>
<script>
Polymer({
is: 'docs-body-media-type-selector',
/**
* Fired when selected body content type have changed.
* It is not fired if value is not set or if change was caused by
* computation of available content types automatically set fist available
* content type.
*
* @event raml-docs-media-type-changed
* @param {String} value Newly selected media type.
*/
properties: {
/**
* The `body` part of the response type of RAML definition.
* The body must be array. RAML to JSON parser output must be transformed
* from Object to Array by adding a `key` property on each body item.
*/
body: Array,
/**
* Computed list of media types found in body declaration.
*/
availableMediaTypes: {
type: Array,
computed: '_computeMediaTypes(body)'
},
/**
* A name of selected media type of the body.
* E.g. "application/json".
*/
mediaType: {
type: String,
notify: true
},
/**
* Currently select type definition for selected media type.
*/
selectedType: {
type: Object,
computed: '_computeSelectedBody(body, mediaType)',
notify: true
}
},
observers: [
'_mediaTypesChanged(availableMediaTypes)',
'_mediaTypeChanged(mediaType)'
],
attached: function() {
this.listen(window, 'raml-docs-media-type-changed',
'_bodyMediaTypeHandler');
},
detached: function() {
this.unlisten(window, 'raml-docs-media-type-changed',
'_bodyMediaTypeHandler');
},
// Computes a list of media types for given `body`.
_computeMediaTypes: function(body) {
if (!body || !body.length) {
return [];
}
return body.map(function(item) {
// TODO: new RAML parser output replaces objects with arrays and
// key will be replaced with name.
return item.key;
});
},
/**
* Handler for `raml-docs-media-type-changed` to change `mediaType`
* value.
*/
_bodyMediaTypeHandler: function(e) {
if (e.target === this) {
return;
}
this._cancelBodyContentTypeEvent = true;
this.set('mediaType', e.detail.value);
this._cancelBodyContentTypeEvent = false;
},
/**
* Fires the `raml-docs-content-type-changed` event when selected body
* content type changes.
* It will not be fired if `_cancelBodyContentTypeEvent` is set
* or if current selection does not have a value.
*/
_mediaTypeChanged: function(mediaType) {
if (this._cancelBodyContentTypeEvent || !mediaType) {
return;
}
this.fire('raml-docs-media-type-changed', {
value: mediaType
});
},
/**
* When the selection of body types change it will compute proper
* body object.
*/
_computeSelectedBody: function(body, mediaType) {
if (!mediaType || !body || !body.length) {
return undefined;
}
for (var i = 0, len = body.length; i < len; i++) {
if (body[i].key === mediaType) {
return body[i];
}
}
return undefined;
},
/**
* When the `mediaTypes` changes it sets a default (first) media type as
* selected media type.
*/
_mediaTypesChanged: function(types) {
var mediaType;
if (types && types.length) {
mediaType = types[0];
}
this._cancelBodyContentTypeEvent = true;
this.set('mediaType', mediaType);
this._cancelBodyContentTypeEvent = false;
}
});
</script>
</dom-module>