diff --git a/database/db.js b/database/db.js index 86bb90f..ba6f39c 100644 --- a/database/db.js +++ b/database/db.js @@ -943,7 +943,7 @@ db.hotBookmarks = function(date) { }; db.addNote = function(note) { - var sql = "INSERT INTO `notes` (`user_id`, `content`, `tag_id`) VALUES ('"+ note.user_id +"', "+ client.escape(note.content) +", '"+ note.tag_id +"')"; + var sql = "INSERT INTO `notes` (`user_id`, `content`, `tag_id`) VALUES ('" + note.user_id + "', " + client.escape(note.content) + ", '" + note.tag_id + "')"; console.log(sql); return new Promise(function(resolve, reject) { @@ -958,17 +958,57 @@ 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+"' ORDER BY `created_at` DESC"; + 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 + "'"; + + if (params.searchWord) { + sql += " AND notes.content LIKE '%" + params.searchWord + "%'"; + } + + sql += " ORDER BY `created_at` DESC" console.log(sql); return new Promise(function(resolve, reject) { client.query(sql, (err, result) => { if (err) { reject(err); } else { - resolve(result); + params.currentPage = params.currentPage || 1; + params.perPageItems = params.perPageItems || 20; + var searchData = { + totalItems: result.length, + notes: result.slice((params.currentPage - 1) * params.perPageItems, params.currentPage * params.perPageItems), + } + resolve(searchData); + } + }); + }) +}; + +db.delNote = function(id) { + var sql = "DELETE FROM `notes` WHERE (`id`='" + id + "')"; + console.log(sql); + return new Promise(function(resolve, reject) { + client.query(sql, (err, result) => { + if (err) { + reject(err); + } else { + resolve(result.affectedRows); } }); }); -}; +} + +db.updateNote = function(id, content) { + var sql = "UPDATE `notes` SET `content`=" + client.escape(content) + " WHERE (`id`='" + id + "')"; + console.log(sql); + return new Promise(function(resolve, reject) { + client.query(sql, (err, result) => { + if (err) { + reject(err); + } else { + resolve(result.affectedRows); + } + }); + }); +} module.exports = db; diff --git a/public/images/add-note.png b/public/images/add-note.png new file mode 100644 index 0000000..855a194 Binary files /dev/null and b/public/images/add-note.png differ diff --git a/public/scripts/app-angular.js b/public/scripts/app-angular.js index 907204d..8d1133e 100644 --- a/public/scripts/app-angular.js +++ b/public/scripts/app-angular.js @@ -27,6 +27,9 @@ app.config(function($stateProvider, $urlRouterProvider, $httpProvider) { .state('note', { url: '/note', templateUrl: '/views/note.html', + params: { + searchWord: null, + }, controller: 'noteCtr' }) .state('search', { diff --git a/public/scripts/controllers/bookmarks-controller.js b/public/scripts/controllers/bookmarks-controller.js index f1e22ec..8df674f 100644 --- a/public/scripts/controllers/bookmarks-controller.js +++ b/public/scripts/controllers/bookmarks-controller.js @@ -219,7 +219,6 @@ app.controller('bookmarksCtr', ['$scope', '$state', '$stateParams', '$filter', ' } }) } - $timeout(function() { timeagoInstance.cancel(); timeagoInstance.render(document.querySelectorAll('.need_to_be_rendered'), 'zh_CN'); @@ -262,6 +261,10 @@ app.controller('bookmarksCtr', ['$scope', '$state', '$stateParams', '$filter', ' }) if (!find) { $scope.bookmarks.unshift(data); + $timeout(function() { + timeagoInstance.cancel(); + timeagoInstance.render(document.querySelectorAll('.need_to_be_rendered'), 'zh_CN'); + }, 100) } } else { $scope.forbidTransition = true; @@ -307,6 +310,10 @@ app.controller('bookmarksCtr', ['$scope', '$state', '$stateParams', '$filter', ' $scope.bookmarks.push(bookmark); } }) + $timeout(function() { + timeagoInstance.cancel(); + timeagoInstance.render(document.querySelectorAll('.need_to_be_rendered'), 'zh_CN'); + }, 100) } else if (params.showStyle == 'costomTag') { $scope.costomTags.forEach((tag) => { if (tag.clicked) { @@ -376,7 +383,7 @@ app.controller('bookmarksCtr', ['$scope', '$state', '$stateParams', '$filter', ' function animation() { var data = ['scale', 'fade', 'fade up', 'fade down', 'fade left', 'fade right', 'horizontal flip', 'vertical flip', 'drop', 'fly left', 'fly right', 'fly up', 'fly down', - 'browse', 'browse right', 'slide down', 'slide up', 'slide left', 'slide right' + 'browse', 'browse right', 'slide down', 'slide up', 'slide left', 'slide right' ]; var t = data[parseInt(Math.random() * 1000) % data.length]; @@ -406,7 +413,7 @@ app.controller('bookmarksCtr', ['$scope', '$state', '$stateParams', '$filter', ' } } - function clickCmp(a, b){ + function clickCmp(a, b) { var click1 = parseInt(a.click_count); var click2 = parseInt(b.click_count); if (click1 > click2) { diff --git a/public/scripts/controllers/menus-controller.js b/public/scripts/controllers/menus-controller.js index 2c9a6ac..d6ac60a 100644 --- a/public/scripts/controllers/menus-controller.js +++ b/public/scripts/controllers/menus-controller.js @@ -74,8 +74,15 @@ app.controller('menuCtr', ['$scope', '$stateParams', '$state', '$window', '$time $window.open('https://github.com/search?utf8=%E2%9C%93&q=' + encodeURIComponent(searchWord) + '&type=', '_blank'); } else if (searchOption == 3) { $window.open('https://stackoverflow.com/search?q=' + encodeURIComponent(searchWord), '_blank'); - } else { + } else if (searchOption == 4) { $window.open('http://www.baidu.com/s?tn=mybookmark.cn&ch=3&ie=utf-8&wd=' + encodeURIComponent(searchWord), '_blank'); + } else if (searchOption == 5) { + $state.go('note', { + searchWord: searchWord, + }, { + reload: true, + }) + updateMenuActive($scope.selectLoginIndex = 6); } if (!searchWord) { diff --git a/public/scripts/controllers/note-controller.js b/public/scripts/controllers/note-controller.js index e8231d5..b3fdc7c 100644 --- a/public/scripts/controllers/note-controller.js +++ b/public/scripts/controllers/note-controller.js @@ -1,10 +1,20 @@ app.controller('noteCtr', ['$scope', '$state', '$stateParams', '$filter', '$window', '$timeout', 'ngDialog', 'bookmarkService', 'pubSubService', function($scope, $state, $stateParams, $filter, $window, $timeout, ngDialog, bookmarkService, pubSubService) { console.log("Hello noteCtr...", $stateParams); + const perPageItems = 36; + var dialog = null; $scope.loadBusy = false; + $scope.add = false; $scope.edit = false; + $scope.preContent = ''; $scope.content = ''; + $scope.currentNoteId = null; $scope.notes = []; + $scope.totalPages = 0; + $scope.currentPage = 1; + $scope.inputPage = ''; + + var timeagoInstance = timeago(); bookmarkService.autoLogin() .then((data) => { @@ -20,13 +30,35 @@ app.controller('noteCtr', ['$scope', '$state', '$stateParams', '$filter', '$wind console.log('autoLogin err', err) }); - $scope.showAddNote = function(){ - $scope.edit = (!$scope.edit); + $scope.changeCurrentPage = function(currentPage) { + currentPage = parseInt(currentPage) || 0; + console.log('currentPage = ', currentPage); + if (currentPage <= $scope.totalPages && currentPage >= 1) { + $scope.currentPage = currentPage; + $scope.inputPage = ''; + getNotes(); + } else { + $scope.currentPage = $scope.totalPages + } + } + + $scope.showAddNote = function() { + $scope.add = (!$scope.add); + $scope.edit = false; + $scope.content = ''; updateEditPos(); } - $scope.addNote = function(close){ - $scope.edit = close; + $scope.addNote = function(close) { + if ($scope.content == '') { + toastr.error('不允许备忘录内容为空!', "提示"); + return; + } + if ($scope.preContent == $scope.content) { + toastr.error('您刚刚添加了这条内容!', "提示"); + return; + } + $scope.add = close; var note = { tag_id: -1, content: $scope.content, @@ -35,18 +67,20 @@ app.controller('noteCtr', ['$scope', '$state', '$stateParams', '$filter', '$wind bookmarkService.addNote(note) .then((data) => { console.log(JSON.stringify(data)); + $scope.preContent = $scope.content; $scope.content = ''; getNotes(); + updateEditPos(); }) .catch((err) => { console.log('addNote err', err) }); } - $scope.copy = function(id, content){ + $scope.copy = function(id, content) { console.log("copy note....."); - var showContent = content.length >= 180 ? content.substr(0, 180)+'...' : content; - var clipboard = new Clipboard("#noteid"+id, { + var showContent = content.length >= 180 ? content.substr(0, 180) + '...' : content; + var clipboard = new Clipboard("#noteid" + id, { text: function() { return content; } @@ -63,26 +97,112 @@ app.controller('noteCtr', ['$scope', '$state', '$stateParams', '$filter', '$wind }); } - $scope.delNote = function(id){ - toastr.warning('暂未实现', "提示"); + $scope.delNote = function(id, content) { + $scope.currentNoteId = id; + $scope.content = content; + var width = content.length >= 500 ? "50%" : "30%"; + dialog = ngDialog.open({ + template: './views/dialog-del-note.html', + className: 'ngdialog-theme-default', + width: width, + scope: $scope + }); } - $scope.editNote = function(id){ - toastr.warning('暂未实现', "提示"); + $scope.confirmDelNote = function() { + if ($scope.currentNoteId) { + var params = { + id: $scope.currentNoteId + } + ngDialog.close(dialog); + bookmarkService.delNote(params) + .then((data) => { + if (data.result == 1) { + $("#" + $scope.currentNoteId).transition({ + animation: animation(), + duration: 500, + onComplete: function() { + $("#" + $scope.currentNoteId).remove(); + } + }); + toastr.success('备忘删除成功!', "提示"); + } else { + toastr.error('没有找到对应的备忘录,删除失败!请刷新页面再尝试', "提示"); + } + }) + .catch((err) => { + toastr.error('备忘删除失败!错误提示:' + JSON.stringify(err), "提示"); + }); + } else { + toastr.error('删除失败!请刷新页面再尝试', "提示"); + } } - $scope.detailNote = function(id){ - toastr.warning('暂未实现', "提示"); + $scope.editNote = function(id, content) { + $scope.add = true; + $scope.edit = true; + $scope.content = content; + $scope.currentNoteId = id; + } + + $scope.updateNote = function() { + var params = { + id: $scope.currentNoteId, + content: $scope.content, + } + + bookmarkService.updateNote(params) + .then((data) => { + if (data.result == 1) { + toastr.success('备忘更新成功!', "提示"); + $scope.notes.forEach((note) => { + if (note.id == $scope.currentNoteId) { + note.content = $scope.content; + } + }) + $scope.add = false; + $scope.edit = false; + updateEditPos(); + } else { + toastr.error('备忘更新失败!请刷新页面再尝试', "提示"); + } + }) + .catch((err) => { + toastr.error('备忘更新失败!错误提示:' + JSON.stringify(err), "提示"); + }); + } + + $scope.detailNote = function(content) { + $scope.content = content; + var width = content.length >= 500 ? "50%" : "30%"; + dialog = ngDialog.open({ + template: './views/dialog-detail-note.html', + className: 'ngdialog-theme-default', + width: width, + scope: $scope + }); } // $('.js-segment-praise').transition('hide'); - function getNotes(){ - var params = {}; + function getNotes() { + $scope.loadBusy = true; + var params = { + currentPage: $scope.currentPage, + perPageItems: perPageItems, + searchWord: $stateParams.searchWord, + }; bookmarkService.getNotes(params) .then((data) => { - $scope.notes = data; + $scope.notes = data.notes; + $scope.totalPages = Math.ceil(data.totalItems / perPageItems); + $timeout(function() { + timeagoInstance.cancel(); + timeagoInstance.render(document.querySelectorAll('.need_to_be_rendered'), 'zh_CN'); + }, 100) + $scope.loadBusy = false; }) .catch((err) => { $scope.notes = []; + $scope.loadBusy = false; }); } diff --git a/public/scripts/directives/js-init-directive.js b/public/scripts/directives/js-init-directive.js index 3b85471..880f7eb 100644 --- a/public/scripts/directives/js-init-directive.js +++ b/public/scripts/directives/js-init-directive.js @@ -226,56 +226,56 @@ app.filter('searchType', function() { types[2] = 'Github'; types[3] = '栈溢出'; types[4] = '百度'; - + types[5] = '备忘录'; return types[type]; } }); -app.filter('characters', function () { - return function (input, chars, breakOnWord) { - if (isNaN(chars)) return input; - if (chars <= 0) return ''; - if (input && input.length > chars) { - input = input.substring(0, chars); +app.filter('characters', function() { + return function(input, chars, breakOnWord) { + if (isNaN(chars)) return input; + if (chars <= 0) return ''; + if (input && input.length > chars) { + input = input.substring(0, chars); - if (!breakOnWord) { - var lastspace = input.lastIndexOf(' '); - //get last space - if (lastspace !== -1) { - input = input.substr(0, lastspace); + if (!breakOnWord) { + var lastspace = input.lastIndexOf(' '); + //get last space + if (lastspace !== -1) { + input = input.substr(0, lastspace); + } + } else { + while (input.charAt(input.length - 1) === ' ') { + input = input.substr(0, input.length - 1); + } } - }else{ - while(input.charAt(input.length-1) === ' '){ - input = input.substr(0, input.length -1); + return input + '…'; + } + return input; + }; + }) + .filter('splitcharacters', function() { + return function(input, chars) { + if (isNaN(chars)) return input; + if (chars <= 0) return ''; + if (input && input.length > chars) { + var prefix = input.substring(0, chars / 2); + var postfix = input.substring(input.length - chars / 2, input.length); + return prefix + '...' + postfix; + } + return input; + }; + }) + .filter('words', function() { + return function(input, words) { + if (isNaN(words)) return input; + if (words <= 0) return ''; + if (input) { + var inputWords = input.split(/\s+/); + if (inputWords.length > words) { + input = inputWords.slice(0, words).join(' ') + '…'; } } - return input + '…'; - } - return input; - }; -}) -.filter('splitcharacters', function() { - return function (input, chars) { - if (isNaN(chars)) return input; - if (chars <= 0) return ''; - if (input && input.length > chars) { - var prefix = input.substring(0, chars/2); - var postfix = input.substring(input.length-chars/2, input.length); - return prefix + '...' + postfix; - } - return input; - }; -}) -.filter('words', function () { - return function (input, words) { - if (isNaN(words)) return input; - if (words <= 0) return ''; - if (input) { - var inputWords = input.split(/\s+/); - if (inputWords.length > words) { - input = inputWords.slice(0, words).join(' ') + '…'; - } - } - return input; - }; -}); + return input; + }; + }); diff --git a/public/scripts/services/bookmark-service.js b/public/scripts/services/bookmark-service.js index b4c6d8b..5624f2a 100644 --- a/public/scripts/services/bookmark-service.js +++ b/public/scripts/services/bookmark-service.js @@ -392,6 +392,32 @@ app.factory('bookmarkService', ['$http', '$q', function($http, $q) { }); return def.promise; }, + delNote: function(params) { + var def = $q.defer(); + $http.delete('/api/delNote/', { + params: params + }) + .success(function(data) { + def.resolve(data); + }) + .error(function(data) { + def.reject('delNote error'); + }); + return def.promise; + }, + updateNote: function(params) { + var def = $q.defer(); + $http.post('/api/updateNote/', { + params: params + }) + .success(function(data) { + def.resolve(data); + }) + .error(function(data) { + def.reject('updateNote error'); + }); + return def.promise; + }, }; return service; diff --git a/public/views/bookmarks.html b/public/views/bookmarks.html index abcef5c..59b187b 100644 --- a/public/views/bookmarks.html +++ b/public/views/bookmarks.html @@ -147,7 +147,9 @@
- {{ bookmark.created_at }} + 创建于: + +
diff --git a/public/views/dialog-del-note.html b/public/views/dialog-del-note.html new file mode 100644 index 0000000..512c4a0 --- /dev/null +++ b/public/views/dialog-del-note.html @@ -0,0 +1,11 @@ +
+

删除提示

+

您确认要删除备忘:

+ {{ content | characters:600:false }} +

+

如果删除该备忘,那么再也无法查看到。

+
+
+ + +
diff --git a/public/views/dialog-detail-note.html b/public/views/dialog-detail-note.html new file mode 100644 index 0000000..28e8448 --- /dev/null +++ b/public/views/dialog-detail-note.html @@ -0,0 +1,8 @@ +
+

备忘详情

+

{{ content }} +

+
+
+ +
diff --git a/public/views/menus.html b/public/views/menus.html index 77a8380..23e8180 100644 --- a/public/views/menus.html +++ b/public/views/menus.html @@ -1,6 +1,6 @@
diff --git a/public/views/note.html b/public/views/note.html index dadc27d..aeaff5f 100644 --- a/public/views/note.html +++ b/public/views/note.html @@ -1,41 +1,51 @@
-
+
-
取消
-
提交关闭
-
提交继续
+
取消
+
提交关闭
+
提交继续
+
更新
-
-
+
+
-
-
-

{{ note.content | characters:200:false }}

+
+
+ {{ note.content }}
- - - - + + + +
-
- +
+ +
+
+
+
共找到备忘一共约{{notes.length}}个
+
+ +
diff --git a/routes/api.js b/routes/api.js index 0b21775..7a8e398 100644 --- a/routes/api.js +++ b/routes/api.js @@ -1118,7 +1118,7 @@ api.getFaviconByTimer = function() { // http://www.cnblogs.com/zhangwei595806165/p/4984912.html 各种方法都试一遍 var faviconUrl = "http://favicon.byi.pw/?url=" + url; // 默认地址 if (faviconState == 1) { - faviconUrl = "http://g.soz.im/" + url ; + faviconUrl = "http://g.soz.im/" + url; } else if (faviconState == 2) { faviconUrl = "http://www.google.com/s2/favicons?domain=" + url; } @@ -1134,7 +1134,7 @@ api.getFaviconByTimer = function() { }); }).catch((err) => { var newFaviconState = -1; - console.log("boomarkid = " + id + ", url = " + url +", download over") + console.log("boomarkid = " + id + ", url = " + url + ", download over") if (faviconState == 0 || faviconState == 1) { newFaviconState = faviconState + 1; } else if (faviconState == 2) { @@ -1299,11 +1299,49 @@ api.get('/notes', function(req, res) { var params = req.query; params.user_id = req.session.user.id; db.getNotes(params) - .then((notes) => res.json(notes)) + .then((data) => res.json(data)) .catch((err) => console.log('notes', err)); }); +api.delete('/delNote', function(req, res) { + console.log("delBookmark username = ", req.session.username); + if (!req.session.user) { + res.send(401); + return; + } + var noteId = req.query.id; + db.delNote(noteId) + .then((affectedRows) => res.json({ + result: affectedRows + })) + .catch((err) => { + console.log('delBookmark err', err); + res.json({ + result: -1 + }) + }); +}) +api.post('/updateNote', function(req, res) { + console.log("updateNote username = ", req.session.username); + if (!req.session.user) { + res.send(401); + return; + } + + var note = req.body.params; + console.log('hello updateNote', JSON.stringify(note)); + db.updateNote(note.id, note.content) + .then((affectedRows) => res.json({ + result: affectedRows + })) + .catch((err) => { + console.log('updateBookmark err', err); + res.json({ + result: -1 + }) + }); // oops! +}) function md5(str) { return crypto.createHash('md5').update(str).digest('hex');