';
Note.starNotesO = $('#starNotes');
Note.renderStars = function(notes) {
var me = this;
var notes = notes || me.starNotes;
me.starNotes = notes;
me.starNotesO.html('');
for(var i = 0; i < notes.length; ++i) {
var note = notes[i];
var t = tt(me.starItemT, note.NoteId, note.Title || getMsg('Untitled'));
me.starNotesO.append(t);
}
if(notes.length == 0) {
me.starNotesO.html('
' + 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();
// 把当前笔记放在第一位
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 content
// 池, 最大10个
Note._loadContentPool = [];
Note._loadContentPoolSeq = 0;
Note._startedGetNoteContentLazy = false;
Note._stopGetNoteContentLazy = false;
Note._loadContentRunSeq = 0;
Note._loadContentStarted = {};
// 在render notes时
// 延迟加载内容
// 重新render notes时, 重置pool
Note.resetGetNoteContentLazy = function() {
var me = this;
me._loadContentPool = [];
me._loadContentPoolSeq = 0;
me._stopGetNoteContentLazy = false;
me._startedGetNoteContentLazy = false;
me._loadContentRunSeq++;
};
// 添加到池子中
Note.addGetNoteContentLazy = function(noteId) {
var me = this;
Note._loadContentPool.push(noteId);
me.startGetNoteContentLazy();
};
// render notes后,
// 开始加载
Note.startGetNoteContentLazy = function() {
var me = this;
if (me._loadContentStarted[me._loadContentRunSeq]) {
return;
}
me._loadContentStarted[me._loadContentRunSeq] = true;
me.getNoteContentLazy(me._loadContentRunSeq);
};
// 得到下一个要处理的noteId
Note._getNextNoteId = function () {
var me = this;
var noteId = me._loadContentPool[me._loadContentPoolSeq];
me._loadContentPoolSeq++;
return noteId;
};
Note.getNoteContentLazy = function(runSeq) {
var me = this;
// // 暂停了
// if (me._stopGetNoteContentLazy) {
// return;
// }
// 不是一个时候了
if (runSeq != me._loadContentRunSeq) {
console.log('不是一个时候了 ' + runSeq + '_' + me._loadContentRunSeq);
return;
}
var noteId = me._getNextNoteId();
if (!noteId) {
return;
}
var note = me.getNote(noteId);
if (note && !note.InitSync) {
console.log('不用加载');
me.getNoteContentLazy(runSeq);
return;
}
console.log('正在加载....' + noteId);
setTimeout(function () {
NoteService.getNoteContent(noteId, function(contentO) {
if(typeof contentO == 'object') {
Note.contentSynced(noteId, contentO.Content);
me.getNoteContentLazy(runSeq);
}
});
}, 800);
};
Note.stopGetNoteContentLazy = function() {
var me = this;
me._stopGetNoteContentLazy = true;
};
//
//--------------
// 这里速度不慢, 很快
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, true);
}
});
this.unPublicBlog = new gui.MenuItem({
label: getMsg("Cancel public"),
click: function(e) {
Note.setNote2Blog(self.target, false);
}
});
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) {
if (Note.inBatch) {
var noteIds = Note.getBatchNoteIds();
}
else {
var noteIds = [$(self.target).attr('noteId')];
}
// var note = Note.getNote();
clickBac && clickBac(noteIds);
}
});
exportMenus[i].menu = menuItem;
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);
}
T = this;
this.enable = function(name, ok) {
this[name].enabled = ok;
}
// 控制disable
this.popup = function(e, target) {
self.target = target;
var noteIds;
if (Note.inBatch) {
noteIds = Note.getBatchNoteIds();
}
else {
noteIds = [$(target).attr("noteId")];
}
// 导出的enabled
for(var i = 0; i < exportMenus.length; ++i) {
exportMenus[i].menu.enabled = exportMenus[i].enabled(noteIds);
}
// 批量, 除了导出pdf都可以操作
if (Note.inBatch) {
this.copy.enabled = true;
this.move.enabled = true;
this.publicBlog.enabled = true;
this.unPublicBlog.enabled = true;
} else {
var note = Note.getNote(noteIds[0]);
if (!note) {
return;
}
if(note.IsTrash || Notebook.curNotebookIsTrash()) {
this.copy.enabled = false; // 没用
this.publicBlog.enabled = false;
this.unPublicBlog.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);
this.menu.popup(gui.getCurrentWindow(), e.pageX, e.pageY);
}
}
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 循环之
/*
leanote官abcefedafadfadfadfadfad方文档.doc
*/
var html = "";
var attachNum = attachs.length;
// console.log(attachs);
for(var i = 0; i < attachNum; ++i) {
var each = attachs[i];
var path = each.Path;
// 本地是否有, 没有, 是否是在显示的时候才去从服务器上抓? 不
var disabled = '';
if(path) {
var d = '';
} else {
d = '...'
disabled = 'disabled';
// 通过后端去下载
NoteService.downloadAttachFromServer(Note.curNoteId, each.ServerFileId, each.FileId);
}
html += '