mirror of
https://github.com/leanote/desktop-app.git
synced 2025-10-17 00:15:12 +00:00
<- sync
notebook: add [ok] <- sync notebook: add [ok]
This commit is contained in:
39
node_modules/api.js
generated
vendored
39
node_modules/api.js
generated
vendored
@@ -1,5 +1,7 @@
|
||||
var db = require('db');
|
||||
var User = require('user');
|
||||
var Note = require('note');
|
||||
var Notebook = require('notebook');
|
||||
var Common = require('common');
|
||||
var Tags = db.tags;
|
||||
var needle = require('needle');
|
||||
@@ -8,9 +10,9 @@ var fs = require('fs');
|
||||
function log(o) {
|
||||
console.log(o);
|
||||
}
|
||||
log(Common);
|
||||
log(db);
|
||||
log("??")
|
||||
// log(Common);
|
||||
// log(db);
|
||||
// log("??")
|
||||
|
||||
// 远程数据服务
|
||||
var Api = {
|
||||
@@ -152,6 +154,7 @@ var Api = {
|
||||
log(url);
|
||||
needle.get(url, function(error, response) {
|
||||
if(error) {
|
||||
log(error);
|
||||
return callback && callback(false);
|
||||
}
|
||||
var ret = response.body;
|
||||
@@ -222,12 +225,15 @@ var Api = {
|
||||
//------------
|
||||
// 笔记本操作
|
||||
//------------
|
||||
// 添加
|
||||
addNotebook: function(notebook, callback) {
|
||||
var me = this;
|
||||
// notebook.ParentNotebookId是本的, 要得到远程的
|
||||
Notebook.getServerNotebookIdByNotebookId(notebook.ParentNotebookId, function(serverNotebookId) {
|
||||
var data = {
|
||||
title: notebook.Title,
|
||||
seq: notebook.Seq,
|
||||
parentNotebookId: notebook.ParentNotebookId
|
||||
parentNotebookId: serverNotebookId
|
||||
}
|
||||
log('add notebook');
|
||||
log(data);
|
||||
@@ -243,15 +249,19 @@ var Api = {
|
||||
callback(false);
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
},
|
||||
// 更新
|
||||
updateNotebook: function(notebook, callback) {
|
||||
var me = this;
|
||||
Notebook.getServerNotebookIdByNotebookId(notebook.ParentNotebookId, function(serverNotebookId) {
|
||||
var data = {
|
||||
NotebookId: notebook.NotebookId,
|
||||
NotebookId: notebook.ServerNotebookId,
|
||||
Title: notebook.Title,
|
||||
Usn: notebook.Title,
|
||||
Seq: notebook.Seq,
|
||||
ParentNotebookId: notebook.ParentNotebookId
|
||||
ParentNotebookId: serverNotebookId
|
||||
}
|
||||
needle.post(me.getUrl('notebook/updateNotebook'), data, {}, function(err, resp) {
|
||||
if(err) {
|
||||
@@ -264,9 +274,24 @@ var Api = {
|
||||
callback(false);
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
deleteNotebook: function(notebook, callback) {
|
||||
|
||||
// 删除
|
||||
deleteNotebook: function(serverNotebookId, callback) {
|
||||
var me = this;
|
||||
var data = {NotebookId: serverNotebookId};
|
||||
needle.post(me.getUrl('notebook/deleteNotebook'), data, {}, function(err, resp) {
|
||||
if(err) {
|
||||
return callback(false);
|
||||
}
|
||||
var ret = resp.body;
|
||||
if(Common.isOk(ret)) {
|
||||
callback(ret);
|
||||
} else {
|
||||
callback(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
module.exports = Api;
|
||||
|
117
node_modules/note.js
generated
vendored
117
node_modules/note.js
generated
vendored
@@ -3,11 +3,13 @@ var db = require('db');
|
||||
var User = require('user');
|
||||
var Notebook = require('notebook');
|
||||
var Tag = require('tag');
|
||||
var Api = require('api');
|
||||
// var Api = require('api');
|
||||
var Server = require('server');
|
||||
var Common = require('common');
|
||||
var Notes = db.notes;
|
||||
|
||||
var Api = null; // require('api')
|
||||
|
||||
function log(o) {
|
||||
console.log(o);
|
||||
}
|
||||
@@ -150,6 +152,9 @@ var Note = {
|
||||
updateNoteContentForce: function(noteId, content, callback) {
|
||||
// <img src="http://localhost:9000/api/file/getImage?fileId=54c2083f99c37bea5f000001">
|
||||
// 改成<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');
|
||||
content = content.replace(reg, 'src="' + Server.localUrl + '/api/file/getImage');
|
||||
@@ -183,6 +188,7 @@ var Note = {
|
||||
*/
|
||||
|
||||
// 得到笔记内容
|
||||
// noteId是本地Id
|
||||
getNoteContent: function(noteId, callback) {
|
||||
var me = this;
|
||||
log('getNoteContent------')
|
||||
@@ -195,18 +201,31 @@ var Note = {
|
||||
// 如果笔记是刚同步过来的, 那么内容要重新获取
|
||||
if(note.InitSync) {
|
||||
log('need load from server');
|
||||
|
||||
if(!Api) {
|
||||
Api = require('api')
|
||||
}
|
||||
|
||||
// 远程获取
|
||||
Api.getNoteContent(noteId, function(noteContent) {
|
||||
me.getServerNoteIdByNoteId(noteId, function(serverNoteId) {
|
||||
if(!serverNoteId) {
|
||||
return 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 {
|
||||
log('not need');
|
||||
callback && callback(note);
|
||||
@@ -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 是否真的删除 ?
|
||||
// 有可能服务器上删除了是误删 ?
|
||||
deleteNoteForce: function(noteId, callback) {
|
||||
var me = this;
|
||||
Notes.remove({NoteId: noteId}, function(err, n) {
|
||||
Notes.remove({ServerNoteId: noteId}, function(err, n) {
|
||||
if(err) {
|
||||
callback && callback(false);
|
||||
} else {
|
||||
@@ -233,9 +286,15 @@ var Note = {
|
||||
});
|
||||
},
|
||||
// 添加笔记本, note object
|
||||
// note是服务器传过来的, 需要处理下fix
|
||||
// NoteId, ServerNoteId, NotebookId(本地的)
|
||||
addNoteForce: function(note, callback) {
|
||||
note.InitSync = true; // 刚同步完, 表示content, images, attach没有同步
|
||||
note.IsDirty = false;
|
||||
note.ServerNoteId = note.NoteId;
|
||||
note.NoteId = Common.objectId();
|
||||
Notebook.getNotebookIdByServerNotebookId(note.NotebookId, function(localNotebookId) {
|
||||
note.NotebookId = localNotebookId;
|
||||
Notes.insert(note, function (err, newDoc) { // Callback is optional
|
||||
if(err) {
|
||||
console.log(err);
|
||||
@@ -244,13 +303,20 @@ var Note = {
|
||||
callback && callback(newDoc);
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
// 更新笔记本, 合并之, 内容要重新获取
|
||||
// note是服务器传过来的, 需要处理下fix
|
||||
updateNoteForce: function(note, callback) {
|
||||
var me = this;
|
||||
note.IsDirty = false;
|
||||
note.InitSync = true;
|
||||
note.LocalIsNew = false;
|
||||
Notes.update({NoteId: note.NoteId}, {$set: note}, {}, function (err, cnt) { // Callback is optional
|
||||
me.getNoteIdByServerNoteId(note.NoteId, function(localNoteId) {
|
||||
note.NoteId = localNoteId;
|
||||
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);
|
||||
@@ -259,9 +325,32 @@ var Note = {
|
||||
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) {
|
||||
var me = this;
|
||||
me.getNote(noteId, function(note) {
|
||||
@@ -296,18 +385,25 @@ var Note = {
|
||||
fixConflicts: function(noteSyncInfo, noteWeb, callback) {
|
||||
var me = this;
|
||||
// 处理冲突
|
||||
if(!noteWeb) {
|
||||
return callback && callback();
|
||||
}
|
||||
|
||||
var conflictNotes = noteSyncInfo.conflicts;
|
||||
log('fix note conflicts');
|
||||
if(conflictNotes) {
|
||||
async.eachSeries(conflictNotes, function(note, cb) {
|
||||
var noteId = note.NoteId;
|
||||
// 复制一份
|
||||
me.copyNoteForConfict(noteId, function(newNote) {
|
||||
if(newNote) {
|
||||
// 更新之前的
|
||||
me.updateNoteForce(note, function() {
|
||||
me.updateNoteForceForConflict(note, function(err, note2) {
|
||||
cb();
|
||||
if(!err) {
|
||||
// 前端来处理, 全量sync时不用前端一个个处理
|
||||
noteWeb.fixSyncConflict && noteWeb.fixSyncConflict(note, newNote);
|
||||
noteWeb && noteWeb.fixSyncConflict(note2, newNote);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
cb();
|
||||
@@ -317,21 +413,24 @@ var Note = {
|
||||
// 最后调用
|
||||
callback && callback();
|
||||
});
|
||||
} else {
|
||||
callback && callback();
|
||||
}
|
||||
|
||||
// 处理添加的
|
||||
var addNotes = noteSyncInfo.adds;
|
||||
log('has add...');
|
||||
log(addNotes);
|
||||
noteWeb && noteWeb.addSyncNotes(addNotes);
|
||||
noteWeb.addSync(addNotes);
|
||||
|
||||
log('has updates...');
|
||||
log(noteSyncInfo);
|
||||
log(noteSyncInfo.updates);
|
||||
// 处理更新的
|
||||
noteWeb.updateSyncNotes(noteSyncInfo.updates);
|
||||
noteWeb.updateSync(noteSyncInfo.updates);
|
||||
|
||||
// 处理删除的
|
||||
noteWeb.deleteSyncNotes(noteSyncInfo.deletes);
|
||||
noteWeb.deleteSync(noteSyncInfo.deletes);
|
||||
}
|
||||
};
|
||||
|
||||
|
125
node_modules/notebook.js
generated
vendored
125
node_modules/notebook.js
generated
vendored
@@ -1,4 +1,5 @@
|
||||
var db = require('db');
|
||||
var async = require('async');
|
||||
var User = require('user');
|
||||
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) {
|
||||
var me = this;
|
||||
NB.remove({NotebookId: notebookId}, function(err, n) {
|
||||
NB.remove({ServerNotebookId: notebookId}, function(err, n) {
|
||||
if(err) {
|
||||
callback && callback(false);
|
||||
} else {
|
||||
@@ -176,8 +215,21 @@ var Notebook = {
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// 添加笔记本, notebook object
|
||||
// 这里的notebook是服务器传过来的数据, 需要fix下,
|
||||
addNotebookForce: function(notebook, callback) {
|
||||
var me = this;
|
||||
notebook.ServerNotebookId = notebook.NotebookId;
|
||||
// notebook.NotebookId = Common.objectId();
|
||||
notebook.NotebookId = notebook.NotebookId; // 就采用服务器的, 怕失去了层级
|
||||
me.getNotebookIdByServerNotebookId(notebook.ParentNotebookId, function(parentNotebookId) {
|
||||
// 如果是第一次添加可能会有问题, 数据库还没有数据, 那么还找不到
|
||||
if(parentNotebookId) {
|
||||
notebook.ParentNotebookId = parentNotebookId;
|
||||
} else {
|
||||
// 否则, 就用服务器上的
|
||||
}
|
||||
NB.insert(notebook, function (err, newDoc) { // Callback is optional
|
||||
if(err) {
|
||||
console.log(err);
|
||||
@@ -186,12 +238,18 @@ var Notebook = {
|
||||
callback && callback(newDoc);
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
// 更新笔记本
|
||||
// 这里的notebook是服务器传过来的数据, 需要fix下,
|
||||
updateNotebookForce: function(notebook, callback) {
|
||||
notebook.IsDirty = false;
|
||||
notebook.LocalIsNew = false;
|
||||
NB.update({NotebookId: notebook.NotebookId}, {$set: notebook}, {}, function (err, updates) { // Callback is optional
|
||||
var serverNotebookId = notebook.NotebookId;
|
||||
me.getNotebookIdByServerNotebookId(notebook.ParentNotebookId, function(parentNotebookId) {
|
||||
notebook.ParentNotebookId = parentNotebookId;
|
||||
delete notebook['NotebookId'];
|
||||
NB.update({ServerNotebookId: serverNotebookId}, {$set: notebook}, {}, function (err, updates) { // Callback is optional
|
||||
if(err) {
|
||||
console.log(err);
|
||||
callback && callback(false);
|
||||
@@ -199,12 +257,20 @@ var Notebook = {
|
||||
callback && callback(notebook);
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
//
|
||||
|
||||
// 更新笔记本, NoteId可能也要更改
|
||||
// notebook是服务器传过来的
|
||||
updateNotebookForceForSendChange: function(notebookId, notebook, callback) {
|
||||
notebook.IsDirty = false;
|
||||
notebook.LocalIsNew = false;
|
||||
NB.update({NotebookId: notebookId}, {$set: notebook}, {}, function (err, updates) { // Callback is optional
|
||||
notebook.ServerNotebookId = notebook.NotebookId;
|
||||
me.getNotebookIdByServerNotebookId(notebook.ParentNotebookId, function(parentNotebookId) {
|
||||
notebook.ParentNotebookId = parentNotebookId;
|
||||
NB.update({NotebookId: notebookId}, {$set: notebook}, {}, function (err, n) {
|
||||
if(err) {
|
||||
console.log(err);
|
||||
callback && callback(false);
|
||||
@@ -212,6 +278,7 @@ var Notebook = {
|
||||
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;
|
24
node_modules/sync.js
generated
vendored
24
node_modules/sync.js
generated
vendored
@@ -119,20 +119,22 @@ var Sync = {
|
||||
(function(notebook) {
|
||||
|
||||
var usn = notebook.Usn;
|
||||
var notebookId = notebook.NotebookId;
|
||||
var notebookId = notebook.NotebookId; // 服务器端的
|
||||
|
||||
// 1) 服务器端删除了, 本地肯定删除
|
||||
if(notebook.IsDeleted) {
|
||||
log('delete: ');
|
||||
log(notebook);
|
||||
Notebook.getNotebookIdByServerNotebookId(notebookId, function(localNotebookId) {
|
||||
Notebook.deleteNotebookForce(notebookId, function() {
|
||||
me._syncInfo.notebook.deletes.push(notebookId);
|
||||
me._syncInfo.notebook.deletes.push(localNotebookId);
|
||||
canCall();
|
||||
})
|
||||
});
|
||||
return;
|
||||
}
|
||||
// 2) 查看本地的, 与本地合并
|
||||
Notebook.getNotebook(notebookId, function(notebookLocal) {
|
||||
Notebook.getNotebookByServerNotebookId(notebookId, function(notebookLocal) {
|
||||
// 2.1 本地没有, 表示是新建
|
||||
if(!notebookLocal) {
|
||||
log('add: ...')
|
||||
@@ -245,14 +247,16 @@ var Sync = {
|
||||
if(note.IsDeleted) {
|
||||
log('delete: ');
|
||||
log(note);
|
||||
Note.getNoteIdByServerNoteId(noteId, function(localNoteId) {
|
||||
Note.deleteNoteForce(noteId, function() {
|
||||
me._syncInfo.note.deletes.push(noteId);
|
||||
me._syncInfo.note.deletes.push(localNoteId);
|
||||
canCall();
|
||||
});
|
||||
});
|
||||
return;
|
||||
}
|
||||
// 2) 查看本地的, 与本地合并
|
||||
Note.getNote(noteId, function(noteLocal) {
|
||||
Note.getNoteByServerNoteId(noteId, function(noteLocal) {
|
||||
// 2.1 本地没有, 表示是新建
|
||||
if(!noteLocal) {
|
||||
log('add: ...')
|
||||
@@ -265,6 +269,8 @@ var Sync = {
|
||||
// 冲突, 将本地修改的笔记复制一份(设置冲突字段, ConflictNoteId), 远程的覆盖本地的
|
||||
if(noteLocal.IsDirty) {
|
||||
log('冲突....')
|
||||
note.ServerNoteId = note.NoteId;
|
||||
note.NoteId = noteLocal.NoteId;
|
||||
me._syncInfo.note.conflicts.push(note);
|
||||
return canCall();
|
||||
// 2.3 服务器是最新的, 用服务器的
|
||||
@@ -360,9 +366,12 @@ var Sync = {
|
||||
fixConflicts: function(callback) {
|
||||
var me = this;
|
||||
var afterInfo = me._syncInfo;
|
||||
log('处理冲突....')
|
||||
log('处理冲突....');
|
||||
log(me._syncInfo);
|
||||
if(me._notebookWeb) {
|
||||
Notebook.fixConflicts(me._syncInfo.notebook, me._notebookWeb);
|
||||
}
|
||||
if(me._noteWeb) {
|
||||
Note.fixConflicts(me._syncInfo.note, me._noteWeb, function() {
|
||||
// 避免无限循环, 别send changes了
|
||||
if(!me._needIncrSyncAgain) {
|
||||
@@ -370,6 +379,7 @@ var Sync = {
|
||||
callback && callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
// 增量同步
|
||||
@@ -461,6 +471,8 @@ var Sync = {
|
||||
|
||||
if(notebook.LocalIsNew) {
|
||||
newNotebook.OldNotebookId = notebook.NotebookId;
|
||||
// 把之前本持的LocalNotebookId存起来
|
||||
newNotebook.LocalNotebookId = notebook.NotebookId;
|
||||
me._syncInfo.notebook.changeAdds.push(newNotebook);
|
||||
// me._sendInfo.notebook.adds.push(newNotebook);
|
||||
} else {
|
||||
|
@@ -1919,7 +1919,7 @@ Note.fixSyncConflict = function(note, newNote) {
|
||||
};
|
||||
|
||||
// 添加同步的notes
|
||||
Note.addSyncNotes = function(notes) {
|
||||
Note.addSync = function(notes) {
|
||||
if(isEmpty(notes)) {
|
||||
return;
|
||||
}
|
||||
@@ -1933,7 +1933,7 @@ Note.addSyncNotes = function(notes) {
|
||||
}
|
||||
}
|
||||
// 更新
|
||||
Note.updateSyncNotes = function(notes) {
|
||||
Note.updateSync = function(notes) {
|
||||
log("??")
|
||||
if(isEmpty(notes)) {
|
||||
return;
|
||||
@@ -1954,7 +1954,7 @@ Note.updateSyncNotes = function(notes) {
|
||||
}
|
||||
|
||||
// 删除
|
||||
Note.deleteSyncNotes = function(notes) {
|
||||
Note.deleteSync = function(notes) {
|
||||
if(isEmpty(notes)) {
|
||||
return;
|
||||
}
|
||||
|
@@ -843,22 +843,22 @@ Notebook.deleteNotebook = function(target) {
|
||||
|
||||
ajaxGet("/notebook/deleteNotebook", {notebookId: notebookId}, function(ret) {
|
||||
if(ret.Ok) {
|
||||
/*
|
||||
$(target).parent().remove();
|
||||
*/
|
||||
self.deleteNotebookFromTree(notebookId);
|
||||
} else {
|
||||
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();
|
||||
} else {
|
||||
alert(ret.Msg);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
$(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);
|
||||
}
|
||||
}
|
@@ -805,7 +805,7 @@
|
||||
},
|
||||
//method of operate ztree dom
|
||||
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) {
|
||||
return;
|
||||
}
|
||||
@@ -836,21 +836,26 @@
|
||||
parentNode.Subs = [];
|
||||
}
|
||||
}
|
||||
if(is_edit == undefined) {
|
||||
is_edit = true;
|
||||
}
|
||||
|
||||
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(parentNode.Subs.length == 1) {
|
||||
view.expandCollapseParentNode(setting, parentNode, true, true, function() {
|
||||
if(is_edit) {
|
||||
view.editNode(setting, newNodes[0]);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
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) {
|
||||
@@ -1021,7 +1026,7 @@
|
||||
},
|
||||
// 第一次一起调用, nodes所有notebooks
|
||||
// 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;
|
||||
var root = data.getRoot(setting),
|
||||
childKey = setting.data.key.children,
|
||||
@@ -1029,11 +1034,15 @@
|
||||
root.createdNodes = [];
|
||||
var zTreeHtml = view.appendNodes(setting, level, nodes, parentNode, true, openFlag);
|
||||
// 根节点下添加
|
||||
if(is_edit == undefined) {
|
||||
is_edit = true;
|
||||
}
|
||||
if (!parentNode) {
|
||||
if(is_new) {
|
||||
// 在最前面添加
|
||||
setting.treeObj.find("li").eq(0).after(zTreeHtml.join(''));
|
||||
// edit it
|
||||
if(is_edit)
|
||||
view.editNode(setting, nodes[0]);
|
||||
} else {
|
||||
setting.treeObj.append(zTreeHtml.join(''));
|
||||
@@ -1044,6 +1053,7 @@
|
||||
if(is_new) {
|
||||
ulObj.prepend(zTreeHtml.join(''));
|
||||
// edit it
|
||||
if(is_edit)
|
||||
view.editNode(setting, nodes[0]);
|
||||
} else {
|
||||
ulObj.append(zTreeHtml.join(''));
|
||||
@@ -1521,13 +1531,13 @@
|
||||
|
||||
var zTreeTools = {
|
||||
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 (!parentNode) parentNode = null;
|
||||
if (parentNode && !parentNode.isParent && setting.data.keep.leaf) return null;
|
||||
var xNewNodes = tools.clone(tools.isArray(newNodes)? newNodes: [newNodes]);
|
||||
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)) {
|
||||
|
Reference in New Issue
Block a user