notebook:
     add [ok]

<- sync
notebook:
     add [ok]
This commit is contained in:
life
2015-01-25 22:07:08 +08:00
parent a137aada9f
commit 72232dc86e
7 changed files with 495 additions and 142 deletions

89
node_modules/api.js generated vendored
View File

@@ -1,5 +1,7 @@
var db = require('db'); var db = require('db');
var User = require('user'); var User = require('user');
var Note = require('note');
var Notebook = require('notebook');
var Common = require('common'); var Common = require('common');
var Tags = db.tags; var Tags = db.tags;
var needle = require('needle'); var needle = require('needle');
@@ -8,9 +10,9 @@ var fs = require('fs');
function log(o) { function log(o) {
console.log(o); console.log(o);
} }
log(Common); // log(Common);
log(db); // log(db);
log("??") // log("??")
// 远程数据服务 // 远程数据服务
var Api = { var Api = {
@@ -152,6 +154,7 @@ var Api = {
log(url); log(url);
needle.get(url, function(error, response) { needle.get(url, function(error, response) {
if(error) { if(error) {
log(error);
return callback && callback(false); return callback && callback(false);
} }
var ret = response.body; var ret = response.body;
@@ -222,38 +225,63 @@ var Api = {
//------------ //------------
// 笔记本操作 // 笔记本操作
//------------ //------------
// 添加
addNotebook: function(notebook, callback) { addNotebook: function(notebook, callback) {
var me = this; var me = this;
var data = { // notebook.ParentNotebookId是本的, 要得到远程的
title: notebook.Title, Notebook.getServerNotebookIdByNotebookId(notebook.ParentNotebookId, function(serverNotebookId) {
seq: notebook.Seq, var data = {
parentNotebookId: notebook.ParentNotebookId title: notebook.Title,
} seq: notebook.Seq,
log('add notebook'); parentNotebookId: serverNotebookId
log(data);
needle.post(me.getUrl('notebook/addNotebook'), data, {}, function(err, resp) {
if(err) {
return callback(false);
} }
var ret = resp.body; log('add notebook');
log(ret); log(data);
if(Common.isOk(ret)) { needle.post(me.getUrl('notebook/addNotebook'), data, {}, function(err, resp) {
callback(ret); if(err) {
} else { return callback(false);
callback(false); }
} var ret = resp.body;
}); log(ret);
if(Common.isOk(ret)) {
callback(ret);
} else {
callback(false);
}
});
});
}, },
// 更新
updateNotebook: function(notebook, callback) { updateNotebook: function(notebook, callback) {
var me = this; var me = this;
var data = { Notebook.getServerNotebookIdByNotebookId(notebook.ParentNotebookId, function(serverNotebookId) {
NotebookId: notebook.NotebookId, var data = {
Title: notebook.Title, NotebookId: notebook.ServerNotebookId,
Usn: notebook.Title, Title: notebook.Title,
Seq: notebook.Seq, Usn: notebook.Title,
ParentNotebookId: notebook.ParentNotebookId Seq: notebook.Seq,
} ParentNotebookId: serverNotebookId
needle.post(me.getUrl('notebook/updateNotebook'), data, {}, function(err, resp) { }
needle.post(me.getUrl('notebook/updateNotebook'), data, {}, function(err, resp) {
if(err) {
return callback(false);
}
var ret = resp.body;
if(Common.isOk(ret)) {
callback(ret);
} else {
callback(false);
}
});
});
},
// 删除
deleteNotebook: function(serverNotebookId, callback) {
var me = this;
var data = {NotebookId: serverNotebookId};
needle.post(me.getUrl('notebook/deleteNotebook'), data, {}, function(err, resp) {
if(err) { if(err) {
return callback(false); return callback(false);
} }
@@ -264,9 +292,6 @@ var Api = {
callback(false); callback(false);
} }
}); });
},
deleteNotebook: function(notebook, callback) {
var me = this;
} }
}; };
module.exports = Api; module.exports = Api;

189
node_modules/note.js generated vendored
View File

@@ -3,11 +3,13 @@ var db = require('db');
var User = require('user'); var User = require('user');
var Notebook = require('notebook'); var Notebook = require('notebook');
var Tag = require('tag'); var Tag = require('tag');
var Api = require('api'); // var Api = require('api');
var Server = require('server'); var Server = require('server');
var Common = require('common'); var Common = require('common');
var Notes = db.notes; var Notes = db.notes;
var Api = null; // require('api')
function log(o) { function log(o) {
console.log(o); console.log(o);
} }
@@ -150,6 +152,9 @@ var Note = {
updateNoteContentForce: function(noteId, content, callback) { updateNoteContentForce: function(noteId, content, callback) {
// <img src="http://localhost:9000/api/file/getImage?fileId=54c2083f99c37bea5f000001"> // <img src="http://localhost:9000/api/file/getImage?fileId=54c2083f99c37bea5f000001">
// 改成<img src="http://localhost:3232/api/file/getImage?fileId=xxx" // 改成<img src="http://localhost:3232/api/file/getImage?fileId=xxx"
if(!Api) {
Api = require('api');
}
var reg = new RegExp('src="' + Api.leanoteUrl + '/api/file/getImage', 'g'); var reg = new RegExp('src="' + Api.leanoteUrl + '/api/file/getImage', 'g');
content = content.replace(reg, 'src="' + Server.localUrl + '/api/file/getImage'); content = content.replace(reg, 'src="' + Server.localUrl + '/api/file/getImage');
@@ -183,6 +188,7 @@ var Note = {
*/ */
// 得到笔记内容 // 得到笔记内容
// noteId是本地Id
getNoteContent: function(noteId, callback) { getNoteContent: function(noteId, callback) {
var me = this; var me = this;
log('getNoteContent------') log('getNoteContent------')
@@ -195,17 +201,30 @@ var Note = {
// 如果笔记是刚同步过来的, 那么内容要重新获取 // 如果笔记是刚同步过来的, 那么内容要重新获取
if(note.InitSync) { if(note.InitSync) {
log('need load from server'); log('need load from server');
if(!Api) {
Api = require('api')
}
// 远程获取 // 远程获取
Api.getNoteContent(noteId, function(noteContent) { me.getServerNoteIdByNoteId(noteId, function(serverNoteId) {
// 同步到本地 if(!serverNoteId) {
if(Common.isOk(noteContent)) { return callback && callback(false);
me.updateNoteContentForce(noteId, noteContent.Content, function(content) {
noteContent.Content = content;
callback && callback(noteContent);
});
} else {
callback && callback(false);
} }
Api.getNoteContent(serverNoteId, function(noteContent) {
// 同步到本地
if(Common.isOk(noteContent)) {
me.updateNoteContentForce(noteId, noteContent.Content, function(content) {
noteContent.Content = content;
noteContent.NoteId = noteId;
callback && callback(noteContent);
});
} else {
callback && callback(false);
}
});
}); });
} else { } else {
log('not need'); log('not need');
@@ -219,12 +238,46 @@ var Note = {
// 同步 // 同步
//---------------- //----------------
getNoteByServerNoteId: function(noteId, callback) {
var me = this;
Notes.findOne({ServerNoteId: noteId}, function(err, doc) {
if(err || !doc) {
log('getNoteByServerNoteId 不存在' + noteId);
callback && callback(false);
} else {
callback && callback(doc);
}
});
},
getNoteIdByServerNoteId: function(noteId, callback) {
var me = this;
Notes.findOne({ServerNoteId: noteId}, function(err, doc) {
if(err || !doc) {
log('getNoteIdByServerNoteId 不存在' + noteId);
callback && callback(false);
} else {
callback && callback(doc.NoteId);
}
});
},
getServerNoteIdByNoteId: function(noteId, callback) {
var me = this;
Notes.findOne({NoteId: noteId}, function(err, doc) {
if(err || !doc) {
log('getServerNoteIdByNoteId 不存在');
callback && callback(false);
} else {
callback && callback(doc.ServerNoteId);
}
});
},
// 强制删除 // 强制删除
// TODO 是否真的删除 ? // TODO 是否真的删除 ?
// 有可能服务器上删除了是误删 ? // 有可能服务器上删除了是误删 ?
deleteNoteForce: function(noteId, callback) { deleteNoteForce: function(noteId, callback) {
var me = this; var me = this;
Notes.remove({NoteId: noteId}, function(err, n) { Notes.remove({ServerNoteId: noteId}, function(err, n) {
if(err) { if(err) {
callback && callback(false); callback && callback(false);
} else { } else {
@@ -233,35 +286,71 @@ var Note = {
}); });
}, },
// 添加笔记本, note object // 添加笔记本, note object
// note是服务器传过来的, 需要处理下fix
// NoteId, ServerNoteId, NotebookId(本地的)
addNoteForce: function(note, callback) { addNoteForce: function(note, callback) {
note.InitSync = true; // 刚同步完, 表示content, images, attach没有同步 note.InitSync = true; // 刚同步完, 表示content, images, attach没有同步
note.IsDirty = false; note.IsDirty = false;
Notes.insert(note, function (err, newDoc) { // Callback is optional note.ServerNoteId = note.NoteId;
if(err) { note.NoteId = Common.objectId();
console.log(err); Notebook.getNotebookIdByServerNotebookId(note.NotebookId, function(localNotebookId) {
callback && callback(false); note.NotebookId = localNotebookId;
} else { Notes.insert(note, function (err, newDoc) { // Callback is optional
callback && callback(newDoc); if(err) {
} console.log(err);
callback && callback(false);
} else {
callback && callback(newDoc);
}
});
}); });
}, },
// 更新笔记本, 合并之, 内容要重新获取 // 更新笔记本, 合并之, 内容要重新获取
// note是服务器传过来的, 需要处理下fix
updateNoteForce: function(note, callback) { updateNoteForce: function(note, callback) {
var me = this;
note.IsDirty = false; note.IsDirty = false;
note.InitSync = true; note.InitSync = true;
note.LocalIsNew = false; note.LocalIsNew = false;
Notes.update({NoteId: note.NoteId}, {$set: note}, {}, function (err, cnt) { // Callback is optional me.getNoteIdByServerNoteId(note.NoteId, function(localNoteId) {
if(err) { note.NoteId = localNoteId;
console.log(err); Notebook.getNotebookIdByServerNotebookId(note.NotebookId, function(localNotebookId) {
callback && callback(false); note['NotebookId'] = localNotebookId;
} else { Notes.update({NoteId: localNoteId}, {$set: note}, {}, function (err, cnt) { // Callback is optional
log('强制更新...'); if(err) {
callback && callback(note); console.log(err);
} callback && callback(false);
} else {
log('强制更新...');
callback && callback(note);
}
});
});
});
},
// 为冲突更新, note已有有NoteId, ServerNoteId, 但NotebookId是服务器端的
updateNoteForceForConflict: function(note, callback) {
var me = this;
note.IsDirty = false;
note.InitSync = true;
note.LocalIsNew = false;
Notebook.getNotebookIdByServerNotebookId(note.NotebookId, function(localNotebookId) {
note['NotebookId'] = localNotebookId;
Notes.update({NoteId: localNoteId}, {$set: note}, {}, function (err, cnt) { // Callback is optional
if(err) {
console.log(err);
callback && callback(false);
} else {
log('强制更新...');
callback && callback(note);
}
});
}); });
}, },
// 将本地冲突的笔记复制一份 // 将本地冲突的笔记复制一份
// serverNoteId
copyNoteForConfict: function(noteId, callback) { copyNoteForConfict: function(noteId, callback) {
var me = this; var me = this;
me.getNote(noteId, function(note) { me.getNote(noteId, function(note) {
@@ -296,42 +385,52 @@ var Note = {
fixConflicts: function(noteSyncInfo, noteWeb, callback) { fixConflicts: function(noteSyncInfo, noteWeb, callback) {
var me = this; var me = this;
// 处理冲突 // 处理冲突
if(!noteWeb) {
return callback && callback();
}
var conflictNotes = noteSyncInfo.conflicts; var conflictNotes = noteSyncInfo.conflicts;
log('fix note conflicts'); log('fix note conflicts');
async.eachSeries(conflictNotes, function(note, cb) { if(conflictNotes) {
var noteId = note.NoteId; async.eachSeries(conflictNotes, function(note, cb) {
// 复制一份 var noteId = note.NoteId;
me.copyNoteForConfict(noteId, function(newNote) { // 复制一份
if(newNote) { me.copyNoteForConfict(noteId, function(newNote) {
// 更新之前的 if(newNote) {
me.updateNoteForce(note, function() { // 更新之前的
me.updateNoteForceForConflict(note, function(err, note2) {
cb();
if(!err) {
// 前端来处理, 全量sync时不用前端一个个处理
noteWeb && noteWeb.fixSyncConflict(note2, newNote);
}
});
} else {
cb(); cb();
// 前端来处理, 全量sync时不用前端一个个处理 }
noteWeb.fixSyncConflict && noteWeb.fixSyncConflict(note, newNote); });
}); }, function() {
} else { // 最后调用
cb(); callback && callback();
}
}); });
}, function() { } else {
// 最后调用
callback && callback(); callback && callback();
}); }
// 处理添加的 // 处理添加的
var addNotes = noteSyncInfo.adds; var addNotes = noteSyncInfo.adds;
log('has add...'); log('has add...');
log(addNotes); log(addNotes);
noteWeb && noteWeb.addSyncNotes(addNotes); noteWeb.addSync(addNotes);
log('has updates...'); log('has updates...');
log(noteSyncInfo); log(noteSyncInfo);
log(noteSyncInfo.updates); log(noteSyncInfo.updates);
// 处理更新的 // 处理更新的
noteWeb.updateSyncNotes(noteSyncInfo.updates); noteWeb.updateSync(noteSyncInfo.updates);
// 处理删除的 // 处理删除的
noteWeb.deleteSyncNotes(noteSyncInfo.deletes); noteWeb.deleteSync(noteSyncInfo.deletes);
} }
}; };

159
node_modules/notebook.js generated vendored
View File

@@ -1,4 +1,5 @@
var db = require('db'); var db = require('db');
var async = require('async');
var User = require('user'); var User = require('user');
var NB = db.notebooks; var NB = db.notebooks;
@@ -161,14 +162,52 @@ var Notebook = {
}); });
}, },
//---------------- //----------------
// 同步 // 同步
//---------------- //----------------
getNotebookByServerNotebookId: function(notebookId, callback) {
var me = this;
NB.findOne({ServerNotebookId: notebookId}, function(err, doc) {
if(err || !doc) {
log('不存在');
callback && callback(false);
} else {
callback && callback(doc);
}
});
},
// 通过ServerNotebookId得到NotebookId
getNotebookIdByServerNotebookId: function(serverNotebookId, callback) {
if(!serverNotebookId) {
return callback(false);
}
NB.findOne({ServerNotebookId: serverNotebookId}, function(err, notebook) {
if(err || !notebook) {
return callback(false);
}
callback(notebook.NotebookId);
});
},
// 发送changes时用 api调用
getServerNotebookIdByNotebookId: function(notebookId, callback) {
if(!serverNotebookId) {
return callback(false);
}
NB.findOne({NotebookId: notebookId}, function(err, notebook) {
if(err || !notebook) {
return callback(false);
}
callback(notebook.ServerNotebookId);
});
},
// 强制删除 // 强制删除
deleteNotebookForce: function(notebookId, callback) { deleteNotebookForce: function(notebookId, callback) {
var me = this; var me = this;
NB.remove({NotebookId: notebookId}, function(err, n) { NB.remove({ServerNotebookId: notebookId}, function(err, n) {
if(err) { if(err) {
callback && callback(false); callback && callback(false);
} else { } else {
@@ -176,41 +215,69 @@ var Notebook = {
} }
}); });
}, },
// 添加笔记本, notebook object // 添加笔记本, notebook object
// 这里的notebook是服务器传过来的数据, 需要fix下,
addNotebookForce: function(notebook, callback) { addNotebookForce: function(notebook, callback) {
NB.insert(notebook, function (err, newDoc) { // Callback is optional var me = this;
if(err) { notebook.ServerNotebookId = notebook.NotebookId;
console.log(err); // notebook.NotebookId = Common.objectId();
callback && callback(false); notebook.NotebookId = notebook.NotebookId; // 就采用服务器的, 怕失去了层级
me.getNotebookIdByServerNotebookId(notebook.ParentNotebookId, function(parentNotebookId) {
// 如果是第一次添加可能会有问题, 数据库还没有数据, 那么还找不到
if(parentNotebookId) {
notebook.ParentNotebookId = parentNotebookId;
} else { } else {
callback && callback(newDoc); // 否则, 就用服务器上的
} }
NB.insert(notebook, function (err, newDoc) { // Callback is optional
if(err) {
console.log(err);
callback && callback(false);
} else {
callback && callback(newDoc);
}
});
}); });
}, },
// 更新笔记本 // 更新笔记本
// 这里的notebook是服务器传过来的数据, 需要fix下,
updateNotebookForce: function(notebook, callback) { updateNotebookForce: function(notebook, callback) {
notebook.IsDirty = false; notebook.IsDirty = false;
notebook.LocalIsNew = false; notebook.LocalIsNew = false;
NB.update({NotebookId: notebook.NotebookId}, {$set: notebook}, {}, function (err, updates) { // Callback is optional var serverNotebookId = notebook.NotebookId;
if(err) { me.getNotebookIdByServerNotebookId(notebook.ParentNotebookId, function(parentNotebookId) {
console.log(err); notebook.ParentNotebookId = parentNotebookId;
callback && callback(false); delete notebook['NotebookId'];
} else { NB.update({ServerNotebookId: serverNotebookId}, {$set: notebook}, {}, function (err, updates) { // Callback is optional
callback && callback(notebook); if(err) {
} console.log(err);
callback && callback(false);
} else {
callback && callback(notebook);
}
});
}); });
}, },
//
// 更新笔记本, NoteId可能也要更改 // 更新笔记本, NoteId可能也要更改
// notebook是服务器传过来的
updateNotebookForceForSendChange: function(notebookId, notebook, callback) { updateNotebookForceForSendChange: function(notebookId, notebook, callback) {
notebook.IsDirty = false; notebook.IsDirty = false;
notebook.LocalIsNew = false; notebook.LocalIsNew = false;
NB.update({NotebookId: notebookId}, {$set: notebook}, {}, function (err, updates) { // Callback is optional notebook.ServerNotebookId = notebook.NotebookId;
if(err) { me.getNotebookIdByServerNotebookId(notebook.ParentNotebookId, function(parentNotebookId) {
console.log(err); notebook.ParentNotebookId = parentNotebookId;
callback && callback(false); NB.update({NotebookId: notebookId}, {$set: notebook}, {}, function (err, n) {
} else { if(err) {
callback && callback(notebook); console.log(err);
} callback && callback(false);
} else {
callback && callback(notebook);
}
});
}); });
}, },
@@ -226,8 +293,58 @@ var Notebook = {
}); });
}, },
fixConflicts: function() { // 处理冲突
// notes是服务器的数据, 与本地的有冲突
// 1) 将本地的note复制一份
// 2) 服务器替换之前
fixConflicts: function(notebookSyncInfo, notebookWeb, callback) {
var me = this;
if(!notebookWeb) {
return callback && callback();
}
// 处理冲突
var conflictNotebooks = notebookSyncInfo.conflicts || [];
log('fix notebook conflicts');
async.eachSeries(conflictNotebooks, function(notebook, cb) {
/*
var noteId = note.NoteId;
// 复制一份
me.copyNoteForConfict(noteId, function(newNote) {
if(newNote) {
// 更新之前的
me.updateNotebookForce(note, function() {
cb();
// 前端来处理, 全量sync时不用前端一个个处理
notebookWeb.fixSyncConflict && notebookWeb.fixSyncConflict(note, newNote);
});
} else {
cb();
}
});
*/
cb();
}, function() {
// 最后调用
callback && callback();
});
// 处理添加的
var adds = notebookSyncInfo.adds;
log('has add...');
log(adds);
notebookWeb.addSync(adds);
log('has changeAdds')
log(notebookSyncInfo.changeAdds)
notebookWeb.addChange(notebookSyncInfo.changeAdds);
log('has updates...');
log(notebookSyncInfo);
log(notebookSyncInfo.updates);
// 处理更新的
notebookWeb.updateSync(notebookSyncInfo.updates);
// 处理删除的
notebookWeb.deleteSync(notebookSyncInfo.deletes);
} }
}; };
module.exports = Notebook; module.exports = Notebook;

48
node_modules/sync.js generated vendored
View File

@@ -119,20 +119,22 @@ var Sync = {
(function(notebook) { (function(notebook) {
var usn = notebook.Usn; var usn = notebook.Usn;
var notebookId = notebook.NotebookId; var notebookId = notebook.NotebookId; // 服务器端的
// 1) 服务器端删除了, 本地肯定删除 // 1) 服务器端删除了, 本地肯定删除
if(notebook.IsDeleted) { if(notebook.IsDeleted) {
log('delete: '); log('delete: ');
log(notebook); log(notebook);
Notebook.deleteNotebookForce(notebookId, function() { Notebook.getNotebookIdByServerNotebookId(notebookId, function(localNotebookId) {
me._syncInfo.notebook.deletes.push(notebookId); Notebook.deleteNotebookForce(notebookId, function() {
canCall(); me._syncInfo.notebook.deletes.push(localNotebookId);
canCall();
})
}); });
return; return;
} }
// 2) 查看本地的, 与本地合并 // 2) 查看本地的, 与本地合并
Notebook.getNotebook(notebookId, function(notebookLocal) { Notebook.getNotebookByServerNotebookId(notebookId, function(notebookLocal) {
// 2.1 本地没有, 表示是新建 // 2.1 本地没有, 表示是新建
if(!notebookLocal) { if(!notebookLocal) {
log('add: ...') log('add: ...')
@@ -245,14 +247,16 @@ var Sync = {
if(note.IsDeleted) { if(note.IsDeleted) {
log('delete: '); log('delete: ');
log(note); log(note);
Note.deleteNoteForce(noteId, function() { Note.getNoteIdByServerNoteId(noteId, function(localNoteId) {
me._syncInfo.note.deletes.push(noteId); Note.deleteNoteForce(noteId, function() {
canCall(); me._syncInfo.note.deletes.push(localNoteId);
canCall();
});
}); });
return; return;
} }
// 2) 查看本地的, 与本地合并 // 2) 查看本地的, 与本地合并
Note.getNote(noteId, function(noteLocal) { Note.getNoteByServerNoteId(noteId, function(noteLocal) {
// 2.1 本地没有, 表示是新建 // 2.1 本地没有, 表示是新建
if(!noteLocal) { if(!noteLocal) {
log('add: ...') log('add: ...')
@@ -265,6 +269,8 @@ var Sync = {
// 冲突, 将本地修改的笔记复制一份(设置冲突字段, ConflictNoteId), 远程的覆盖本地的 // 冲突, 将本地修改的笔记复制一份(设置冲突字段, ConflictNoteId), 远程的覆盖本地的
if(noteLocal.IsDirty) { if(noteLocal.IsDirty) {
log('冲突....') log('冲突....')
note.ServerNoteId = note.NoteId;
note.NoteId = noteLocal.NoteId;
me._syncInfo.note.conflicts.push(note); me._syncInfo.note.conflicts.push(note);
return canCall(); return canCall();
// 2.3 服务器是最新的, 用服务器的 // 2.3 服务器是最新的, 用服务器的
@@ -360,16 +366,20 @@ var Sync = {
fixConflicts: function(callback) { fixConflicts: function(callback) {
var me = this; var me = this;
var afterInfo = me._syncInfo; var afterInfo = me._syncInfo;
log('处理冲突....') log('处理冲突....');
log(me._syncInfo); log(me._syncInfo);
Notebook.fixConflicts(me._syncInfo.notebook, me._notebookWeb); if(me._notebookWeb) {
Note.fixConflicts(me._syncInfo.note, me._noteWeb, function() { Notebook.fixConflicts(me._syncInfo.notebook, me._notebookWeb);
// 避免无限循环, 别send changes了 }
if(!me._needIncrSyncAgain) { if(me._noteWeb) {
// send changes Note.fixConflicts(me._syncInfo.note, me._noteWeb, function() {
callback && callback(); // 避免无限循环, 别send changes了
} if(!me._needIncrSyncAgain) {
}); // send changes
callback && callback();
}
});
}
}, },
// 增量同步 // 增量同步
@@ -461,6 +471,8 @@ var Sync = {
if(notebook.LocalIsNew) { if(notebook.LocalIsNew) {
newNotebook.OldNotebookId = notebook.NotebookId; newNotebook.OldNotebookId = notebook.NotebookId;
// 把之前本持的LocalNotebookId存起来
newNotebook.LocalNotebookId = notebook.NotebookId;
me._syncInfo.notebook.changeAdds.push(newNotebook); me._syncInfo.notebook.changeAdds.push(newNotebook);
// me._sendInfo.notebook.adds.push(newNotebook); // me._sendInfo.notebook.adds.push(newNotebook);
} else { } else {

View File

@@ -1919,7 +1919,7 @@ Note.fixSyncConflict = function(note, newNote) {
}; };
// 添加同步的notes // 添加同步的notes
Note.addSyncNotes = function(notes) { Note.addSync = function(notes) {
if(isEmpty(notes)) { if(isEmpty(notes)) {
return; return;
} }
@@ -1933,7 +1933,7 @@ Note.addSyncNotes = function(notes) {
} }
} }
// 更新 // 更新
Note.updateSyncNotes = function(notes) { Note.updateSync = function(notes) {
log("??") log("??")
if(isEmpty(notes)) { if(isEmpty(notes)) {
return; return;
@@ -1954,7 +1954,7 @@ Note.updateSyncNotes = function(notes) {
} }
// 删除 // 删除
Note.deleteSyncNotes = function(notes) { Note.deleteSync = function(notes) {
if(isEmpty(notes)) { if(isEmpty(notes)) {
return; return;
} }

View File

@@ -843,22 +843,22 @@ Notebook.deleteNotebook = function(target) {
ajaxGet("/notebook/deleteNotebook", {notebookId: notebookId}, function(ret) { ajaxGet("/notebook/deleteNotebook", {notebookId: notebookId}, function(ret) {
if(ret.Ok) { if(ret.Ok) {
/* self.deleteNotebookFromTree(notebookId);
$(target).parent().remove();
*/
self.tree.removeNode(self.tree.getNodeByTId(notebookId));
if(self.tree2) {
self.tree2.removeNode(self.tree2.getNodeByTId(notebookId));
}
delete Notebook.cache[notebookId];
// 改变nav
Notebook.changeNav();
} else { } else {
alert(ret.Msg); alert(ret.Msg);
} }
}); });
} };
Notebook.deleteNotebookFromTree = function() {
var self = this;
self.tree.removeNode(self.tree.getNodeByTId(notebookId));
if(self.tree2) {
self.tree2.removeNode(self.tree2.getNodeByTId(notebookId));
}
delete Notebook.cache[notebookId];
// 改变nav
Notebook.changeNav();
};
$(function() { $(function() {
//------------------- //-------------------
@@ -977,6 +977,96 @@ $(function() {
//--------------- //---------------
// 笔记本解决冲突 // 笔记本解决冲突
Notebook.fixConflict = function() { //----------------------
// 冲突解决, 增量sync时
// note是服务器端的笔记, newNote是本地复制后的笔记
Notebook.fixSyncConflict = function(note, newNote) {
// Note.cache[note.NoteId] = note;
// Note.cache[newNote.NoteId] = newNote;
/*
Note.addNoteCache(note);
Note.addNoteCache(newNote);
}; var target = $(tt('[noteId="?"]', note.NoteId)); //
// 如果当前笔记在笔记列表中, 那么生成一个新笔记放在这个笔记上面
if(target.length > 0) {
var newHtmlObject = Note._getNoteHtmlObjct(note);
newHtmlObject.insertBefore(target);
}
// 当前这个换成新复制的
target.attr('noteId', newNote.NoteId);
// 重新render 左侧下, 因为有冲突了, 不要render内容啊
// 如果当前编辑的是这个笔记, 那切换到newNote上来
if(Note.curNoteId == note.NoteId) {
Note.curNoteId = newNote.NoteId;
}
*/
};
// notebooks
// <- server 服务器端添加过来的
Notebook.addSync = function(notebooks) {
var me = this;
if(isEmpty(notebooks)) {
return;
}
log('add sync');
for(var i in notebooks) {
var notebook = notebooks[i];
Notebook.setCache(notebook);
me.tree.addNodes(me.tree.getNodeByTId(notebook.ParentNotebookId), {Title: notebook.Title, NotebookId: notebook.NotebookId, IsNew: true}, true, true, false);
/*
// 添加到当前的笔记列表中
var newHtmlObject = Note._getNoteHtmlObjct(note);
log(newHtmlObject);
$('#noteItemList').prepend(newHtmlObject);
*/
}
}
// 本地 -> 添加到服务器上的
// LocalNotebookId
// NotebookId是新的
Notebook.addChange = function(notebooks) {
var me = this;
if(isEmpty(notebooks)) {
return;
}
for(var i in notebooks) {
var notebook = notebooks[i];
me.tree.addNodes(self.tree.getNodeByTId(notebook.ParentNotebookId), {Title: notebook.Title, NotebookId: notebook.NotebookId, IsNew: true}, true, true, false);
}
};
// 更新
Notebook.updateSync = function(notes) {
log("??")
if(isEmpty(notes)) {
return;
}
log("what?")
for(var i in notes) {
var note = notes[i];
note.InitSync = true; // 需要重新获取内容
Note.addNoteCache(note);
// 如果当前修改的是本笔记, 那么重新render之
log('->>>')
log(Note.curNoteId);
if(Note.curNoteId == note.NoteId) {
log('yes---');
Note.changeNote(Note.curNoteId);
}
}
};
// 删除
Notebook.deleteSync = function(notebooks) {
var me = this;
if(isEmpty(notes)) {
return;
}
for(var i in notebooks) {
var notebookId = notebooks[i];
// 删除
me.deleteNotebookFromTree(notebookId);
}
}

View File

@@ -805,7 +805,7 @@
}, },
//method of operate ztree dom //method of operate ztree dom
view = { view = {
addNodes: function(setting, parentNode, newNodes, isSilent, is_new) { // life is_new addNodes: function(setting, parentNode, newNodes, isSilent, is_new, is_edit) { // life is_new
if (setting.data.keep.leaf && parentNode && !parentNode.isParent) { if (setting.data.keep.leaf && parentNode && !parentNode.isParent) {
return; return;
} }
@@ -836,21 +836,26 @@
parentNode.Subs = []; parentNode.Subs = [];
} }
} }
if(is_edit == undefined) {
is_edit = true;
}
data.addNodesData(setting, parentNode, newNodes); data.addNodesData(setting, parentNode, newNodes);
view.createNodes(setting, parentNode.level + 1, newNodes, parentNode, is_new); view.createNodes(setting, parentNode.level + 1, newNodes, parentNode, is_new, is_edit);
// 之前这个父没有子的, 先添加子, 再展开, 再编辑! // 之前这个父没有子的, 先添加子, 再展开, 再编辑!
if (!isSilent) { if (!isSilent) {
if(parentNode.Subs.length == 1) { if(parentNode.Subs.length == 1) {
view.expandCollapseParentNode(setting, parentNode, true, true, function() { view.expandCollapseParentNode(setting, parentNode, true, true, function() {
view.editNode(setting, newNodes[0]); if(is_edit) {
view.editNode(setting, newNodes[0]);
}
}); });
} }
} }
} else { } else {
data.addNodesData(setting, data.getRoot(setting), newNodes); data.addNodesData(setting, data.getRoot(setting), newNodes);
view.createNodes(setting, 0, newNodes, null, is_new); view.createNodes(setting, 0, newNodes, null, is_new, is_edit);
} }
}, },
appendNodes: function(setting, level, nodes, parentNode, initFlag, openFlag) { appendNodes: function(setting, level, nodes, parentNode, initFlag, openFlag) {
@@ -1021,7 +1026,7 @@
}, },
// 第一次一起调用, nodes所有notebooks // 第一次一起调用, nodes所有notebooks
// life is_new, 为了在最前面添加 // life is_new, 为了在最前面添加
createNodes: function(setting, level, nodes, parentNode, is_new) { createNodes: function(setting, level, nodes, parentNode, is_new, is_edit) {
if (!nodes || nodes.length == 0) return; if (!nodes || nodes.length == 0) return;
var root = data.getRoot(setting), var root = data.getRoot(setting),
childKey = setting.data.key.children, childKey = setting.data.key.children,
@@ -1029,12 +1034,16 @@
root.createdNodes = []; root.createdNodes = [];
var zTreeHtml = view.appendNodes(setting, level, nodes, parentNode, true, openFlag); var zTreeHtml = view.appendNodes(setting, level, nodes, parentNode, true, openFlag);
// 根节点下添加 // 根节点下添加
if(is_edit == undefined) {
is_edit = true;
}
if (!parentNode) { if (!parentNode) {
if(is_new) { if(is_new) {
// 在最前面添加 // 在最前面添加
setting.treeObj.find("li").eq(0).after(zTreeHtml.join('')); setting.treeObj.find("li").eq(0).after(zTreeHtml.join(''));
// edit it // edit it
view.editNode(setting, nodes[0]); if(is_edit)
view.editNode(setting, nodes[0]);
} else { } else {
setting.treeObj.append(zTreeHtml.join('')); setting.treeObj.append(zTreeHtml.join(''));
} }
@@ -1044,7 +1053,8 @@
if(is_new) { if(is_new) {
ulObj.prepend(zTreeHtml.join('')); ulObj.prepend(zTreeHtml.join(''));
// edit it // edit it
view.editNode(setting, nodes[0]); if(is_edit)
view.editNode(setting, nodes[0]);
} else { } else {
ulObj.append(zTreeHtml.join('')); ulObj.append(zTreeHtml.join(''));
} }
@@ -1521,13 +1531,13 @@
var zTreeTools = { var zTreeTools = {
setting : setting, setting : setting,
addNodes : function(parentNode, newNodes, isSilent, is_new) { // is_new life addNodes : function(parentNode, newNodes, isSilent, is_new, is_edit) { // is_new life
if (!newNodes) return null; if (!newNodes) return null;
if (!parentNode) parentNode = null; if (!parentNode) parentNode = null;
if (parentNode && !parentNode.isParent && setting.data.keep.leaf) return null; if (parentNode && !parentNode.isParent && setting.data.keep.leaf) return null;
var xNewNodes = tools.clone(tools.isArray(newNodes)? newNodes: [newNodes]); var xNewNodes = tools.clone(tools.isArray(newNodes)? newNodes: [newNodes]);
function addCallback() { function addCallback() {
view.addNodes(setting, parentNode, xNewNodes, (isSilent==true), is_new); view.addNodes(setting, parentNode, xNewNodes, (isSilent==true), is_new, is_edit);
} }
if (tools.canAsync(setting, parentNode)) { if (tools.canAsync(setting, parentNode)) {