1 import * as DOM from "../services/dom";
3 class TemplateManager {
7 this.list = elem.querySelector('[template-manager-list]');
8 this.searching = false;
10 // Template insert action buttons
11 DOM.onChildEvent(this.elem, '[template-action]', 'click', this.handleTemplateActionClick.bind(this));
13 // Template list pagination click
14 DOM.onChildEvent(this.elem, '.pagination a', 'click', this.handlePaginationClick.bind(this));
16 // Template list item content click
17 DOM.onChildEvent(this.elem, '.template-item-content', 'click', this.handleTemplateItemClick.bind(this));
19 this.setupSearchBox();
22 handleTemplateItemClick(event, templateItem) {
23 const templateId = templateItem.closest('[template-id]').getAttribute('template-id');
24 this.insertTemplate(templateId, 'replace');
27 handleTemplateActionClick(event, actionButton) {
28 event.stopPropagation();
30 const action = actionButton.getAttribute('template-action');
31 const templateId = actionButton.closest('[template-id]').getAttribute('template-id');
32 this.insertTemplate(templateId, action);
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);
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;
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');
54 async function performSearch() {
55 const searchTerm = input.value;
56 const resp = await window.$http.get(`/templates`, {
59 cancelButton.style.display = searchTerm ? 'block' : 'none';
60 this.list.innerHTML = resp.data;
62 performSearch = performSearch.bind(this);
64 // Searchbox enter press
65 searchBox.addEventListener('keypress', event => {
66 if (event.key === 'Enter') {
67 event.preventDefault();
72 // Submit button press
73 submitButton.addEventListener('click', event => {
77 // Cancel button press
78 cancelButton.addEventListener('click', event => {
85 export default TemplateManager;