UNPKG

api-console-assets

Version:

This repo only exists to publish api console components to npm

190 lines (178 loc) 5.95 kB
<!-- @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>