diff --git a/node_modules/common.js b/node_modules/common.js index 57b9da6e..d989b56c 100644 --- a/node_modules/common.js +++ b/node_modules/common.js @@ -26,6 +26,28 @@ var Common = { objectId: function() { return ObjectId() }, + + // 是否是数组 + isArray: function(obj) { + return Object.prototype.toString.call(obj) === '[object Array]'; + }, + /** + * 是否为空 + * 可判断任意类型,string array + */ + isEmpty: function(obj) { + if(!obj) { + return true; + } + + if(isArray(obj)) { + if(obj.length == 0) { + return true; + } + } + + return false; + }, _uuid: 1, uuid: function() { this._uuid++; diff --git a/node_modules/note.js b/node_modules/note.js index c16a3e5e..fc4bc613 100644 --- a/node_modules/note.js +++ b/node_modules/note.js @@ -10,6 +10,7 @@ var Notebook = require('notebook'); var Server = require('server'); var Common = require('common'); var Web = require('web'); + // var Notes = db.notes; var Api = null; // require('api') @@ -160,7 +161,22 @@ var Note = { }, // 公开/取消为博客 - setNote2Blog: function(noteId, isBlog, callback) { + setNote2Blog: function(noteIds, isBlog, callback) { + var me = this; + var ok = false; + async.eachSeries(noteIds, function(noteId, cb) { + me._setNote2Blog(noteId, isBlog, function (ret) { + if (ret) { + ok = true; + } + cb(); + }); + }, function () { + callback(ok); + }); + }, + + _setNote2Blog: function(noteId, isBlog, callback) { var me = this; me.getNote(noteId, function(note) { if(note) { @@ -168,7 +184,7 @@ var Note = { return callback && callback(true); } // 更新, 设置isDirty - db.notes.update({NoteId: noteId}, { $set: {IsBlog: isBlog, IsDirty: true} }, {}, function (err, numReplaced) { + db.notes.update({_id: note._id}, { $set: {IsBlog: isBlog, IsDirty: true} }, {}, function (err, numReplaced) { return callback && callback(true); }); } else { @@ -309,18 +325,35 @@ var Note = { }); }, - deleteNote: function(noteId, callback) { + deleteNote: function(noteIds, callback) { + var me = this; + async.eachSeries(noteIds, function (noteId, cb) { + me._deleteNote(noteId, function () { + cb(); + }); + }, function () { + callback(true); + }); + }, + + _deleteNote: function(noteId, callback) { var me = this; me.getNote(noteId, function(note) { if(!note) { callback(false); } + + // 如果已经是trash, 再删除则彻底删除 + if (note.IsTrash) { + me.deleteTrash(note, callback); + return; + } + db.notes.update({NoteId: noteId}, {$set: {IsTrash: true, IsDirty: true}}, function(err, n) { if(err || !n) { callback(false); } else { callback(true); - // 重新统计 Notebook.reCountNotebookNumberNotes(note.NotebookId); } @@ -334,56 +367,66 @@ var Note = { }); }, // 彻底删除笔记, 如果有tags, 则需要更新tags's count - deleteTrash: function(noteId, callback) { + deleteTrash: function(note, callback) { var me = this; - me.getNote(noteId, function(note) { - if(note) { - note.LocalIsDelete = true; - note.IsDirty = true; + if(note) { + note.LocalIsDelete = true; + note.IsDirty = true; - // TODO 删除附件 - db.notes.update({_id: note._id}, {$set: {IsDirty: true, LocalIsDelete: true}}, function(err, n) { - if(n) { - // 如果有tags, 则重新更新tags' count - me.updateTagCount(note.Tags); - callback(true); - } - callback(false); - }); - } else { + // TODO 删除附件 + db.notes.update({_id: note._id}, {$set: {IsDirty: true, LocalIsDelete: true}}, function(err, n) { + if(n) { + // 如果有tags, 则重新更新tags' count + me.updateTagCount(note.Tags); + callback(true); + } callback(false); - } - }); - - /* - db.notes.update({NoteId: noteId}, {$set: {IsDirty: true, LocalIsDelete: true}}, function(err, n) { - if(err || !n) { - callback(false); - } else { - callback(true); - } - }); - */ + }); + } else { + callback(false); + } }, // 移动note // 重新统计另一个notebookId的笔记数 - moveNote: function(noteId, notebookId, callback) { + moveNote: function(noteIds, notebookId, callback) { var me = this; - me.getNote(noteId, function(note) { - if(note) { - var to = !note.Star; - var preNotebookId = note.NotebookId; - note.NotebookId = notebookId; - db.notes.update({_id: note._id}, {$set: {IsDirty: true, NotebookId: notebookId, IsTrash: false, LocalIsDelete: false, UpdatedTime: new Date()}}, function(err, n) { - // 重新统计 - Notebook.reCountNotebookNumberNotes(preNotebookId); - Notebook.reCountNotebookNumberNotes(notebookId); - callback(note); - }); - } else { - callback(false); + if (Common.isEmpty(noteIds)) { + callback(false); + return; + } + + var preNotebookIds = {}; + async.eachSeries(noteIds, function(noteId, cb) { + me.getNote(noteId, function(note) { + if(note) { + // var to = !note.Star; + // 是原笔记本 + var preNotebookId = note.NotebookId; + if (preNotebookId == notebookId && !note.IsTrash) { + cb(); + return; + } + preNotebookIds[preNotebookId] = true; + note.NotebookId = notebookId; + db.notes.update({_id: note._id}, {$set: {IsDirty: true, NotebookId: notebookId, IsTrash: false, LocalIsDelete: false, UpdatedTime: new Date()}}, function(err, n) { + cb(); + }); + } else { + cb(); + } + }); + + }, function () { + // 重新统计 + for (var i in preNotebookIds) { + Notebook.reCountNotebookNumberNotes(i); } + if (!preNotebookIds[notebookId]) { + Notebook.reCountNotebookNumberNotes(notebookId); + } + + callback(true); }); }, @@ -1001,7 +1044,27 @@ var Note = { }, // 复制笔记到某笔记本下, 本地使用 - copyNote: function(noteId, notebookId, callback) { + copyNote: function(noteIds, notebookId, callback) { + var me = this; + var newNotes = []; + if (Common.isEmpty(noteIds)) { + callback(false); + return; + } + async.eachSeries(noteIds, function(noteId, cbTop) { + me._copyNote(noteId, notebookId, function (newNote) { + newNotes.push(newNote); + cbTop(); + }); + }, function () { + // 重新统计下 + Notebook.reCountNotebookNumberNotes(notebookId); + callback(newNotes); + }); + }, + + // 复制单个笔记 + _copyNote: function (noteId, notebookId, callback) { var me = this; me.getNote(noteId, function(note) { if(!note) { @@ -1049,15 +1112,14 @@ var Note = { */ }, function() { note.Attachs = newAttachs; - console.log('conflict 复制后的'); - console.log(note.Attachs); + // console.log('conflict 复制后的'); + // console.log(note.Attachs); db.notes.insert(note, function(err, newNote) { if(err) { callback(false); } else { callback(newNote); // 重新统计下 - Notebook.reCountNotebookNumberNotes(newNote.NotebookId); } }); }); @@ -1620,6 +1682,9 @@ var Note = { if(!serverNoteId) { callback({Ok: false, Msg: 'noteNotExists'}); } else { + if (!Api) { + Api = require('api'); + } Api.exportPdf(serverNoteId, callback); } }) diff --git a/note.html b/note.html index 104331dc..eb7382bf 100755 --- a/note.html +++ b/note.html @@ -272,13 +272,13 @@ function log(o) {
-
+

loading...
-
+
noNoteNewNoteTips

@@ -288,6 +288,15 @@ function log(o) {
canntNewNoteTips
+ +
+
+
+
+

+
+
+
@@ -565,7 +574,7 @@ function log(o) {