/
linked-block.js
75 lines (66 loc) · 2.16 KB
/
linked-block.js
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
import { inject as service } from '@ember/service';
import Component from '@ember/component';
import hbs from 'htmlbars-inline-precompile';
import { encodePath } from 'vault/utils/path-encoding-helpers';
/**
* @module LinkedBlock
* LinkedBlock components are linkable divs that yield any content nested within them. They are often used in list views such as when listing the secret engines.
*
* @example
* ```js
* <LinkedBlock
* @params={{array 'vault.cluster.secrets.backend.show 'my-secret-path'}}
* @queryParams={{hash version=1}}
* @class="list-item-row"
* data-test-list-item-link
* >
* // Use any wrapped content here
* </LinkedBlock>
* ```
*
* @param {Array} params=null - These are values sent to the router's transitionTo method. First item is route, second is the optional path.
* @param {Object} [queryParams=null] - queryParams can be passed via this property. It needs to be an object.
* @param {String} [linkPrefix=null] - Overwrite the params with custom route. See KMIP.
* @param {Boolean} [encode=false] - Encode the path.
*/
let LinkedBlockComponent = Component.extend({
router: service(),
layout: hbs`{{yield}}`,
classNames: 'linked-block',
queryParams: null,
linkPrefix: null,
encode: false,
click(event) {
const $target = event.target;
const isAnchorOrButton =
$target.tagName === 'A' ||
$target.tagName === 'BUTTON' ||
$target.closest('button') ||
$target.closest('a');
if (!isAnchorOrButton) {
let params = this.params;
if (this.encode) {
params = params.map((param, index) => {
if (index === 0 || typeof param !== 'string') {
return param;
}
return encodePath(param);
});
}
const queryParams = this.queryParams;
if (queryParams) {
params.push({ queryParams });
}
if (this.linkPrefix) {
let targetRoute = this.params[0];
targetRoute = `${this.linkPrefix}.${targetRoute}`;
this.params[0] = targetRoute;
}
this.router.transitionTo(...params);
}
},
});
LinkedBlockComponent.reopenClass({
positionalParams: 'params',
});
export default LinkedBlockComponent;