-
-
- 您可以在任意界面按快捷键A(不区分大小写)增加备忘录。双击备忘录可查看详情! -
diff --git a/database/db.js b/database/db.js index 0d6c92b..8b81659 100644 --- a/database/db.js +++ b/database/db.js @@ -546,13 +546,13 @@ db.getBookmarksNavigate = function(tags) { // var sql = "SELECT t.id as tag_id, t.name as tag_name, b.* FROM `tags` as t LEFT OUTER JOIN tags_bookmarks as tb ON t.id = tb.tag_id LEFT OUTER JOIN bookmarks as b ON tb.bookmark_id = b.id WHERE t.user_id='" + user_id + "' ORDER BY t.id ASC, b.click_count DESC"; var sql = ""; tags.forEach((tag, index) => { - var t = 't' + tag.id; - if (index >= 1) { - sql += " UNION " - } - sql += "(SELECT * FROM ((SELECT t.id AS tag_id, t.`name` as tag_name, t.sort, b.* FROM `tags` as t, `bookmarks`as b, `tags_bookmarks` as tb WHERE t.id = tb.tag_id AND b.id = tb.bookmark_id AND t.id = " + tag.id + " ORDER BY b.click_count DESC LIMIT 0, 16) UNION (SELECT t.id AS tag_id, t.`name` as tag_name, t.sort, b.* FROM `tags` as t, `bookmarks`as b, `tags_bookmarks` as tb WHERE t.id = tb.tag_id AND b.id = tb.bookmark_id AND t.id = " + tag.id + " ORDER BY b.created_at DESC LIMIT 0, 16)) as " + t + " ORDER BY " + t + ".click_count DESC, " + t + ".created_at DESC)"; - }) - // console.log('getBookmarksNavigate ', sql); + var t = 't' + tag.id; + if (index >= 1) { + sql += " UNION " + } + sql += "(SELECT * FROM ((SELECT t.id AS tag_id, t.`name` as tag_name, t.sort, b.* FROM `tags` as t, `bookmarks`as b, `tags_bookmarks` as tb WHERE t.id = tb.tag_id AND b.id = tb.bookmark_id AND t.id = " + tag.id + " ORDER BY b.click_count DESC LIMIT 0, 16) UNION (SELECT t.id AS tag_id, t.`name` as tag_name, t.sort, b.* FROM `tags` as t, `bookmarks`as b, `tags_bookmarks` as tb WHERE t.id = tb.tag_id AND b.id = tb.bookmark_id AND t.id = " + tag.id + " ORDER BY b.created_at DESC LIMIT 0, 16)) as " + t + " ORDER BY " + t + ".click_count DESC, " + t + ".created_at DESC)"; + }) + // console.log('getBookmarksNavigate ', sql); return new Promise(function(resolve, reject) { client.query(sql, (err, result) => { @@ -1030,12 +1030,16 @@ db.addNote = function(note) { }; db.getNotes = function(params) { - var sql = "SELECT notes.id, notes.content, notes.tag_id, DATE_FORMAT(notes.created_at, '%Y-%m-%d %H:%i:%s') as created_at, tags.name FROM `notes` LEFT JOIN tags ON tags.id = notes.tag_id WHERE notes.user_id = '" + params.user_id + "'"; + var sql = "SELECT notes.id, notes.content, notes.tag_id, DATE_FORMAT(notes.created_at, '%Y-%m-%d %H:%i:%s') as created_at, tags.name as tagName FROM `notes` LEFT JOIN tags ON tags.id = notes.tag_id WHERE notes.user_id = '" + params.user_id + "'"; if (params.searchWord) { sql += " AND notes.content LIKE '%" + params.searchWord + "%'"; } + if (params.tagId) { + sql += " AND notes.tag_id = '" + params.tagId + "'"; + } + sql += " ORDER BY `created_at` DESC" console.log(sql); return new Promise(function(resolve, reject) { @@ -1069,8 +1073,8 @@ db.delNote = function(id) { }); } -db.updateNote = function(id, content) { - var sql = "UPDATE `notes` SET `content`=" + client.escape(content) + " WHERE (`id`='" + id + "')"; +db.updateNote = function(id, content, tag_id) { + var sql = "UPDATE `notes` SET `content`=" + client.escape(content) + ", `tag_id`='" + tag_id + "' WHERE (`id`='" + id + "')"; console.log(sql); return new Promise(function(resolve, reject) { client.query(sql, (err, result) => { diff --git a/public/scripts/controllers/note-controller.js b/public/scripts/controllers/note-controller.js index bea1a39..5513c40 100644 --- a/public/scripts/controllers/note-controller.js +++ b/public/scripts/controllers/note-controller.js @@ -9,7 +9,9 @@ app.controller('noteCtr', ['$scope', '$state', '$stateParams', '$filter', '$wind $scope.edit = false; $scope.preContent = ''; $scope.content = ''; + $scope.currentTagId = null; $scope.currentNoteId = null; + $scope.tags = []; // 书签数据 $scope.notes = []; $scope.totalPages = 0; $scope.currentPage = 1; @@ -28,6 +30,7 @@ app.controller('noteCtr', ['$scope', '$state', '$stateParams', '$filter', '$wind login: login, index: index }); + getTags(); getNotes(); }) .catch((err) => { @@ -79,30 +82,42 @@ app.controller('noteCtr', ['$scope', '$state', '$stateParams', '$filter', '$wind return; } $scope.add = close; + var tagName = ''; + + $scope.tags.forEach((tag) => { + if ($scope.currentTagId === tag.id) { + tagName = tag.name; + } + if (!$scope.currentTagId) { + if (tag.name == '未分类') { + $scope.currentTagId = tag.id; + tagName = tag.name + } + } + }) + var note = { - tag_id: -1, + tag_id: $scope.currentTagId, content: $scope.content, } bookmarkService.addNote(note) .then((data) => { - console.log(JSON.stringify(data)); - if (data.retCode == 0) { - note.id = data.insertId; - note.created_at = $filter('date')(new Date(), "yyyy-MM-dd HH:mm:ss"); - note.name = ''; - $scope.notes.unshift(note); - $timeout(function() { - timeagoInstance.cancel(); - timeagoInstance.render(document.querySelectorAll('.need_to_be_rendered'), 'zh_CN'); - }, 100) - } + // 增加成功,重新获取一次备忘录 + $scope.tags.forEach((tag) => { + tag.clicked = false; + }) $scope.preContent = $scope.content; $scope.content = ''; updateEditPos(); + $scope.currentTagId = null; + $scope.currentPage = 1; + $scope.searchWord = ''; + getNotes(); }) .catch((err) => { - console.log('addNote err', err) + console.log('addNote err', err); + $scope.currentTagId = null; }); } @@ -160,17 +175,38 @@ app.controller('noteCtr', ['$scope', '$state', '$stateParams', '$filter', '$wind } } - $scope.editNote = function(id, content) { + $scope.editNote = function(id, content, tagId) { $scope.add = true; $scope.edit = true; $scope.content = content; $scope.currentNoteId = id; + $scope.currentTagId = tagId; + $scope.tags.forEach((tag) => { + tag.clicked = false; + if (tag.id == tagId) { + tag.clicked = true; + } + }) } $scope.updateNote = function() { + var tagName = ''; + $scope.tags.forEach((tag) => { + if ($scope.currentTagId === tag.id) { + tagName = tag.name; + } + if (!$scope.currentTagId) { + if (tag.name == '未分类') { + $scope.currentTagId = tag.id; + tagName = tag.name + } + } + }) + var params = { id: $scope.currentNoteId, content: $scope.content, + tag_id: $scope.currentTagId, } bookmarkService.updateNote(params) @@ -180,6 +216,8 @@ app.controller('noteCtr', ['$scope', '$state', '$stateParams', '$filter', '$wind $scope.notes.forEach((note) => { if (note.id == $scope.currentNoteId) { note.content = $scope.content; + note.tagName = tagName; + note.tag_id = $scope.currentTagId; } }) $scope.add = false; @@ -225,7 +263,7 @@ app.controller('noteCtr', ['$scope', '$state', '$stateParams', '$filter', '$wind var key = event.key.toUpperCase(); if ($scope.hoverNote && dataService.keyShortcuts()) { if (key == 'E') { - $scope.editNote($scope.hoverNote.id, $scope.hoverNote.content) + $scope.editNote($scope.hoverNote.id, $scope.hoverNote.content, $scope.hoverNote.tag_id) } else if (key == 'I') { $scope.detailNote($scope.hoverNote.content) } else if (key == 'D') { @@ -237,13 +275,17 @@ app.controller('noteCtr', ['$scope', '$state', '$stateParams', '$filter', '$wind }) }); - function getNotes() { + function getNotes(tagId) { + $scope.notes = []; $scope.loadBusy = true; var params = { currentPage: $scope.currentPage, perPageItems: perPageItems, searchWord: $scope.searchWord, }; + if (tagId) { + params.tagId = tagId; + } bookmarkService.getNotes(params) .then((data) => { $scope.notes = data.notes; @@ -270,6 +312,48 @@ app.controller('noteCtr', ['$scope', '$state', '$stateParams', '$filter', '$wind }); } + function getTags(params) { + $scope.loadBusy = true; + bookmarkService.getTags(params) + .then((data) => { + $scope.tags = [] + var find = false; + data.forEach((tag) => { + $scope.tags.push(tag); + if (tag.id == $scope.currentTagId) { + find = true; // 如果是删了分类返回来,那么要重新默认选中第一个分类 + } + }) + if (!find) $scope.currentTagId = null; + + if ($scope.currentTagId) { + getTags($scope.currentTagId); + } + $scope.loadBusy = false; + }) + .catch((err) => { + console.log('getTags err', err); + $scope.loadBusy = false; + }); + } + + $scope.clickTag = function(id, clicked) { + $scope.currentTagId = id; + // 只允许选择一个 + $scope.tags.forEach((tag) => { + tag.clicked = false; + if (tag.id == id) { + tag.clicked = true; + } + }) + + if ($scope.add || $scope.edit) { + + } else { + getNotes($scope.currentTagId); + } + } + $('.js-note-card').transition('hide'); function transition() { diff --git a/public/views/note.html b/public/views/note.html index 469c527..86765a1 100644 --- a/public/views/note.html +++ b/public/views/note.html @@ -1,14 +1,5 @@ -