]> BookStack Code Mirror - bookstack/blob - resources/assets/js/components/template-manager.js
Fixed inline code overflowing off of page in issue #1575.
[bookstack] / resources / assets / js / components / template-manager.js
1 import * as DOM from "../services/dom";
2
3 class TemplateManager {
4
5     constructor(elem) {
6         this.elem = elem;
7         this.list = elem.querySelector('[template-manager-list]');
8         this.searching = false;
9
10         // Template insert action buttons
11         DOM.onChildEvent(this.elem, '[template-action]', 'click', this.handleTemplateActionClick.bind(this));
12
13         // Template list pagination click
14         DOM.onChildEvent(this.elem, '.pagination a', 'click', this.handlePaginationClick.bind(this));
15
16         // Template list item content click
17         DOM.onChildEvent(this.elem, '.template-item-content', 'click', this.handleTemplateItemClick.bind(this));
18
19         this.setupSearchBox();
20     }
21
22     handleTemplateItemClick(event, templateItem) {
23         const templateId = templateItem.closest('[template-id]').getAttribute('template-id');
24         this.insertTemplate(templateId, 'replace');
25     }
26
27     handleTemplateActionClick(event, actionButton) {
28         event.stopPropagation();
29
30         const action = actionButton.getAttribute('template-action');
31         const templateId = actionButton.closest('[template-id]').getAttribute('template-id');
32         this.insertTemplate(templateId, action);
33     }
34
35     async insertTemplate(templateId, action = 'replace') {
36         const resp = await window.$http.get(`/templates/${templateId}`);
37         const eventName = 'editor::' + action;
38         window.$events.emit(eventName, resp.data);
39     }
40
41     async handlePaginationClick(event, paginationLink) {
42         event.preventDefault();
43         const paginationUrl = paginationLink.getAttribute('href');
44         const resp = await window.$http.get(paginationUrl);
45         this.list.innerHTML = resp.data;
46     }
47
48     setupSearchBox() {
49         const searchBox = this.elem.querySelector('.search-box');
50         const input = searchBox.querySelector('input');
51         const submitButton = searchBox.querySelector('button');
52         const cancelButton = searchBox.querySelector('button.search-box-cancel');
53
54         async function performSearch() {
55             const searchTerm = input.value;
56             const resp = await window.$http.get(`/templates`, {
57                 search: searchTerm
58             });
59             cancelButton.style.display = searchTerm ? 'block' : 'none';
60             this.list.innerHTML = resp.data;
61         }
62         performSearch = performSearch.bind(this);
63
64         // Searchbox enter press
65         searchBox.addEventListener('keypress', event => {
66             if (event.key === 'Enter') {
67                 event.preventDefault();
68                 performSearch();
69             }
70         });
71
72         // Submit button press
73         submitButton.addEventListener('click', event => {
74             performSearch();
75         });
76
77         // Cancel button press
78         cancelButton.addEventListener('click', event => {
79             input.value = '';
80             performSearch();
81         });
82     }
83 }
84
85 export default TemplateManager;