api-console-assets
Version:
This repo only exists to publish api console components to npm
170 lines (155 loc) • 4.67 kB
HTML
<!--
@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="../paper-tooltip/paper-tooltip.html">
<!--
A display for the base URL with variables highlighter.
Variables after click will be transformed into input elements where the user can enter a value
of the variable. Hosted app should intercept the `uri-parameter-changed` custom event and set
other relevant parts of the application accordingly.
To disable variables editor, set `no-editor` attribute on the element.
-->
<dom-module id="url-label">
<template>
<style>
:host {
/*display: inline-block;*/
@apply(--arc-font-body1);
font-size: inherit;
}
.content {
@apply(--layout-horizontal);
@apply(--layout-center);
height: 42px;
}
div[hidden] {
display: none ;
}
.variable {
font-weight: 500;
font-style: italic;
}
.variable:before {
content: "\007B";
}
.variable:after {
content: "\007D";
}
.part {
font-weight: 400;
}
</style>
<div class="content" on-opened-changed="_test">
<template is="dom-repeat" items="[[parts]]">
<div class$="[[_computeClass(item.variable)]]" id$="uri-part[[index]]">[[item.value]]</div>
<template is="dom-if" if="[[item.variable]]">
<paper-tooltip for="uri-part[[index]]">This is a URI variable.</paper-tooltip>
</template>
</template>
</div>
</template>
<script>
Polymer({
is: 'url-label',
properties: {
// The base URL.
url: {
type: String,
observer: '_urlChanged'
},
// Computed value, parsed parts of the URL
parts: {
type: Array,
readOnly: true
},
// RAML query parameters definition array.
uriParameters: Array,
},
observers: [
'_computePartsMeta(parts, uriParameters)'
],
// Adds meta property to the `parts` items.
_computePartsMeta: function(parts, params) {
if (!parts || !parts.length || !params || !params.length) {
return;
}
for (var i = 0, len = parts.length; i < len; i++) {
if (!parts[i].variable) {
continue;
}
var index = this._getVariableIndex(parts[i].key, params);
if (index === -1) {
continue;
}
this.set(['parts', i, 'meta'], params[index]);
}
},
// Handler for the URL change
_urlChanged: function(url) {
if (!url) {
return this._setParts(undefined);
}
var parts = [];
var index;
while ((index = url.indexOf('{')) !== -1) {
var end = url.indexOf('}');
var before = url.substr(0, index);
var part = url.substr(index + 1, end - index - 1);
url = url.substr(end + 1);
if (before) {
parts.push({
value: before,
variable: false
});
}
if (part) {
parts.push({
value: part,
variable: true,
key: part
});
}
}
if (url) {
parts.push({
value: url,
variable: false
});
}
this._setParts(parts);
},
/**
* Computes class of the URL segment.
*/
_computeClass: function(isVariable) {
return isVariable ? 'variable' : 'part';
},
// Get RAML URI parameter definition's index in the `uriParameters` array
_getVariableIndex: function(variable, variables) {
var result = -1;
variables = variables || this.uriParameters;
if (!variables || !variables.length) {
return result;
}
for (var i = 0, len = variables.length; i < len; i++) {
if (variables[i].key === variable) {
result = i;
break;
}
}
return result;
}
});
</script>
</dom-module>