2023-04-18 17:20:02 -04:00
|
|
|
import Sortable, {MultiDrag} from 'sortablejs';
|
|
|
|
import {Component} from './component';
|
|
|
|
import {htmlToDom} from '../services/dom';
|
2019-06-06 08:09:58 -04:00
|
|
|
|
|
|
|
// Auto sort control
|
|
|
|
const sortOperations = {
|
2023-04-18 17:20:02 -04:00
|
|
|
name(a, b) {
|
2019-06-06 08:09:58 -04:00
|
|
|
const aName = a.getAttribute('data-name').trim().toLowerCase();
|
|
|
|
const bName = b.getAttribute('data-name').trim().toLowerCase();
|
|
|
|
return aName.localeCompare(bName);
|
|
|
|
},
|
2023-04-18 17:20:02 -04:00
|
|
|
created(a, b) {
|
2019-06-06 08:09:58 -04:00
|
|
|
const aTime = Number(a.getAttribute('data-created'));
|
|
|
|
const bTime = Number(b.getAttribute('data-created'));
|
|
|
|
return bTime - aTime;
|
|
|
|
},
|
2023-04-18 17:20:02 -04:00
|
|
|
updated(a, b) {
|
2019-06-06 08:09:58 -04:00
|
|
|
const aTime = Number(a.getAttribute('data-updated'));
|
|
|
|
const bTime = Number(b.getAttribute('data-updated'));
|
|
|
|
return bTime - aTime;
|
|
|
|
},
|
2023-04-18 17:20:02 -04:00
|
|
|
chaptersFirst(a, b) {
|
2019-06-06 08:09:58 -04:00
|
|
|
const aType = a.getAttribute('data-type');
|
|
|
|
const bType = b.getAttribute('data-type');
|
|
|
|
if (aType === bType) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return (aType === 'chapter' ? -1 : 1);
|
|
|
|
},
|
2023-04-18 17:20:02 -04:00
|
|
|
chaptersLast(a, b) {
|
2019-06-06 08:09:58 -04:00
|
|
|
const aType = a.getAttribute('data-type');
|
|
|
|
const bType = b.getAttribute('data-type');
|
|
|
|
if (aType === bType) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return (aType === 'chapter' ? 1 : -1);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-01-27 08:08:35 -05:00
|
|
|
/**
|
|
|
|
* The available move actions.
|
|
|
|
* The active function indicates if the action is possible for the given item.
|
|
|
|
* The run function performs the move.
|
|
|
|
* @type {{up: {active(Element, ?Element, Element): boolean, run(Element, ?Element, Element)}}}
|
|
|
|
*/
|
|
|
|
const moveActions = {
|
|
|
|
up: {
|
2023-04-19 10:20:04 -04:00
|
|
|
active(elem, parent) {
|
2023-01-27 08:08:35 -05:00
|
|
|
return !(elem.previousElementSibling === null && !parent);
|
|
|
|
},
|
2023-04-19 10:20:04 -04:00
|
|
|
run(elem, parent) {
|
2023-01-27 08:08:35 -05:00
|
|
|
const newSibling = elem.previousElementSibling || parent;
|
|
|
|
newSibling.insertAdjacentElement('beforebegin', elem);
|
2023-04-18 17:20:02 -04:00
|
|
|
},
|
2023-01-27 08:08:35 -05:00
|
|
|
},
|
|
|
|
down: {
|
2023-04-19 10:20:04 -04:00
|
|
|
active(elem, parent) {
|
2023-01-27 08:08:35 -05:00
|
|
|
return !(elem.nextElementSibling === null && !parent);
|
|
|
|
},
|
2023-04-19 10:20:04 -04:00
|
|
|
run(elem, parent) {
|
2023-01-27 08:08:35 -05:00
|
|
|
const newSibling = elem.nextElementSibling || parent;
|
|
|
|
newSibling.insertAdjacentElement('afterend', elem);
|
2023-04-18 17:20:02 -04:00
|
|
|
},
|
2023-01-27 08:08:35 -05:00
|
|
|
},
|
|
|
|
next_book: {
|
|
|
|
active(elem, parent, book) {
|
|
|
|
return book.nextElementSibling !== null;
|
|
|
|
},
|
|
|
|
run(elem, parent, book) {
|
|
|
|
const newList = book.nextElementSibling.querySelector('ul');
|
|
|
|
newList.prepend(elem);
|
2023-04-18 17:20:02 -04:00
|
|
|
},
|
2023-01-27 08:08:35 -05:00
|
|
|
},
|
|
|
|
prev_book: {
|
|
|
|
active(elem, parent, book) {
|
|
|
|
return book.previousElementSibling !== null;
|
|
|
|
},
|
|
|
|
run(elem, parent, book) {
|
|
|
|
const newList = book.previousElementSibling.querySelector('ul');
|
|
|
|
newList.appendChild(elem);
|
2023-04-18 17:20:02 -04:00
|
|
|
},
|
2023-01-27 08:08:35 -05:00
|
|
|
},
|
|
|
|
next_chapter: {
|
2023-04-19 10:20:04 -04:00
|
|
|
active(elem, parent) {
|
2023-01-27 08:08:35 -05:00
|
|
|
return elem.dataset.type === 'page' && this.getNextChapter(elem, parent);
|
|
|
|
},
|
2023-04-19 10:20:04 -04:00
|
|
|
run(elem, parent) {
|
2023-01-27 08:08:35 -05:00
|
|
|
const nextChapter = this.getNextChapter(elem, parent);
|
|
|
|
nextChapter.querySelector('ul').prepend(elem);
|
|
|
|
},
|
|
|
|
getNextChapter(elem, parent) {
|
|
|
|
const topLevel = (parent || elem);
|
|
|
|
const topItems = Array.from(topLevel.parentElement.children);
|
|
|
|
const index = topItems.indexOf(topLevel);
|
2023-04-19 10:20:04 -04:00
|
|
|
return topItems.slice(index + 1).find(item => item.dataset.type === 'chapter');
|
2023-04-18 17:20:02 -04:00
|
|
|
},
|
2023-01-27 08:08:35 -05:00
|
|
|
},
|
|
|
|
prev_chapter: {
|
2023-04-19 10:20:04 -04:00
|
|
|
active(elem, parent) {
|
2023-01-27 08:08:35 -05:00
|
|
|
return elem.dataset.type === 'page' && this.getPrevChapter(elem, parent);
|
|
|
|
},
|
2023-04-19 10:20:04 -04:00
|
|
|
run(elem, parent) {
|
2023-01-27 08:08:35 -05:00
|
|
|
const prevChapter = this.getPrevChapter(elem, parent);
|
|
|
|
prevChapter.querySelector('ul').append(elem);
|
|
|
|
},
|
|
|
|
getPrevChapter(elem, parent) {
|
|
|
|
const topLevel = (parent || elem);
|
|
|
|
const topItems = Array.from(topLevel.parentElement.children);
|
|
|
|
const index = topItems.indexOf(topLevel);
|
2023-04-19 10:20:04 -04:00
|
|
|
return topItems.slice(0, index).reverse().find(item => item.dataset.type === 'chapter');
|
2023-04-18 17:20:02 -04:00
|
|
|
},
|
2023-01-27 08:08:35 -05:00
|
|
|
},
|
|
|
|
book_end: {
|
2023-04-19 10:20:04 -04:00
|
|
|
active(elem, parent) {
|
2023-01-27 08:08:35 -05:00
|
|
|
return parent || (parent === null && elem.nextElementSibling);
|
|
|
|
},
|
|
|
|
run(elem, parent, book) {
|
|
|
|
book.querySelector('ul').append(elem);
|
2023-04-18 17:20:02 -04:00
|
|
|
},
|
2023-01-27 08:08:35 -05:00
|
|
|
},
|
|
|
|
book_start: {
|
2023-04-19 10:20:04 -04:00
|
|
|
active(elem, parent) {
|
2023-01-27 08:08:35 -05:00
|
|
|
return parent || (parent === null && elem.previousElementSibling);
|
|
|
|
},
|
|
|
|
run(elem, parent, book) {
|
|
|
|
book.querySelector('ul').prepend(elem);
|
2023-04-18 17:20:02 -04:00
|
|
|
},
|
2023-01-27 08:08:35 -05:00
|
|
|
},
|
|
|
|
before_chapter: {
|
2023-04-19 10:20:04 -04:00
|
|
|
active(elem, parent) {
|
2023-01-27 08:08:35 -05:00
|
|
|
return parent;
|
|
|
|
},
|
2023-04-19 10:20:04 -04:00
|
|
|
run(elem, parent) {
|
2023-01-27 08:08:35 -05:00
|
|
|
parent.insertAdjacentElement('beforebegin', elem);
|
2023-04-18 17:20:02 -04:00
|
|
|
},
|
2023-01-27 08:08:35 -05:00
|
|
|
},
|
|
|
|
after_chapter: {
|
2023-04-19 10:20:04 -04:00
|
|
|
active(elem, parent) {
|
2023-01-27 08:08:35 -05:00
|
|
|
return parent;
|
|
|
|
},
|
2023-04-19 10:20:04 -04:00
|
|
|
run(elem, parent) {
|
2023-01-27 08:08:35 -05:00
|
|
|
parent.insertAdjacentElement('afterend', elem);
|
2023-04-18 17:20:02 -04:00
|
|
|
},
|
2023-01-27 08:08:35 -05:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-11-15 06:24:31 -05:00
|
|
|
export class BookSort extends Component {
|
2019-06-06 08:09:58 -04:00
|
|
|
|
2022-11-15 06:24:31 -05:00
|
|
|
setup() {
|
|
|
|
this.container = this.$el;
|
|
|
|
this.sortContainer = this.$refs.sortContainer;
|
|
|
|
this.input = this.$refs.input;
|
2019-06-06 08:09:58 -04:00
|
|
|
|
2023-01-27 06:16:17 -05:00
|
|
|
Sortable.mount(new MultiDrag());
|
|
|
|
|
2022-11-15 06:24:31 -05:00
|
|
|
const initialSortBox = this.container.querySelector('.sort-box');
|
2019-06-06 08:09:58 -04:00
|
|
|
this.setupBookSortable(initialSortBox);
|
|
|
|
this.setupSortPresets();
|
2023-01-27 08:08:35 -05:00
|
|
|
this.setupMoveActions();
|
2019-06-06 08:09:58 -04:00
|
|
|
|
2023-01-27 12:06:39 -05:00
|
|
|
window.$events.listen('entity-select-change', this.bookSelect.bind(this));
|
2019-06-06 08:09:58 -04:00
|
|
|
}
|
|
|
|
|
2023-01-27 08:08:35 -05:00
|
|
|
/**
|
2023-01-27 11:25:06 -05:00
|
|
|
* Set up the handlers for the item-level move buttons.
|
2023-01-27 08:08:35 -05:00
|
|
|
*/
|
|
|
|
setupMoveActions() {
|
|
|
|
// Handle move button click
|
|
|
|
this.container.addEventListener('click', event => {
|
|
|
|
if (event.target.matches('[data-move]')) {
|
|
|
|
const action = event.target.getAttribute('data-move');
|
|
|
|
const sortItem = event.target.closest('[data-id]');
|
|
|
|
this.runSortAction(sortItem, action);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.updateMoveActionStateForAll();
|
|
|
|
}
|
|
|
|
|
2019-06-06 08:09:58 -04:00
|
|
|
/**
|
2023-01-27 11:25:06 -05:00
|
|
|
* Set up the handlers for the preset sort type buttons.
|
2019-06-06 08:09:58 -04:00
|
|
|
*/
|
|
|
|
setupSortPresets() {
|
|
|
|
let lastSort = '';
|
|
|
|
let reverse = false;
|
|
|
|
const reversibleTypes = ['name', 'created', 'updated'];
|
|
|
|
|
|
|
|
this.sortContainer.addEventListener('click', event => {
|
|
|
|
const sortButton = event.target.closest('.sort-box-options [data-sort]');
|
|
|
|
if (!sortButton) return;
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
const sortLists = sortButton.closest('.sort-box').querySelectorAll('ul');
|
|
|
|
const sort = sortButton.getAttribute('data-sort');
|
|
|
|
|
|
|
|
reverse = (lastSort === sort) ? !reverse : false;
|
|
|
|
let sortFunction = sortOperations[sort];
|
|
|
|
if (reverse && reversibleTypes.includes(sort)) {
|
2023-04-19 10:20:04 -04:00
|
|
|
sortFunction = function reverseSortOperation(a, b) {
|
2023-04-18 17:20:02 -04:00
|
|
|
return 0 - sortOperations[sort](a, b);
|
2019-06-06 08:09:58 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-04-18 17:20:02 -04:00
|
|
|
for (const list of sortLists) {
|
2019-06-06 08:09:58 -04:00
|
|
|
const directItems = Array.from(list.children).filter(child => child.matches('li'));
|
|
|
|
directItems.sort(sortFunction).forEach(sortedItem => {
|
|
|
|
list.appendChild(sortedItem);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
lastSort = sort;
|
|
|
|
this.updateMapInput();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle book selection from the entity selector.
|
|
|
|
* @param {Object} entityInfo
|
|
|
|
*/
|
|
|
|
bookSelect(entityInfo) {
|
2022-11-15 06:24:31 -05:00
|
|
|
const alreadyAdded = this.container.querySelector(`[data-type="book"][data-id="${entityInfo.id}"]`) !== null;
|
2019-06-06 08:09:58 -04:00
|
|
|
if (alreadyAdded) return;
|
|
|
|
|
2023-04-18 17:20:02 -04:00
|
|
|
const entitySortItemUrl = `${entityInfo.link}/sort-item`;
|
2019-06-06 08:09:58 -04:00
|
|
|
window.$http.get(entitySortItemUrl).then(resp => {
|
2022-11-15 06:24:31 -05:00
|
|
|
const newBookContainer = htmlToDom(resp.data);
|
2019-06-06 08:09:58 -04:00
|
|
|
this.sortContainer.append(newBookContainer);
|
|
|
|
this.setupBookSortable(newBookContainer);
|
2023-01-27 08:08:35 -05:00
|
|
|
this.updateMoveActionStateForAll();
|
2023-01-27 11:25:06 -05:00
|
|
|
|
|
|
|
const summary = newBookContainer.querySelector('summary');
|
|
|
|
summary.focus();
|
2019-06-06 08:09:58 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-27 06:16:17 -05:00
|
|
|
* Set up the given book container element to have sortable items.
|
2019-06-06 08:09:58 -04:00
|
|
|
* @param {Element} bookContainer
|
|
|
|
*/
|
|
|
|
setupBookSortable(bookContainer) {
|
2023-01-27 11:25:06 -05:00
|
|
|
const sortElems = Array.from(bookContainer.querySelectorAll('.sort-list, .sortable-page-sublist'));
|
2019-06-06 08:09:58 -04:00
|
|
|
|
|
|
|
const bookGroupConfig = {
|
|
|
|
name: 'book',
|
|
|
|
pull: ['book', 'chapter'],
|
|
|
|
put: ['book', 'chapter'],
|
|
|
|
};
|
|
|
|
|
|
|
|
const chapterGroupConfig = {
|
|
|
|
name: 'chapter',
|
|
|
|
pull: ['book', 'chapter'],
|
2023-04-18 17:20:02 -04:00
|
|
|
put(toList, fromList, draggedElem) {
|
2019-06-06 08:09:58 -04:00
|
|
|
return draggedElem.getAttribute('data-type') === 'page';
|
2023-04-18 17:20:02 -04:00
|
|
|
},
|
2019-06-06 08:09:58 -04:00
|
|
|
};
|
|
|
|
|
2023-01-27 06:16:17 -05:00
|
|
|
for (const sortElem of sortElems) {
|
|
|
|
Sortable.create(sortElem, {
|
2019-06-06 08:09:58 -04:00
|
|
|
group: sortElem.classList.contains('sort-list') ? bookGroupConfig : chapterGroupConfig,
|
|
|
|
animation: 150,
|
|
|
|
fallbackOnBody: true,
|
|
|
|
swapThreshold: 0.65,
|
2023-04-19 10:20:04 -04:00
|
|
|
onSort: () => {
|
2023-04-18 17:20:02 -04:00
|
|
|
this.ensureNoNestedChapters();
|
2023-01-27 12:06:39 -05:00
|
|
|
this.updateMapInput();
|
|
|
|
this.updateMoveActionStateForAll();
|
|
|
|
},
|
2019-06-06 08:09:58 -04:00
|
|
|
dragClass: 'bg-white',
|
|
|
|
ghostClass: 'primary-background-light',
|
2020-04-27 11:53:27 -04:00
|
|
|
multiDrag: true,
|
2023-01-27 06:16:17 -05:00
|
|
|
multiDragKey: 'Control',
|
2020-04-27 11:53:27 -04:00
|
|
|
selectedClass: 'sortable-selected',
|
2019-06-06 08:09:58 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-27 12:39:51 -05:00
|
|
|
/**
|
|
|
|
* Handle nested chapters by moving them to the parent book.
|
|
|
|
* Needed since sorting with multi-sort only checks group rules based on the active item,
|
|
|
|
* not all in group, therefore need to manually check after a sort.
|
|
|
|
* Must be done before updating the map input.
|
|
|
|
*/
|
|
|
|
ensureNoNestedChapters() {
|
|
|
|
const nestedChapters = this.container.querySelectorAll('[data-type="chapter"] [data-type="chapter"]');
|
|
|
|
for (const chapter of nestedChapters) {
|
|
|
|
const parentChapter = chapter.parentElement.closest('[data-type="chapter"]');
|
|
|
|
parentChapter.insertAdjacentElement('afterend', chapter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-06 08:09:58 -04:00
|
|
|
/**
|
|
|
|
* Update the input with our sort data.
|
|
|
|
*/
|
|
|
|
updateMapInput() {
|
|
|
|
const pageMap = this.buildEntityMap();
|
|
|
|
this.input.value = JSON.stringify(pageMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build up a mapping of entities with their ordering and nesting.
|
|
|
|
* @returns {Array}
|
|
|
|
*/
|
|
|
|
buildEntityMap() {
|
|
|
|
const entityMap = [];
|
2022-11-15 06:24:31 -05:00
|
|
|
const lists = this.container.querySelectorAll('.sort-list');
|
2019-06-06 08:09:58 -04:00
|
|
|
|
2023-04-18 17:20:02 -04:00
|
|
|
for (const list of lists) {
|
2019-06-06 08:09:58 -04:00
|
|
|
const bookId = list.closest('[data-type="book"]').getAttribute('data-id');
|
|
|
|
const directChildren = Array.from(list.children)
|
|
|
|
.filter(elem => elem.matches('[data-type="page"], [data-type="chapter"]'));
|
|
|
|
for (let i = 0; i < directChildren.length; i++) {
|
|
|
|
this.addBookChildToMap(directChildren[i], i, bookId, entityMap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return entityMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a sort item and add it to a data-map array.
|
|
|
|
* Parses sub0items if existing also.
|
|
|
|
* @param {Element} childElem
|
|
|
|
* @param {Number} index
|
|
|
|
* @param {Number} bookId
|
|
|
|
* @param {Array} entityMap
|
|
|
|
*/
|
|
|
|
addBookChildToMap(childElem, index, bookId, entityMap) {
|
|
|
|
const type = childElem.getAttribute('data-type');
|
|
|
|
const parentChapter = false;
|
|
|
|
const childId = childElem.getAttribute('data-id');
|
|
|
|
|
|
|
|
entityMap.push({
|
|
|
|
id: childId,
|
|
|
|
sort: index,
|
2023-04-18 17:20:02 -04:00
|
|
|
parentChapter,
|
|
|
|
type,
|
|
|
|
book: bookId,
|
2019-06-06 08:09:58 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
const subPages = childElem.querySelectorAll('[data-type="page"]');
|
|
|
|
for (let i = 0; i < subPages.length; i++) {
|
|
|
|
entityMap.push({
|
|
|
|
id: subPages[i].getAttribute('data-id'),
|
|
|
|
sort: i,
|
|
|
|
parentChapter: childId,
|
|
|
|
type: 'page',
|
2023-04-18 17:20:02 -04:00
|
|
|
book: bookId,
|
2019-06-06 08:09:58 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-27 08:08:35 -05:00
|
|
|
/**
|
|
|
|
* Run the given sort action up the provided sort item.
|
|
|
|
* @param {Element} item
|
|
|
|
* @param {String} action
|
|
|
|
*/
|
|
|
|
runSortAction(item, action) {
|
|
|
|
const parentItem = item.parentElement.closest('li[data-id]');
|
|
|
|
const parentBook = item.parentElement.closest('[data-type="book"]');
|
|
|
|
moveActions[action].run(item, parentItem, parentBook);
|
|
|
|
this.updateMapInput();
|
|
|
|
this.updateMoveActionStateForAll();
|
|
|
|
item.scrollIntoView({behavior: 'smooth', block: 'nearest'});
|
|
|
|
item.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the state of the available move actions on this item.
|
|
|
|
* @param {Element} item
|
|
|
|
*/
|
|
|
|
updateMoveActionState(item) {
|
|
|
|
const parentItem = item.parentElement.closest('li[data-id]');
|
|
|
|
const parentBook = item.parentElement.closest('[data-type="book"]');
|
|
|
|
for (const [action, functions] of Object.entries(moveActions)) {
|
|
|
|
const moveButton = item.querySelector(`[data-move="${action}"]`);
|
|
|
|
moveButton.disabled = !functions.active(item, parentItem, parentBook);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updateMoveActionStateForAll() {
|
|
|
|
const items = this.container.querySelectorAll('[data-type="chapter"],[data-type="page"]');
|
|
|
|
for (const item of items) {
|
|
|
|
this.updateMoveActionState(item);
|
|
|
|
}
|
|
|
|
}
|
2023-04-18 17:20:02 -04:00
|
|
|
|
|
|
|
}
|