Dan Brown

Added cross-book page/chapter sorting

...@@ -9,6 +9,7 @@ use Illuminate\Support\Facades\Auth; ...@@ -9,6 +9,7 @@ use Illuminate\Support\Facades\Auth;
9 use Illuminate\Support\Str; 9 use Illuminate\Support\Str;
10 use Oxbow\Http\Requests; 10 use Oxbow\Http\Requests;
11 use Oxbow\Repos\BookRepo; 11 use Oxbow\Repos\BookRepo;
12 +use Oxbow\Repos\ChapterRepo;
12 use Oxbow\Repos\PageRepo; 13 use Oxbow\Repos\PageRepo;
13 14
14 class BookController extends Controller 15 class BookController extends Controller
...@@ -16,16 +17,19 @@ class BookController extends Controller ...@@ -16,16 +17,19 @@ class BookController extends Controller
16 17
17 protected $bookRepo; 18 protected $bookRepo;
18 protected $pageRepo; 19 protected $pageRepo;
20 + protected $chapterRepo;
19 21
20 /** 22 /**
21 * BookController constructor. 23 * BookController constructor.
22 - * @param BookRepo $bookRepo 24 + * @param BookRepo $bookRepo
23 - * @param PageRepo $pageRepo 25 + * @param PageRepo $pageRepo
26 + * @param ChapterRepo $chapterRepo
24 */ 27 */
25 - public function __construct(BookRepo $bookRepo, PageRepo $pageRepo) 28 + public function __construct(BookRepo $bookRepo, PageRepo $pageRepo, ChapterRepo $chapterRepo)
26 { 29 {
27 $this->bookRepo = $bookRepo; 30 $this->bookRepo = $bookRepo;
28 $this->pageRepo = $pageRepo; 31 $this->pageRepo = $pageRepo;
32 + $this->chapterRepo = $chapterRepo;
29 parent::__construct(); 33 parent::__construct();
30 } 34 }
31 35
...@@ -134,6 +138,71 @@ class BookController extends Controller ...@@ -134,6 +138,71 @@ class BookController extends Controller
134 } 138 }
135 139
136 /** 140 /**
141 + * Shows the view which allows pages to be re-ordered and sorted.
142 + * @param string $bookSlug
143 + * @return \Illuminate\View\View
144 + */
145 + public function sort($bookSlug)
146 + {
147 + $this->checkPermission('book-update');
148 + $book = $this->bookRepo->getBySlug($bookSlug);
149 + $books = $this->bookRepo->getAll();
150 + return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books]);
151 + }
152 +
153 + public function getSortItem($bookSlug)
154 + {
155 + $book = $this->bookRepo->getBySlug($bookSlug);
156 + return view('books/sort-box', ['book' => $book]);
157 + }
158 +
159 + /**
160 + * Saves an array of sort mapping to pages and chapters.
161 + *
162 + * @param string $bookSlug
163 + * @param Request $request
164 + * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
165 + */
166 + public function saveSort($bookSlug, Request $request)
167 + {
168 + $this->checkPermission('book-update');
169 + $book = $this->bookRepo->getBySlug($bookSlug);
170 +
171 + // Return if no map sent
172 + if (!$request->has('sort-tree')) {
173 + return redirect($book->getUrl());
174 + }
175 +
176 + $sortedBooks = [];
177 + // Sort pages and chapters
178 + $sortMap = json_decode($request->get('sort-tree'));
179 + $defaultBookId = $book->id;
180 + foreach ($sortMap as $index => $bookChild) {
181 + $id = $bookChild->id;
182 + $isPage = $bookChild->type == 'page';
183 + $bookId = $this->bookRepo->exists($bookChild->book) ? $bookChild->book : $defaultBookId;
184 + $model = $isPage ? $this->pageRepo->getById($id) : $this->chapterRepo->getById($id);
185 + $isPage ? $this->pageRepo->setBookId($bookId, $model) : $this->chapterRepo->setBookId($bookId, $model);
186 + $model->priority = $index;
187 + if ($isPage) {
188 + $model->chapter_id = ($bookChild->parentChapter === false) ? 0 : $bookChild->parentChapter;
189 + }
190 + $model->save();
191 + if (!in_array($bookId, $sortedBooks)) {
192 + $sortedBooks[] = $bookId;
193 + }
194 + }
195 +
196 + // Add activity for books
197 + foreach ($sortedBooks as $bookId) {
198 + $updatedBook = $this->bookRepo->getById($bookId);
199 + Activity::add($updatedBook, 'book_sort', $updatedBook->id);
200 + }
201 +
202 + return redirect($book->getUrl());
203 + }
204 +
205 + /**
137 * Remove the specified book from storage. 206 * Remove the specified book from storage.
138 * 207 *
139 * @param $bookSlug 208 * @param $bookSlug
......
...@@ -143,50 +143,6 @@ class PageController extends Controller ...@@ -143,50 +143,6 @@ class PageController extends Controller
143 } 143 }
144 144
145 /** 145 /**
146 - * Shows the view which allows pages to be re-ordered and sorted.
147 - * @param $bookSlug
148 - * @return \Illuminate\View\View
149 - */
150 - public function sortPages($bookSlug)
151 - {
152 - $this->checkPermission('book-update');
153 - $book = $this->bookRepo->getBySlug($bookSlug);
154 - return view('pages/sort', ['book' => $book, 'current' => $book]);
155 - }
156 -
157 - /**
158 - * Saves an array of sort mapping to pages and chapters.
159 - *
160 - * @param $bookSlug
161 - * @param Request $request
162 - * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
163 - */
164 - public function savePageSort($bookSlug, Request $request)
165 - {
166 - $this->checkPermission('book-update');
167 - $book = $this->bookRepo->getBySlug($bookSlug);
168 - // Return if no map sent
169 - if (!$request->has('sort-tree')) {
170 - return redirect($book->getUrl());
171 - }
172 -
173 - // Sort pages and chapters
174 - $sortMap = json_decode($request->get('sort-tree'));
175 - foreach ($sortMap as $index => $bookChild) {
176 - $id = $bookChild->id;
177 - $isPage = $bookChild->type == 'page';
178 - $model = $isPage ? $this->pageRepo->getById($id) : $this->chapterRepo->getById($id);
179 - $model->priority = $index;
180 - if ($isPage) {
181 - $model->chapter_id = ($bookChild->parentChapter === false) ? 0 : $bookChild->parentChapter;
182 - }
183 - $model->save();
184 - }
185 - Activity::add($book, 'book_sort', $book->id);
186 - return redirect($book->getUrl());
187 - }
188 -
189 - /**
190 * Show the deletion page for the specified page. 146 * Show the deletion page for the specified page.
191 * @param $bookSlug 147 * @param $bookSlug
192 * @param $pageSlug 148 * @param $pageSlug
......
...@@ -12,14 +12,16 @@ Route::group(['middleware' => 'auth'], function () { ...@@ -12,14 +12,16 @@ Route::group(['middleware' => 'auth'], function () {
12 Route::get('/{slug}/edit', 'BookController@edit'); 12 Route::get('/{slug}/edit', 'BookController@edit');
13 Route::put('/{slug}', 'BookController@update'); 13 Route::put('/{slug}', 'BookController@update');
14 Route::delete('/{id}', 'BookController@destroy'); 14 Route::delete('/{id}', 'BookController@destroy');
15 + Route::get('/{slug}/sort-item', 'BookController@getSortItem');
15 Route::get('/{slug}', 'BookController@show'); 16 Route::get('/{slug}', 'BookController@show');
16 Route::get('/{slug}/delete', 'BookController@showDelete'); 17 Route::get('/{slug}/delete', 'BookController@showDelete');
18 + Route::get('/{bookSlug}/sort', 'BookController@sort');
19 + Route::put('/{bookSlug}/sort', 'BookController@saveSort');
20 +
17 21
18 // Pages 22 // Pages
19 Route::get('/{bookSlug}/page/create', 'PageController@create'); 23 Route::get('/{bookSlug}/page/create', 'PageController@create');
20 Route::post('/{bookSlug}/page', 'PageController@store'); 24 Route::post('/{bookSlug}/page', 'PageController@store');
21 - Route::get('/{bookSlug}/sort', 'PageController@sortPages');
22 - Route::put('/{bookSlug}/sort', 'PageController@savePageSort');
23 Route::get('/{bookSlug}/page/{pageSlug}', 'PageController@show'); 25 Route::get('/{bookSlug}/page/{pageSlug}', 'PageController@show');
24 Route::get('/{bookSlug}/page/{pageSlug}/edit', 'PageController@edit'); 26 Route::get('/{bookSlug}/page/{pageSlug}/edit', 'PageController@edit');
25 Route::get('/{bookSlug}/page/{pageSlug}/delete', 'PageController@showDelete'); 27 Route::get('/{bookSlug}/page/{pageSlug}/delete', 'PageController@showDelete');
......
...@@ -36,6 +36,16 @@ class BookRepo ...@@ -36,6 +36,16 @@ class BookRepo
36 } 36 }
37 37
38 /** 38 /**
39 + * Checks if a book exists.
40 + * @param $id
41 + * @return bool
42 + */
43 + public function exists($id)
44 + {
45 + return $this->book->where('id', '=', $id)->exists();
46 + }
47 +
48 + /**
39 * Get a new book instance from request input. 49 * Get a new book instance from request input.
40 * @param $input 50 * @param $input
41 * @return Book 51 * @return Book
......
...@@ -80,4 +80,15 @@ class ChapterRepo ...@@ -80,4 +80,15 @@ class ChapterRepo
80 return $chapters; 80 return $chapters;
81 } 81 }
82 82
83 + public function setBookId($bookId, Chapter $chapter)
84 + {
85 + $chapter->book_id = $bookId;
86 + foreach($chapter->activity as $activity) {
87 + $activity->book_id = $bookId;
88 + $activity->save();
89 + }
90 + $chapter->save();
91 + return $chapter;
92 + }
93 +
83 } 94 }
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -186,6 +186,17 @@ class PageRepo ...@@ -186,6 +186,17 @@ class PageRepo
186 return $query->count() > 0; 186 return $query->count() > 0;
187 } 187 }
188 188
189 + public function setBookId($bookId, Page $page)
190 + {
191 + $page->book_id = $bookId;
192 + foreach($page->activity as $activity) {
193 + $activity->book_id = $bookId;
194 + $activity->save();
195 + }
196 + $page->save();
197 + return $page;
198 + }
199 +
189 /** 200 /**
190 * Gets a suitable slug for the resource 201 * Gets a suitable slug for the resource
191 * 202 *
......
...@@ -13,7 +13,7 @@ $(function () { ...@@ -13,7 +13,7 @@ $(function () {
13 $('.chapter-toggle').click(function(e) { 13 $('.chapter-toggle').click(function(e) {
14 e.preventDefault(); 14 e.preventDefault();
15 $(this).toggleClass('open'); 15 $(this).toggleClass('open');
16 - $(this).closest('.book-child').find('.inset-list').slideToggle(180); 16 + $(this).closest('.chapter').find('.inset-list').slideToggle(180);
17 }); 17 });
18 18
19 }); 19 });
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
37 cursor: pointer; 37 cursor: pointer;
38 margin: 0 0 $-l 0; 38 margin: 0 0 $-l 0;
39 transition: all ease-in-out 180ms; 39 transition: all ease-in-out 180ms;
40 + user-select: none;
40 i { 41 i {
41 transition: all ease-in-out 180ms; 42 transition: all ease-in-out 180ms;
42 transform: rotate(0deg); 43 transform: rotate(0deg);
...@@ -154,10 +155,18 @@ ...@@ -154,10 +155,18 @@
154 // Sortable Lists 155 // Sortable Lists
155 .sortable-page-list, .sortable-page-list ul { 156 .sortable-page-list, .sortable-page-list ul {
156 list-style: none; 157 list-style: none;
158 + background-color: #FFF;
159 +}
160 +.sort-box {
161 + margin-bottom: $-m;
162 + padding: 0 $-l 0 $-l;
163 + border-left: 4px solid $color-book;
157 } 164 }
158 .sortable-page-list { 165 .sortable-page-list {
159 margin-left: 0; 166 margin-left: 0;
160 - box-shadow: 0 0 1px 0 rgba(0, 0, 0, 0.1); 167 + > ul {
168 + margin-left: 0;
169 + }
161 ul { 170 ul {
162 margin-bottom: 0; 171 margin-bottom: 0;
163 margin-top: 0; 172 margin-top: 0;
......
...@@ -16,4 +16,8 @@ table.table { ...@@ -16,4 +16,8 @@ table.table {
16 tr:hover { 16 tr:hover {
17 background-color: #EEE; 17 background-color: #EEE;
18 } 18 }
19 +}
20 +
21 +table {
22 + max-width: 100%;
19 } 23 }
...\ No newline at end of file ...\ No newline at end of file
......
1 +<div class="sort-box" data-type="book" data-id="{{ $book->id }}">
2 + <h3 class="text-book"><i class="zmdi zmdi-book"></i>{{ $book->name }}</h3>
3 + <ul class="sortable-page-list sort-list">
4 + @foreach($book->children() as $bookChild)
5 + <li data-id="{{$bookChild->id}}" data-type="{{ $bookChild->getName() }}" class="text-{{ $bookChild->getName() }}">
6 + <i class="zmdi {{ $bookChild->isA('chapter') ? 'zmdi-collection-bookmark':'zmdi-file-text'}}"></i>{{ $bookChild->name }}
7 + @if($bookChild->isA('chapter'))
8 + <ul>
9 + @foreach($bookChild->pages as $page)
10 + <li data-id="{{$page->id}}" class="text-page" data-type="page">
11 + <i class="zmdi zmdi-file-text"></i>
12 + {{ $page->name }}
13 + </li>
14 + @endforeach
15 + </ul>
16 + @endif
17 + </li>
18 + @endforeach
19 + </ul>
20 +</div>
...\ No newline at end of file ...\ No newline at end of file
1 +@extends('base')
2 +
3 +@section('content')
4 +
5 + <div class="container">
6 + <h1>Sorting Pages & Chapters<span class="subheader">For {{ $book->name }}</span></h1>
7 + <div class="row">
8 + <div class="col-md-8" id="sort-boxes">
9 +
10 + @include('books/sort-box', ['book' => $book])
11 +
12 + </div>
13 +
14 + <div class="col-md-4">
15 + <h3>Show Other Books</h3>
16 + @foreach($books as $otherBook)
17 + @if($otherBook->id !== $book->id)
18 + <div id="additional-books">
19 + <a href="/books/{{ $otherBook->slug }}/sort-item" class="text-book"><i class="zmdi zmdi-book"></i>{{ $otherBook->name }}</a>
20 + </div>
21 + @endif
22 + @endforeach
23 + </div>
24 +
25 + </div>
26 +
27 + <form action="{{$book->getUrl()}}/sort" method="POST">
28 + {!! csrf_field() !!}
29 + <input type="hidden" name="_method" value="PUT">
30 + <input type="hidden" id="sort-tree-input" name="sort-tree">
31 + <div class="list">
32 + <a href="{{$book->getUrl()}}" class="button muted">Cancel</a>
33 + <button class="button pos" type="submit">Save Order</button>
34 + </div>
35 + </form>
36 +
37 + </div>
38 +
39 + <script>
40 + $(document).ready(function() {
41 +
42 + var sortableOptions = {
43 + group: 'serialization',
44 + onDrop: function($item, container, _super) {
45 + var pageMap = buildPageMap();
46 + $('#sort-tree-input').val(JSON.stringify(pageMap));
47 + _super($item, container);
48 + },
49 + isValidTarget: function ($item, container) {
50 + // Prevent nested chapters
51 + return !($item.is('[data-type="chapter"]') && container.target.closest('li').attr('data-type') == 'chapter');
52 + }
53 + };
54 +
55 + var group = $('.sort-list').sortable(sortableOptions);
56 +
57 + $('#additional-books').on('click', 'a', function(e) {
58 + e.preventDefault();
59 + var $link = $(this);
60 + var url = $link.attr('href');
61 + $.get(url, function(data) {
62 + $('#sort-boxes').append(data);
63 + group.sortable("destroy");
64 + $('.sort-list').sortable(sortableOptions);
65 + });
66 + $link.remove();
67 + });
68 +
69 + function buildPageMap() {
70 + var pageMap = [];
71 + var $lists = $('.sort-list');
72 + $lists.each(function(listIndex) {
73 + var list = $(this);
74 + var bookId = list.closest('[data-type="book"]').attr('data-id');
75 + var $childElements = list.find('[data-type="page"], [data-type="chapter"]');
76 + $childElements.each(function(childIndex) {
77 + var $childElem = $(this);
78 + var type = $childElem.attr('data-type');
79 + var parentChapter = false;
80 + if(type === 'page' && $childElem.closest('[data-type="chapter"]').length === 1) {
81 + parentChapter = $childElem.closest('[data-type="chapter"]').attr('data-id');
82 + }
83 + pageMap.push({
84 + id: $childElem.attr('data-id'),
85 + parentChapter: parentChapter,
86 + type: type,
87 + book: bookId
88 + });
89 + });
90 + });
91 + return pageMap;
92 + }
93 +
94 + });
95 + </script>
96 +@stop
1 -@extends('base')
2 -
3 -@section('content')
4 -
5 - <div class="container small">
6 - <h1>Sorting Pages & Chapters<span class="subheader">For {{ $book->name }}</span></h1>
7 -
8 - <ul class="sortable-page-list" id="sort-list">
9 - @foreach($book->children() as $bookChild)
10 - <li data-id="{{$bookChild->id}}" data-type="{{ $bookChild->getName() }}" class="text-{{ $bookChild->getName() }}">
11 - <i class="zmdi {{ $bookChild->isA('chapter') ? 'zmdi-collection-bookmark':'zmdi-file-text'}}"></i>{{ $bookChild->name }}
12 - @if($bookChild->isA('chapter'))
13 - <ul>
14 - @foreach($bookChild->pages as $page)
15 - <li data-id="{{$page->id}}" class="text-page" data-type="page">
16 - <i class="zmdi zmdi-file-text"></i>
17 - {{ $page->name }}
18 - </li>
19 - @endforeach
20 - </ul>
21 - @endif
22 - </li>
23 - @endforeach
24 - </ul>
25 -
26 - <form action="{{$book->getUrl()}}/sort" method="POST">
27 - {!! csrf_field() !!}
28 - <input type="hidden" name="_method" value="PUT">
29 - <input type="hidden" id="sort-tree-input" name="sort-tree">
30 - <div class="list">
31 - <a href="{{$book->getUrl()}}" class="button muted">Cancel</a>
32 - <button class="button pos" type="submit">Save Order</button>
33 - </div>
34 - </form>
35 -
36 - </div>
37 -
38 - <script>
39 - $(document).ready(function() {
40 -
41 - var group = $('#sort-list').sortable({
42 - group: 'serialization',
43 - onDrop: function($item, container, _super) {
44 - var data = group.sortable('serialize').get();
45 - var pageMap = buildPageMap(data[0]);
46 - $('#sort-tree-input').val(JSON.stringify(pageMap));
47 - _super($item, container);
48 - }
49 - });
50 -
51 - function buildPageMap(data) {
52 - var pageMap = [];
53 - for(var i = 0; i < data.length; i++) {
54 - var bookChild = data[i];
55 - pageMap.push({
56 - id: bookChild.id,
57 - parentChapter: false,
58 - type: bookChild.type
59 - });
60 - if(bookChild.type == 'chapter' && bookChild.children) {
61 - var chapterId = bookChild.id;
62 - var chapterChildren = bookChild.children[0];
63 - for(var j = 0; j < chapterChildren.length; j++) {
64 - var page = chapterChildren[j];
65 - pageMap.push({
66 - id: page.id,
67 - parentChapter: chapterId,
68 - type: 'page'
69 - });
70 - }
71 - }
72 - }
73 - return pageMap;
74 - }
75 -
76 - });
77 - </script>
78 -@stop