// 1. notebook change // notebook一改变, 当前的肯定要保存, ajax是异步的. 此时先清空所有note信息. -> 得到该notebook的notes, 显示出来, 并选中第一个! // 在这期间定时器还会保存, curNoteId还没换, 所以会清空curNoteId的content!!! // 2. note change, save cur, 立即curNoteId = ""!! // 3. 什么时候设置curNoteId? 是ajax得到内容之后设置 // note Note.curNoteId = ""; Note.interval = ""; // 定时器 // 这里, settings, blog, star Note.itemIsBlog = '
?
? ?
?
?
? ?
?
?
? ?
?
' + note.Content + ""); } else { $("#noteReadContent").html(note.Content); } } //--------------------------- // 搜索 // 有点小复杂, 因为速度过快会导致没加载完, 然后就保存上一个 => 致使标题没有 // 为什么会标题没有? Note.lastSearch = null; Note.lastKey = null; // 判断是否与上一个相等, 相等就不查询, 如果是等了很久再按enter? Note.lastSearchTime = new Date(); Note.isOver2Seconds = false; Note.isSameSearch = function(key) { // 判断时间是否超过了1秒, 超过了就认为是不同的 var now = new Date(); var duration = now.getTime() - Note.lastSearchTime.getTime(); Note.isOver2Seconds = duration > 2000 ? true : false; if(!Note.lastKey || Note.lastKey != key || duration > 1000) { Note.lastKey = key; Note.lastSearchTime = now; return false; } if(key == Note.lastKey) { return true; } Note.lastSearchTime = now; Note.lastKey = key; return false; } // 搜索笔记 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(getMsg('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) { // 定位到all Notebook.changeNotebook("0"); return; } // 判断是否与上一个是相同的搜索, 是则不搜索 if(Note.isSameSearch(val)) { return; } // 之前有, 还有结束的 // if(Note.lastSearch) { // Note.lastSearch.abort(); // } // 步骤与tag的搜索一样 // 1 Note.curChangedSaveIt(); // 2 先清空所有 Note.clearAll(); // 发送请求之 // 先取消上一个 showLoading(); Note.searchSeq++; var t = Note.searchSeq; NoteService.searchNote(val, function(notes) { hideLoading(); if(t == Note.searchSeq && notes) { Note.searchKey = val; Notebook.changeCurNotebookTitle(getMsg('Search results'), false, notes.length, false, true); Note.renderNotes(notes); // markdown一旦setContent就focus, 导致搜索失去焦点 setTimeout(function() { $("#searchNoteInput").focus(); }) if(!isEmpty(notes)) { Note.changeNote(notes[0].NoteId, false/*, true || Note.isOver2Seconds*/); // isShare, needSaveChanged?, 超过2秒就要保存 } } else { // abort的 } }); // Note.lastSearch.abort(); } //---------- //设为blog/unset Note.setNote2Blog = function(target) { var noteId = $(target).attr("noteId"); var note = Note.cache[noteId]; var isBlog = true; if(note.IsBlog != undefined) { isBlog = !note.IsBlog; } // 标志添加/去掉 function setBlog() { // alert(noteId + " => " + isBlog); NoteService.setNote2Blog(noteId, isBlog, function(ret) { if(ret) { // 触发同步 incrSync(); // Note.setNoteCache({NoteId: noteId, IsBlog: isBlog}, false); // 不清空NotesByNotebookId缓存 // 同步后会设置 /* if(isBlog) { $(target).find(".item-blog").removeAttr('style'); } else { $(target).find(".item-blog").hide(); } */ } }); } // 是新笔记 或 当前笔记就是它的, 则先保存之 if(note.IsNew || note.curNoteId == noteId) { Note.curChangedSaveIt(true, function(note) { setBlog(); }); } else { setBlog(); } }; // 设置notebook的blog状态 // 当修改notebook是否是blog时调用 Note.setAllNoteBlogStatus = function(notebookId, isBlog) { if(!notebookId) { return; } var notes = Note.getNotesByNotebookId(notebookId); if(!isArray(notes)) { return; } var len = notes.length; if(len == 0) { for(var i in Note.cache) { if(Note.cache[i].NotebookId == notebookId) { Note.cache[i].IsBlog = isBlog; } } } else { for(var i = 0; i < len; ++i) { notes[i].IsBlog = isBlog; } } }; // 移动 Note.moveNote = function(target, data) { var noteId = $(target).attr("noteId"); var note = Note.cache[noteId]; var notebookId = data.notebookId; if(!note.IsTrash && note.NotebookId == notebookId) { return; } // 修改数量 Notebook.incrNotebookNumberNotes(notebookId); if(!note.IsTrash) { Notebook.minusNotebookNumberNotes(note.NotebookId); } NoteService.moveNote(noteId, notebookId, function(ret) { // }); // ajaxGet("/note/moveNote", {noteId: noteId, notebookId: notebookId}, function(ret) { if(ret && ret.NoteId) { if(note.IsTrash) { Note.changeToNext(target); $(target).remove(); Note.clearCacheByNotebookId(notebookId); } else { // 不是trash, 移动, 那么判断是当前是否是all下 // 不在all下, 就删除之 // 如果当前是active, 那么clearNoteInfo之 if(!Notebook.curActiveNotebookIsAll()) { Note.changeToNext(target); if($(target).hasClass("item-active")) { Note.clearNoteInfo(); } $(target).remove(); } else { // 不移动, 那么要改变其notebook title $(target).find(".note-notebook").html(Notebook.getNotebookTitle(notebookId)); } // 重新清空cache 之前的和之后的 Note.clearCacheByNotebookId(note.NotebookId); Note.clearCacheByNotebookId(notebookId); } // 改变缓存 Note.setNoteCache(ret) } }); }; // 复制 // data是自动传来的, 是contextmenu数据 Note.copyNote = function(target, data, isShared) { var noteId = $(target).attr("noteId"); var note = Note.cache[noteId]; var notebookId = data.notebookId; // trash不能复制, 不能复制给自己 if(note.IsTrash || note.NotebookId == notebookId) { return; } /* var url = "/note/copyNote"; var data = {noteId: noteId, notebookId: notebookId}; if(isShared) { url = "/note/copySharedNote"; data.fromUserId = note.UserId; } */ NoteService.copyNote(noteId, notebookId, function(newNote) { if(newNote && newNote.NoteId) { // 重新清空cache 之后的 Note.clearCacheByNotebookId(notebookId); // 改变缓存, 添加之 Note.setNoteCache(newNote) // 增加数量 Notebook.incrNotebookNumberNotes(notebookId) } else { alert('error'); } }); }; // 删除笔记标签 // item = {noteId => usn} Note.deleteNoteTag = function(item, tag) { if(!item) { return; } for(var noteId in item) { var note = Note.getNote(noteId); if(note) { note.Tags = note.Tags || []; for(var i in note.Tags) { if(note.Tags[i] == tag) { note.Tags.splice(i, 1); continue; } } // 如果当前笔记是展示的笔记, 则重新renderTags if(noteId == Note.curNoteId) { Tag.renderTags(note.Tags); } } } }; Note.readOnly = false; // 默认为false要好? LEA.readOnly = false; // 切换只读模式 Note.toggleReadOnly = function() { var me = this; var note = me.getCurNote(); // tinymce var $editor = $('#editor'); $editor.addClass('read-only').removeClass('all-tool'); // 不要全部的 // 不可写 $('#editorContent').attr('contenteditable', false); // markdown $('#mdEditor').addClass('read-only'); if(!note) { return; } if(note.IsMarkdown) { $('#mdInfoToolbar .created-time').html(goNowToDatetime(note.CreatedTime)); $('#mdInfoToolbar .updated-time').html(goNowToDatetime(note.UpdatedTime)); } else { $('#infoToolbar .created-time').html(goNowToDatetime(note.CreatedTime)); $('#infoToolbar .updated-time').html(goNowToDatetime(note.UpdatedTime)); } Note.readOnly = true; LEA.readOnly = true; if(note.readOnly) { return; } if(!note.IsMarkdown) { // 里面的pre也设为不可写 $('#editorContent pre').each(function() { LeaAce.setAceReadOnly($(this), true); }); } note.readOnly = true; }; // 切换到编辑模式 LEA.toggleWriteable = Note.toggleWriteable = function() { var me = Note; // $('#infoToolbar').hide(); $('#editor').removeClass('read-only'); $('#editorContent').attr('contenteditable', true); // markdown $('#mdEditor').removeClass('read-only'); var note = me.getCurNote(); if(!note) { return; } Note.readOnly = false; LEA.readOnly = false; if(!note.readOnly) { return; } if(!note.IsMarkdown) { // 里面的pre也设为不可写 $('#editorContent pre').each(function() { LeaAce.setAceReadOnly($(this), false); }); } else { if(MD) { MD.onResize(); } } note.readOnly = false; }; // 渲染列表 Note.starNotes = []; Note.starItemT = '
' + getMsg('No Starred Note') + '
'); } }; // 点击笔记, 判断是否在star中, 如果在, 则也选中 Note.selectStar = function(noteId) { var me = this; var target = me.starNotesO.find('li[data-id="' + noteId + '"]'); me.starNotesO.find('li').removeClass('selected'); target.addClass('selected'); }; // 点击, note Note.renderStarNote = function(target) { var me = this; var noteId = target.data('id'); // 如果没有target, 则选第一个 if(!noteId) { target = me.starNotesO.find('li').eq(0); } var noteId = target.data('id'); if(!noteId) { return; } me.starNotesO.find('li').removeClass('selected'); target.addClass('selected'); // 大BUG start // 先保存现有的啊 me.curChangedSaveIt(); console.log('ok...'); // 把当前笔记放在第一位 me.clearAll(); // 如果数据改了, me.starNotes 的content不是最新的 me.starNotes || (me.starNotes = []); for(var i = 0; i < me.starNotes.length; ++i) { me.starNotes[i] = me.getNote(me.starNotes[i].NoteId); } // 大BUG end me.renderNotes(me.starNotes); me.changeNoteForPjax(noteId, true, false); me.directToNote(noteId); // $('#curNotebookForLisNote').text("Starred"); Notebook.changeCurNotebookTitle(getMsg('Starred'), true); }; // 笔记标题改了后, 如果在star中, 则也要改标题 Note.changeStarNoteTitle = function(noteId, title) { var me = this; var cacheNote = me.getNote(noteId); /* if(!cacheNote.Star) { return; } */ var target = me.starNotesO.find('li[data-id="' + noteId + '"]'); if(target.length == 1) { target.find('a').html((title || 'Untitled') + 'X'); } }; // 取消star, note delete/trash时取消star Note.unStar = function(noteId) { var me = this; // 删除该stars var has = false; for(var i = 0; i < me.starNotes.length; ++i) { var tNote = me.starNotes[i]; if(tNote.NoteId == noteId) { var has = true; me.starNotes.splice(i, 1); break; } } if(has) { // 重新渲染之 me.renderStars(me.starNotes); } }; // 收藏或取消收藏 Note.star = function(noteId) { var me = this; var note = me.getNote(noteId); if(!note || note.IsTrash) { return; } var $target = $('[noteId="' + noteId + '"]'); NoteService.star(noteId, function(ok, isStarred) { if(ok) { note.Star = isStarred; if(isStarred) { me.starNotes.unshift(note); $target.addClass('item-is-star'); } else { $target.removeClass('item-is-star'); // 删除该stars for(var i = 0; i < me.starNotes.length; ++i) { var tNote = me.starNotes[i]; if(tNote.NoteId == noteId) { me.starNotes.splice(i, 1); break; } } } // 重新渲染之 me.renderStars(me.starNotes); } }); }; // 显示 Note._curFixNoteId = ''; // 当前要处理的 Note._curFixNoteTarget = ''; Note._conflictTipsElem = $('#conflictTips'); Note._showConflictInfoInited = false; // 初始化事件 Note._initshowConflictInfo = function() { var me = this; // 点击与之冲突的笔记, 则将该笔记显示到它前面, 并选中 Note._conflictTipsElem.find('.conflict-title').click(function() { var conflictNoteId = $(this).data('id'); var conflictNote = me.getNote(conflictNoteId); if(!conflictNote) { alert('The note is not exists'); return; } // 是否在该列表中? var target = $(tt('[noteId="?"]', conflictNoteId)); // // 如果当前笔记在笔记列表中, 那么生成一个新笔记放在这个笔记上面 if(target.length > 0) { } else { target = me._getNoteHtmlObjct(conflictNote); } // console.log(">....>"); // console.log(me._curFixNoteTarget); // console.log(target); target.insertAfter(me._curFixNoteTarget); // alert(3); // me._curFixNoteTarget.insertBefore(target); // 选中与之冲突的笔记 me.changeNote(conflictNoteId); }); }; Note.showConflictInfo = function(target, e) { var me = this; var $li = $(target).closest('li'); var noteId = $li.attr('noteId'); var note = me.getNote(noteId); if(!note) { return; } var conflictNoteId = note.ConflictNoteId; function conflictIsFixed() { // 去掉item-confict class // 并且改变 $li.removeClass('item-conflict'); note.ConflictNoteId = ""; NoteService.conflictIsFixed(noteId); } if(!conflictNoteId) { return conflictIsFixed(); } var conflictNote = me.getNote(conflictNoteId); if(!conflictNote) { return conflictIsFixed(); } me._curFixNoteId = noteId; me._curFixNoteTarget = $li; if(!me._showConflictInfoInited) { me._showConflictInfoInited = true; me._initshowConflictInfo(); } // 初始化数据 var titleElem = Note._conflictTipsElem.find('.conflict-title'); titleElem.text(conflictNote.Title); titleElem.data('id', conflictNoteId); Note._conflictTipsElem.find('.conflict-resolved').prop('checked', false); ContextTips.show('#conflictTips', e, function() { if(Note._conflictTipsElem.find('.conflict-resolved').prop('checked')) { conflictIsFixed(); } }); }; // 内容已同步成功 Note.contentSynced = function(noteId, content) { var me = this; var note = me.getNote(noteId); if(!note) { // 可能之前还没有 // me.setNoteCache(noteId, {Content: content}); return; } if(note.InitSync) { // 重新render内容 note.InitSync = false; note.Content = content; if(me.curNoteId == noteId || me.inChangeNoteId == noteId) { // alert(note.Title); // 重新渲染 // alert(me.curNoteId == noteId); false // alert(me.inChangeNoteId == noteId); true Note.reRenderNote(noteId); } else { // 生成desc me.renderNoteDesc(note); } } }; // 延迟加载内容 Note.getNoteContentLazy = function(noteId) { setTimeout(function() { NoteService.getNoteContent(noteId, function(contentO) { if(typeof contentO == 'object') { Note.contentSynced(noteId, contentO.Content); } }); }, 10); }; // 这里速度不慢, 很快 Note.getContextNotebooks = function(notebooks) { var moves = []; var copys = []; var copys2 = []; for(var i in notebooks) { var notebook = notebooks[i]; var move = {text: notebook.Title, notebookId: notebook.NotebookId, action: Note.moveNote} var copy = {text: notebook.Title, notebookId: notebook.NotebookId, action: Note.copyNote} var copy2 = {text: notebook.Title, notebookId: notebook.NotebookId, action: Share.copySharedNote} if(!isEmpty(notebook.Subs)) { var mc = Note.getContextNotebooks(notebook.Subs); move.items = mc[0]; copy.items = mc[1]; copy2.items = mc[2]; move.type = "group"; move.width = 150; copy.type = "group"; copy.width = 150; copy2.type = "group"; copy2.width = 150; } moves.push(move); copys.push(copy); copys2.push(copy2); } return [moves, copys, copys2]; }; Note.target = null; // 当前处理的note Note.menuItemsForMove = {}; // notebookId => menu Note.menuItemsForCopy = {}; // notebookId => menu Note.getContextNotebooksSys = function(notebooks) { var submenuMoves = new gui.Menu(); var submenuCopys = new gui.Menu(); for(var i in notebooks) { (function(j) { var notebook = notebooks[j]; var moveMenu = { label: notebook.Title, click: function() { Note.moveNote(Note.target, {notebookId: notebook.NotebookId}); } }; var copyMenu = { label: notebook.Title, click: function() { Note.copyNote(Note.target, {notebookId: notebook.NotebookId}); } }; if(!isEmpty(notebook.Subs)) { var mc = Note.getContextNotebooksSys(notebook.Subs); moveMenu.submenu = mc[0]; moveMenu.type = 'submenu'; copyMenu.submenu = mc[1]; copyMenu.type = 'submenu'; } var move = new gui.MenuItem(moveMenu); var copy = new gui.MenuItem(copyMenu); Note.menuItemsForMove[notebook.NotebookId] = move; Note.menuItemsForCopy[notebook.NotebookId] = copy; submenuMoves.append(move); submenuCopys.append(copy); })(i); } return [submenuMoves, submenuCopys]; }; // Notebook调用 Note.contextmenu = null; Note.notebooksCopy = []; // share会用到 Note.initContextmenu = function() { var self = Note; var notebooks = Notebook.everNotebooks; //------------------- // 右键菜单 function noteMenu() { var me = this; // this.target = ''; this.menu = new gui.Menu(); this.del = new gui.MenuItem({ label: getMsg("Delete"), click: function(e) { Note.deleteNote(self.target); } }); this.publicBlog = new gui.MenuItem({ label: getMsg("Public as blog"), click: function(e) { Note.setNote2Blog(self.target); } }); this.unPublicBlog = new gui.MenuItem({ label: getMsg("Cancel public"), click: function(e) { Note.setNote2Blog(self.target); } }); var ms = Note.getContextNotebooksSys(notebooks); // this.move.submenu = ms[0]; // this.copy.submenu = ms[1]; this.move = new gui.MenuItem({ label: getMsg("Move"), submenu: ms[0], // 必须要放这里, 之后不能赋值 click: function(e) { } }); this.copy = new gui.MenuItem({ label: getMsg("Copy"), submenu: ms[1], click: function(e) { } }); if (!UserInfo.IsLocal) { this.menu.append(this.publicBlog); this.menu.append(this.unPublicBlog); this.menu.append(gui.getSeparatorMenu()); } this.menu.append(this.del); this.menu.append(gui.getSeparatorMenu()); this.menu.append(this.move); this.menu.append(this.copy); // 导出 var exportsSubMenus = new gui.Menu(); var exportMenus = Api.getExportMenus() || []; for(var i = 0; i < exportMenus.length; ++i) { (function(j) { var menu = exportMenus[j]; var clickBac = menu.click; var menuItem = new gui.MenuItem({ label: menu.label, click: function(e) { var note = Note.getNote($(self.target).attr('noteId')); clickBac && clickBac(note); } }); exportsSubMenus.append(menuItem); })(i); } if(exportMenus.length > 0) { this.exports = new gui.MenuItem({ label: getMsg('Export'), submenu: exportsSubMenus, click: function(e) { } }); this.menu.append(gui.getSeparatorMenu()); this.menu.append(this.exports); } this.enable = function(name, ok) { this[name].enabled = ok; } this.popup = function(e, target) { self.target = target; var noteId = $(target).attr('noteId'); var note = Note.getNote(noteId); if(!note) { return; } var notebookId = note.NotebookId; if(note.IsTrash) { this.copy.enabled = false; } else { this.copy.enabled = true; } if(note.IsBlog) { this.publicBlog.enabled = false; this.unPublicBlog.enabled = true; } else { this.publicBlog.enabled = true; this.unPublicBlog.enabled = false; } this.menu.popup(gui.getCurrentWindow(), e.originalEvent.x, e.originalEvent.y); } } var noteMenuSys = new noteMenu(); Note.noteMenuSys = noteMenuSys; }; // 附件 // 笔记的附件需要ajax获取 // 建一张附件表? attachId, noteId, 其它信息 // note里有attach_nums字段记录个数 // [ok] var Attach = { loadedNoteAttachs: {}, // noteId => [attch1Info, attach2Info...] // 按笔记 attachsMap: {}, // attachId => attachInfo getAttach: function(attachId) { return this.attachsMap[attachId]; }, init: function() { var self = this; var me = this; // 显示attachs $("#showAttach").click(function(){ self.renderAttachs(Note.curNoteId); }); // 防止点击隐藏 self.attachListO.click(function(e) { e.stopPropagation(); }); // 删除 self.attachListO.on("click", ".delete-attach", function(e) { e.stopPropagation(); var attachId = $(this).closest('li').data("id"); var t = this; if(confirm(getMsg("Are you sure to delete it ?"))) { // $(t).button("loading"); self.deleteAttach(attachId); // $(t).button("reset"); } }); // 下载 var curAttachId = ''; self.attachListO.on("click", ".download-attach", function(e) { e.stopPropagation(); var $li = $(this).closest('li'); var attachId = $li.data("id"); var title = $li.find('.attach-title').text(); gui.dialog.showSaveDialog(gui.getCurrentWindow(), {title: title, defaultPath: gui.app.getPath('userDesktop') + '/' + title}, function(targetPath) { if(targetPath) { var curAttach = me.getAttach(attachId); if(curAttach) { FileService.download(curAttach.Path, targetPath, function(ok, msg) { if(!ok) { Notify.show({type: 'warning', title: 'Warning', body: 'File saved failed!'}); } else { Notify.show({title: 'Info', body: 'File saved successful!'}); } }); } else { alert('error'); } } else { } }); }); // make link self.attachListO.on("click", ".link-attach", function(e) { e.stopPropagation(); 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 { tinymce.activeEditor.insertContent('' + attach.Title + ''); } }); // make all link self.linkAllBtnO.on("click",function(e) { // 暂不支持 return; e.stopPropagation(); var note = Note.getCurNote(); if(!note) { return; } 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) { MD.insertLink(src, title); } else { tinymce.activeEditor.insertContent('' + title + ''); } }); // 添加Attach $('#chooseFile').click(function() { gui.dialog.showOpenDialog(gui.getCurrentWindow(), { defaultPath: gui.app.getPath('userDesktop'), properties: ['openFile', 'multiSelections'] }, function(paths) { if(!paths) { return; } // 如果是新建的笔记, 必须先保存note var note = Note.getCurNote(); if(note && note.IsNew) { Note.curChangedSaveIt(true); } FileService.addAttach(paths, Note.curNoteId, function(files) { if(files) { me.addAttachs(files); } }); } ); }); }, attachListO: $("#attachList"), attachNumO: $("#attachNum"), attachDropdownO: $("#attachDropdown"), downloadAllBtnO: $("#downloadAllBtn"), linkAllBtnO: $("#linkAllBtn"), // 添加笔记时 clearNoteAttachNum: function() { var self = this; self.attachNumO.html("").hide(); }, renderNoteAttachNum: function(noteId, needHide) { var self = this; var note = Note.getNote(noteId); var attachs = note.Attachs; var attachNum = attachs ? attachs.length : 0; if(attachNum) { self.attachNumO.html("(" + attachNum + ")").show(); self.downloadAllBtnO.show(); self.linkAllBtnO.show(); } else { self.attachNumO.hide(); self.downloadAllBtnO.hide(); self.linkAllBtnO.hide(); } // 隐藏掉 if(needHide) { self.attachDropdownO.removeClass("open"); } }, _renderAttachs: function(attachs) { var self = this; // foreach 循环之 /*