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