2018-09-25 16:28:26 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
import Component from '@ember/component';
|
2018-04-03 14:16:57 +00:00
|
|
|
import hbs from 'htmlbars-inline-precompile';
|
2019-03-01 16:08:30 +00:00
|
|
|
import { encodePath } from 'vault/utils/path-encoding-helpers';
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
let LinkedBlockComponent = Component.extend({
|
|
|
|
router: service(),
|
|
|
|
|
2018-04-03 14:16:57 +00:00
|
|
|
layout: hbs`{{yield}}`,
|
|
|
|
|
|
|
|
classNames: 'linked-block',
|
|
|
|
|
|
|
|
queryParams: null,
|
2019-06-21 21:05:45 +00:00
|
|
|
linkPrefix: null,
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2019-03-01 16:08:30 +00:00
|
|
|
encode: false,
|
|
|
|
|
2018-04-03 14:16:57 +00:00
|
|
|
click(event) {
|
2019-06-20 13:37:27 +00:00
|
|
|
const $target = event.target;
|
2018-04-03 14:16:57 +00:00
|
|
|
const isAnchorOrButton =
|
2019-06-20 13:37:27 +00:00
|
|
|
$target.tagName === 'A' ||
|
|
|
|
$target.tagName === 'BUTTON' ||
|
|
|
|
$target.closest('button') ||
|
|
|
|
$target.closest('a');
|
2018-04-03 14:16:57 +00:00
|
|
|
if (!isAnchorOrButton) {
|
2019-03-01 16:08:30 +00:00
|
|
|
let params = this.get('params');
|
|
|
|
if (this.encode) {
|
|
|
|
params = params.map((param, index) => {
|
|
|
|
if (index === 0 || typeof param !== 'string') {
|
|
|
|
return param;
|
|
|
|
}
|
|
|
|
return encodePath(param);
|
|
|
|
});
|
|
|
|
}
|
2018-04-03 14:16:57 +00:00
|
|
|
const queryParams = this.get('queryParams');
|
|
|
|
if (queryParams) {
|
|
|
|
params.push({ queryParams });
|
|
|
|
}
|
2019-06-21 21:05:45 +00:00
|
|
|
if (this.linkPrefix) {
|
|
|
|
let targetRoute = this.params[0];
|
|
|
|
targetRoute = `${this.linkPrefix}.${targetRoute}`;
|
|
|
|
this.params[0] = targetRoute;
|
|
|
|
}
|
2018-09-25 16:28:26 +00:00
|
|
|
this.get('router').transitionTo(...params);
|
2018-04-03 14:16:57 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
LinkedBlockComponent.reopenClass({
|
|
|
|
positionalParams: 'params',
|
|
|
|
});
|
|
|
|
|
|
|
|
export default LinkedBlockComponent;
|