1 @extends('simple-layout')
5 <div class="container">
8 @include('partials.breadcrumbs', ['crumbs' => [
10 $book->getUrl('/sort') => [
11 'text' => trans('entities.books_sort'),
17 <div class="grid left-focus gap-xl">
19 <div class="card content-wrap">
20 <h1 class="list-heading mb-l">{{ trans('entities.books_sort') }}</h1>
22 @include('books.sort-box', ['book' => $book, 'bookChildren' => $bookChildren])
25 <form action="{{ $book->getUrl('/sort') }}" method="POST">
27 <input type="hidden" name="_method" value="PUT">
28 <input type="hidden" id="sort-tree-input" name="sort-tree">
29 <div class="list text-right">
30 <a href="{{ $book->getUrl() }}" class="button outline">{{ trans('common.cancel') }}</a>
31 <button class="button primary" type="submit">{{ trans('entities.books_sort_save') }}</button>
38 <div class="card content-wrap">
39 <h2 class="list-heading mb-m">{{ trans('entities.books_sort_show_other') }}</h2>
41 @include('components.entity-selector', ['name' => 'books_list', 'selectorSize' => 'compact', 'entityTypes' => 'book', 'entityPermission' => 'update', 'showAdd' => true])
52 <script src="{{ baseUrl("/libs/jquery-sortable/jquery-sortable.min.js") }}"></script>
54 $(document).ready(function() {
56 const $container = $('#sort-boxes');
59 const sortableOptions = {
60 group: 'serialization',
61 containerSelector: 'ul',
64 onDrop: function ($item, container, _super) {
66 _super($item, container);
68 isValidTarget: function ($item, container) {
69 // Prevent nested chapters
70 return !($item.is('[data-type="chapter"]') && container.target.closest('li').attr('data-type') === 'chapter');
74 // Create our sortable group
75 let group = $('.sort-list').sortable(sortableOptions);
77 // Add book on selection confirm
78 window.$events.listen('entity-select-confirm', function(entityInfo) {
79 const alreadyAdded = $container.find(`[data-type="book"][data-id="${entityInfo.id}"]`).length > 0;
80 if (alreadyAdded) return;
82 const entitySortItemUrl = entityInfo.link + '/sort-item';
83 window.$http.get(entitySortItemUrl).then(resp => {
84 $container.append(resp.data);
85 group.sortable("destroy");
86 group = $('.sort-list').sortable(sortableOptions);
91 * Update the input with our sort data.
93 function updateMapInput() {
94 const pageMap = buildEntityMap();
95 $('#sort-tree-input').val(JSON.stringify(pageMap));
99 * Build up a mapping of entities with their ordering and nesting.
102 function buildEntityMap() {
103 const entityMap = [];
104 const $lists = $('.sort-list');
105 $lists.each(function(listIndex) {
106 const $list = $(this);
107 const bookId = $list.closest('[data-type="book"]').attr('data-id');
108 const $directChildren = $list.find('> [data-type="page"], > [data-type="chapter"]');
109 $directChildren.each(function(directChildIndex) {
110 const $childElem = $(this);
111 const type = $childElem.attr('data-type');
112 const parentChapter = false;
113 const childId = $childElem.attr('data-id');
117 sort: directChildIndex,
118 parentChapter: parentChapter,
123 $childElem.find('[data-type="page"]').each(function(pageIndex) {
124 const $chapterChild = $(this);
126 id: $chapterChild.attr('data-id'),
128 parentChapter: childId,
141 const sortOperations = {
142 name: function(a, b) {
143 const aName = a.getAttribute('data-name').trim().toLowerCase();
144 const bName = b.getAttribute('data-name').trim().toLowerCase();
145 return aName.localeCompare(bName);
147 created: function(a, b) {
148 const aTime = Number(a.getAttribute('data-created'));
149 const bTime = Number(b.getAttribute('data-created'));
150 return bTime - aTime;
152 updated: function(a, b) {
153 const aTime = Number(a.getAttribute('data-updated'));
154 const bTime = Number(b.getAttribute('data-updated'));
155 return bTime - aTime;
157 chaptersFirst: function(a, b) {
158 const aType = a.getAttribute('data-type');
159 const bType = b.getAttribute('data-type');
160 if (aType === bType) {
163 return (aType === 'chapter' ? -1 : 1);
165 chaptersLast: function(a, b) {
166 const aType = a.getAttribute('data-type');
167 const bType = b.getAttribute('data-type');
168 if (aType === bType) {
171 return (aType === 'chapter' ? 1 : -1);
177 const reversibleTypes = ['name', 'created', 'updated'];
179 $container.on('click', '.sort-box-options [data-sort]', function(event) {
180 event.preventDefault();
181 const $sortLists = $(this).closest('.sort-box').find('ul');
182 const sort = $(this).attr('data-sort');
184 reverse = (lastSort === sort) ? !reverse : false;
185 let sortFunction = sortOperations[sort];
186 if (reverse && reversibleTypes.includes(sort)) {
187 sortFunction = function(a, b) {
188 return 0 - sortOperations[sort](a, b)
192 $sortLists.each(function() {
193 const $list = $(this);
194 $list.children('li').sort(sortFunction).appendTo($list);