2019-08-12 03:04:43 +08:00
|
|
|
import * as DOM from "../services/dom";
|
|
|
|
|
|
|
|
class TemplateManager {
|
|
|
|
|
|
|
|
constructor(elem) {
|
|
|
|
this.elem = elem;
|
|
|
|
this.list = elem.querySelector('[template-manager-list]');
|
|
|
|
this.searching = false;
|
|
|
|
|
|
|
|
// Template insert action buttons
|
|
|
|
DOM.onChildEvent(this.elem, '[template-action]', 'click', this.handleTemplateActionClick.bind(this));
|
|
|
|
|
|
|
|
// Template list pagination click
|
|
|
|
DOM.onChildEvent(this.elem, '.pagination a', 'click', this.handlePaginationClick.bind(this));
|
|
|
|
|
|
|
|
// Template list item content click
|
|
|
|
DOM.onChildEvent(this.elem, '.template-item-content', 'click', this.handleTemplateItemClick.bind(this));
|
|
|
|
|
2019-08-26 22:34:03 +08:00
|
|
|
// Template list item drag start
|
|
|
|
DOM.onChildEvent(this.elem, '.template-item', 'dragstart', this.handleTemplateItemDragStart.bind(this));
|
|
|
|
|
2019-08-12 03:04:43 +08:00
|
|
|
this.setupSearchBox();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleTemplateItemClick(event, templateItem) {
|
|
|
|
const templateId = templateItem.closest('[template-id]').getAttribute('template-id');
|
|
|
|
this.insertTemplate(templateId, 'replace');
|
2019-08-26 22:34:03 +08: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-12 03:04:43 +08: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}`);
|
|
|
|
const eventName = 'editor::' + action;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
setupSearchBox() {
|
|
|
|
const searchBox = this.elem.querySelector('.search-box');
|
2020-04-04 07:09:58 +08:00
|
|
|
|
|
|
|
// Search box may not exist if there are no existing templates in the system.
|
|
|
|
if (!searchBox) return;
|
|
|
|
|
2019-08-12 03:04:43 +08:00
|
|
|
const input = searchBox.querySelector('input');
|
|
|
|
const submitButton = searchBox.querySelector('button');
|
|
|
|
const cancelButton = searchBox.querySelector('button.search-box-cancel');
|
|
|
|
|
|
|
|
async function performSearch() {
|
|
|
|
const searchTerm = input.value;
|
|
|
|
const resp = await window.$http.get(`/templates`, {
|
|
|
|
search: searchTerm
|
|
|
|
});
|
|
|
|
cancelButton.style.display = searchTerm ? 'block' : 'none';
|
|
|
|
this.list.innerHTML = resp.data;
|
|
|
|
}
|
|
|
|
performSearch = performSearch.bind(this);
|
|
|
|
|
2020-04-04 07:09:58 +08:00
|
|
|
// Search box enter press
|
2019-08-12 03:04:43 +08:00
|
|
|
searchBox.addEventListener('keypress', event => {
|
|
|
|
if (event.key === 'Enter') {
|
|
|
|
event.preventDefault();
|
|
|
|
performSearch();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Submit button press
|
|
|
|
submitButton.addEventListener('click', event => {
|
|
|
|
performSearch();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Cancel button press
|
|
|
|
cancelButton.addEventListener('click', event => {
|
|
|
|
input.value = '';
|
|
|
|
performSearch();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default TemplateManager;
|