]> BookStack Code Mirror - bookstack/blob - resources/js/components/attachments-list.js
Finished updating remainder of JS components to new system
[bookstack] / resources / js / components / attachments-list.js
1 import {Component} from "./component";
2
3 /**
4  * Attachments List
5  * Adds '?open=true' query to file attachment links
6  * when ctrl/cmd is pressed down.
7  */
8 export class AttachmentsList extends Component {
9
10     setup() {
11         this.container = this.$el;
12         this.setupListeners();
13     }
14
15     setupListeners() {
16         const isExpectedKey = (event) => event.key === 'Control' || event.key === 'Meta';
17         window.addEventListener('keydown', event => {
18              if (isExpectedKey(event)) {
19                 this.addOpenQueryToLinks();
20              }
21         }, {passive: true});
22         window.addEventListener('keyup', event => {
23             if (isExpectedKey(event)) {
24                 this.removeOpenQueryFromLinks();
25             }
26         }, {passive: true});
27     }
28
29     addOpenQueryToLinks() {
30         const links = this.container.querySelectorAll('a.attachment-file');
31         for (const link of links) {
32             if (link.href.split('?')[1] !== 'open=true') {
33                 link.href = link.href + '?open=true';
34                 link.setAttribute('target', '_blank');
35             }
36         }
37     }
38
39     removeOpenQueryFromLinks() {
40         const links = this.container.querySelectorAll('a.attachment-file');
41         for (const link of links) {
42             link.href = link.href.split('?')[0];
43             link.removeAttribute('target');
44         }
45     }
46 }