diff --git a/node_modules/common.js b/node_modules/common.js index 54bb972f..28e17267 100644 --- a/node_modules/common.js +++ b/node_modules/common.js @@ -124,6 +124,23 @@ var Common = { ret.nameNotExt = ret.name; } return ret; + }, + // 2014-01-06T18:29:48.802+08:00 + goNowToDate: function (goNow) { + if(!goNow) { + return new Date(); + } + // new Date(); + if(typeof goNow == 'object') { + alert(3); + return date; + } + var str = goNow.substr(0, 10) + " " + goNow.substr(11, 8); + try { + return new Date(str); + } catch(e) { + return new Date(); + } } }; module.exports = Common; diff --git a/node_modules/evt.js b/node_modules/evt.js index fa4d9910..94441407 100644 --- a/node_modules/evt.js +++ b/node_modules/evt.js @@ -26,6 +26,9 @@ var Evt = { getAttachLocalUrl: function(fileId) { return this.localUrl + '/api/file/getAttach?fileId=' + fileId; }, + getAllAttachLocalUrl: function(noteId) { + return this.localUrl + '/api/file/getAllAttachs?noteId=' + noteId; + }, // 项目绝对地址 getBasePath: function() { var me = this; @@ -39,8 +42,8 @@ var Evt = { }, setDataBasePath: function(dataBasePath) { var me = this; - console.log('...........') - console.log(dataBasePath); + // console.log('...........') + // console.log(dataBasePath); me.dataBasePath = dataBasePath; } }; diff --git a/node_modules/note.js b/node_modules/note.js index 54787fed..1e88eb14 100644 --- a/node_modules/note.js +++ b/node_modules/note.js @@ -365,8 +365,9 @@ var Note = { var reg2 = new RegExp(Evt.leanoteUrl + '/api/file/getAttach', 'g'); content = content.replace(reg2, Evt.localUrl + '/api/file/getAttach'); - var reg3 = new RegExp(Evt.leanoteUrl + '/api/file/getAllAttach', 'g'); - content = content.replace(reg3, Evt.localUrl + '/api/file/getAllAttach'); + // api/file/getAllAttachs?noteId=xxxxxxxxx, 这里的noteId是服务器上的noteId啊 + var reg3 = new RegExp(Evt.leanoteUrl + '/api/file/getAllAttachs', 'g'); + content = content.replace(reg3, Evt.localUrl + '/api/file/getAllAttachs'); return content; }, @@ -384,8 +385,8 @@ var Note = { var reg2 = new RegExp(Evt.localUrl + '/api/file/getAttach', 'g'); content = content.replace(reg2, Evt.leanoteUrl + '/api/file/getAttach'); - var reg3 = new RegExp(Evt.localUrl + '/api/file/getAllAttach', 'g'); - content = content.replace(reg3, Evt.leanoteUrl + '/api/file/getAllAttach'); + var reg3 = new RegExp(Evt.localUrl + '/api/file/getAllAttachs', 'g'); + content = content.replace(reg3, Evt.leanoteUrl + '/api/file/getAllAttachs'); return content; }, @@ -546,6 +547,10 @@ var Note = { note.NoteId = Common.objectId(); note.LocalIsDelete = false; + console.error('add note' + note.Title + Common.goNowToDate(note.CreatedTime)); + note.CreatedTime = Common.goNowToDate(note.CreatedTime); + note.UpdatedTime = Common.goNowToDate(note.UpdatedTime); + // 附件操作 var files = note.Files || []; var attachs = []; @@ -567,6 +572,9 @@ var Note = { console.log(err); callback && callback(false); } else { + // console.log("?????????") + // console.log(note); + // console.log(note.CreatedTime); callback && callback(newDoc); // 下载内容, 图片, 附件 @@ -660,6 +668,10 @@ var Note = { console.log(everNote.NoteId); console.log(everNote); + // 不要服务器上的 + delete note['UpdatedTime']; + delete note['CreatedTime']; + Notes.update({NoteId: note.NoteId}, {$set: note}, {}, function (err, cnt) { // Callback is optional console.log('re:'); console.log(err); @@ -740,6 +752,8 @@ var Note = { console.log(everAttachs); delete note['Files']; + delete note['UpdatedTime']; + delete note['CreatedTime']; Notes.update({NoteId: note.NoteId}, {$set: note}, function(err, n) { if(err || !n) { @@ -991,7 +1005,6 @@ var Note = { }); })(i); - } // 服务器没有, 但是是发送更新的, 所以需要作为添加以后再send changes diff --git a/node_modules/notebook.js b/node_modules/notebook.js index c192afa1..5fbe873f 100644 --- a/node_modules/notebook.js +++ b/node_modules/notebook.js @@ -278,6 +278,10 @@ var Notebook = { } else { // 否则, 就用服务器上的 } + + notebook.CreatedTime = Common.goNowToDate(notebook.CreatedTime); + notebook.UpdatedTime = Common.goNowToDate(notebook.UpdatedTime); + NB.insert(notebook, function (err, newDoc) { // Callback is optional if(err) { console.log(err); diff --git a/node_modules/user.js b/node_modules/user.js index a513c265..0f312702 100644 --- a/node_modules/user.js +++ b/node_modules/user.js @@ -227,6 +227,26 @@ User = { db.g.update({_id: '1'}, {$set: data}, {upsert: true}, function() { callback && callback(); }); + }, + /** + * [saveCurState description] + * @param {[type]} state [description] + * @return {[type]} [description] + User.saveCurState({ + StarredOpened: StarredOpened, + NotebookOpened: NotebookOpened, + TagOpened: TagOpened, + CurNoteId: CurNoteId, + CurIsStarred: CurIsStarred, + CurNotebookId: CurNotebookId, + CurTag: CurTag + }, callback); + */ + saveCurState: function(state, callback) { + state = state || {}; + db.g.update({_id: '1'}, {$set: state}, {upsert: true}, function() { + callback && callback(); + }); } }; diff --git a/note.html b/note.html index 79e281d9..ef59866f 100755 --- a/note.html +++ b/note.html @@ -106,7 +106,7 @@ function log(o) { -
+
@@ -346,14 +346,19 @@ function log(o) { Choose Files + +
diff --git a/package.json b/package.json index 7472ad68..d589a59a 100755 --- a/package.json +++ b/package.json @@ -6,8 +6,8 @@ "window": { "toolbar": true, - "frame": false, - "transparent": true, + "frame": true, + "transparent": false, "min_width": 400, "min_height": 200, diff --git a/public/js/app/note.js b/public/js/app/note.js index 3ed7e9bf..ea7909b4 100644 --- a/public/js/app/note.js +++ b/public/js/app/note.js @@ -201,7 +201,31 @@ Note.renderNotesAndFirstOneContent = function(ret) { Note.changeNoteForPjax(ret[0].NoteId, true, false); } else { } -} +}; + +// 渲染并定位到特定的 +Note.renderNotesAndTargetNote = function(ret, noteId) { + // 错误的ret是一个Object + if(!isArray(ret)) { + return; + } + + // note 导航 + Note.renderNotes(ret); + // 渲染特定的 + if(!isEmpty(ret[0])) { + if(noteId) { + // Note.changeNoteForPjax(noteId, true, false); + Note.changeNoteForPjax(noteId, true, false); + if(!Note.directToNote(noteId)) { + // 找不到啊 + Note.changeNoteForPjax(ret[0].NoteId, true, false); + } + } else { + Note.changeNoteForPjax(ret[0].NoteId, true, false); + } + } +}; // 当前的note是否改变过了? // 返回已改变的信息 @@ -428,7 +452,7 @@ Note.curChangedSaveIt = function(force, callback) { Note.setNoteCache(hasChanged, false); // 设置更新时间 - Note.setNoteCache({"NoteId": hasChanged.NoteId, "UpdatedTime": (new Date()).format("yyyy-MM-ddThh:mm:ss.S")}, false); + Note.setNoteCache({"NoteId": hasChanged.NoteId, "UpdatedTime": new Date()}, false); // 表示有未完成的保存 /* @@ -445,7 +469,7 @@ Note.curChangedSaveIt = function(force, callback) { me.saveInProcess[hasChanged.NoteId] = true; - console.error('保存当前的笔记: ' + hasChanged.NoteId); + // console.error('保存当前的笔记: ' + hasChanged.NoteId); NoteService.updateNoteOrContent(hasChanged, function(ret) { me.saveInProcess[hasChanged.NoteId] = false; @@ -524,13 +548,19 @@ Note.hideContentLoading = function() { $("#noteMaskForLoading").css("z-index", -1); }; +// 定位到笔记 Note.directToNote = function(noteId) { var $p = $("#noteItemList"); var pHeight = $p.height(); // 相对于父亲的位置 - var pTop = $("[noteId='" + noteId + "']").position().top; + var $t = $("[noteId='" + noteId + "']"); + if($t.length == 0) { + return false; + } + // position方法返回的是元素的在页面内的绝对位置信息,top和left + var pTop = $t.position().top; var scrollTop = $p.scrollTop(); - pTop += scrollTop; + pTop += scrollTop + 66; // 66是上面的title, search /* log(".."); log(noteId); @@ -539,16 +569,13 @@ Note.directToNote = function(noteId) { // 当前的可视范围的元素位置是[scrollTop, pHeight + scrollTop] if(pTop >= scrollTop && pTop <= pHeight + scrollTop) { + // alert(pTop + ' ' + scrollTop + ' ' + pHeight) } else { var top = pTop; - log("定位到特定note, 在可视范围内"); - // 手机不用slimScroll - if(!LEA.isMobile && !Mobile.isMobile()) { - $("#noteItemList").scrollTop(top); - // $("#noteItemList").slimScroll({ scrollTo: top + 'px', height: "100%", onlyScrollBar: true}); - } else { - } + console.log("定位到特定note, 在可视范围内"); + $("#noteItemList").scrollTop(top - 66); } + return true; }; // mustPush表示是否将状态push到state中, 默认为true @@ -997,7 +1024,16 @@ Note.newNote = function(notebookId, isShare, fromUserId, isMarkdown) { // 保存当前的笔记 Note.curChangedSaveIt(); - var note = {NoteId: getObjectId(), Title: "", Tags:[], Content:"", NotebookId: notebookId, IsNew: true, FromUserId: fromUserId, IsMarkdown: isMarkdown}; // 是新的 + var note = {NoteId: getObjectId(), + Title: "H", + Tags:[], Content:"", + NotebookId: notebookId, + IsNew: true, + FromUserId: fromUserId, + IsMarkdown: isMarkdown, + CreatedTime: new Date(), + UpdatedTime: new Date()}; // 是新的 + // 添加到缓存中 Note.addNoteCache(note); @@ -1429,6 +1465,29 @@ Note.isSameSearch = function(key) { // 搜索笔记 Note.searchSeq = 0; + +// for recoverState +Note.searchNoteSys = function(val, noteId) { + $("#searchNoteInput").val(val); + var me = this; + NoteService.searchNote(val, function(notes) { + if(notes) { + Note.searchKey = val; + Notebook.changeCurNotebookTitle('Search results', false, notes.length, false, true); + Note.renderNotes(notes); + // markdown一旦setContent就focus, 导致搜索失去焦点 + setTimeout(function() { + $("#searchNoteInput").focus(); + }) + if(!isEmpty(notes)) { + Note.renderNotesAndTargetNote(notes, noteId); + } + } else { + // abort的 + } + }); +}; + Note.searchNote = function() { var val = $("#searchNoteInput").val(); if(!val) { @@ -1462,7 +1521,8 @@ Note.searchNote = function() { NoteService.searchNote(val, function(notes) { hideLoading(); if(t == Note.searchSeq && notes) { - Notebook.changeCurNotebookTitle('Search results', false, notes.length); + Note.searchKey = val; + Notebook.changeCurNotebookTitle('Search results', false, notes.length, false, true); Note.renderNotes(notes); // markdown一旦setContent就focus, 导致搜索失去焦点 setTimeout(function() { @@ -2158,6 +2218,8 @@ var Attach = { var attachId = $(this).closest('li').data("id"); var attach = self.attachsMap[attachId]; var src = EvtService.getAttachLocalUrl(attachId); // + "/attach/download?attachId=" + attachId; + // http://leanote.com/attach/download?attachId=54f7481638f4112ff000170f + if(LEA.isMarkdownEditor() && MD) { MD.insertLink(src, attach.Title); } else { @@ -2167,12 +2229,15 @@ var Attach = { // make all link self.linkAllBtnO.on("click",function(e) { + // 暂不支持 + return; e.stopPropagation(); var note = Note.getCurNote(); if(!note) { return; } - var src = UrlPrefix + "/attach/downloadAll?noteId=" + Note.curNoteId + var src = EvtService.getAllAttachLocalUrl(note.NoteId); // UrlPrefix + "/attach/downloadAll?noteId=" + Note.curNoteId + // src = 'http://leanote.com/attach/downloadAll?noteId=' + note.NoteId; var title = note.Title ? note.Title + ".tar.gz" : "all.tar.gz"; if(LEA.isMarkdownEditor() && MD) { diff --git a/public/js/app/notebook.js b/public/js/app/notebook.js index e3f94eb8..6a2bbe0b 100644 --- a/public/js/app/notebook.js +++ b/public/js/app/notebook.js @@ -638,7 +638,7 @@ Notebook.curActiveNotebookIsAll = function() { // 3. 使用Note.RederNotes() // callback Pjax, 当popstate时调用 Notebook.changeNotebookSeq = 1; -Notebook.changeNotebook = function(notebookId, callback) { +Notebook.changeNotebook = function(notebookId, callback, needRendNoteId) { var me = this; Notebook.changeNotebookNav(notebookId); @@ -666,7 +666,7 @@ Notebook.changeNotebook = function(notebookId, callback) { if(callback) { callback(cacheNotes); } else { - Note.renderNotesAndFirstOneContent(cacheNotes); + Note.renderNotesAndTargetNote(cacheNotes, needRendNoteId); } return; } @@ -680,7 +680,7 @@ Notebook.changeNotebook = function(notebookId, callback) { if(callback) { callback(cacheNotes); } else { - Note.renderNotesAndFirstOneContent(cacheNotes); + Note.renderNotesAndTargetNote(cacheNotes, needRendNoteId); } return; } else { @@ -705,7 +705,7 @@ Notebook.changeNotebook = function(notebookId, callback) { if(callback) { callback(cacheNotes); } else { - Note.renderNotesAndFirstOneContent(cacheNotes); + Note.renderNotesAndTargetNote(cacheNotes, needRendNoteId); } me.hideNoteAndEditorLoading(); }; @@ -719,10 +719,12 @@ Notebook.changeNotebook = function(notebookId, callback) { }; // 改变标签, isStarred是否是星笔记本 -Notebook.changeCurNotebookTitle = function(title, isStarred, subTitle) { +Notebook.changeCurNotebookTitle = function(title, isStarred, subTitle, isTag, isSearch) { var me = this; $("#curNotebookForListNote").html(title); me.isStarred = isStarred; + me.isTag = isTag; + me.isSearch = isSearch; }; // 笔记列表与编辑器的mask loading diff --git a/public/js/app/page.js b/public/js/app/page.js index e99e5a80..9d3cb2c9 100644 --- a/public/js/app/page.js +++ b/public/js/app/page.js @@ -1315,19 +1315,120 @@ function incrSync() { SyncService.incrSync(); } +// 历史, 恢复原貌 +var State = { + // 保存当前状态 + // 什么时候调用? 关闭程序, 改变note时 + saveCurState: function(callback) { + // 左侧, 开闭状态 + var StarredOpened = false; + var NotebookOpened = false; + var TagOpened = false; + var $leftOpen = $('.folderNote.opened'); + if($leftOpen.length == 1) { + var id = $leftOpen.attr('id'); + if(id == 'myStarredNotes') { + StarredOpened = true; + } else if(id == 'myNotebooks') { + NotebookOpened = true; + } else if(id == 'myTag') { + TagOpened = true; + } + } + // 当前笔记 + var CurNoteId = Note.curNoteId; // 当前打开的笔记 + var CurIsStarred = false; // 当前是在星下 + var CurNotebookId = ''; // 定位到某个笔记本 + var CurTag = ''; // 搜索tag + if(Notebook.isSearch) { + var CurSearchKey = Note.searchKey; + } + if(Notebook.isStarred) { + CurIsStarred = true; + } else if(Notebook.isTag) { + CurTag = Tag.curTag; + } + CurNotebookId = Notebook.curNotebookId; + + var state = { + StarredOpened: StarredOpened, + NotebookOpened: NotebookOpened, + TagOpened: TagOpened, + + CurNoteId: CurNoteId, + CurIsStarred: CurIsStarred, + CurNotebookId: CurNotebookId, + CurTag: CurTag, + CurSearchKey: CurSearchKey + }; + console.log(state); + UserService.saveCurState(state, callback); + }, + + // 恢复状态 + recoverState: function(state) { + console.log(state); + // 1. 左侧哪个open + if(!state.NotebookOpened) { + $('.folderNote.opened').removeClass('opened').addClass('closed'); + if(state.StarredOpened) { + $('#myStarredNotes').removeClass('closed').addClass('opened'); + } else if(state.TagOpened) { + $('#myTag').removeClass('closed').addClass('opened'); + } + } + // 2. + // 当前是starred notes + var notebookId = state.CurNotebookId; + if(state.CurIsStarred) { + Note.renderStarNote($('#myStarredNotes li[data-id="' + state.CurNoteId + '"]')); + } + // 搜索标签 + else if(state.CurTag) { + Tag.searchTag(state.CurTag, state.CurNoteId); + } + // 搜索笔记 + else if(state.CurSearchKey) { + Note.searchNoteSys(state.CurSearchKey, state.CurNoteId); + } + // 笔记本了 + else { + Notebook.expandNotebookTo(notebookId); + Notebook.changeNotebook(notebookId, false, state.CurNoteId); + } + + } +}; + // note.html调用 // 实始化页面 // 判断是否登录 function initPage() { + win.on('close', function() { + State.saveCurState(function() { + win.close(true); + }); + }); + // 注入前端变量# WebService.set(Notebook, Note, Attach, Tag); + // 在显示notebooks, stars, tags后才recoverState + var i = 0; + function ok() { + i++; + if(i == 3) { + State.recoverState(UserInfo); + } + } + function _init() { $(function() { // 获取笔记本 Service.notebookService.getNotebooks(function(notebooks) { log(notebooks); Notebook.renderNotebooks(notebooks); + ok(); }); // 获得笔记 @@ -1340,6 +1441,7 @@ function initPage() { // 获取star笔记 NoteService.getStarNotes(function(notes) { Note.renderStars(notes); + ok(); }); // 指定笔记, 也要保存最新笔记 @@ -1352,6 +1454,7 @@ function initPage() { // 标签 TagService.getTags(function(tags) { Tag.renderTagNav(tags); + ok(); }); // init notebook后才调用 @@ -1506,6 +1609,7 @@ function userMenu() { changeTheme(UserInfo.Theme); } + $(function() { initUploadImage(); userMenu(); diff --git a/public/js/app/service.js b/public/js/app/service.js index e0f8c0a7..bd352d12 100644 --- a/public/js/app/service.js +++ b/public/js/app/service.js @@ -79,10 +79,7 @@ $(function() { // bind close event // 保存当前打开的笔记 -win.on('close', function() { - // TODO - win.close(true); -}); + win.on('focus', function() { console.log('window is focused '); }); diff --git a/public/js/app/tag.js b/public/js/app/tag.js index 1c018392..dda8d666 100644 --- a/public/js/app/tag.js +++ b/public/js/app/tag.js @@ -281,6 +281,40 @@ Tag.addTagNav = function(newTag) { me.renderTagNav(me.tags); }; +Tag.searchTag = function(tag, noteId) { + // var $li = $(this).closest('li'); + // var tag = $.trim($li.data("tag")); + // tag = Tag.mapCn2En[tag] || tag; + + // 学习changeNotebook + + // 1 + Note.curChangedSaveIt(); + + // 2 先清空所有 + // 也会把curNoteId清空 + Note.clearAll(); + + // $("#tagSearch").html($li.html()).show(); + + Notebook.changeCurNotebookTitle(tag, false, '', true); + Tag.curTag = tag; + // $('#curNotebookForListNote').find('i, em').remove(); + // $("#tagSearch .tag-delete").remove(); + + NoteService.searchNoteByTag(tag, function(notes) { + hideLoading(); + if(notes) { + // 和note搜索一样 + // 设空, 防止发生上述情况 + // Note.curNoteId = ""; + Note.renderNotes(notes); + Note.renderNotesAndTargetNote(notes, noteId); + } + }); +}; + + // 事件 $(function() { // tag @@ -365,6 +399,10 @@ $(function() { function searchTag() { var $li = $(this).closest('li'); var tag = $.trim($li.data("tag")); + + Tag.searchTag(tag); + /* + // tag = Tag.mapCn2En[tag] || tag; // 学习changeNotebook @@ -379,7 +417,8 @@ $(function() { // $("#tagSearch").html($li.html()).show(); var h = $li.html(); - Notebook.changeCurNotebookTitle(h); + Notebook.changeCurNotebookTitle(h, false, '', true); + Tag.curTag = h; $('#curNotebookForListNote').find('i, em').remove(); // $("#tagSearch .tag-delete").remove(); @@ -396,7 +435,9 @@ $(function() { } } }); - } + */ + }; + $("#myTag .folderBody").on("click", "li .label", searchTag); // $("#minTagNav").on("click", "li", searchTag); diff --git a/test.js b/test.js index 46681dcd..5c0d52fc 100755 --- a/test.js +++ b/test.js @@ -6,6 +6,7 @@ var Api = require('api'); var User = require('user'); var Server = require('server'); var Evt = require('evt'); +var Common = require('common'); /* Notebook.addNotebook("1", "life"); @@ -20,7 +21,7 @@ Api.addNotebook({ // Api.uploadImage(); User.userId = '54bdc65599c37b0da9000002'; User.userId = '54d7620d99c37b030600002c'; -User.userId = '54f08a1699c37b877b0001ee'; +User.userId = '54f6e72899c37b6e20000044'; // 54d7624205fcd105da00005 @@ -37,12 +38,11 @@ User.init(function() { */ - Note.getNoteByServerNoteId('54f1a1f899c37b4faf000001', function(note) { - console.log(note); - }); + // Note.getNoteByServerNoteId('54f1a1f899c37b4faf000001', function(note) { + // console.log(note); + // }); - Note.getDirtyNotes(function(ret) { - console.log('getDirtyNotes: '); + Note.getNotes('', function(ret) { console.log(ret); }); }); @@ -112,5 +112,6 @@ console.log(reg); while(s = reg.exec(a)) { console.log(s); } +*/ -*/ \ No newline at end of file +console.log(Common.goNowToDate('2014-01-06T18:29:48.802+08:00')); \ No newline at end of file