BookStack/resources/views/books/sort.blade.php

215 lines
8.3 KiB
PHP
Raw Normal View History

@extends('simple-layout')
2015-09-06 09:35:53 -04:00
2019-02-17 06:44:02 -05:00
{{--TODO - Load books in via selector interface--}}
@section('body')
<div class="container">
2015-09-06 09:35:53 -04:00
<div class="my-s">
2019-01-31 15:37:12 -05:00
@include('partials.breadcrumbs', ['crumbs' => [
$book,
2019-02-17 06:44:02 -05:00
$book->getUrl('/sort') => [
'text' => trans('entities.books_sort'),
'icon' => 'sort',
]
2019-01-31 15:37:12 -05:00
]])
</div>
2015-09-06 09:35:53 -04:00
2019-01-31 15:37:12 -05:00
<div class="grid left-focus large-gap">
<div>
<div class="card content-wrap">
<h1 class="list-heading">{{ trans('entities.books_sort') }}</h1>
<div id="sort-boxes">
2019-02-17 06:44:02 -05:00
@include('books.sort-box', ['book' => $book, 'bookChildren' => $bookChildren])
</div>
2019-01-31 15:37:12 -05:00
<form action="{{ $book->getUrl('/sort') }}" method="POST">
{!! csrf_field() !!}
<input type="hidden" name="_method" value="PUT">
<input type="hidden" id="sort-tree-input" name="sort-tree">
<div class="list text-right">
<a href="{{ $book->getUrl() }}" class="button outline">{{ trans('common.cancel') }}</a>
<button class="button primary" type="submit">{{ trans('entities.books_sort_save') }}</button>
</div>
</form>
</div>
2015-09-06 09:35:53 -04:00
</div>
2019-01-31 15:37:12 -05:00
<div>
@if(count($books) > 1)
<div class="card content-wrap">
<h2 class="list-heading">{{ trans('entities.books_sort_show_other') }}</h2>
<div id="additional-books">
@foreach($books as $otherBook)
@if($otherBook->id !== $book->id)
<div>
<a href="{{ $otherBook->getUrl('/sort-item') }}" class="text-book">@icon('book'){{ $otherBook->name }}</a>
</div>
@endif
@endforeach
</div>
</div>
2019-01-31 15:37:12 -05:00
@endif
</div>
2015-09-06 09:35:53 -04:00
</div>
2019-01-31 15:37:12 -05:00
2015-09-06 09:35:53 -04:00
</div>
@stop
@section('scripts')
<script src="{{ baseUrl("/libs/jquery-sortable/jquery-sortable.min.js") }}"></script>
2015-09-06 09:35:53 -04:00
<script>
$(document).ready(function() {
2019-02-17 06:44:02 -05:00
const $container = $('#sort-boxes');
// Sortable options
const sortableOptions = {
2015-09-06 09:35:53 -04:00
group: 'serialization',
2019-02-17 06:44:02 -05:00
containerSelector: 'ul',
itemPath: '',
itemSelector: 'li',
onDrop: function ($item, container, _super) {
updateMapInput();
2015-09-06 09:35:53 -04:00
_super($item, container);
},
2019-02-17 06:44:02 -05:00
isValidTarget: function ($item, container) {
2015-09-06 09:35:53 -04:00
// Prevent nested chapters
2019-02-17 06:44:02 -05:00
return !($item.is('[data-type="chapter"]') && container.target.closest('li').attr('data-type') === 'chapter');
2015-09-06 09:35:53 -04:00
}
};
2019-02-17 06:44:02 -05:00
// Create our sortable group
let group = $('.sort-list').sortable(sortableOptions);
2015-09-06 09:35:53 -04:00
2019-02-17 06:44:02 -05:00
// Add additional books into the view on select.
2015-09-06 09:35:53 -04:00
$('#additional-books').on('click', 'a', function(e) {
e.preventDefault();
2019-02-17 06:44:02 -05:00
const $link = $(this);
const url = $link.attr('href');
2015-09-06 09:35:53 -04:00
$.get(url, function(data) {
2019-02-17 06:44:02 -05:00
$container.append(data);
2015-09-06 09:35:53 -04:00
group.sortable("destroy");
2019-02-17 06:44:02 -05:00
group = $('.sort-list').sortable(sortableOptions);
2015-09-06 09:35:53 -04:00
});
$link.remove();
});
2019-02-17 06:44:02 -05:00
/**
* Update the input with our sort data.
*/
function updateMapInput() {
const pageMap = buildEntityMap();
$('#sort-tree-input').val(JSON.stringify(pageMap));
}
/**
* Build up a mapping of entities with their ordering and nesting.
* @returns {Array}
*/
function buildEntityMap() {
2019-02-17 06:44:02 -05:00
const entityMap = [];
const $lists = $('.sort-list');
2015-09-06 09:35:53 -04:00
$lists.each(function(listIndex) {
2019-02-17 06:44:02 -05:00
const $list = $(this);
const bookId = $list.closest('[data-type="book"]').attr('data-id');
const $directChildren = $list.find('> [data-type="page"], > [data-type="chapter"]');
$directChildren.each(function(directChildIndex) {
2019-02-17 06:44:02 -05:00
const $childElem = $(this);
const type = $childElem.attr('data-type');
const parentChapter = false;
const childId = $childElem.attr('data-id');
entityMap.push({
id: childId,
sort: directChildIndex,
2015-09-06 09:35:53 -04:00
parentChapter: parentChapter,
type: type,
book: bookId
});
2019-02-17 06:44:02 -05:00
$childElem.find('[data-type="page"]').each(function(pageIndex) {
const $chapterChild = $(this);
entityMap.push({
id: $chapterChild.attr('data-id'),
sort: pageIndex,
parentChapter: childId,
type: 'page',
book: bookId
});
});
2019-02-17 06:44:02 -05:00
2015-09-06 09:35:53 -04:00
});
});
return entityMap;
2015-09-06 09:35:53 -04:00
}
2019-02-17 06:44:02 -05:00
// Auto sort control
const sortOperations = {
name: function(a, b) {
const aName = a.getAttribute('data-name').trim().toLowerCase();
const bName = b.getAttribute('data-name').trim().toLowerCase();
return aName.localeCompare(bName);
},
created: function(a, b) {
const aTime = Number(a.getAttribute('data-created'));
const bTime = Number(b.getAttribute('data-created'));
return bTime - aTime;
},
updated: function(a, b) {
const aTime = Number(a.getAttribute('data-update'));
const bTime = Number(b.getAttribute('data-update'));
return bTime - aTime;
},
chaptersFirst: function(a, b) {
const aType = a.getAttribute('data-type');
const bType = b.getAttribute('data-type');
if (aType === bType) {
return 0;
}
return (aType === 'chapter' ? -1 : 1);
},
chaptersLast: function(a, b) {
const aType = a.getAttribute('data-type');
const bType = b.getAttribute('data-type');
if (aType === bType) {
return 0;
}
return (aType === 'chapter' ? 1 : -1);
},
};
let lastSort = '';
let reverse = false;
const reversableTypes = ['name', 'created', 'updated'];
$container.on('click', '.sort-box-options [data-sort]', function(event) {
event.preventDefault();
const $sortLists = $(this).closest('.sort-box').find('ul');
const sort = $(this).attr('data-sort');
reverse = (lastSort === sort) ? !reverse : false;
let sortFunction = sortOperations[sort];
if (reverse && reversableTypes.includes(sort)) {
sortFunction = function(a, b) {
return 0 - sortOperations[sort](a, b)
};
}
$sortLists.each(function() {
const $list = $(this);
$list.children('li').sort(sortFunction).appendTo($list);
});
lastSort = sort;
updateMapInput();
});
2015-09-06 09:35:53 -04:00
});
</script>
@stop