-
Notifications
You must be signed in to change notification settings - Fork 916
/
url.ts
224 lines (195 loc) · 6.71 KB
/
url.ts
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
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Any modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you 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.
*/
import { i18n } from '@osd/i18n';
import { escape, memoize } from 'lodash';
import { getHighlightHtml } from '../utils';
import { OSD_FIELD_TYPES } from '../../osd_field_types/types';
import { FieldFormat } from '../field_format';
import {
TextContextTypeConvert,
HtmlContextTypeConvert,
IFieldFormatMetaParams,
FIELD_FORMAT_IDS,
} from '../types';
const templateMatchRE = /{{([\s\S]+?)}}/g;
const allowedUrlSchemes = ['http://', 'https://'];
const URL_TYPES = [
{
kind: 'a',
text: i18n.translate('data.fieldFormats.url.types.link', {
defaultMessage: 'Link',
}),
},
{
kind: 'img',
text: i18n.translate('data.fieldFormats.url.types.img', {
defaultMessage: 'Image',
}),
},
{
kind: 'audio',
text: i18n.translate('data.fieldFormats.url.types.audio', {
defaultMessage: 'Audio',
}),
},
];
const DEFAULT_URL_TYPE = 'a';
export class UrlFormat extends FieldFormat {
static id = FIELD_FORMAT_IDS.URL;
static title = i18n.translate('data.fieldFormats.url.title', {
defaultMessage: 'Url',
});
static fieldType = [
OSD_FIELD_TYPES.NUMBER,
OSD_FIELD_TYPES.BOOLEAN,
OSD_FIELD_TYPES.DATE,
OSD_FIELD_TYPES.IP,
OSD_FIELD_TYPES.STRING,
OSD_FIELD_TYPES.MURMUR3,
OSD_FIELD_TYPES.UNKNOWN,
OSD_FIELD_TYPES.CONFLICT,
];
static urlTypes = URL_TYPES;
constructor(params: IFieldFormatMetaParams) {
super(params);
this.compileTemplate = memoize(this.compileTemplate);
}
getParamDefaults() {
return {
type: DEFAULT_URL_TYPE,
urlTemplate: null,
labelTemplate: null,
width: null,
height: null,
};
}
private formatLabel(value: string, url?: string): string {
const template = this.param('labelTemplate');
if (url == null) url = this.formatUrl(value);
if (!template) return url;
return this.compileTemplate(template)({
value,
url,
});
}
private formatUrl(value: string): string {
const template = this.param('urlTemplate');
if (!template) return value;
return this.compileTemplate(template)({
value: encodeURIComponent(value),
rawValue: value,
});
}
private compileTemplate(template: string): Function {
// trim all the odd bits, the variable names
const parts = template.split(templateMatchRE).map((part, i) => (i % 2 ? part.trim() : part));
return function (locals: Record<string, any>): string {
// replace all the odd bits with their local var
let output = '';
let i = -1;
while (++i < parts.length) {
if (i % 2) {
if (locals.hasOwnProperty(parts[i])) {
const local = locals[parts[i]];
output += local == null ? '' : local;
}
} else {
output += parts[i];
}
}
return output;
};
}
private generateImgHtml(url: string, imageLabel: string): string {
const isValidWidth = !isNaN(parseInt(this.param('width'), 10));
const isValidHeight = !isNaN(parseInt(this.param('height'), 10));
const maxWidth = isValidWidth ? `${this.param('width')}px` : 'none';
const maxHeight = isValidHeight ? `${this.param('height')}px` : 'none';
return `<img src="${url}" alt="${imageLabel}" style="width:auto; height:auto; max-width:${maxWidth}; max-height:${maxHeight};">`;
}
textConvert: TextContextTypeConvert = (value) => this.formatLabel(value);
htmlConvert: HtmlContextTypeConvert = (rawValue, options = {}) => {
const { field, hit } = options;
const { parsedUrl } = this._params;
const { basePath, pathname, origin } = parsedUrl || {};
const url = escape(this.formatUrl(rawValue));
const label = escape(this.formatLabel(rawValue, url));
switch (this.param('type')) {
case 'audio':
return `<audio controls preload="none" src="${url}">`;
case 'img':
// If the URL hasn't been formatted to become a meaningful label then the best we can do
// is tell screen readers where the image comes from.
const imageLabel =
label === url ? `A dynamically-specified image located at ${url}` : label;
return this.generateImgHtml(url, imageLabel);
default:
const allowed = allowedUrlSchemes.some((scheme) => url.indexOf(scheme) === 0);
if (!allowed && !parsedUrl) {
return url;
}
let prefix = '';
/**
* This code attempts to convert a relative url into a OpenSearch Dashboards absolute url
*
* SUPPORTED:
* - /app/opensearch-dashboards/
* - ../app/opensearch-dashboards
* - #/discover
*
* UNSUPPORTED
* - app/opensearch-dashboards
*/
if (!allowed) {
// Handles urls like: `#/discover`
if (url[0] === '#') {
prefix = `${origin}${pathname}`;
}
// Handle urls like: `/app/opensearch-dashboards` or `/xyz/app/opensearch-dashboards`
else if (url.indexOf(basePath || '/') === 0) {
prefix = `${origin}`;
}
// Handle urls like: `../app/opensearch-dashboards`
else {
const prefixEnd = url[0] === '/' ? '' : '/';
prefix = `${origin}${basePath || ''}/app${prefixEnd}`;
}
}
let linkLabel;
if (hit && hit.highlight && hit.highlight[field.name]) {
linkLabel = getHighlightHtml(label, hit.highlight[field.name]);
} else {
linkLabel = label;
}
const linkTarget = this.param('openLinkInCurrentTab') ? '_self' : '_blank';
return `<a href="${prefix}${url}" target="${linkTarget}" rel="noopener noreferrer">${linkLabel}</a>`;
}
};
}
// console.log(UrlFormat);