diff --git a/application/controllers/api/frontend/v1/Bookmark.php b/application/controllers/api/frontend/v1/Bookmark.php index 3e646bb51..f925e9662 100644 --- a/application/controllers/api/frontend/v1/Bookmark.php +++ b/application/controllers/api/frontend/v1/Bookmark.php @@ -31,6 +31,7 @@ class Bookmark extends FHCAPI_Controller 'delete' => self::PERM_LOGGED, 'insert' => self::PERM_LOGGED, 'update' => self::PERM_LOGGED, + 'changeOrder' => self::PERM_LOGGED, ]); $this->load->model('dashboard/Bookmark_model', 'BookmarkModel'); @@ -51,7 +52,7 @@ class Bookmark extends FHCAPI_Controller */ public function getBookmarks() { - $this->BookmarkModel->addOrder("bookmark_id"); + $this->BookmarkModel->addOrder("sort"); $bookmarks = $this->BookmarkModel->loadWhere(["uid"=>$this->uid]); $bookmarks = $this->getDataOrTerminateWithError($bookmarks); @@ -99,8 +100,9 @@ class Bookmark extends FHCAPI_Controller $url = $this->input->post('url',true); $title = $this->input->post('title',true); $tag = $this->input->post('tag', true); + $sort = $this->input->post('sort', true); - $insert_into_result = $this->BookmarkModel->insert(['uid'=>$this->uid, 'url'=>$url, 'title'=>$title,'tag'=>$tag, 'insertvon'=>$this->uid, 'updateamum'=>NULL, 'updatevon'=>NULL]); + $insert_into_result = $this->BookmarkModel->insert(['uid'=>$this->uid, 'url'=>$url, 'title'=>$title,'tag'=>$tag, 'insertvon'=>$this->uid, 'updateamum'=>NULL, 'updatevon'=>NULL, 'sort'=>$sort,]); $insert_into_result = $this->getDataOrTerminateWithError($insert_into_result); @@ -134,5 +136,32 @@ class Bookmark extends FHCAPI_Controller $this->terminateWithSuccess($update_result); } + + /** + * changes sort of two bookmarks in the bookmark table + * @access public + * @return void + */ + public function changeOrder($bookmark_id1, $bookmark_id2) + { + + $result1 = $this->BookmarkModel->load($bookmark_id1); + $data1 = $this->getDataOrTerminateWithError($result1); + $sort1 = current($data1)->sort; + + $result2 = $this->BookmarkModel->load(["bookmark_id"=>$bookmark_id2]); + $data2 = $this->getDataOrTerminateWithError($result2); + $sort2 = current($data2)->sort; + + $update_result1 = $this->BookmarkModel->update($bookmark_id1,['sort'=>$sort2,]); + $update_result[] = $this->getDataOrTerminateWithError($update_result1); + + $update_result2 = $this->BookmarkModel->update($bookmark_id2,['sort'=>$sort1,]); + $update_result[] = $this->getDataOrTerminateWithError($update_result2); + + + + $this->terminateWithSuccess($update_result); + } } diff --git a/public/js/api/factory/widget/bookmark.js b/public/js/api/factory/widget/bookmark.js index 9768e25ac..76c38cea7 100644 --- a/public/js/api/factory/widget/bookmark.js +++ b/public/js/api/factory/widget/bookmark.js @@ -35,11 +35,17 @@ export default { params: { url, title } }; }, - insert({ url, title, tag }) { + insert({ url, title, tag, sort }) { return { method: 'post', url: `/api/frontend/v1/Bookmark/insert`, - params: { url, title, tag } + params: { url, title, tag, sort } }; + }, + changeOrder(bookmark_id1, bookmark_id2) { + return { + method: 'post', + url: `/api/frontend/v1/Bookmark/changeOrder/${bookmark_id1}/${bookmark_id2}`, + }; } }; \ No newline at end of file diff --git a/public/js/components/DashboardWidget/Url.js b/public/js/components/DashboardWidget/Url.js index 16eacab90..e07e55b18 100644 --- a/public/js/components/DashboardWidget/Url.js +++ b/public/js/components/DashboardWidget/Url.js @@ -23,6 +23,7 @@ export default { bookmark_id: null, title_input: "", url_input: "", + sort: null, validation: { invalidURL: false, invalidTitel: false, @@ -42,6 +43,25 @@ export default { return false; }, + newSort(){ + if(this.shared.length == 0) + return 1; + else + return Math.max(...this.shared.map(b => b.sort)) + 1; + }, + maxSort(){ + if(this.shared.length == 0) + return 0; + else + return Math.max(...this.shared.map(b => b.sort)); + }, + minSort(){ + if(this.shared.length == 0) + return 0; + else + return Math.min(...this.shared.map(b => b.sort)); + }, + }, methods: { stopDrag(event){ @@ -94,11 +114,15 @@ export default { // early return if validation failed if (!this.isValidationSuccessfull()) return; + // get highest Sort + this.sort = this.newSort; + this.$api .call(ApiBookmark.insert({ tag: this.config.tag, title: this.title_input, url: this.url_input, + sort: this.sort })) .then((res) => res.data) .then((result) => { @@ -150,6 +174,42 @@ export default { }) .catch(this.$fhcAlert.handleSystemError); }, + sortDown(bookmark_id){ + const current = this.shared.find(b => b.bookmark_id === bookmark_id); + + const next = this.shared + .filter(b => b.sort > current.sort) + .sort((a, b) => a.sort - b.sort)[0]; + + if (!next) { + console.log("lowest sort item, no change"); + return; + } + this.changeOrder(current.bookmark_id, next.bookmark_id); + }, + sortUp(bookmark_id){ + const current = this.shared.find(b => b.bookmark_id === bookmark_id); + + const next = this.shared + .filter(b => b.sort < current.sort) + .sort((a, b) => a.sort + b.sort)[0]; + + if (!next) { + console.log("highest sort item, no change"); + return; + } + this.changeOrder(current.bookmark_id, next.bookmark_id); + }, + changeOrder(bookmark_id_1, bookmark_id_2){ + this.$api + .call(ApiBookmark.changeOrder(bookmark_id_1, bookmark_id_2)) + .then((res) => res.data) + .then((result) => { + // refetch the bookmarks to see the updates + this.fetchBookmarks(); + }) + .catch(this.$fhcAlert.handleSystemError); + } }, async mounted() { await this.fetchBookmarks(); @@ -163,6 +223,7 @@ export default {
+