// 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 = ""; // 定时器 Note.itemIsBlog = '
?
? ?
?
?
? ?
?
?
? ?
?
class="each-content">??> 时间: ? |
' + 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.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.lastSearch = $.post("/note/searchNote", {key: val}, function(notes) { hideLoading(); if(notes) { // 成功后设为空 Note.lastSearch = null; // renderNotes只是note列表加载, 右侧笔记详情还没加载 // 这个时候, 定位第一个, 保存之前的, // 如果: 第一次搜索, renderNotes OK, 还没等到changeNote时 // 第二次搜索来到, Note.curChangedSaveIt(); // 导致没有标题了 // 不是这个原因, 下面的Note.changeNote会导致保存 // 设空, 防止发生上述情况 // Note.curNoteId = ""; Note.renderNotes(notes); 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; } // 标志添加/去掉 if(isBlog) { $(target).find(".item-blog").show(); } else { $(target).find(".item-blog").hide(); } ajaxPost("/blog/setNote2Blog", {noteId: noteId, isBlog: isBlog}, function(ret) { if(ret) { Note.setNoteCache({NoteId: noteId, IsBlog: isBlog}, false); // 不清空NotesByNotebookId缓存 } }); } // 设置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; } 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; } ajaxGet(url, data, function(ret) { if(ret && ret.NoteId) { // 重新清空cache 之后的 Note.clearCacheByNotebookId(notebookId); // 改变缓存, 添加之 Note.setNoteCache(ret) } }); } // 这里速度不慢, 很快 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]; } // Notebook调用 Note.contextmenu = null; Note.notebooksCopy = []; // share会用到 Note.initContextmenu = function() { var self = Note; if(Note.contextmenu) { Note.contextmenu.destroy(); } // 得到可移动的notebook var notebooks = Notebook.everNotebooks; var mc = self.getContextNotebooks(notebooks); var notebooksMove = mc[0]; var notebooksCopy = mc[1]; self.notebooksCopy = mc[2]; //--------------------- // context menu //--------------------- var noteListMenu = { width: 150, items: [ { text: "分享给好友", alias: 'shareToFriends', icon: "", faIcon: "fa-share-square-o", action: Note.listNoteShareUserInfo}, { type: "splitLine" }, { text: "公开为博客", alias: 'set2Blog', icon: "", action: Note.setNote2Blog }, { text: "取消公开为博客", alias: 'unset2Blog', icon: "", action: Note.setNote2Blog }, { type: "splitLine" }, // { text: "发送长微博", alias: 'html2Image', icon: "", action: Note.html2Image , width: 150, type: "group", items:[{text: "a"}]}, // { type: "splitLine" }, { text: "删除", icon: "", faIcon: "fa-trash-o", action: Note.deleteNote }, { text: "移动", alias: "move", icon: "", type: "group", width: 150, items: notebooksMove }, { text: "复制", alias: "copy", icon: "", type: "group", width: 150, items: notebooksCopy } ], onShow: applyrule, onContextMenu: beforeContextMenu, parent: "#noteItemList", children: ".item-my", } function menuAction(target) { // $('#myModal').modal('show') showDialog("dialogUpdateNotebook", {title: "修改笔记本", postShow: function() { }}); } function applyrule(menu) { var noteId = $(this).attr("noteId"); var note = Note.cache[noteId]; if(!note) { return; } // 要disable的items var items = []; // 如果是trash, 什么都不能做 if(note.IsTrash) { items.push("shareToFriends"); items.push("shareStatus"); items.push("unset2Blog"); items.push("set2Blog"); items.push("copy"); } else { // 是否已公开为blog if(!note.IsBlog) { items.push("unset2Blog"); } else { items.push("set2Blog"); } // 移动与复制不能是本notebook下 var notebookTitle = Notebook.getNotebookTitle(note.NotebookId); items.push("move." + notebookTitle); items.push("copy." + notebookTitle); } menu.applyrule({ name: "target..", disable: true, items: items }); } function beforeContextMenu() { return this.id != "target3"; } // 这里很慢!! Note.contextmenu = $("#noteItemList .item-my").contextmenu(noteListMenu); } //------------------- 事件 $(function() { //----------------- // for list nav $("#noteItemList").on("click", ".item", function(event) { event.stopPropagation(); // 找到上级.item var parent = findParents(this, ".item"); if(!parent) { return; } var noteId = parent.attr("noteId"); if(!noteId) { return; } // 当前的和所选的是一个, 不改变 if(Note.curNoteId == noteId) { return; } Note.changeNote(noteId); }); //------------------ // 新建笔记 // 1. 直接点击新建 OR // 2. 点击nav for new note $("#newNoteBtn, #editorMask .note").click(function() { var notebookId = $("#curNotebookForNewNote").attr('notebookId'); Note.newNote(notebookId); }); $("#newNoteMarkdownBtn, #editorMask .markdown").click(function() { var notebookId = $("#curNotebookForNewNote").attr('notebookId'); Note.newNote(notebookId, false, "", true); }); $("#notebookNavForNewNote").on("click", "li div", function() { var notebookId = $(this).attr("notebookId"); if($(this).hasClass("new-note-right")) { Note.newNote(notebookId, false, "", true); } else { Note.newNote(notebookId); } }); $("#searchNotebookForAdd").click(function(e) { e.stopPropagation(); }); $("#searchNotebookForAdd").keyup(function() { var key = $(this).val(); Notebook.searchNotebookForAddNote(key); }); $("#searchNotebookForList").keyup(function() { var key = $(this).val(); Notebook.searchNotebookForList(key); }); //--------------------------- // 搜索, 按enter才搜索 /* $("#searchNoteInput").on("keyup", function(e) { Note.searchNote(); }); */ $("#searchNoteInput").on("keydown", function(e) { var theEvent = e; // window.event || arguments.callee.caller.arguments[0]; if(theEvent.keyCode == 13 || theEvent.keyCode == 108) { theEvent.preventDefault(); Note.searchNote(); return false; } }); //-------------------- // Note.initContextmenu(); //------------ // 文档历史 $("#contentHistory").click(function() { Note.listNoteContentHistories() }); $("#saveBtn").click(function() { Note.curChangedSaveIt(true); }); // blog $("#noteItemList").on("click", ".item-blog", function(e) { e.preventDefault(); e.stopPropagation(); // 得到ID var noteId = $(this).parent().attr('noteId'); window.open("/blog/view/" + noteId); }); // note setting $("#noteItemList").on("click", ".item-my .item-setting", function(e) { e.preventDefault(); e.stopPropagation(); var $p = $(this).parent(); Note.contextmenu.showMenu(e, $p); }); }); // 定时器启动 Note.startInterval();