2023-04-18 23:20:02 +02:00
|
|
|
import * as DOM from '../services/dom';
|
|
|
|
import {Component} from './component';
|
2019-08-11 21:04:43 +02:00
|
|
|
|
2022-11-16 14:04:22 +01:00
|
|
|
export class TemplateManager extends Component {
|
2019-08-11 21:04:43 +02:00
|
|
|
|
2022-11-16 14:04:22 +01:00
|
|
|
setup() {
|
|
|
|
this.container = this.$el;
|
|
|
|
this.list = this.$refs.list;
|
2019-08-11 21:04:43 +02:00
|
|
|
|
2022-11-16 14:04:22 +01:00
|
|
|
this.searchInput = this.$refs.searchInput;
|
|
|
|
this.searchButton = this.$refs.searchButton;
|
|
|
|
this.searchCancel = this.$refs.searchCancel;
|
|
|
|
|
|
|
|
this.setupListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
setupListeners() {
|
2019-08-11 21:04:43 +02:00
|
|
|
// Template insert action buttons
|
2022-11-16 14:04:22 +01:00
|
|
|
DOM.onChildEvent(this.container, '[template-action]', 'click', this.handleTemplateActionClick.bind(this));
|
2019-08-11 21:04:43 +02:00
|
|
|
|
|
|
|
// Template list pagination click
|
2022-11-16 14:04:22 +01:00
|
|
|
DOM.onChildEvent(this.container, '.pagination a', 'click', this.handlePaginationClick.bind(this));
|
2019-08-11 21:04:43 +02:00
|
|
|
|
|
|
|
// Template list item content click
|
2022-11-16 14:04:22 +01:00
|
|
|
DOM.onChildEvent(this.container, '.template-item-content', 'click', this.handleTemplateItemClick.bind(this));
|
2019-08-11 21:04:43 +02:00
|
|
|
|
2019-08-26 16:34:03 +02:00
|
|
|
// Template list item drag start
|
2022-11-16 14:04:22 +01:00
|
|
|
DOM.onChildEvent(this.container, '.template-item', 'dragstart', this.handleTemplateItemDragStart.bind(this));
|
2019-08-26 16:34:03 +02:00
|
|
|
|
2022-11-16 14:04:22 +01:00
|
|
|
// Search box enter press
|
|
|
|
this.searchInput.addEventListener('keypress', event => {
|
|
|
|
if (event.key === 'Enter') {
|
|
|
|
event.preventDefault();
|
|
|
|
this.performSearch();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Search submit button press
|
2023-04-19 16:20:04 +02:00
|
|
|
this.searchButton.addEventListener('click', () => this.performSearch());
|
2022-11-16 14:04:22 +01:00
|
|
|
|
|
|
|
// Search cancel button press
|
2023-04-19 16:20:04 +02:00
|
|
|
this.searchCancel.addEventListener('click', () => {
|
2022-11-16 14:04:22 +01:00
|
|
|
this.searchInput.value = '';
|
|
|
|
this.performSearch();
|
|
|
|
});
|
2019-08-11 21:04:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
handleTemplateItemClick(event, templateItem) {
|
|
|
|
const templateId = templateItem.closest('[template-id]').getAttribute('template-id');
|
|
|
|
this.insertTemplate(templateId, 'replace');
|
2019-08-26 16:34:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
handleTemplateItemDragStart(event, templateItem) {
|
|
|
|
const templateId = templateItem.closest('[template-id]').getAttribute('template-id');
|
|
|
|
event.dataTransfer.setData('bookstack/template', templateId);
|
|
|
|
event.dataTransfer.setData('text/plain', templateId);
|
2019-08-11 21:04:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
handleTemplateActionClick(event, actionButton) {
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
|
|
const action = actionButton.getAttribute('template-action');
|
|
|
|
const templateId = actionButton.closest('[template-id]').getAttribute('template-id');
|
|
|
|
this.insertTemplate(templateId, action);
|
|
|
|
}
|
|
|
|
|
|
|
|
async insertTemplate(templateId, action = 'replace') {
|
|
|
|
const resp = await window.$http.get(`/templates/${templateId}`);
|
2023-04-18 23:20:02 +02:00
|
|
|
const eventName = `editor::${action}`;
|
2019-08-11 21:04:43 +02:00
|
|
|
window.$events.emit(eventName, resp.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
async handlePaginationClick(event, paginationLink) {
|
|
|
|
event.preventDefault();
|
|
|
|
const paginationUrl = paginationLink.getAttribute('href');
|
|
|
|
const resp = await window.$http.get(paginationUrl);
|
|
|
|
this.list.innerHTML = resp.data;
|
|
|
|
}
|
|
|
|
|
2022-11-16 14:04:22 +01:00
|
|
|
async performSearch() {
|
|
|
|
const searchTerm = this.searchInput.value;
|
2023-04-18 23:20:02 +02:00
|
|
|
const resp = await window.$http.get('/templates', {
|
|
|
|
search: searchTerm,
|
2019-08-11 21:04:43 +02:00
|
|
|
});
|
2022-11-16 14:04:22 +01:00
|
|
|
this.searchCancel.style.display = searchTerm ? 'block' : 'none';
|
|
|
|
this.list.innerHTML = resp.data;
|
2019-08-11 21:04:43 +02:00
|
|
|
}
|
2023-04-18 23:20:02 +02:00
|
|
|
|
|
|
|
}
|