2023-04-19 05:20:02 +08:00
|
|
|
import {Component} from './component';
|
2022-11-15 07:19:02 +08:00
|
|
|
|
2021-06-06 20:55:56 +08:00
|
|
|
/**
|
|
|
|
* Attachments List
|
|
|
|
* Adds '?open=true' query to file attachment links
|
|
|
|
* when ctrl/cmd is pressed down.
|
|
|
|
*/
|
2022-11-15 07:19:02 +08:00
|
|
|
export class AttachmentsList extends Component {
|
2021-06-06 20:55:56 +08:00
|
|
|
|
|
|
|
setup() {
|
|
|
|
this.container = this.$el;
|
2024-01-23 23:01:07 +08:00
|
|
|
this.fileLinks = this.$manyRefs.linkTypeFile;
|
|
|
|
|
2021-06-06 20:55:56 +08:00
|
|
|
this.setupListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
setupListeners() {
|
2023-04-19 05:20:02 +08:00
|
|
|
const isExpectedKey = event => event.key === 'Control' || event.key === 'Meta';
|
2021-06-06 20:55:56 +08:00
|
|
|
window.addEventListener('keydown', event => {
|
2023-04-19 05:20:02 +08:00
|
|
|
if (isExpectedKey(event)) {
|
2021-06-06 20:55:56 +08:00
|
|
|
this.addOpenQueryToLinks();
|
2023-04-19 05:20:02 +08:00
|
|
|
}
|
2021-06-06 20:55:56 +08:00
|
|
|
}, {passive: true});
|
|
|
|
window.addEventListener('keyup', event => {
|
|
|
|
if (isExpectedKey(event)) {
|
|
|
|
this.removeOpenQueryFromLinks();
|
|
|
|
}
|
|
|
|
}, {passive: true});
|
|
|
|
}
|
|
|
|
|
|
|
|
addOpenQueryToLinks() {
|
2024-01-23 23:01:07 +08:00
|
|
|
for (const link of this.fileLinks) {
|
2021-06-06 20:55:56 +08:00
|
|
|
if (link.href.split('?')[1] !== 'open=true') {
|
2023-04-19 05:20:02 +08:00
|
|
|
link.href += '?open=true';
|
2021-06-06 20:55:56 +08:00
|
|
|
link.setAttribute('target', '_blank');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
removeOpenQueryFromLinks() {
|
2024-01-23 23:01:07 +08:00
|
|
|
for (const link of this.fileLinks) {
|
2021-06-06 20:55:56 +08:00
|
|
|
link.href = link.href.split('?')[0];
|
|
|
|
link.removeAttribute('target');
|
|
|
|
}
|
|
|
|
}
|
2023-04-19 05:20:02 +08:00
|
|
|
|
|
|
|
}
|