notebook, note同步

note待同步内容, 图片
This commit is contained in:
life
2015-01-22 00:41:49 +08:00
parent 1674b9f91d
commit 1d015642a6
8 changed files with 339 additions and 73 deletions

29
node_modules/api.js generated vendored
View File

@@ -14,6 +14,9 @@ var Api = {
baseUrl: 'http://localhost:9000/api', baseUrl: 'http://localhost:9000/api',
getUrl: function(url, param) { getUrl: function(url, param) {
var url = this.baseUrl + '/' + url; var url = this.baseUrl + '/' + url;
var token = User.getToken();
param = param || {};
param.token = token;
if(param) { if(param) {
var paramStr = ''; var paramStr = '';
for(var i in param) { for(var i in param) {
@@ -25,19 +28,6 @@ var Api = {
} }
return url + '?' + paramStr; return url + '?' + paramStr;
}, },
isOk: function(ret) {
// 数组
if(length in ret) {
return true;
}
if(ret == 'object') {
if(!ret.Ok) { // 指明了Ok
return false;
}
return true;
}
return false;
},
// 登录 // 登录
auth: function(email, pwd, callback) { auth: function(email, pwd, callback) {
var me = this; var me = this;
@@ -47,7 +37,7 @@ var Api = {
var ret = response.body; var ret = response.body;
// 登录成功, 保存token // 登录成功, 保存token
log(ret); log(ret);
if(me.isOk(ret)) { if(Common.isOk(ret)) {
ret.Pwd = pwd; ret.Pwd = pwd;
User.setCurUser(ret); User.setCurUser(ret);
callback && callback(ret); callback && callback(ret);
@@ -94,9 +84,10 @@ var Api = {
}, },
getSyncNotebooks: function(afterUsn, maxEntry, callback) { getSyncNotebooks: function(afterUsn, maxEntry, callback) {
var me = this; var me = this;
needle.get(this.getUrl('notebook/getSyncNotebooks', {afterUsn: afterUsn, maxEntry: maxEntry}), function(error, response) { var url = this.getUrl('notebook/getSyncNotebooks', {afterUsn: afterUsn, maxEntry: maxEntry});
needle.get(url, function(error, response) {
var ret = response.body; var ret = response.body;
if(me.isOk(ret)) { if(Common.isOk(ret)) {
callback && callback(ret); callback && callback(ret);
} else { } else {
callback && callback(false); callback && callback(false);
@@ -105,9 +96,11 @@ var Api = {
}, },
getSyncNotes: function(afterUsn, maxEntry, callback) { getSyncNotes: function(afterUsn, maxEntry, callback) {
var me = this; var me = this;
needle.get(this.getUrl('note/getSyncNotes', {afterUsn: afterUsn, maxEntry: maxEntry}), function(error, response) { var url = this.getUrl('note/getSyncNotes', {afterUsn: afterUsn, maxEntry: maxEntry});
log(url);
needle.get(url, function(error, response) {
var ret = response.body; var ret = response.body;
if(me.isOk(ret)) { if(Common.isOk(ret)) {
callback && callback(ret); callback && callback(ret);
} else { } else {
callback && callback(false); callback && callback(false);

16
node_modules/common.js generated vendored
View File

@@ -18,6 +18,22 @@ var Common = {
this._uuid++; this._uuid++;
return ((new Date()).getTime()) + '_' + this._uuid; return ((new Date()).getTime()) + '_' + this._uuid;
}, },
isOk: function(ret) {
if(!ret) {
return ret;
}
// 数组
if('length' in ret) {
return true;
}
if(ret == 'object') {
if(!ret.Ok) { // 指明了Ok
return false;
}
return true;
}
return false;
},
// FileReaderWeb 是 web上的FileReader, 可能与nodejs这个有冲突 // FileReaderWeb 是 web上的FileReader, 可能与nodejs这个有冲突
pasteImage: function(event, FileReaderWeb, callback) { pasteImage: function(event, FileReaderWeb, callback) {
var me = this; var me = this;

53
node_modules/note.js generated vendored
View File

@@ -117,6 +117,59 @@ var Note = {
return callback && callback(notes); return callback && callback(notes);
}); });
}, },
// 得到笔记
getNote: function(noteId, callback) {
var me = this;
Notes.findOne({NoteId: noteId}, function(err, doc) {
if(err || !doc) {
log('不存在');
callback && callback(false);
} else {
callback && callback(doc);
}
});
},
//----------------
// 同步
//----------------
// 强制删除
deleteNoteForce: function(noteId, callback) {
var me = this;
Notes.remove({NoteId: noteId}, function(err, n) {
if(err) {
callback && callback(false);
} else {
callback && callback(true);
}
});
},
// 添加笔记本, note object
addNoteForce: function(note, callback) {
note.InitSync = true; // 刚同步完, 表示content, images, attach没有同步
Notes.insert(note, function (err, newDoc) { // Callback is optional
if(err) {
console.log(err);
callback && callback(false);
} else {
callback && callback(newDoc);
}
});
},
// 更新笔记本
updateNoteForce: function(note, callback) {
note.IsDirty = false;
Notes.update({NoteId: note.NoteId}, {$set: note}, {}, function (err, updates) { // Callback is optional
if(err) {
console.log(err);
callback && callback(false);
} else {
callback && callback(note);
}
});
},
}; };
module.exports = Note; module.exports = Note;

57
node_modules/notebook.js generated vendored
View File

@@ -106,7 +106,8 @@ var Notebook = {
Title: title, Title: title,
Seq: -1, Seq: -1,
UserId: User.getCurActiveUserId(), UserId: User.getCurActiveUserId(),
ParentNotebookId: parentNotebookId ParentNotebookId: parentNotebookId,
IsDirty: true, // 必须, 同步后才为非dirty状态
// TODO UrlTitle // TODO UrlTitle
} }
if(notebookId) { if(notebookId) {
@@ -134,6 +135,58 @@ var Notebook = {
log(count); log(count);
NB.update({NotebookId: notebookId}, {$set: {NumberNotes: count}}, {}) NB.update({NotebookId: notebookId}, {$set: {NumberNotes: count}}, {})
}); });
} },
// 得到笔记本
getNotebook: function(notebookId, callback) {
var me = this;
NB.findOne({NotebookId: notebookId}, function(err, doc) {
if(err || !doc) {
log('不存在');
callback && callback(false);
} else {
callback && callback(doc);
}
});
},
//----------------
// 同步
//----------------
// 强制删除
deleteNotebookForce: function(notebookId, callback) {
var me = this;
NB.remove({NotebookId: notebookId}, function(err, n) {
if(err) {
callback && callback(false);
} else {
callback && callback(true);
}
});
},
// 添加笔记本, notebook object
addNotebookForce: function(notebook, callback) {
NB.insert(notebook, function (err, newDoc) { // Callback is optional
if(err) {
console.log(err);
callback && callback(false);
} else {
callback && callback(newDoc);
}
});
},
// 更新笔记本
updateNotebookForce: function(notebook, callback) {
notebook.IsDirty = false;
NB.update({NotebookId: notebook.NotebookId}, {$set: notebook}, {}, function (err, updates) { // Callback is optional
if(err) {
console.log(err);
callback && callback(false);
} else {
callback && callback(notebook);
}
});
},
}; };
module.exports = Notebook; module.exports = Notebook;

235
node_modules/sync.js generated vendored
View File

@@ -6,6 +6,7 @@ var needle = require('needle');
var fs = require('fs'); var fs = require('fs');
var Api = require('api'); var Api = require('api');
var Notebook = require('notebook'); var Notebook = require('notebook');
var Note = require('note');
function log(o) { function log(o) {
console.log(o); console.log(o);
@@ -19,32 +20,51 @@ var Sync = {
note: {adds: [], deletes: [], updates: [], conflicts: []}, note: {adds: [], deletes: [], updates: [], conflicts: []},
tag: {} tag: {}
}, },
// notebook
_syncNotebookIsLastChunk: false, _syncNotebookIsLastChunk: false,
_totalSyncNotebookNum: 0, // 需要同步的数量 _totalSyncNotebookNum: 0, // 需要同步的数量
_tocalHasSyncNotebookNum: 0, // 已同步的数量 _tocalHasSyncNotebookNum: 0, // 已同步的数量
_notebookMaxEntry: 1000, _notebookMaxEntry: 1,
// note
_syncNoteIsLastChunk: false,
_totalSyncNoteNum: 0, // 需要同步的数量
_noteMaxEntry: 1,
_initSyncInfo: function() { _initSyncInfo: function() {
var me = this; var me = this;
// notebook
me._syncNotebookIsLastChunk = false; me._syncNotebookIsLastChunk = false;
me._totalSyncNotebookNum = 0; me._totalSyncNotebookNum = 0;
me._tocalHasSyncNotebookNum = 0; me._totalHasSyncNotebookNum = 0;
me._lockNotebook = 1; me._lockNotebook = 1;
// note
me._syncNoteIsLastChunk = false;
me._totalSyncNoteNum = 0;
me._totalHasSyncNoteNum = 0;
me._lockNote = 1;
me._syncInfo = { me._syncInfo = {
notebook: {adds: [], deletes: [], updates: []}, notebook: {ok: false, adds: [], deletes: [], updates: []},
note: {adds: [], deletes: [], updates: [], conflicts: []}, note: {ok: false, adds: [], deletes: [], updates: [], conflicts: []},
tag: {} tag: {ok: false}
}; };
}, },
//---------------
// notebook
//---------------
// 增加, 有锁 // 增加, 有锁
_lockNotebook: 1, _lockNotebook: 1,
_addSyncNotebookNum: function() { _addSyncNotebookNum: function() {
if(me._lock) { var me = this;
if(me._lockNotebook) {
me._lockNotebook = 0; me._lockNotebook = 0;
me._tocalHasSyncNotebookNum++; me._totalHasSyncNotebookNum++;
me._lockNotebook = 1; me._lockNotebook = 1;
} else { } else {
me._addSyncNotebookNum(); me._addSyncNotebookNum();
@@ -53,13 +73,17 @@ var Sync = {
// 同步笔记本 // 同步笔记本
_syncNotebookToLocal: function(notebooks, callback) { _syncNotebookToLocal: function(notebooks, callback) {
var me = this;
function canCall() { function canCall() {
// 是最后一块, 且 // 是最后一块, 且
me._addSyncNotebookNum(); me._addSyncNotebookNum();
if(me._syncNotebookIsLastChunk && me._tocalHasSyncNotebookNum == me._tocalSyncNotebookNum) { log(me._totalHasSyncNotebookNum + ' ' + me._totalSyncNotebookNum);
if(me._syncNotebookIsLastChunk && me._totalHasSyncNotebookNum >= me._totalSyncNotebookNum) {
me._syncInfo.notebook.ok = true;
callback && callback(true); callback && callback(true);
} }
} }
if(!notebooks || notebooks.length == 0) { if(!notebooks || notebooks.length == 0) {
return canCall(); return canCall();
} }
@@ -68,56 +92,65 @@ var Sync = {
var notebook = notebooks[i]; var notebook = notebooks[i];
// 得到本地的, 与之对比 // 得到本地的, 与之对比
var usn = notebook.Usn; (function(notebook) {
var notebookId = notebook.NotebookId;
// 1) 服务器端删除了, 本地肯定删除 var usn = notebook.Usn;
if(!notebook.CreatedTime) { var notebookId = notebook.NotebookId;
// TODO
Notebook.deleteNotebookForce(notebookId, function() { // 1) 服务器端删除了, 本地肯定删除
// TODO 添加到信息 if(notebook.IsDeleted) {
canCall(); log('delete: ');
}); log(notebook);
return; Notebook.deleteNotebookForce(notebookId, function() {
} me._syncInfo.notebook.deletes.push(notebookId);
// 2) 查看本地的, 与本地合并
Notebook.GetNotebook(notebookId, function(notebookLocal) {
// 2.1 本地没有, 表示是新建
if(!notebookLocal) {
// TODO
Notebook.addNotebookForce(notebook, function() {
// TODO 添加到信息
canCall(); canCall();
}); });
} else { return;
// 2.2 本地是否修改了, 需要合并, 使用服务端的数据
if(notebook.isDirty) {
// 2.3 服务器是最新的, 用服务器的
} else {
}
// 这里都是用服务器端的数据, 不处理冲突
notebook.UpdateNotebookForce(notebookId, notebook.Title, notebook.ParentNotebookId, function() {
// TODO 添加到信息
canCall();
})
} }
}); // 2) 查看本地的, 与本地合并
Notebook.getNotebook(notebookId, function(notebookLocal) {
callback && callback(true); // 2.1 本地没有, 表示是新建
if(!notebookLocal) {
log('add: ...')
// TODO
Notebook.addNotebookForce(notebook, function(notebook) {
me._syncInfo.notebook.adds.push(notebook);
canCall();
});
} else {
// 2.2 本地是否修改了, 需要合并, 使用服务端的数据
if(notebook.IsDirty) {
log('冲突....')
// 2.3 服务器是最新的, 用服务器的
} else {
}
// 这里都是用服务器端的数据, 不处理冲突
Notebook.updateNotebookForce(notebook, function(notebook) {
if(notebook) {
me._syncInfo.notebook.updates.push(notebook);
}
canCall();
})
}
});
})(notebook);
} }
}, },
syncNotebook: function(afterUsn, callback) { syncNotebook: function(afterUsn, callback) {
var me = this; var me = this;
Api.getSyncNotebooks(afterUsn, me._notebookMaxEntry, function(notebooks) { Api.getSyncNotebooks(afterUsn, me._notebookMaxEntry, function(notebooks) {
if(notebooks) { log('syncNotebook')
me._tocalSyncNotebookNum += notebooks.length; log(notebooks);
if(Common.isOk(notebooks)) {
me._totalSyncNotebookNum += notebooks.length;
// 证明可能还有要同步的 // 证明可能还有要同步的
if(notebooks.length == me._notebookMaxEntry) { if(notebooks.length == me._notebookMaxEntry) {
me._syncNotebookToLocal(notebooks); me._syncNotebookToLocal(notebooks);
var last = notebooks[notebooks.length-1]; var last = notebooks[notebooks.length-1];
me.syncNotebook(last.Usn, callback); me.syncNotebook(last.Usn, callback);
} else { } else {
log('no more');
me._syncNotebookIsLastChunk = true; me._syncNotebookIsLastChunk = true;
me._syncNotebookToLocal(notebooks, callback); me._syncNotebookToLocal(notebooks, callback);
} }
@@ -130,10 +163,117 @@ var Sync = {
}); });
}, },
// 同步笔记
syncNote: function(afterUsn, callback) { //-------------
callback && callback(true); // note
//-------------
// 增加, 有锁
_lockNote: 1,
_addSyncNoteNum: function() {
var me = this;
if(me._lockNote) {
me._lockNote = 0;
me._totalHasSyncNoteNum++;
me._lockNote = 1;
} else {
me._addSyncNoteNum();
}
}, },
// 同步笔记本
_syncNoteToLocal: function(notes, callback) {
var me = this;
function canCall() {
// 是最后一块, 且
me._addSyncNoteNum();
log(me._totalHasSyncNoteNum + ' ' + me._totalSyncNoteNum);
if(me._syncNoteIsLastChunk && me._totalHasSyncNoteNum >= me._totalSyncNoteNum) {
me._syncInfo.note.ok = true;
callback && callback(true);
}
}
if(!notes || notes.length == 0) {
return canCall();
}
for(var i in notes) {
var note = notes[i];
// 得到本地的, 与之对比
(function(note) {
var usn = note.Usn;
var noteId = note.NoteId;
// 1) 服务器端删除了, 本地肯定删除
if(note.IsDeleted) {
log('delete: ');
log(note);
Note.deleteNoteForce(noteId, function() {
me._syncInfo.note.deletes.push(noteId);
canCall();
});
return;
}
// 2) 查看本地的, 与本地合并
Note.getNote(noteId, function(noteLocal) {
// 2.1 本地没有, 表示是新建
if(!noteLocal) {
log('add: ...')
// TODO
Note.addNoteForce(note, function(note) {
me._syncInfo.note.adds.push(note);
canCall();
});
} else {
// 2.2 本地是否修改了, 需要合并, 使用服务端的数据
if(note.IsDirty) {
log('冲突....')
// 2.3 服务器是最新的, 用服务器的
} else {
}
// 这里都是用服务器端的数据, 不处理冲突
Note.updateNoteForce(note, function(note) {
if(note) {
me._syncInfo.note.updates.push(note);
}
canCall();
})
}
});
})(note);
}
},
syncNote: function(afterUsn, callback) {
var me = this;
Api.getSyncNotes(afterUsn, me._noteMaxEntry, function(notes) {
log('syncNote')
log(notes);
if(Common.isOk(notes)) {
me._totalSyncNoteNum += notes.length;
// 证明可能还有要同步的
if(notes.length == me._noteMaxEntry) {
me._syncNoteToLocal(notes);
var last = notes[notes.length-1];
me.syncNote(last.Usn, callback);
} else {
log('no more');
me._syncNoteIsLastChunk = true;
me._syncNoteToLocal(notes, callback);
}
} else {
// 同步失败
me._syncInfo.note.ok = false;
me._syncInfo.note.msg = "cann't get all chunks";
callback && callback(false);
}
});
},
// 同步标签 // 同步标签
syncTag: function(afterUsn, callback) { syncTag: function(afterUsn, callback) {
callback && callback(true); callback && callback(true);
@@ -148,6 +288,7 @@ var Sync = {
// 同步笔记本 // 同步笔记本
me.syncNotebook(-1, function(ok) { me.syncNotebook(-1, function(ok) {
if(ok) { if(ok) {
log('------------------')
// 同步笔记 // 同步笔记
me.syncNote(-1, function(ok) { me.syncNote(-1, function(ok) {
if(ok) { if(ok) {
@@ -160,6 +301,7 @@ var Sync = {
} }
}); });
} else { } else {
log('no-------')
callback && callback(me._syncInfo); callback && callback(me._syncInfo);
} }
}); });
@@ -169,6 +311,7 @@ var Sync = {
incrSync: function() { incrSync: function() {
var me = this; var me = this;
me._initSyncInfo(); me._initSyncInfo();
log('full sync start');
// 得到当前LastSyncUsn // 得到当前LastSyncUsn
User.getLastSyncInfo(function(lastSyncUsn, lastSyncTime) { User.getLastSyncInfo(function(lastSyncUsn, lastSyncTime) {
@@ -191,4 +334,4 @@ var Sync = {
} }
}; };
module.exports = Sync module.exports = Sync;

3
node_modules/user.js generated vendored
View File

@@ -74,6 +74,9 @@ var User = {
getCurActiveUserId: function() { getCurActiveUserId: function() {
return this.userId || "user1"; return this.userId || "user1";
}, },
getToken: function() {
return this.token || "user1";
},
getCurUserImagesPath: function() { getCurUserImagesPath: function() {
return Evt.getBasePath() + '/' + this.getCurUserImagesAppPath(); return Evt.getBasePath() + '/' + this.getCurUserImagesAppPath();
}, },

View File

@@ -1295,8 +1295,13 @@ LeaAce = {
}; };
// 全量同步 // 全量同步
function fullSync() { function fullSync(callback) {
log('full sync');
SyncService.fullSync(function(ret) {
log('after....')
log(ret);
callback && callback();
});
} }
// note.html调用 // note.html调用
@@ -1340,7 +1345,9 @@ function initPage() {
UserService.init(function(userInfo) { UserService.init(function(userInfo) {
if(userInfo) { if(userInfo) {
UserInfo = userInfo; UserInfo = userInfo;
_init(); fullSync(function() {
_init();
});
} else { } else {
location.href = 'login.html'; location.href = 'login.html';
} }

View File

@@ -8,13 +8,13 @@ var Service = {
userService: require('user'), userService: require('user'),
tagService: require('tag'), tagService: require('tag'),
apiService: require('api'), apiService: require('api'),
syncServie: require('sync'); syncServie: require('sync')
}; };
// 全局变量 // 全局变量
var ApiService = Service.apiService; var ApiService = Service.apiService;
var UserService = Service.userService; var UserService = Service.userService;
var SyncService = Service.syncService; var SyncService = Service.syncServie;
// 分发服务 // 分发服务
// route = /note/notebook // route = /note/notebook
@@ -51,8 +51,6 @@ $(document).on('contextmenu', function (e) {
}); });
var gui = require('nw.gui'); var gui = require('nw.gui');
console.log("life")
console.log(gui);
function Menu() { function Menu() {
this.menu = new gui.Menu(); this.menu = new gui.Menu();
this.cut = new gui.MenuItem({ this.cut = new gui.MenuItem({