diff --git a/package.json b/package.json index e21956b..2d7a0a8 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "dependencies": { "cheerio": "^1.0.0-rc.3", "fs-extra": "^9.0.0", + "node-readability": "^3.0.0", "think-cache": "^1.0.0", "think-cache-file": "^1.0.8", "think-logger3": "^1.0.0", diff --git a/src/config/adapter.js b/src/config/adapter.js index 907c704..9395d9b 100644 --- a/src/config/adapter.js +++ b/src/config/adapter.js @@ -30,7 +30,7 @@ exports.cache = { exports.model = { type: 'mysql', common: { - logConnect: isDev, + logConnect: false, logSql: isDev, logger: msg => think.logger.info(msg) }, diff --git a/src/controller/api.js b/src/controller/api.js index 3edf042..1f5e0af 100644 --- a/src/controller/api.js +++ b/src/controller/api.js @@ -1,6 +1,7 @@ const Base = require('./base.js'); const crypto = require('crypto'); const fs = require('fs-extra'); +const read = require('node-readability'); function md5(str) { return crypto.createHash('md5').update(str).digest('hex'); @@ -84,8 +85,8 @@ module.exports = class extends Base { if (param.bookmarkCount) { tag.bookmarkCount = await this.model('bookmarks').where({ tagId: tag.id }).count(); } - if (param.notes) { - tag.bookmarkCount = await this.model('notes').where({ tagId: tag.id }).count(); + if (param.noteCount) { + tag.noteCount = await this.model('notes').where({ tagId: tag.id }).count(); } } this.json({ code: 0, data: tags, msg: '' }); @@ -104,6 +105,19 @@ module.exports = class extends Base { } } + // 获取书签 + // @todo 如果是自己的任意获取,如果是别人的必须公开才能获取 + async bookmarkAction() { + let id = this.get("id"); + try { + let data = await this.model('bookmarks').where({ id }).find(); + this.json({ code: 0, data }); + } catch (error) { + this.json({ code: 1, msg: error.toString() }); + } + } + + // 添加书签 async addBookmarkAction() { let bookmark = this.post(); bookmark.userId = this.ctx.state.user.id; @@ -140,6 +154,69 @@ module.exports = class extends Base { } } + // 点击书签 + async clickBookmarkAction() { + let id = this.post("id"); + try { + let data = await this.model('bookmarks').where({ + userId: this.ctx.state.user.id, + id + }).update({ + clickCount: ['exp', 'clickCount+1'], + lastClick: ['exp', 'NOW()'] + }); + this.json({ code: 0, data }); + } catch (error) { + this.json({ code: 1, msg: error.toString() }); + } + } + + // 更新书签 + async updateBookmarkAction() { + let bookmark = this.post(); + try { + let data = await this.model('bookmarks').where({ + userId: this.ctx.state.user.id, + id: bookmark.id + }).update(bookmark); + this.json({ code: 0, data }); + } catch (error) { + this.json({ code: 1, msg: error.toString() }); + } + } + // 获取文章 + async getArticleAction() { + let url = this.get("url"); + async function readArticle(url) { + return new Promise(function (resolve, reject) { + read(url, (err, article, meta) => { + if (err) { + reject(err) + } else { + resolve({ + title: article.title + }); + article.close(); + } + }); + }) + } + + try { + let article = await readArticle(url); + this.json({ + code: 0, + data: { + title: article.title + } + }); + } catch (error) { + this.json({ + code: 1, + msg: error.toString() + }); + } + } // 新增留言 async addAdviceAction() { let advice = this.post(); @@ -161,4 +238,53 @@ module.exports = class extends Base { this.json({ code: 1, data: '', msg: error.toString() }); } } + + // 新增 + async addNoteAction() { + let note = this.post(); + note.userId = this.ctx.state.user.id; + try { + let data = await this.model("notes").add(note); + this.json({ code: 0, data, msg: `备忘添加成功` }); + } catch (error) { + this.json({ code: 1, msg: error.toString() }); + } + } + + // 更新备忘 + async updateNoteAction() { + let note = this.post(); + note.userId = this.ctx.state.user.id; + try { + let data = await this.model('bookmarks').where({ + userId: this.ctx.state.user.id, + id: note.id + }).update(note); + this.json({ code: 0, data, msg: `备忘更新成功` }); + } catch (error) { + this.json({ code: 1, msg: error.toString() }); + } + } + + // 更新 + async delNoteAction() { + let note = this.post(); + note.userId = this.ctx.state.user.id; + try { + let data = await this.model("notes").where(note).delete(); + this.json({ code: 0, data, msg: `备忘删除成功` }); + } catch (error) { + this.json({ code: 1, msg: error.toString() }); + } + } + + async notesAction() { + let where = {}; + try { + let data = await this.model('notes').where(where).order("createdAt DESC").page(this.get('page'), this.get('pageSize')).countSelect(); + this.json({ code: 0, data }); + } catch (error) { + this.json({ code: 1, msg: error.toString() }); + } + } }; diff --git a/www/scripts/controllers/bookmark-info-controller.js b/www/scripts/controllers/bookmark-info-controller.js index 6c8d6d5..246940e 100644 --- a/www/scripts/controllers/bookmark-info-controller.js +++ b/www/scripts/controllers/bookmark-info-controller.js @@ -1,72 +1,63 @@ -app.controller('bookmarkInfoCtr', ['$scope', '$state', '$timeout', '$sce', '$window', '$filter', '$document', '$timeout', 'bookmarkService', 'pubSubService', 'dataService', function ($scope, $state, $timeout, $sce, $window, $filter, $document, $timeout, bookmarkService, pubSubService, dataService) { - console.log("Hello bookmarkInfoCtr"); - $scope.bookmark = {} - $scope.content = ''; - $scope.loading = false; - - pubSubService.subscribe('TagCtr.showBookmarkInfo', $scope, function (event, bookmark) { - console.log('subscribe TagCtr.showBookmarkInfo', bookmark); - $('.ui.modal.js-bookmark-info').modal({ - closable: false, - }).modal('setting', 'transition', dataService.animation()).modal('show'); - bookmark.favicon_url = 'http://favicon.luchenqun.com/?url=' + bookmark.url; - bookmark.snap_url = bookmark.snap_url || ('./images/snap/' + bookmark.id + '.png'); - $scope.bookmark = bookmark; - $scope.bookmark.description = $sce.trustAsHtml(bookmark.description); - $scope.content = $sce.trustAsHtml(bookmark.content) || ''; - var params = { - url: bookmark.url, - requestId: 1 - } - if (!$scope.content) { - $timeout(function () { - $('.ui.modal.js-bookmark-info').modal("refresh"); - $("p").css("word-wrap", "break-word"); - }, 500); - $scope.loading = true - bookmarkService.getArticle(params) - .then((data) => { - $scope.content = data.content ? $sce.trustAsHtml(data.content) : $sce.trustAsHtml('
数据获取失败,可能是服务器不允许获取,或者是https网站!
'); - setTimeout(function () { - $('.ui.modal.js-bookmark-info').modal && $('.ui.modal.js-bookmark-info').modal("refresh"); - }, 100); - $scope.loading = false; - }) - .catch((err) => { - $scope.content = $sce.trustAsHtml('数据获取失败:' + JSON.stringify(err) + '
'); - $scope.loading = false; - }) - } else { - setTimeout(function () { - $('.ui.modal.js-bookmark-info').modal && $('.ui.modal.js-bookmark-info').modal("refresh"); - }, 10); - setTimeout(function () { - $('.modals').animate({ scrollTop: 0 }, 100); - }, 500); - } - }); - - $scope.jumpToUrl = function (url, id) { - $window.open(url, '_blank'); - if ($scope.bookmark.own) { - bookmarkService.clickBookmark({ - id: id - }); - $scope.bookmark.click_count += 1; - $scope.bookmark.last_click = $filter("date")(new Date(), "yyyy-MM-dd HH:mm:ss"); - } - } - - $scope.copy = function (url) { - dataService.clipboard(url); - } - - $document.bind("keydown", function (event) { - $scope.$apply(function () { - // Esc按键,退出 - if (event.keyCode == 27) { - $('.ui.modal.js-bookmark-info').modal("hide"); - } - }) - }); -}]); +app.controller('bookmarkInfoCtr', ['$scope', '$state', '$timeout', '$sce', '$window', '$filter', '$document', '$timeout', 'bookmarkService', 'pubSubService', 'dataService', function ($scope, $state, $timeout, $sce, $window, $filter, $document, $timeout, bookmarkService, pubSubService, dataService) { + console.log("Hello bookmarkInfoCtr"); + $scope.bookmark = {} + $scope.content = ''; + $scope.loading = false; + + pubSubService.subscribe('TagCtr.showBookmarkInfo', $scope, async function (event, bookmark) { + console.log('subscribe TagCtr.showBookmarkInfo', bookmark); + $('.ui.modal.js-bookmark-info').modal({ + closable: false, + }).modal('setting', 'transition', dataService.animation()).modal('show'); + bookmark.favicon_url = 'http://favicon.luchenqun.com/?url=' + bookmark.url; + $scope.bookmark = bookmark; + $scope.bookmark.description = $sce.trustAsHtml(bookmark.description); + $scope.content = $sce.trustAsHtml(bookmark.content) || ''; + if (!$scope.content) { + $timeout(function () { + $('.ui.modal.js-bookmark-info').modal("refresh"); + $("p").css("word-wrap", "break-word"); + }, 500); + $scope.loading = true; + try { + let data = get("getArticle", { url: bookmark.url }); + $scope.content = data.content ? $sce.trustAsHtml(data.content) : $sce.trustAsHtml('数据获取失败,可能是服务器不允许获取,或者是https网站!
'); + setTimeout(function () { + $('.ui.modal.js-bookmark-info').modal && $('.ui.modal.js-bookmark-info').modal("refresh"); + }, 100); + } catch (error) { + + } + $scope.loading = false; + } else { + setTimeout(function () { + $('.ui.modal.js-bookmark-info').modal && $('.ui.modal.js-bookmark-info').modal("refresh"); + }, 10); + setTimeout(function () { + $('.modals').animate({ scrollTop: 0 }, 100); + }, 500); + } + }); + + $scope.jumpToUrl = async function (url, id) { + $window.open(url, '_blank'); + if ($scope.bookmark.own) { + await post('clickBookmark', { id }); + $scope.bookmark.clickCount += 1; + $scope.bookmark.lastClick = $filter("date")(new Date(), "yyyy-MM-dd HH:mm:ss"); + } + } + + $scope.copy = function (url) { + dataService.clipboard(url); + } + + $document.bind("keydown", function (event) { + $scope.$apply(function () { + // Esc按键,退出 + if (event.keyCode == 27) { + $('.ui.modal.js-bookmark-info').modal("hide"); + } + }) + }); +}]); diff --git a/www/scripts/controllers/bookmarks-controller.js b/www/scripts/controllers/bookmarks-controller.js index c02939f..05c56ba 100644 --- a/www/scripts/controllers/bookmarks-controller.js +++ b/www/scripts/controllers/bookmarks-controller.js @@ -1,414 +1,409 @@ -app.controller('bookmarksCtr', ['$scope', '$state', '$stateParams', '$filter', '$window', '$timeout', '$document', 'ngDialog', 'bookmarkService', 'pubSubService', 'dataService', function ($scope, $state, $stateParams, $filter, $window, $timeout, $document, ngDialog, bookmarkService, pubSubService, dataService) { - console.log("Hello bookmarksCtr...", $stateParams); - if (dataService.smallDevice()) { - $window.location = "http://m.mybookmark.cn/#/tags"; - return; - } - - $scope.bookmarks = []; // 书签数据 - $scope.showSearch = false; // 搜索对话框 - $scope.bookmarkNormalHover = false; - $scope.bookmarkEditHover = false; - $scope.hoverBookmark = null; - var menusScope = $('div[ng-controller="menuCtr"]').scope(); - $scope.showStyle = ($stateParams && $stateParams.showStyle) || (menusScope && menusScope.showStyle); // 显示风格'navigate', 'costomTag', 'card', 'table' - const perPageItems = 20; - var dialog = null; - $scope.totalPages = 0; - $scope.currentPage = 1; - $scope.inputPage = ''; - $scope.loadBusy = false; - $scope.waitDelBookmark = {}; - $scope.order = [false, false, false]; - $scope.order[($stateParams && $stateParams.orderIndex) || 0] = true; - $scope.bookmarkData = {}; - $scope.costomTags = [{ - index: 0, - clicked: true, - name: '最近使用' - }, { - index: 1, - clicked: false, - name: '最近添加' - }, { - index: 2, - clicked: false, - name: '最多使用' - }] - var timeagoInstance = timeago(); - - updateShowStyle(); - getBookmarks(); - - $scope.changeCurrentPage = function (currentPage) { - currentPage = parseInt(currentPage) || 0; - console.log('currentPage = ', currentPage); - if (currentPage <= $scope.totalPages && currentPage >= 1) { - $scope.currentPage = currentPage; - $scope.inputPage = ''; - getBookmarks(); - } else { - $scope.currentPage = $scope.totalPages - } - } - - $scope.jumpToUrl = function (url, id) { - $window.open(url, '_blank'); - bookmarkService.clickBookmark({ - id: id - }); - - if ($scope.showStyle != 'navigate') { - var bookmarks = $scope.showStyle == 'table' ? $scope.bookmarkData.bookmarks : $scope.bookmarkData; - bookmarks.forEach(function (bookmark) { - if (bookmark.id == id) { - bookmark.click_count += 1; - bookmark.last_click = $filter("date")(new Date(), "yyyy-MM-dd HH:mm:ss"); - } - }) - } else { - - } - - $timeout(function () { - timeagoInstance.cancel(); - timeagoInstance.render(document.querySelectorAll('.need_to_be_rendered'), 'zh_CN'); - }, 100) - } - - $scope.delBookmark = function (bookmark) { - console.log('delBookmark..........') - $scope.waitDelBookmark = $.extend(true, {}, bookmark); // 利用jQuery执行深度拷贝 - dialog = ngDialog.open({ - template: './views/dialog-del-bookmark.html', - className: 'ngdialog-theme-default', - scope: $scope - }); - } - - $scope.confirmDelBookmark = function (bookmarkId) { - var params = { - id: bookmarkId - } - ngDialog.close(dialog); - bookmarkService.delBookmark(params) - .then((data) => { - $("#" + bookmarkId).transition({ - animation: dataService.animation(), - duration: 500, - onComplete: function () { - $("#" + bookmarkId).remove(); - } - }); - toastr.success($scope.waitDelBookmark.title + ' 书签删除成功!', "提示"); - }) - .catch((err) => { - toastr.error($scope.waitDelBookmark.title + ' 书签删除失败!错误提示:' + JSON.stringify(err), "提示"); - }); - } - - $scope.editBookmark = function (bookmarkId) { - pubSubService.publish('bookmarksCtr.editBookmark', { - 'bookmarkId': bookmarkId - }); - } - - $scope.detailBookmark = function (b) { - var bookmark = $.extend(true, {}, b); // 利用jQuery执行深度拷贝 - bookmark.own = true; - if ($scope.showStyle == 'navigate') { - bookmark.tags = [{ - id: bookmark.tag_id, - name: bookmark.tag_name - }]; - } - pubSubService.publish('TagCtr.showBookmarkInfo', bookmark); - bookmarkService.clickBookmark({ - id: bookmark.id - }); - } - - $scope.copy = function (url) { - dataService.clipboard(url); - } - - $scope.jumpToTags = function (tagId) { - $state.go('tags', { - tagId: tagId, - }) - } - - $scope.addBookmarkbyFile = function () { - console.log("addBookmarkbyFile"); - $state.go('settings', { - formIndex: 2, - }); - pubSubService.publish('Common.menuActive', { - login: true, - index: dataService.LoginIndexSettings - }); - } - - $scope.closeMsg = function () { - $('.js-msg').transition({ - animation: dataService.animation(), - duration: '500ms', - onComplete: function () { - $(".js-msg").remove(); - } - }); - } - - $scope.loadCardData = function () { - console.log('loadCardData.........') - if (!$scope.loadBusy) { - $scope.changeCurrentPage($scope.currentPage += 1) - } - } - - $scope.changeOrder = function (index) { - if (index < 0 || index >= $scope.order.length) { - return; - } - $scope.order = $scope.order.map(() => false); - $scope.order[index] = true; - $scope.bookmarks = []; - if ($scope.order[0]) { - $scope.bookmarkData.bookmarks.sort(clickCmp) - $scope.bookmarkData.bookmarks.forEach((bookmark) => { - if (bookmark.type == 1) { - $scope.bookmarks.push(bookmark); - } - }) - } else if ($scope.order[1]) { - $scope.bookmarkData.bookmarks.sort((a, b) => a.created_at >= b.created_at ? -1 : 1); - $scope.bookmarkData.bookmarks.forEach((bookmark) => { - if (bookmark.type == 2) { - $scope.bookmarks.push(bookmark); - } - }) - } else { - $scope.bookmarkData.bookmarks.sort((a, b) => a.last_click >= b.last_click ? -1 : 1); - $scope.bookmarkData.bookmarks.forEach((bookmark) => { - if (bookmark.type == 3) { - $scope.bookmarks.push(bookmark); - } - }) - } - $timeout(function () { - timeagoInstance.cancel(); - timeagoInstance.render(document.querySelectorAll('.need_to_be_rendered'), 'zh_CN'); - }, 100) - } - - $scope.updateCostomTagBookmarks = function (index) { - console.log('updateCostomTagBookmarks index = ' + index); - $scope.costomTags.forEach((tag, i) => { - $scope.costomTags[i].clicked = false; - }) - $scope.costomTags[index].clicked = true; - - if (index == 0) { - $scope.bookmarkData.sort((a, b) => a.last_click >= b.last_click ? -1 : 1); - } else if (index == 1) { - $scope.bookmarkData.sort((a, b) => a.created_at >= b.created_at ? -1 : 1); - } else { - $scope.bookmarkData.sort(clickCmp) - } - $scope.bookmarks = $scope.bookmarkData.slice(0, 79); - } - - pubSubService.subscribe('EditCtr.inserBookmarsSuccess', $scope, function (event, data) { - console.log('subscribe EditCtr.inserBookmarsSuccess', JSON.stringify(data)); - - var menusScope = $('div[ng-controller="menuCtr"]').scope(); - if (menusScope.login && menusScope.selectLoginIndex == 0) { - $scope.forbidTransition = true; - if ($scope.showStyle == 'card') { - var find = false; - $scope.bookmarks.forEach((bookmark) => { - if (bookmark.id == data.id) { - bookmark.title = data.title; - bookmark.url = data.url; - bookmark.tags = data.tags; - bookmark.description = data.description; - find = true; - } - }) - 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; - getBookmarks(); - } - } - }); - - $scope.setHoverBookmark = function (bookmark) { - $scope.hoverBookmark = bookmark; - } - - // 在输入文字的时候也会触发,所以不要用Ctrl,Shift之类的按键 - $document.bind("keydown", function (event) { - $scope.$apply(function () { - var key = event.key.toUpperCase(); - console.log(key); - if ($scope.hoverBookmark && dataService.keyShortcuts()) { - if (key == 'E') { - $scope.editBookmark($scope.hoverBookmark.id) - } else if (key == 'I') { - $scope.detailBookmark($scope.hoverBookmark) - } else if (key == 'D') { - $scope.delBookmark($scope.hoverBookmark) - } else if (key == 'C') { - $scope.copy($scope.hoverBookmark.url) - } - } - }) - }); - - async function getBookmarks() { - var params = {} - params.showStyle = $scope.showStyle - params.currentPage = $scope.currentPage; - params.perPageItems = perPageItems; - - if (!params.showStyle) { - bookmarkService.userInfo({}) - .then((user) => { - $scope.showStyle = (user && user.show_style) || 'navigate'; - updateShowStyle(); - getBookmarks(); // 拿到默认显示风格了,继续取获取书签 - }) - .catch((err) => dataService.netErrorHandle(err, $state)); - } else { - $scope.loadBusy = true; - if (params.showStyle == 'table' && (!$scope.forbidTransition)) { - $('.js-table-bookmarks').transition('hide'); - } - bookmarkService.getBookmarks(params) - .then((data) => { - if (params.showStyle != 'navigate') { - $scope.bookmarkData = data; - $scope.totalPages = Math.ceil(data.totalItems / perPageItems); - if (data.totalItems == 0) { - toastr.info('您还没有书签,请点击菜单栏的添加按钮进行添加', "提示"); - } - if (params.showStyle == 'card') { - $scope.bookmarkData.bookmarks.sort((a, b) => a.created_at >= b.created_at ? -1 : 1); - $scope.bookmarkData.bookmarks.forEach(bookmark => { - if (bookmark.type == 2) { - bookmark.edit = false; - $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) { - $scope.updateCostomTagBookmarks(tag.index) - } - }) - } else { - $scope.changeOrder($scope.order.indexOf(true)); - } - } else { - $scope.bookmarks = data; - if ($scope.bookmarks.length <= 2) { - $(".js-msg").removeClass("hidden"); - } - if ($scope.bookmarks.length == 0) { - toastr.info('您还没有书签,请点击菜单栏的添加按钮进行添加', "提示"); - } - } - - pubSubService.publish('Common.menuActive', { - login: true, - index: dataService.LoginIndexBookmarks - }); - if (!$scope.forbidTransition) { - transition(); - } - $scope.forbidTransition = false; - $scope.loadBusy = false; - }) - .catch((err) => { - dataService.netErrorHandle(err, $state); - $scope.loadBusy = false; - }); - } - } - - function updateShowStyle() { - $timeout(function () { - if ($scope.showStyle) { - $('.js-bookmark-dropdown' + ' .radio.checkbox').checkbox('set unchecked'); - $('.js-radio-' + $scope.showStyle).checkbox('set checked'); - $('.js-bookmark-dropdown' + ' .field.item').removeClass('active selected'); - $('.js-field-' + $scope.showStyle).addClass('active selected'); - } - }, 1000) - } - - function transition() { - if ($scope.showStyle == 'card' && $scope.currentPage > 1) { - return; - } - var className = 'js-segment-navigate'; - if ($scope.showStyle == 'card') { - className = 'js-segment-card' - } else if ($scope.showStyle == 'table') { - className = 'js-table-bookmarks' - } else if ($scope.showStyle == 'costomTag') { - className = 'js-segment-costomTag' - } - $('.' + className).transition('hide'); - $('.' + className).transition({ - animation: dataService.animation(), - duration: 500, - }); - } - - // TODO: 我要将编辑按钮固定在容器的右上角 - $(window).resize(updateEditPos); - updateEditPos(); - - function updateEditPos() { - if ($scope.showStyle == 'navigate') { - for (var i = 1; i <= 100; i += 10) { - setTimeout(function () { - var offset = $('.js-segment-navigate').offset(); - if (offset) { - var t = offset.top; - var l = offset.left; - var w = $('.js-segment-navigate').width(); - $('.js-bookmark-edit').offset({ - top: t + 10, - left: l + w - 10, - }) - } - }, 100 * i) - } - } - } - - function clickCmp(a, b) { - var click1 = parseInt(a.click_count); - var click2 = parseInt(b.click_count); - if (click1 > click2) { - return -1; - } else if (click1 == click2) { - return a.created_at >= b.created_at ? -1 : 1; - } else { - return 1; - } - } -}]); +app.controller('bookmarksCtr', ['$scope', '$state', '$stateParams', '$filter', '$window', '$timeout', '$document', 'ngDialog', 'bookmarkService', 'pubSubService', 'dataService', function ($scope, $state, $stateParams, $filter, $window, $timeout, $document, ngDialog, bookmarkService, pubSubService, dataService) { + console.log("Hello bookmarksCtr...", $stateParams); + if (dataService.smallDevice()) { + $window.location = "http://m.mybookmark.cn/#/tags"; + return; + } + + $scope.bookmarks = []; // 书签数据 + $scope.showSearch = false; // 搜索对话框 + $scope.bookmarkNormalHover = false; + $scope.bookmarkEditHover = false; + $scope.hoverBookmark = null; + var menusScope = $('div[ng-controller="menuCtr"]').scope(); + $scope.showStyle = ($stateParams && $stateParams.showStyle) || (menusScope && menusScope.showStyle); // 显示风格'navigate', 'costomTag', 'card', 'table' + const perPageItems = 20; + var dialog = null; + $scope.totalPages = 0; + $scope.currentPage = 1; + $scope.inputPage = ''; + $scope.loadBusy = false; + $scope.waitDelBookmark = {}; + $scope.order = [false, false, false]; + $scope.order[($stateParams && $stateParams.orderIndex) || 0] = true; + $scope.bookmarkData = {}; + $scope.costomTags = [{ + index: 0, + clicked: true, + name: '最近使用' + }, { + index: 1, + clicked: false, + name: '最近添加' + }, { + index: 2, + clicked: false, + name: '最多使用' + }] + var timeagoInstance = timeago(); + + updateShowStyle(); + getBookmarks(); + + $scope.changeCurrentPage = function (currentPage) { + currentPage = parseInt(currentPage) || 0; + console.log('currentPage = ', currentPage); + if (currentPage <= $scope.totalPages && currentPage >= 1) { + $scope.currentPage = currentPage; + $scope.inputPage = ''; + getBookmarks(); + } else { + $scope.currentPage = $scope.totalPages + } + } + + $scope.jumpToUrl = async function (url, id) { + $window.open(url, '_blank'); + await post("clickBookmark", { id }); + + + if ($scope.showStyle != 'navigate') { + var bookmarks = $scope.showStyle == 'table' ? $scope.bookmarkData.bookmarks : $scope.bookmarkData; + bookmarks.forEach(function (bookmark) { + if (bookmark.id == id) { + bookmark.click_count += 1; + bookmark.last_click = $filter("date")(new Date(), "yyyy-MM-dd HH:mm:ss"); + } + }) + } else { + + } + + $timeout(function () { + timeagoInstance.cancel(); + timeagoInstance.render(document.querySelectorAll('.need_to_be_rendered'), 'zh_CN'); + }, 100) + } + + $scope.delBookmark = function (bookmark) { + console.log('delBookmark..........') + $scope.waitDelBookmark = $.extend(true, {}, bookmark); // 利用jQuery执行深度拷贝 + dialog = ngDialog.open({ + template: './views/dialog-del-bookmark.html', + className: 'ngdialog-theme-default', + scope: $scope + }); + } + + $scope.confirmDelBookmark = function (bookmarkId) { + var params = { + id: bookmarkId + } + ngDialog.close(dialog); + bookmarkService.delBookmark(params) + .then((data) => { + $("#" + bookmarkId).transition({ + animation: dataService.animation(), + duration: 500, + onComplete: function () { + $("#" + bookmarkId).remove(); + } + }); + toastr.success($scope.waitDelBookmark.title + ' 书签删除成功!', "提示"); + }) + .catch((err) => { + toastr.error($scope.waitDelBookmark.title + ' 书签删除失败!错误提示:' + JSON.stringify(err), "提示"); + }); + } + + $scope.editBookmark = function (id) { + pubSubService.publish('bookmarksCtr.editBookmark', { id }); + } + + $scope.detailBookmark = async function (b) { + var bookmark = $.extend(true, {}, b); // 利用jQuery执行深度拷贝 + bookmark.own = true; + if ($scope.showStyle == 'navigate') { + bookmark.tags = [{ + id: bookmark.tag_id, + name: bookmark.tag_name + }]; + } + pubSubService.publish('TagCtr.showBookmarkInfo', bookmark); + await post("clickBookmark", { id: bookmark.id }); + } + + $scope.copy = function (url) { + dataService.clipboard(url); + } + + $scope.jumpToTags = function (tagId) { + $state.go('tags', { + tagId: tagId, + }) + } + + $scope.addBookmarkbyFile = function () { + console.log("addBookmarkbyFile"); + $state.go('settings', { + formIndex: 2, + }); + pubSubService.publish('Common.menuActive', { + login: true, + index: dataService.LoginIndexSettings + }); + } + + $scope.closeMsg = function () { + $('.js-msg').transition({ + animation: dataService.animation(), + duration: '500ms', + onComplete: function () { + $(".js-msg").remove(); + } + }); + } + + $scope.loadCardData = function () { + console.log('loadCardData.........') + if (!$scope.loadBusy) { + $scope.changeCurrentPage($scope.currentPage += 1) + } + } + + $scope.changeOrder = function (index) { + if (index < 0 || index >= $scope.order.length) { + return; + } + $scope.order = $scope.order.map(() => false); + $scope.order[index] = true; + $scope.bookmarks = []; + if ($scope.order[0]) { + $scope.bookmarkData.bookmarks.sort(clickCmp) + $scope.bookmarkData.bookmarks.forEach((bookmark) => { + if (bookmark.type == 1) { + $scope.bookmarks.push(bookmark); + } + }) + } else if ($scope.order[1]) { + $scope.bookmarkData.bookmarks.sort((a, b) => a.created_at >= b.created_at ? -1 : 1); + $scope.bookmarkData.bookmarks.forEach((bookmark) => { + if (bookmark.type == 2) { + $scope.bookmarks.push(bookmark); + } + }) + } else { + $scope.bookmarkData.bookmarks.sort((a, b) => a.last_click >= b.last_click ? -1 : 1); + $scope.bookmarkData.bookmarks.forEach((bookmark) => { + if (bookmark.type == 3) { + $scope.bookmarks.push(bookmark); + } + }) + } + $timeout(function () { + timeagoInstance.cancel(); + timeagoInstance.render(document.querySelectorAll('.need_to_be_rendered'), 'zh_CN'); + }, 100) + } + + $scope.updateCostomTagBookmarks = function (index) { + console.log('updateCostomTagBookmarks index = ' + index); + $scope.costomTags.forEach((tag, i) => { + $scope.costomTags[i].clicked = false; + }) + $scope.costomTags[index].clicked = true; + + if (index == 0) { + $scope.bookmarkData.sort((a, b) => a.last_click >= b.last_click ? -1 : 1); + } else if (index == 1) { + $scope.bookmarkData.sort((a, b) => a.created_at >= b.created_at ? -1 : 1); + } else { + $scope.bookmarkData.sort(clickCmp) + } + $scope.bookmarks = $scope.bookmarkData.slice(0, 79); + } + + pubSubService.subscribe('EditCtr.inserBookmarsSuccess', $scope, function (event, data) { + console.log('subscribe EditCtr.inserBookmarsSuccess', JSON.stringify(data)); + + var menusScope = $('div[ng-controller="menuCtr"]').scope(); + if (menusScope.login && menusScope.selectLoginIndex == 0) { + $scope.forbidTransition = true; + if ($scope.showStyle == 'card') { + var find = false; + $scope.bookmarks.forEach((bookmark) => { + if (bookmark.id == data.id) { + bookmark.title = data.title; + bookmark.url = data.url; + bookmark.tags = data.tags; + bookmark.description = data.description; + find = true; + } + }) + 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; + getBookmarks(); + } + } + }); + + $scope.setHoverBookmark = function (bookmark) { + $scope.hoverBookmark = bookmark; + } + + // 在输入文字的时候也会触发,所以不要用Ctrl,Shift之类的按键 + $document.bind("keydown", function (event) { + $scope.$apply(function () { + var key = event.key.toUpperCase(); + console.log(key); + if ($scope.hoverBookmark && dataService.keyShortcuts()) { + if (key == 'E') { + $scope.editBookmark($scope.hoverBookmark.id) + } else if (key == 'I') { + $scope.detailBookmark($scope.hoverBookmark) + } else if (key == 'D') { + $scope.delBookmark($scope.hoverBookmark) + } else if (key == 'C') { + $scope.copy($scope.hoverBookmark.url) + } + } + }) + }); + + async function getBookmarks() { + var params = {} + params.showStyle = $scope.showStyle + params.currentPage = $scope.currentPage; + params.perPageItems = perPageItems; + + if (!params.showStyle) { + bookmarkService.userInfo({}) + .then((user) => { + $scope.showStyle = (user && user.show_style) || 'navigate'; + updateShowStyle(); + getBookmarks(); // 拿到默认显示风格了,继续取获取书签 + }) + .catch((err) => dataService.netErrorHandle(err, $state)); + } else { + $scope.loadBusy = true; + if (params.showStyle == 'table' && (!$scope.forbidTransition)) { + $('.js-table-bookmarks').transition('hide'); + } + bookmarkService.getBookmarks(params) + .then((data) => { + if (params.showStyle != 'navigate') { + $scope.bookmarkData = data; + $scope.totalPages = Math.ceil(data.totalItems / perPageItems); + if (data.totalItems == 0) { + toastr.info('您还没有书签,请点击菜单栏的添加按钮进行添加', "提示"); + } + if (params.showStyle == 'card') { + $scope.bookmarkData.bookmarks.sort((a, b) => a.created_at >= b.created_at ? -1 : 1); + $scope.bookmarkData.bookmarks.forEach(bookmark => { + if (bookmark.type == 2) { + bookmark.edit = false; + $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) { + $scope.updateCostomTagBookmarks(tag.index) + } + }) + } else { + $scope.changeOrder($scope.order.indexOf(true)); + } + } else { + $scope.bookmarks = data; + if ($scope.bookmarks.length <= 2) { + $(".js-msg").removeClass("hidden"); + } + if ($scope.bookmarks.length == 0) { + toastr.info('您还没有书签,请点击菜单栏的添加按钮进行添加', "提示"); + } + } + + pubSubService.publish('Common.menuActive', { + login: true, + index: dataService.LoginIndexBookmarks + }); + if (!$scope.forbidTransition) { + transition(); + } + $scope.forbidTransition = false; + $scope.loadBusy = false; + }) + .catch((err) => { + dataService.netErrorHandle(err, $state); + $scope.loadBusy = false; + }); + } + } + + function updateShowStyle() { + $timeout(function () { + if ($scope.showStyle) { + $('.js-bookmark-dropdown' + ' .radio.checkbox').checkbox('set unchecked'); + $('.js-radio-' + $scope.showStyle).checkbox('set checked'); + $('.js-bookmark-dropdown' + ' .field.item').removeClass('active selected'); + $('.js-field-' + $scope.showStyle).addClass('active selected'); + } + }, 1000) + } + + function transition() { + if ($scope.showStyle == 'card' && $scope.currentPage > 1) { + return; + } + var className = 'js-segment-navigate'; + if ($scope.showStyle == 'card') { + className = 'js-segment-card' + } else if ($scope.showStyle == 'table') { + className = 'js-table-bookmarks' + } else if ($scope.showStyle == 'costomTag') { + className = 'js-segment-costomTag' + } + $('.' + className).transition('hide'); + $('.' + className).transition({ + animation: dataService.animation(), + duration: 500, + }); + } + + // TODO: 我要将编辑按钮固定在容器的右上角 + $(window).resize(updateEditPos); + updateEditPos(); + + function updateEditPos() { + if ($scope.showStyle == 'navigate') { + for (var i = 1; i <= 100; i += 10) { + setTimeout(function () { + var offset = $('.js-segment-navigate').offset(); + if (offset) { + var t = offset.top; + var l = offset.left; + var w = $('.js-segment-navigate').width(); + $('.js-bookmark-edit').offset({ + top: t + 10, + left: l + w - 10, + }) + } + }, 100 * i) + } + } + } + + function clickCmp(a, b) { + var click1 = parseInt(a.click_count); + var click2 = parseInt(b.click_count); + if (click1 > click2) { + return -1; + } else if (click1 == click2) { + return a.created_at >= b.created_at ? -1 : 1; + } else { + return 1; + } + } +}]); diff --git a/www/scripts/controllers/edit-controller.js b/www/scripts/controllers/edit-controller.js index ce2f230..8630c53 100644 --- a/www/scripts/controllers/edit-controller.js +++ b/www/scripts/controllers/edit-controller.js @@ -5,7 +5,7 @@ app.controller('editCtr', ['$scope', '$state', '$timeout', '$document', 'ngDialo var cancelDefault = false; init(); - $scope.$watch('url', function (newUrl, oldUrl, scope) { + $scope.$watch('url', async function (newUrl, oldUrl, scope) { $timeout(function () { $scope.urlError = $scope.url == '' && $('.ui.modal.js-add-bookmark').modal('is active'); }); @@ -17,23 +17,21 @@ app.controller('editCtr', ['$scope', '$state', '$timeout', '$document', 'ngDialo requestId: 0, } $scope.loadTitle = true; - bookmarkService.getArticle(params) - .then((data) => { - $scope.loadTitle = false; - $scope.originTitle = data.title; - $scope.title = data.title; + try { + let data = await get('getArticle', { url: newUrl }); + $scope.loadTitle = false; + $scope.originTitle = data.title; + $scope.title = data.title; - if (!$scope.title) { - toastr.error('获取书签标题失败,请手动填入', "提示"); - } else { - $scope.title = data.title.split('-')[0].trim(); - } - }) - .catch((err) => { - console.log('getTitle err', err); - toastr.error('获取书签标题失败:' + JSON.stringify(err) + ',请手动填入', "提示"); - $scope.loadTitle = false; - }) + if (!$scope.title) { + toastr.error('获取书签标题失败,请手动填入', "提示"); + } else { + $scope.title = data.title.split('-')[0].trim(); + } + } catch (error) { + toastr.error('获取书签标题失败:' + JSON.stringify(err) + ',请手动填入', "提示"); + $scope.loadTitle = false; + } } } }); @@ -84,7 +82,7 @@ app.controller('editCtr', ['$scope', '$state', '$timeout', '$document', 'ngDialo return; } if ($scope.tagsError) { - toastr.error('您至少要选择一个分类!最多选择三个分类!如果暂时没想到放到哪个分类,可以先选择未分类', "错误"); + toastr.error('您至少要选择一个分类!如果暂时没想到放到哪个分类,可以先选择未分类', "错误"); return; } if ($scope.titleError) { @@ -98,16 +96,10 @@ app.controller('editCtr', ['$scope', '$state', '$timeout', '$document', 'ngDialo $('.ui.modal.js-add-bookmark').modal('hide'); pubSubService.publish('EditCtr.inserBookmarsSuccess', params); } else { - bookmarkService.updateBookmark(params) - .then((data) => { - $('.ui.modal.js-add-bookmark').modal('hide'); - pubSubService.publish('EditCtr.inserBookmarsSuccess', data); - toastr.success('[ ' + params.title + ' ] 更新成功,将自动重新更新书签!', "提示"); - }) - .catch((err) => { - console.log('updateBookmark err', err); - toastr.error('[ ' + params.title + ' ] 更新失败' + JSON.stringify(err), "提示"); - }); + await post('updateBookmark', params); + $('.ui.modal.js-add-bookmark').modal('hide'); + pubSubService.publish('EditCtr.inserBookmarsSuccess', data); + toastr.success('[ ' + params.title + ' ] 更新成功,将自动重新更新书签!', "提示"); } } @@ -168,7 +160,7 @@ app.controller('editCtr', ['$scope', '$state', '$timeout', '$document', 'ngDialo getTags(); }); - pubSubService.subscribe('bookmarksCtr.editBookmark', $scope, function (event, params) { + pubSubService.subscribe('bookmarksCtr.editBookmark', $scope, async function (event, params) { console.log('subscribe bookmarksCtr.editBookmark', params); $('.ui.modal.js-add-bookmark').modal({ closable: false, @@ -178,36 +170,24 @@ app.controller('editCtr', ['$scope', '$state', '$timeout', '$document', 'ngDialo }, 500); $scope.add = false; $scope.loadTags = true; + $scope.autoGettitle = false; + cancelDefault = false; - bookmarkService.getBookmark(params) - .then((data) => { - console.log('getBookmark ', data); - var bookmark = data.bookmark; - $scope.autoGettitle = false; - $scope.id = (bookmark && bookmark.id) || ''; - $scope.url = (bookmark && bookmark.url) || ''; - $scope.title = (bookmark && bookmark.title) || ''; - $scope.description = (bookmark && bookmark.description) || ''; - $scope.tags = data.tags.map((tag) => { - tag.clicked = false; - return tag; - }); - $scope.public = (bookmark && bookmark.id) || '1'; - $('.ui.checkbox.js-public').checkbox((bookmark && bookmark.public && bookmark.public == '1') ? 'set checked' : 'set unchecked') + let bookmark = await get("bookmark", params); + let tags = await get("tags"); - $timeout(function () { - data.bookmarkTags.forEach((tagId) => { - $scope.tags.forEach((tag) => { - if (tag.id == tagId) { - tag.clicked = true; - } - }) - }); - }); - $scope.loadTags = false; - }) - .catch((err) => console.log('updateBookmark err', err)); + $scope.id = (bookmark && bookmark.id) || ''; + $scope.url = (bookmark && bookmark.url) || ''; + $scope.title = (bookmark && bookmark.title) || ''; + $scope.description = (bookmark && bookmark.description) || ''; + $scope.tags = tags.map((tag) => { + tag.clicked = bookmark.tagId == tag.id; + return tag; + }); + $scope.public = (bookmark && bookmark.id) || '1'; + $('.ui.checkbox.js-public').checkbox((bookmark && bookmark.public && bookmark.public == '1') ? 'set checked' : 'set unchecked') + $scope.loadTags = false; }); pubSubService.subscribe('TagCtr.storeBookmark', $scope, function (event, bookmark) { diff --git a/www/scripts/controllers/menus-controller.js b/www/scripts/controllers/menus-controller.js index 1c9e326..962f447 100644 --- a/www/scripts/controllers/menus-controller.js +++ b/www/scripts/controllers/menus-controller.js @@ -26,12 +26,12 @@ app.controller('menuCtr', ['$scope', '$stateParams', '$state', '$window', '$time $scope.loginMenus = dataService.loginMenus; // 登陆之后显示的菜单数据。uiSerf:内部跳转链接。 $scope.notLoginMenus = dataService.notLoginMenus; // 未登陆显示的菜单数据 - (async () => { - $scope.user = await get('own'); - if (data.username === 'lcq') { + get('own').then(user => { + $scope.user = user; + if ($scope.user.username === 'lcq') { $scope.loginMenus[dataService.LoginIndexHot].show = false; } - })(); + }); $scope.toggleReady = function (ready) { if (ready) { diff --git a/www/scripts/controllers/note-controller.js b/www/scripts/controllers/note-controller.js index 56d9f15..de38882 100644 --- a/www/scripts/controllers/note-controller.js +++ b/www/scripts/controllers/note-controller.js @@ -1,441 +1,387 @@ -app.controller('noteCtr', ['$scope', '$state', '$stateParams', '$filter', '$window', '$timeout', '$document', 'ngDialog', 'bookmarkService', 'pubSubService', 'dataService', function ($scope, $state, $stateParams, $filter, $window, $timeout, $document, ngDialog, bookmarkService, pubSubService, dataService) { - console.log("Hello noteCtr...", $stateParams); - if (dataService.smallDevice()) { - $window.location = "http://m.mybookmark.cn/#/tags"; - return; - } - - const perPageItems = 35; - var dialog = null; - $scope.hoverNote = null; - $scope.loadBusy = false; - $scope.add = false; - $scope.edit = false; - $scope.preContent = ''; - $scope.content = ''; - $scope.currentTagId = null; - $scope.currentNoteId = null; - $scope.tags = []; // 书签数据 - $scope.notes = []; - $scope.totalPages = 0; - $scope.currentPage = 1; - $scope.inputPage = ''; - $scope.searchWord = $stateParams.searchWord - $scope.key = $stateParams.key - $scope.totalItems = 0; - - var timeagoInstance = timeago(); - - bookmarkService.autoLogin() - .then((data) => { - var login = data.logined; - var index = login ? dataService.LoginIndexNote : dataService.NotLoginIndexLogin; - pubSubService.publish('Common.menuActive', { - login: login, - index: index - }); - getTags(); - getNotes(); - }) - .catch((err) => { - dataService.netErrorHandle(err, $state) - }); - - $scope.changeCurrentPage = function (currentPage) { - currentPage = parseInt(currentPage) || 0; - if (currentPage <= $scope.totalPages && currentPage >= 1) { - $scope.currentPage = currentPage; - $scope.inputPage = ''; - getNotes(); - } else { - $scope.currentPage = $scope.totalPages - } - } - - // 快捷键a增加书签 - $document.bind("keydown", function (event) { - $scope.$apply(function () { - // a按键,显示 - var key = event.key.toUpperCase(); - if (key == 'A' && dataService.keyShortcuts() && (!$scope.add)) { - $scope.showAddNote(); - } - }) - }); - - $scope.showAddNote = function () { - $scope.add = (!$scope.add); - $scope.edit = false; - $scope.content = ''; - if ($scope.add) { - $timeout(function () { - $("#noteedit")[0].focus(); - }); - } - console.log('$scope.showAddNote'); - // 没有选中分类,默认一个分类 - if (!$scope.currentTagId) { - $scope.tags.forEach((tag) => { - tag.clicked = false; - if (tag.name == '未分类') { - $scope.currentTagId = tag.id; - tag.clicked = true; - } - }) - } - } - - $scope.addNote = function (close) { - if ($scope.content == '') { - toastr.error('不允许备忘录内容为空!', "提示"); - return; - } - if ($scope.preContent == $scope.content) { - toastr.error('您刚刚添加了这条内容!', "提示"); - return; - } - $scope.add = close; - var tagName = ''; - - $scope.tags.forEach((tag) => { - if ($scope.currentTagId === tag.id) { - tagName = tag.name; - tag.ncnt += 1; - } - if (!$scope.currentTagId) { - if (tag.name == '未分类') { - $scope.currentTagId = tag.id; - tagName = tag.name - } - } - }) - - var note = { - tag_id: $scope.currentTagId, - content: $scope.content, - } - - bookmarkService.addNote(note) - .then((data) => { - // 增加成功,重新获取一次备忘录 - $scope.tags.forEach((tag) => { - tag.clicked = false; - }) - $scope.preContent = $scope.content; - $scope.content = ''; - $scope.currentTagId = null; - $scope.currentPage = 1; - $scope.searchWord = ''; - getNotes(); - }) - .catch((err) => { - console.log('addNote err', err); - $scope.currentTagId = null; - }); - } - - $scope.copy = function (content, $event) { - dataService.clipboard(content); - $event && $event.stopPropagation(); - } - - $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.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: dataService.animation(), - duration: 500, - onComplete: function () { - $("#" + $scope.currentNoteId).remove(); - } - }); - toastr.success('备忘删除成功!', "提示"); - $scope.totalItems -= 1; - } else { - toastr.error('没有找到对应的备忘录,删除失败!请刷新页面再尝试', "提示"); - } - }) - .catch((err) => { - toastr.error('备忘删除失败!错误提示:' + JSON.stringify(err), "提示"); - }); - } else { - toastr.error('删除失败!请刷新页面再尝试', "提示"); - } - } - - $scope.editNote = function (id, content, tagId) { - $scope.add = true; - $scope.edit = true; - $scope.content = content; - $scope.currentNoteId = id; - $scope.currentTagId = tagId; - updateSelectTag(tagId); - } - - $scope.updateNote = function () { - if (!$scope.content) { - toastr.error('更新失败,更新内容不能为空', "提示"); - return; - } - 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) - .then((data) => { - if (data.result == 1) { - toastr.success('备忘更新成功!', "提示"); - $scope.notes.forEach((note) => { - if (note.id == $scope.currentNoteId) { - note.content = $scope.content; - note.tagName = tagName; - note.tag_id = $scope.currentTagId; - toPos(note.id); - } - }) - $scope.add = false; - $scope.edit = false; - } 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 - }); - } - - $scope.closeNote = function () { - $('.js-note').transition({ - animation: dataService.animation(), - duration: '500ms', - onComplete: function () { - $(".js-note").remove(); - } - }); - } - - $scope.setHoverNote = function (note) { - $scope.hoverNote = note; - } - - $scope.clickTag = function (id) { - $scope.currentTagId = id; - $scope.totalItems = 0; - updateSelectTag(id); - - if ($scope.add || $scope.edit) { - - } else { - $scope.currentPage = 1; - getNotes($scope.currentTagId); - } - } - - $scope.noteClick = function (note, flag, $event) { - if (!note.detail || flag) { - var detail = note.detail; - $scope.notes.forEach((note) => { - note.detail = false; - $("#" + note.id).removeClass("secondary"); - }) - note.detail = !detail; - note.detail && $("#" + note.id).addClass("secondary") && toPos(note.id); - } - if (flag) { - $event && $event.stopPropagation(); - } - } - - $scope.share = function (note) { - var time = 100; - if (note.public == '0') { - toastr.info('由于打算分享备忘,系统会自动将备忘的私密状态转为公开状态'); - $scope.updatePublic(note, '1'); - time = 1000; - } - setTimeout(() => { - dataService.clipboard(`https://mybookmark.cn/api/notes/?shareNote=${note.id}`); - toastr.info(`将地址 https://mybookmark.cn/api/notes/?shareNote=${note.id} 发给别人粘贴到浏览器地址栏就可以访问到你分享的备忘啦!`, "提示"); - }, time) - - } - - $scope.updatePublic = function (note, public) { - var params = { - id: note.id, - public: public, - } - - bookmarkService.updateNotePublic(params) - .then((data) => { - if (data.result == 1) { - public == 1 && toastr.success('备忘已由私密状态转为公开状态', "提示"); - public == 0 && toastr.success('备忘已由公开状态转为私密状态', "提示"); - note.public = public; - } else { - toastr.error('备忘状态更新失败', "提示"); - } - }) - .catch((err) => { - toastr.error('备忘更新失败!错误提示:' + JSON.stringify(err), "提示"); - }); - } - - function updateSelectTag(tagId) { - $scope.tags.forEach((tag) => { - tag.clicked = false; - if (tag.id == tagId) { - tag.clicked = true; - t = tag; - } - }) - } - - // 在输入文字的时候也会触发,所以不要用Ctrl,Shift之类的按键 - $document.bind("keydown", function (event) { - $scope.$apply(function () { - var key = event.key.toUpperCase(); - if ($scope.hoverNote && dataService.keyShortcuts()) { - if (key == 'E') { - $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') { - $scope.delNote($scope.hoverNote.id, $scope.hoverNote.content) - } else if (key == 'C') { - $scope.copy($scope.hoverNote.content) - } - } - }) - }); - - function getNotes(tagId) { - $scope.notes = []; - $scope.loadBusy = true; - var params = { - currentPage: $scope.currentPage, - perPageItems: perPageItems, - searchWord: $scope.searchWord, - }; - if (tagId || $scope.currentTagId) { - params.tagId = tagId || $scope.currentTagId; - } - bookmarkService.getNotes(params) - .then((data) => { - $scope.notes = data.notes; - $scope.notes.forEach((note) => { - note.brief = note.content || ""; - while (note.brief.indexOf("\n") > 0) { - note.brief = note.brief.replace(/\n/g, ""); - } - note.brief = " " + note.brief.substring(0, 200) + (note.content.length > 200 ? " ......" : ""); - }) - $scope.totalPages = Math.ceil(data.totalItems / perPageItems); - $scope.totalItems = data.totalItems; - $timeout(function () { - timeagoInstance.cancel(); - timeagoInstance.render(document.querySelectorAll('.need_to_be_rendered'), 'zh_CN'); - // 如果需要增加书签 - if ($scope.key == 'A') { - $scope.key = null; - $scope.showAddNote(); - } - }, 100) - $scope.loadBusy = false; - if ($scope.totalItems == 0) { - $(".js-note").removeClass("hidden"); - } - transition(); - }) - .catch((err) => { - $scope.notes = []; - $scope.loadBusy = false; - }); - } - - 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; - }); - } - - $('.js-note-card').transition('hide'); - - function transition() { - var className = 'js-note-card'; - $('.' + className).transition('hide'); - $('.' + className).transition({ - animation: dataService.animation(), - duration: 500, - }); - } - - function toPos(id) { - setTimeout(function () { - $('html,body').animate({ scrollTop: $('#' + id).offset().top - 20 }, 100); - }, 36); - } -}]); +app.controller('noteCtr', ['$scope', '$state', '$stateParams', '$filter', '$window', '$timeout', '$document', 'ngDialog', 'bookmarkService', 'pubSubService', 'dataService', function ($scope, $state, $stateParams, $filter, $window, $timeout, $document, ngDialog, bookmarkService, pubSubService, dataService) { + console.log("Hello noteCtr...", $stateParams); + if (dataService.smallDevice()) { + $window.location = "http://m.mybookmark.cn/#/tags"; + return; + } + + const perPageItems = 35; + var dialog = null; + $scope.hoverNote = null; + $scope.loadBusy = false; + $scope.add = false; + $scope.edit = false; + $scope.preContent = ''; + $scope.content = ''; + $scope.currentTagId = null; + $scope.currentNoteId = null; + $scope.tags = []; // 书签数据 + $scope.notes = []; + $scope.totalPages = 0; + $scope.currentPage = 1; + $scope.inputPage = ''; + $scope.searchWord = $stateParams.searchWord + $scope.key = $stateParams.key + $scope.totalItems = 0; + + var timeagoInstance = timeago(); + + get('own').then(user => { + pubSubService.publish('Common.menuActive', { + login: true, + index: dataService.LoginIndexNote + }); + getTags(); + getNotes(); + }); + + $scope.changeCurrentPage = function (currentPage) { + currentPage = parseInt(currentPage) || 0; + if (currentPage <= $scope.totalPages && currentPage >= 1) { + $scope.currentPage = currentPage; + $scope.inputPage = ''; + getNotes(); + } else { + $scope.currentPage = $scope.totalPages + } + } + + // 快捷键a增加书签 + $document.bind("keydown", function (event) { + $scope.$apply(function () { + // a按键,显示 + var key = event.key.toUpperCase(); + if (key == 'A' && dataService.keyShortcuts() && (!$scope.add)) { + $scope.showAddNote(); + } + }) + }); + + $scope.showAddNote = function () { + $scope.add = (!$scope.add); + $scope.edit = false; + $scope.content = ''; + if ($scope.add) { + $timeout(function () { + $("#noteedit")[0].focus(); + }); + } + console.log('$scope.showAddNote'); + // 没有选中分类,默认一个分类 + if (!$scope.currentTagId) { + $scope.tags.forEach((tag) => { + tag.clicked = false; + if (tag.name == '未分类') { + $scope.currentTagId = tag.id; + tag.clicked = true; + } + }) + } + } + + $scope.addNote = async function (close) { + if ($scope.content == '') { + toastr.error('不允许备忘录内容为空!', "提示"); + return; + } + if ($scope.preContent == $scope.content) { + toastr.error('您刚刚添加了这条内容!', "提示"); + return; + } + $scope.add = close; + var tagName = ''; + + $scope.tags.forEach((tag) => { + if ($scope.currentTagId === tag.id) { + tagName = tag.name; + tag.noteCount += 1; + } + if (!$scope.currentTagId) { + if (tag.name == '未分类') { + $scope.currentTagId = tag.id; + tagName = tag.name + } + } + }) + + var note = { + tagId: $scope.currentTagId, + content: $scope.content, + } + + await post("addNote", note); + + // 增加成功,重新获取一次备忘录 + $scope.tags.forEach((tag) => { + tag.clicked = false; + }) + $scope.preContent = $scope.content; + $scope.content = ''; + $scope.currentTagId = null; + $scope.currentPage = 1; + $scope.searchWord = ''; + getNotes(); + } + + $scope.copy = function (content, $event) { + dataService.clipboard(content); + $event && $event.stopPropagation(); + } + + $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.confirmDelNote = async function () { + if ($scope.currentNoteId) { + var params = { + id: $scope.currentNoteId + } + ngDialog.close(dialog); + await post('delNote', params) + $("#" + $scope.currentNoteId).transition({ + animation: dataService.animation(), + duration: 500, + onComplete: function () { + $("#" + $scope.currentNoteId).remove(); + } + }); + $scope.totalItems -= 1; + } else { + toastr.error('删除失败!请刷新页面再尝试', "提示"); + } + } + + $scope.editNote = function (id, content, tagId) { + $scope.add = true; + $scope.edit = true; + $scope.content = content; + $scope.currentNoteId = id; + $scope.currentTagId = tagId; + updateSelectTag(tagId); + } + + $scope.updateNote = async function () { + if (!$scope.content) { + toastr.error('更新失败,更新内容不能为空', "提示"); + return; + } + 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, + tagId: $scope.currentTagId, + } + + await post("updateNote", params); + $scope.notes.forEach((note) => { + if (note.id == $scope.currentNoteId) { + note.content = $scope.content; + note.tagName = tagName; + note.tag_id = $scope.currentTagId; + toPos(note.id); + } + }) + $scope.add = false; + $scope.edit = false; + } + + $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 + }); + } + + $scope.closeNote = function () { + $('.js-note').transition({ + animation: dataService.animation(), + duration: '500ms', + onComplete: function () { + $(".js-note").remove(); + } + }); + } + + $scope.setHoverNote = function (note) { + $scope.hoverNote = note; + } + + $scope.clickTag = function (id) { + $scope.currentTagId = id; + $scope.totalItems = 0; + updateSelectTag(id); + + if ($scope.add || $scope.edit) { + + } else { + $scope.currentPage = 1; + getNotes($scope.currentTagId); + } + } + + $scope.noteClick = function (note, flag, $event) { + if (!note.detail || flag) { + var detail = note.detail; + $scope.notes.forEach((note) => { + note.detail = false; + $("#" + note.id).removeClass("secondary"); + }) + note.detail = !detail; + note.detail && $("#" + note.id).addClass("secondary") && toPos(note.id); + } + if (flag) { + $event && $event.stopPropagation(); + } + } + + $scope.share = function (note) { + var time = 100; + if (note.public == '0') { + toastr.info('由于打算分享备忘,系统会自动将备忘的私密状态转为公开状态'); + $scope.updatePublic(note, '1'); + time = 1000; + } + setTimeout(() => { + dataService.clipboard(`https://mybookmark.cn/api/notes/?shareNote=${note.id}`); + toastr.info(`将地址 https://mybookmark.cn/api/notes/?shareNote=${note.id} 发给别人粘贴到浏览器地址栏就可以访问到你分享的备忘啦!`, "提示"); + }, time) + } + + $scope.updatePublic = async function (note, public) { + var params = { + id: note.id, + public: public, + } + + await post("updateNode", params); + note.public = public; + } + + function updateSelectTag(tagId) { + $scope.tags.forEach((tag) => { + tag.clicked = false; + if (tag.id == tagId) { + tag.clicked = true; + t = tag; + } + }) + } + + // 在输入文字的时候也会触发,所以不要用Ctrl,Shift之类的按键 + $document.bind("keydown", function (event) { + $scope.$apply(function () { + var key = event.key.toUpperCase(); + if ($scope.hoverNote && dataService.keyShortcuts()) { + if (key == 'E') { + $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') { + $scope.delNote($scope.hoverNote.id, $scope.hoverNote.content) + } else if (key == 'C') { + $scope.copy($scope.hoverNote.content) + } + } + }) + }); + + async function getNotes(tagId) { + $scope.notes = []; + $scope.loadBusy = true; + var params = { + page: $scope.currentPage, + pageSize: perPageItems, + searchWord: $scope.searchWord, + tagId: tagId || $scope.currentTagId + }; + + try { + let reply = await get("notes", params); + $scope.notes = reply.data; + $scope.notes.forEach((note) => { + note.brief = note.content || ""; + while (note.brief.indexOf("\n") > 0) { + note.brief = note.brief.replace(/\n/g, ""); + } + note.brief = " " + note.brief.substring(0, 200) + (note.content.length > 200 ? " ......" : ""); + }) + $scope.totalPages = reply.totalPages; + $scope.totalItems = reply.count; + $timeout(function () { + timeagoInstance.cancel(); + timeagoInstance.render(document.querySelectorAll('.need_to_be_rendered'), 'zh_CN'); + // 如果需要增加书签 + if ($scope.key == 'A') { + $scope.key = null; + $scope.showAddNote(); + } + }, 100) + $scope.loadBusy = false; + if ($scope.totalItems == 0) { + $(".js-note").removeClass("hidden"); + } + transition(); + } catch (error) { + $scope.notes = []; + $scope.loadBusy = false; + } + } + + async function getTags() { + $scope.loadBusy = true; + $scope.tags = [] + + let tags = await get('tags', { noteCount: true }); + let find = false; + tags.forEach((tag) => { + $scope.tags.push(tag); + if (tag.id == $scope.currentTagId) { + find = true; // 如果是删了分类返回来,那么要重新默认选中第一个分类 + } + }) + if (!find) $scope.currentTagId = null; + $scope.loadBusy = false; + } + + $('.js-note-card').transition('hide'); + + function transition() { + var className = 'js-note-card'; + $('.' + className).transition('hide'); + $('.' + className).transition({ + animation: dataService.animation(), + duration: 500, + }); + } + + function toPos(id) { + setTimeout(function () { + $('html,body').animate({ scrollTop: $('#' + id).offset().top - 20 }, 100); + }, 36); + } +}]); \ No newline at end of file diff --git a/www/scripts/controllers/search-controller.js b/www/scripts/controllers/search-controller.js index d17f20e..2403fad 100644 --- a/www/scripts/controllers/search-controller.js +++ b/www/scripts/controllers/search-controller.js @@ -1,312 +1,309 @@ -app.controller('searchCtr', ['$scope', '$state', '$stateParams', '$filter', '$window', '$timeout', '$document', 'ngDialog', 'bookmarkService', 'pubSubService', 'dataService', function ($scope, $state, $stateParams, $filter, $window, $timeout, $document, ngDialog, bookmarkService, pubSubService, dataService) { - console.log("Hello searchCtr...", $stateParams); - if (dataService.smallDevice()) { - $window.location = "http://m.mybookmark.cn/#/tags"; - return; - } - - const perPageItems = 20; - var dialog = null; - $scope.hoverBookmark = null; - $scope.searchBookmarks = []; // 书签数据 - $scope.showSearch = false; // - $scope.showTags = false; // - $scope.searchWord = ($stateParams && $stateParams.searchWord) || '' - $scope.dateCreateBegin = ''; - $scope.dateCreateEnd = ''; - $scope.dateClickBegin = ''; - $scope.dateClickEnd = ''; - $scope.clickCount = ''; - $scope.username = ''; - $scope.userRange = ''; - $scope.bookmarkCount = 0; - $scope.tags = [] - $scope.totalPages = 0; - $scope.currentPage = 1; - $scope.inputPage = ''; - $scope.loading = false; - $scope.waitDelBookmark = {}; - $scope.searchHotBookmarks = false; - var timeagoInstance = timeago(); - - $scope.changeCurrentPage = function (currentPage) { - currentPage = parseInt(currentPage) || 0; - console.log(currentPage); - if (currentPage <= $scope.totalPages && currentPage >= 1) { - $scope.currentPage = currentPage; - $scope.inputPage = ''; - $scope.search(); - } - } - - bookmarkService.getTags({}) - .then((data) => { - $scope.tags = data; - }) - .catch((err) => console.log('getTags err', err)); - // 默认登陆 - pubSubService.publish('Common.menuActive', { - login: true, - index: dataService.LoginIndexBookmarks - }); - - var searchParams = { - searchWord: $scope.searchWord, - currentPage: 1, - perPageItems: perPageItems, - userRange: '1', // 默认搜索自己的书签 - } - if ($scope.searchWord) { - searchBookmarks(searchParams); - } else { - toastr.warning("请输入搜索关键字再进行查询!", "提示"); - } - - $scope.jumpToUrl = function (url, id) { - if (!$scope.edit) { - $window.open(url); - bookmarkService.clickBookmark({ - id: id - }); - $scope.searchBookmarks.forEach(function (bookmark) { - if (bookmark.id == id && bookmark.own) { - bookmark.click_count += 1; - bookmark.last_click = $filter("date")(new Date(), "yyyy-MM-dd HH:mm:ss"); - } - }) - $timeout(function () { - timeagoInstance.cancel(); - timeagoInstance.render(document.querySelectorAll('.need_to_be_rendered'), 'zh_CN'); - }, 100) - } - } - - $scope.delBookmark = function (bookmark) { - $scope.waitDelBookmark = $.extend(true, {}, bookmark); // 利用jQuery执行深度拷贝 - dialog = ngDialog.open({ - template: './views/dialog-del-bookmark.html', - className: 'ngdialog-theme-default', - scope: $scope - }); - } - - $scope.confirmDelBookmark = function (bookmarkId) { - var params = { - id: bookmarkId - } - ngDialog.close(dialog); - bookmarkService.delBookmark(params) - .then((data) => { - $("#" + bookmarkId).transition({ - animation: dataService.animation(), - duration: 500, - onComplete: function () { - $("#" + bookmarkId).remove(); - } - }); - toastr.success($scope.waitDelBookmark.title + ' 书签删除成功!', "提示"); - }) - .catch((err) => { - toastr.error($scope.waitDelBookmark.title + ' 书签删除失败!错误提示:' + JSON.stringify(err), "提示"); - }); - } - - $scope.editBookmark = function (bookmarkId) { - pubSubService.publish('bookmarksCtr.editBookmark', { - 'bookmarkId': bookmarkId - }); - } - - $scope.detailBookmark = function (bookmark) { - pubSubService.publish('TagCtr.showBookmarkInfo', bookmark); - } - - $scope.storeBookmark = function (bookmark) { - var b = $.extend(true, {}, bookmark); // 利用jQuery执行深度拷贝 - pubSubService.publish('TagCtr.storeBookmark', b); - } - - $scope.favoriteBookmark = function (b) { - var bookmark = {} - bookmark.description = ''; - bookmark.title = b.title; - bookmark.url = b.url; - bookmark.public = 1; - bookmark.click_count = 1; - - bookmarkService.favoriteBookmark(bookmark) - .then((data) => { - pubSubService.publish('EditCtr.inserBookmarsSuccess', data); - if (data.title) { - toastr.success('[ ' + data.title + ' ] 收藏成功!', "提示"); - } else { - toastr.error('[ ' + bookmark.title + ' ] 收藏失败!', "提示"); - } - }) - .catch((err) => { - toastr.error('[ ' + bookmark.title + ' ] 收藏失败,' + JSON.stringify(err), "提示"); - }); - } - - $scope.copy = function (url) { - dataService.clipboard(url); - } - - $scope.search = function (page) { - var params = {} - params.userRange = $('.js-user-range').dropdown('get value'); - if (params.userRange == '1') { - var tags = $('.js-search-tags').dropdown('get value') - if (tags) { - params.tags = tags; - } - } else if ($scope.username) { - params.username = $scope.username - } - if ($scope.searchWord) { - params.searchWord = $scope.searchWord; - } - - var dateCreate = $('.js-create-date').dropdown('get value') || undefined; - console.log('dateCreate = ', dateCreate) - if (dateCreate) { - if (dateCreate != -1) { - params.dateCreate = dateCreate; - } - } else { - params.dateCreateBegin = $scope.dateCreateBegin; - params.dateCreateEnd = $scope.dateCreateEnd; - } - - var dateClick = $('.js-click-date').dropdown('get value') || undefined; - console.log('dateClick = ', dateClick) - if (dateClick) { - if (dateClick != -1) { - params.dateClick = dateClick - } - } else { - params.dateClickBegin = $scope.dateClickBegin; - params.dateClickEnd = $scope.dateClickEnd; - } - params.currentPage = page ? page : $scope.currentPage; - params.perPageItems = perPageItems; - - $scope.currentPage = params.currentPage; - searchBookmarks(params) - console.log('search..', page, 'params = ', params) - } - $scope.updateCreateDate = function () { - console.log($scope.dateCreateBegin, $scope.dateCreateEnd); - if ($scope.dateCreateBegin && $scope.dateCreateEnd) { - $('.js-create-date').dropdown('hide'); - $('.js-create-date').dropdown('clear'); - $('.js-create-date .text').text($scope.dateCreateBegin + " 至 " + $scope.dateCreateEnd).removeClass('default'); - } - } - - $scope.updateClickDate = function () { - console.log($scope.dateClickBegin, $scope.dateClickEnd); - if ($scope.dateClickBegin && $scope.dateClickEnd) { - $('.js-click-date').dropdown('hide'); - $('.js-click-date').dropdown('clear'); - $('.js-click-date .text').text($scope.dateClickBegin + " 至 " + $scope.dateClickEnd).removeClass('default'); - } - } - - $scope.updateTagsSelect = function () { - $('.ui.dropdown.js-search-tags .text').removeClass('default'); - var text = $('.ui.dropdown.js-search-tags .text').text().replace('selected', '个已选'); - $('.ui.dropdown.js-search-tags .text').text(text); - } - - $scope.setHoverBookmark = function (bookmark) { - $scope.hoverBookmark = bookmark; - } - - // 在输入文字的时候也会触发,所以不要用Ctrl,Shift之类的按键 - $document.bind("keydown", function (event) { - $scope.$apply(function () { - var key = event.key.toUpperCase(); - console.log($scope.hoverBookmark); - if ($scope.hoverBookmark && dataService.keyShortcuts()) { - if (key == 'E' && $scope.hoverBookmark.own) { - $scope.editBookmark($scope.hoverBookmark.id) - } else if (key == 'I') { - $scope.detailBookmark($scope.hoverBookmark) - } else if (key == 'D' && $scope.hoverBookmark.own) { - $scope.delBookmark($scope.hoverBookmark) - } else if (key == 'C') { - $scope.copy($scope.hoverBookmark.url) - } - } - }) - }); - - pubSubService.subscribe('EditCtr.inserBookmarsSuccess', $scope, function (event, data) { - console.log('subscribe EditCtr.inserBookmarsSuccess', JSON.stringify(data)); - $scope.searchBookmarks.forEach((bookmark) => { - if (bookmark.id == data.id) { - bookmark.title = data.title; - bookmark.url = data.url; - bookmark.description = data.description; - bookmark.tags = data.tags; - } - }) - }); - - function searchBookmarks(params) { - $scope.loading = true; - $('.js-table-search').transition('hide'); - if ($scope.searchHotBookmarks) { - console.log(params); - bookmarkService.searchHotBookmarks(params) - .then((data) => { - $scope.searchBookmarks = []; - data.bookmarks.forEach((bookmark) => { - bookmark.tags = [{ - id: -1, - name: bookmark.created_by, // 给转存用 - }] - bookmark.created_at = $filter('date')(new Date(bookmark.created_at), "yyyy-MM-dd HH:mm:ss"); - bookmark.last_click = $filter('date')(new Date(bookmark.last_click), "yyyy-MM-dd HH:mm:ss"); - $scope.searchBookmarks.push(bookmark); - }) - $scope.bookmarkCount = data.totalItems; - $scope.totalPages = Math.ceil($scope.bookmarkCount / perPageItems); - $scope.loading = false; - transition(); - }) - .catch((err) => { - console.log('searchHotBookmarks err', err); - $scope.loading = false; - }); - } else { - bookmarkService.searchBookmarks(params) - .then((data) => { - $scope.searchBookmarks = data.bookmarks; - $scope.bookmarkCount = data.totalItems; - $scope.totalPages = Math.ceil($scope.bookmarkCount / perPageItems); - $scope.loading = false; - transition(); - }) - .catch((err) => { - console.log('getBookmarks err', err); - $scope.loading = false; - }); - } - } - - function transition() { - $timeout(function () { - timeagoInstance.cancel(); - timeagoInstance.render(document.querySelectorAll('.need_to_be_rendered'), 'zh_CN'); - }, 100) - var className = 'js-table-search'; - $('.' + className).transition('hide'); - $('.' + className).transition({ - animation: dataService.animation(), - duration: 500, - }); - } - -}]); +app.controller('searchCtr', ['$scope', '$state', '$stateParams', '$filter', '$window', '$timeout', '$document', 'ngDialog', 'bookmarkService', 'pubSubService', 'dataService', function ($scope, $state, $stateParams, $filter, $window, $timeout, $document, ngDialog, bookmarkService, pubSubService, dataService) { + console.log("Hello searchCtr...", $stateParams); + if (dataService.smallDevice()) { + $window.location = "http://m.mybookmark.cn/#/tags"; + return; + } + + const perPageItems = 20; + var dialog = null; + $scope.hoverBookmark = null; + $scope.searchBookmarks = []; // 书签数据 + $scope.showSearch = false; // + $scope.showTags = false; // + $scope.searchWord = ($stateParams && $stateParams.searchWord) || '' + $scope.dateCreateBegin = ''; + $scope.dateCreateEnd = ''; + $scope.dateClickBegin = ''; + $scope.dateClickEnd = ''; + $scope.clickCount = ''; + $scope.username = ''; + $scope.userRange = ''; + $scope.bookmarkCount = 0; + $scope.tags = [] + $scope.totalPages = 0; + $scope.currentPage = 1; + $scope.inputPage = ''; + $scope.loading = false; + $scope.waitDelBookmark = {}; + $scope.searchHotBookmarks = false; + var timeagoInstance = timeago(); + + $scope.changeCurrentPage = function (currentPage) { + currentPage = parseInt(currentPage) || 0; + console.log(currentPage); + if (currentPage <= $scope.totalPages && currentPage >= 1) { + $scope.currentPage = currentPage; + $scope.inputPage = ''; + $scope.search(); + } + } + + bookmarkService.getTags({}) + .then((data) => { + $scope.tags = data; + }) + .catch((err) => console.log('getTags err', err)); + // 默认登陆 + pubSubService.publish('Common.menuActive', { + login: true, + index: dataService.LoginIndexBookmarks + }); + + var searchParams = { + searchWord: $scope.searchWord, + currentPage: 1, + perPageItems: perPageItems, + userRange: '1', // 默认搜索自己的书签 + } + if ($scope.searchWord) { + searchBookmarks(searchParams); + } else { + toastr.warning("请输入搜索关键字再进行查询!", "提示"); + } + + $scope.jumpToUrl = async function (url, id) { + if (!$scope.edit) { + $window.open(url); + await post("clickBookmark", { id }); + + $scope.searchBookmarks.forEach(function (bookmark) { + if (bookmark.id == id && bookmark.own) { + bookmark.click_count += 1; + bookmark.last_click = $filter("date")(new Date(), "yyyy-MM-dd HH:mm:ss"); + } + }) + $timeout(function () { + timeagoInstance.cancel(); + timeagoInstance.render(document.querySelectorAll('.need_to_be_rendered'), 'zh_CN'); + }, 100) + } + } + + $scope.delBookmark = function (bookmark) { + $scope.waitDelBookmark = $.extend(true, {}, bookmark); // 利用jQuery执行深度拷贝 + dialog = ngDialog.open({ + template: './views/dialog-del-bookmark.html', + className: 'ngdialog-theme-default', + scope: $scope + }); + } + + $scope.confirmDelBookmark = function (bookmarkId) { + var params = { + id: bookmarkId + } + ngDialog.close(dialog); + bookmarkService.delBookmark(params) + .then((data) => { + $("#" + bookmarkId).transition({ + animation: dataService.animation(), + duration: 500, + onComplete: function () { + $("#" + bookmarkId).remove(); + } + }); + toastr.success($scope.waitDelBookmark.title + ' 书签删除成功!', "提示"); + }) + .catch((err) => { + toastr.error($scope.waitDelBookmark.title + ' 书签删除失败!错误提示:' + JSON.stringify(err), "提示"); + }); + } + + $scope.editBookmark = function (id) { + pubSubService.publish('bookmarksCtr.editBookmark', { id }); + } + + $scope.detailBookmark = function (bookmark) { + pubSubService.publish('TagCtr.showBookmarkInfo', bookmark); + } + + $scope.storeBookmark = function (bookmark) { + var b = $.extend(true, {}, bookmark); // 利用jQuery执行深度拷贝 + pubSubService.publish('TagCtr.storeBookmark', b); + } + + $scope.favoriteBookmark = function (b) { + var bookmark = {} + bookmark.description = ''; + bookmark.title = b.title; + bookmark.url = b.url; + bookmark.public = 1; + bookmark.click_count = 1; + + bookmarkService.favoriteBookmark(bookmark) + .then((data) => { + pubSubService.publish('EditCtr.inserBookmarsSuccess', data); + if (data.title) { + toastr.success('[ ' + data.title + ' ] 收藏成功!', "提示"); + } else { + toastr.error('[ ' + bookmark.title + ' ] 收藏失败!', "提示"); + } + }) + .catch((err) => { + toastr.error('[ ' + bookmark.title + ' ] 收藏失败,' + JSON.stringify(err), "提示"); + }); + } + + $scope.copy = function (url) { + dataService.clipboard(url); + } + + $scope.search = function (page) { + var params = {} + params.userRange = $('.js-user-range').dropdown('get value'); + if (params.userRange == '1') { + var tags = $('.js-search-tags').dropdown('get value') + if (tags) { + params.tags = tags; + } + } else if ($scope.username) { + params.username = $scope.username + } + if ($scope.searchWord) { + params.searchWord = $scope.searchWord; + } + + var dateCreate = $('.js-create-date').dropdown('get value') || undefined; + console.log('dateCreate = ', dateCreate) + if (dateCreate) { + if (dateCreate != -1) { + params.dateCreate = dateCreate; + } + } else { + params.dateCreateBegin = $scope.dateCreateBegin; + params.dateCreateEnd = $scope.dateCreateEnd; + } + + var dateClick = $('.js-click-date').dropdown('get value') || undefined; + console.log('dateClick = ', dateClick) + if (dateClick) { + if (dateClick != -1) { + params.dateClick = dateClick + } + } else { + params.dateClickBegin = $scope.dateClickBegin; + params.dateClickEnd = $scope.dateClickEnd; + } + params.currentPage = page ? page : $scope.currentPage; + params.perPageItems = perPageItems; + + $scope.currentPage = params.currentPage; + searchBookmarks(params) + console.log('search..', page, 'params = ', params) + } + $scope.updateCreateDate = function () { + console.log($scope.dateCreateBegin, $scope.dateCreateEnd); + if ($scope.dateCreateBegin && $scope.dateCreateEnd) { + $('.js-create-date').dropdown('hide'); + $('.js-create-date').dropdown('clear'); + $('.js-create-date .text').text($scope.dateCreateBegin + " 至 " + $scope.dateCreateEnd).removeClass('default'); + } + } + + $scope.updateClickDate = function () { + console.log($scope.dateClickBegin, $scope.dateClickEnd); + if ($scope.dateClickBegin && $scope.dateClickEnd) { + $('.js-click-date').dropdown('hide'); + $('.js-click-date').dropdown('clear'); + $('.js-click-date .text').text($scope.dateClickBegin + " 至 " + $scope.dateClickEnd).removeClass('default'); + } + } + + $scope.updateTagsSelect = function () { + $('.ui.dropdown.js-search-tags .text').removeClass('default'); + var text = $('.ui.dropdown.js-search-tags .text').text().replace('selected', '个已选'); + $('.ui.dropdown.js-search-tags .text').text(text); + } + + $scope.setHoverBookmark = function (bookmark) { + $scope.hoverBookmark = bookmark; + } + + // 在输入文字的时候也会触发,所以不要用Ctrl,Shift之类的按键 + $document.bind("keydown", function (event) { + $scope.$apply(function () { + var key = event.key.toUpperCase(); + console.log($scope.hoverBookmark); + if ($scope.hoverBookmark && dataService.keyShortcuts()) { + if (key == 'E' && $scope.hoverBookmark.own) { + $scope.editBookmark($scope.hoverBookmark.id) + } else if (key == 'I') { + $scope.detailBookmark($scope.hoverBookmark) + } else if (key == 'D' && $scope.hoverBookmark.own) { + $scope.delBookmark($scope.hoverBookmark) + } else if (key == 'C') { + $scope.copy($scope.hoverBookmark.url) + } + } + }) + }); + + pubSubService.subscribe('EditCtr.inserBookmarsSuccess', $scope, function (event, data) { + console.log('subscribe EditCtr.inserBookmarsSuccess', JSON.stringify(data)); + $scope.searchBookmarks.forEach((bookmark) => { + if (bookmark.id == data.id) { + bookmark.title = data.title; + bookmark.url = data.url; + bookmark.description = data.description; + bookmark.tags = data.tags; + } + }) + }); + + function searchBookmarks(params) { + $scope.loading = true; + $('.js-table-search').transition('hide'); + if ($scope.searchHotBookmarks) { + console.log(params); + bookmarkService.searchHotBookmarks(params) + .then((data) => { + $scope.searchBookmarks = []; + data.bookmarks.forEach((bookmark) => { + bookmark.tags = [{ + id: -1, + name: bookmark.created_by, // 给转存用 + }] + bookmark.created_at = $filter('date')(new Date(bookmark.created_at), "yyyy-MM-dd HH:mm:ss"); + bookmark.last_click = $filter('date')(new Date(bookmark.last_click), "yyyy-MM-dd HH:mm:ss"); + $scope.searchBookmarks.push(bookmark); + }) + $scope.bookmarkCount = data.totalItems; + $scope.totalPages = Math.ceil($scope.bookmarkCount / perPageItems); + $scope.loading = false; + transition(); + }) + .catch((err) => { + console.log('searchHotBookmarks err', err); + $scope.loading = false; + }); + } else { + bookmarkService.searchBookmarks(params) + .then((data) => { + $scope.searchBookmarks = data.bookmarks; + $scope.bookmarkCount = data.totalItems; + $scope.totalPages = Math.ceil($scope.bookmarkCount / perPageItems); + $scope.loading = false; + transition(); + }) + .catch((err) => { + console.log('getBookmarks err', err); + $scope.loading = false; + }); + } + } + + function transition() { + $timeout(function () { + timeagoInstance.cancel(); + timeagoInstance.render(document.querySelectorAll('.need_to_be_rendered'), 'zh_CN'); + }, 100) + var className = 'js-table-search'; + $('.' + className).transition('hide'); + $('.' + className).transition({ + animation: dataService.animation(), + duration: 500, + }); + } + +}]); diff --git a/www/scripts/controllers/tags-controller.js b/www/scripts/controllers/tags-controller.js index 8850f4f..0348c82 100644 --- a/www/scripts/controllers/tags-controller.js +++ b/www/scripts/controllers/tags-controller.js @@ -91,12 +91,11 @@ app.controller('tagsCtr', ['$scope', '$filter', '$state', '$window', '$statePara } } - $scope.jumpToUrl = function (url, id) { + $scope.jumpToUrl = async function (url, id) { if (!$scope.editMode) { $window.open(url, '_blank'); - bookmarkService.clickBookmark({ - id: id - }); + await post("clickBookmark", { id }); + $scope.bookmarks.forEach(function (bookmark, index) { if (bookmark.id == id) { bookmark.click_count += 1; @@ -149,18 +148,15 @@ app.controller('tagsCtr', ['$scope', '$filter', '$state', '$window', '$statePara }); } - $scope.editBookmark = function (bookmarkId) { - pubSubService.publish('bookmarksCtr.editBookmark', { - 'bookmarkId': bookmarkId - }); + $scope.editBookmark = function (id) { + console.log('publish bookmarksCtr.editBookmark', { id }); + pubSubService.publish('bookmarksCtr.editBookmark', { id }); } - $scope.detailBookmark = function (bookmark) { + $scope.detailBookmark = async function (bookmark) { bookmark.own = true; pubSubService.publish('TagCtr.showBookmarkInfo', bookmark); - bookmarkService.clickBookmark({ - id: bookmark.id - }); + await post("clickBookmark", { id: bookmark.id }); } $scope.copy = function (url) { diff --git a/www/views/bookmark-info.html b/www/views/bookmark-info.html index 216a929..5d28c51 100644 --- a/www/views/bookmark-info.html +++ b/www/views/bookmark-info.html @@ -1,60 +1,60 @@ -
-
+ | 标题 | -链接 | -点击次数 | -创建日期 | -最后点击 | -分类 | -操作 | -
|---|---|---|---|---|---|---|
|
- |
- - {{ bookmark.url }} - | -{{ bookmark.click_count }} | -- - | -- - | -
-
- {{ tag.name }}
-
- |
-
- |
-
|
- |
- ||||||
| 标题 | +链接 | +点击次数 | +创建日期 | +最后点击 | +分类 | +操作 | +
|---|---|---|---|---|---|---|
|
+ |
+ + {{ bookmark.url }} + | +{{ bookmark.click_count }} | ++ + | ++ + | +
+
+ {{ tag.name }}
+
+ |
+
+ |
+
|
+ |
+ ||||||
{{ note.brief }}
- {{ note.content }}
- {{ note.brief }}
+ {{ note.content }}
+