UNPKG

api-console-assets

Version:

This repo only exists to publish api console components to npm

198 lines (184 loc) 6.29 kB
<!-- @license Copyright 2016 The Advanced REST client authors <arc@mulesoft.com> 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="../prism-highlight/prism-highlight.html"> <link rel="import" href="../text-search-behavior/text-search-behavior.html"> <link rel="import" href="../iron-flex-layout/iron-flex-layout.html"> <!-- An element that parses the HTTP response and displays highlighted result. It splits the response by line (by default it's 500) and if the response has more than that it shows only first 500 lines and the user can request to display the rest or next 500 lines. This is to make the element work faster. If the response is very long it may take some time to parse and tokenize it. Control number of lines by setting the maxRead attribute. ### Example ``` <response-highlighter></response-highlighter> ``` ``` var display = document.querySelector('response-highlighter'); display.responseText = someJsonResponse; display.contentType = 'application/json'; ``` ## Content actions By default the user can copy content of the code to clipboard. This action is always visible. You can add more actions in the actions bar by putting elements as a children of this element. ### Example ``` <response-highlighter> <paper-icon-button title="Additional action" icon="arc:cached"></paper-icon-button> <paper-icon-button title="Clear the code" icon="arc:clear"></paper-icon-button> </response-highlighter> ``` See demo for more information. ## Custom search If the platform doesn't support native text search, this element implements `ArcBehaviors.TextSearchBehavior` and exposes the `query` attribute. Set any text to the `query` attribute and it will automatically highlight occurance of the text. ## Content actions The element can render a actions pane above the code view. Action pane is to display content actions that is relevan in context of the response displayed below the icon buttons. It should be icon buttons or just buttons added to this view. Buttons need to have `content-action` property set to be included to this view. ``` <json-viewer json='{"json": "test"}'> <paper-icon-button content-action title="Copy content to clipboard" icon="arc:content-copy"></paper-icon-button> </json-viewer> ``` ### Styling `<response-highlighter>` provides the following custom properties and mixins for styling: Custom property | Description | Default ----------------|-------------|---------- `--response-highlighter` | Mixin applied to the element | `{}` `--response-highlighter-action-bar` | Mixin applied to the action bar above the highlighted code | `{}` `--no-info-message` | Mixin applied to the "nothing to display" message (theme variable) | `{}` See prism-highlight element for more styling options. @group UI Elements @element response-highlighter @demo demo/index.html --> <dom-module id="response-highlighter"> <template> <style> :host { display: block; @apply(--response-highlighter); } .actions-panel { @apply(--layout-horizontal); @apply(--layout-center); @apply(--response-highlighter-action-bar); } .actions-panel.hidden { display: none; } .no-info { @apply(--no-info-message); } </style> <div class$="[[_computeActionsPanelClass(hasResponse)]]"> <content select="[content-action]"></content> </div> <prism-highlight max-read="[[maxRead]]" hidden$="[[!hasResponse]]" id="prism" code="[[responseText]]" lang$="[[lang]]"></prism-highlight> <p class="no-info" hidden$="[[hasResponse]]">Nothing to dispplay.</p> </template> <script> Polymer({ is: 'response-highlighter', behaviors: [ ArcBehaviors.TextSearchBehavior ], properties: { /** * The response text to display. */ responseText: String, // Computed value, true if the responseText has text. hasResponse: { type: Boolean, value: false, computed: '_computeHasResponse(responseText)' }, /** * Response content type. * It will be used to determine which syntaxt highlighter to use. */ contentType: String, // An element that should be searched for the query. _textSearch: { type: HTMLElement, value: function() { return this.$.prism.$.output; } }, /** * If set it will highlight each occurance of the query in the * XML viewer. */ query: { type: String, observer: '_queryChanged' }, // Attribute binded to the prism-highlight element. maxRead: Number, /** * The lang property for the Prism. */ lang: { type: String, computed: '_computeLang(contentType)' } }, // Highlights the query in text. _queryChanged: function(query) { if (!query) { this.cleanMarked(); return; } this.mark(query); }, // Computes if the element has the response data. _computeHasResponse: function(responseText) { return !!responseText; }, // Computes CSS class for the content actions pane. _computeActionsPanelClass: function(hasResponse) { var clazz = 'actions-panel'; if (!hasResponse) { clazz += ' hidden'; } return clazz; }, /** * Computes a `lang` property for the Prism from the response content-type */ _computeLang: function(contentType) { if (!contentType) { return undefined; } if (!contentType.indexOf) { return undefined; } if (contentType.indexOf(';') !== -1) { return contentType.split(';')[0]; } return contentType; } }); </script> </dom-module>