This commit is contained in:
life
2015-03-31 14:27:26 +08:00
parent 9515f1e58f
commit decf580ed3
585 changed files with 8128 additions and 9023 deletions

File diff suppressed because one or more lines are too long

View File

@ -164,7 +164,16 @@ Note.getNotesByNotebookId = function(notebookId, sortBy, isAsc) {
// 缓存之
Note.cacheByNotebookId[notebookId][sortBy] = notes;
return notes;
}
};
// 该笔记点击后已污染
Note.curNoteIsDirtied = function() {
var me = this;
var note = me.getCurNote();
if(note) {
note.isDirty = true;
}
};
// called by Notebook
// render 所有notes, 和第一个note的content
@ -226,11 +235,18 @@ Note.curHasChanged = function(force) {
IsMarkdown: cacheNote.IsMarkdown, // 是否是markdown笔记
FromUserId: cacheNote.FromUserId, // 是否是共享新建的
NoteId: cacheNote.NoteId,
NotebookId: cacheNote.NotebookId
NotebookId: cacheNote.NotebookId,
Version: cacheNote.Version || 0, // 版本控制
};
if(hasChanged.IsNew) {
$.extend(hasChanged, cacheNote);
} else {
if(!cacheNote.isDirty) {
log("no dirty");
hasChanged.hasChanged = false;
return hasChanged;
}
}
if(cacheNote.Title != title) {
@ -243,7 +259,7 @@ Note.curHasChanged = function(force) {
if(!arrayEqual(cacheNote.Tags, tags)) {
hasChanged.hasChanged = true;
hasChanged.Tags = tags;
hasChanged.Tags = tags.join(","); // 为什么? 因为空数组不会传到后台
}
// 比较text, 因为note Nav会添加dom会导致content改变
@ -375,44 +391,94 @@ Note.getImgSrc = function(content) {
// 以后要定时调用
// force , 默认是true, 表强校验内容
// 定时保存传false
Note.curChangedSaveIt = function(force) {
Note.saveInProcess = {}; // noteId => bool, true表示该note正在保存到服务器, 服务器未响应
Note.savePool = {}; // 保存池, 以后的保存先放在pool中, id => note
Note.curChangedSaveIt = function(force, callback) {
var me = this;
// 如果当前没有笔记, 不保存
if(!Note.curNoteId || Note.isReadOnly) {
return;
}
var hasChanged = Note.curHasChanged(force);
// 把已改变的渲染到左边 item-list
Note.renderChangedNote(hasChanged);
if(hasChanged.hasChanged || hasChanged.IsNew) {
// 把已改变的渲染到左边 item-list
Note.renderChangedNote(hasChanged);
delete hasChanged.hasChanged;
// 先缓存, 把markdown的preview也缓存起来
Note.setNoteCache(hasChanged, false);
// 设置更新时间
Note.setNoteCache({"NoteId": hasChanged.NoteId, "UpdatedTime": (new Date()).format("yyyy-MM-ddThh:mm:ss.S")}, false);
// 表示有未完成的保存
/*
if(me.saveInProcess[hasChanged.NoteId]) {
log("in process");
me.savePool[hasChanged.NoteId] = hasChanged;
me.startUpdatePoolNoteInterval();
return;
}
*/
// 保存之
showMsg(getMsg("saving"));
me.saveInProcess[hasChanged.NoteId] = true;
ajaxPost("/note/updateNoteOrContent", hasChanged, function(ret) {
me.saveInProcess[hasChanged.NoteId] = false;
if(hasChanged.IsNew) {
// 缓存之, 后台得到其它信息
ret.IsNew = false;
Note.setNoteCache(ret, false);
// 新建笔记也要change history
Pjax.changeNote(ret);
}
callback && callback();
showMsg(getMsg("saveSuccess"), 1000);
});
if(hasChanged['Tags'] != undefined && typeof hasChanged['Tags'] == 'string') {
hasChanged['Tags'] = hasChanged['Tags'].split(',');
}
// 先缓存, 把markdown的preview也缓存起来
Note.setNoteCache(hasChanged, false);
// 设置更新时间
Note.setNoteCache({"NoteId": hasChanged.NoteId, "UpdatedTime": (new Date()).format("yyyy-MM-ddThh:mm:ss.S")}, false);
return hasChanged;
}
return false;
}
};
// 更新池里的笔记
Note.updatePoolNote = function() {
var me = this;
for(var noteId in me.savePool) {
if(!noteId) {
continue;
}
// 删除之
delete me.savePool[noteId];
var hasChanged = me.savePool[noteId];
me.saveInProcess[noteId] = true;
ajaxPost("/note/updateNoteOrContent", hasChanged, function(ret) {
me.saveInProcess[noteId] = false;
});
}
};
// 启动保存, 暂不处理
Note.updatePoolNoteInterval = null;
Note.startUpdatePoolNoteInterval = function() {
return;
var me = this;
if(me.updatePoolNoteInterval) {
return;
}
me.updatePoolNoteInterval = setTimeout(function() {
log('update pool');
me.updatePoolNote();
}, 1000);
};
// 样式
Note.selectTarget = function(target) {
@ -661,18 +727,19 @@ Note.clearAll = function() {
Note.clearNoteList();
}
// render到编辑器
// render note
Note.renderNote = function(note) {
if(!note) {
return;
}
// title
$("#noteTitle").val(note.Title);
$("#noteTitle").val(trimTitle(note.Title));
// 当前正在编辑的
// tags
Tag.renderTags(note.Tags);
// 笔记是新render的, 没有污染过
note.isDirty = false;
}
// render content
@ -788,6 +855,7 @@ Note._renderNotes = function(notes, forNewNote, isShared, tang) { // 第几趟
}
var note = notes[i];
var tmp;
note.Title = trimTitle(note.Title);
if(note.ImgSrc) {
tmp = tt(Note.itemTpl, classes, note.NoteId, note.ImgSrc, note.Title, Notebook.getNotebookTitle(note.NotebookId), goNowToDatetime(note.UpdatedTime), note.Desc);
} else {
@ -965,8 +1033,8 @@ Note.deleteNote = function(target, contextmenuItem, isShared) {
// 删除缓存
if(note) {
Note.clearCacheByNotebookId(note.NotebookId)
delete Note.cache[noteId]
Note.clearCacheByNotebookId(note.NotebookId);
delete Note.cache[noteId];
}
showMsg("删除成功!", 500);
@ -1338,7 +1406,31 @@ Note.copyNote = function(target, data, isShared) {
// 增加数量
Notebook.incrNotebookNumberNotes(notebookId)
}
};
// 删除笔记标签
// 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.getContextNotebooks = function(notebooks) {
@ -1697,6 +1789,16 @@ $(function() {
}
});
// 当前笔记可以已修改
$('#editorContent, #wmd-input, #noteTitle').keyup(function() {
Note.curNoteIsDirtied();
});
/*
$('#addTagInput').click(function() {
Note.curNoteIsDirtied();
});
*/
//------------------
// 新建笔记
// 1. 直接点击新建 OR

File diff suppressed because one or more lines are too long

View File

@ -21,6 +21,10 @@ Notebook.getCurNotebookId = function() {
return Notebook.curNotebookId;
};
Notebook.getCurNotebook = function() {
return Notebook.cache[Notebook.curNotebookId];
};
// 笔记本的笔记数量更新
Notebook._updateNotebookNumberNotes = function(notebookId, n) {
var self = this;
@ -249,6 +253,12 @@ Notebook.renderNotebooks = function(notebooks) {
notebooks = [];
}
// title可能有<script>
for(var i = 0, len = notebooks.length; i < len; ++i) {
var notebook = notebooks[i];
notebook.Title = trimTitle(notebook.Title);
}
notebooks = [{NotebookId: Notebook.allNotebookId, Title: getMsg("all"), drop:false, drag: false}].concat(notebooks);
notebooks.push({NotebookId: Notebook.trashNotebookId, Title: getMsg("trash"), drop:false, drag: false});
Notebook.notebooks = notebooks; // 缓存之
@ -579,6 +589,7 @@ Notebook.curActiveNotebookIsAll = function() {
// 2. ajax得到该notebook下的所有note
// 3. 使用Note.RederNotes()
// callback Pjax, 当popstate时调用
Notebook.changeNotebookSeq = 1;
Notebook.changeNotebook = function(notebookId, callback) {
var me = this;
Notebook.changeNotebookNav(notebookId);
@ -602,37 +613,54 @@ Notebook.changeNotebook = function(notebookId, callback) {
param = {};
// 得到全部的...
cacheNotes = Note.getNotesByNotebookId();
if(!isEmpty(cacheNotes)) { // 万一真的是没有呢?
// 数量一致
if(!isEmpty(cacheNotes)) {
if(callback) {
callback(cacheNotes);
} else {
Note.renderNotesAndFirstOneContent(cacheNotes);
}
return;
}
}
} else {
cacheNotes = Note.getNotesByNotebookId(notebookId);
if(!isEmpty(cacheNotes)) { // 万一真的是没有呢? 执行后面的ajax
var notebook = Notebook.cache[notebookId];
var len = cacheNotes ? cacheNotes.length : 0;
// alert( notebook.NumberNotes + " " + len);
if(len == notebook.NumberNotes) {
if(callback) {
callback(cacheNotes);
} else {
Note.renderNotesAndFirstOneContent(cacheNotes);
}
return;
} else {
Note.clearCacheByNotebookId(notebookId);
log('数量不一致');
}
}
// 2 得到笔记本
// 这里可以缓存起来, note按notebookId缓存
// 这里可能点击过快导致前面点击的后来才返回
me.showNoteAndEditorLoading();
ajaxGet(url, param, function(cacheNotes) {
if(callback) {
callback(cacheNotes);
} else {
Note.renderNotesAndFirstOneContent(cacheNotes);
}
me.hideNoteAndEditorLoading();
});
me.changeNotebookSeq++;
(function(seq) {
ajaxGet(url, param, function(cacheNotes) {
// 后面点击过快, 之前的结果不要了
if(seq != me.changeNotebookSeq) {
log("notebook changed too fast!");
log(cacheNotes);
return;
}
if(callback) {
callback(cacheNotes);
} else {
Note.renderNotesAndFirstOneContent(cacheNotes);
}
me.hideNoteAndEditorLoading();
});
})(me.changeNotebookSeq);
}
// 笔记列表与编辑器的mask loading

File diff suppressed because one or more lines are too long

View File

@ -996,7 +996,7 @@ LeaAce = {
aceEditor.setOption("wrap", "free");
aceEditor.setShowInvisibles(false);
aceEditor.setAutoScrollEditorIntoView(true);
aceEditor.setOption("maxLines", 100);
aceEditor.setOption("maxLines", 10000);
aceEditor.commands.addCommand({
name: "undo",
bindKey: {win: "Ctrl-z", mac: "Command-z"},

File diff suppressed because one or more lines are too long

View File

@ -33,8 +33,8 @@ Tag.t = $("#tags");
Tag.getTags = function() {
var tags = [];
Tag.t.children().each(function(){
var text = $(this).text();
text = text.substring(0, text.length - 1); // 把X去掉
var text = $(this).data('tag');
// text = text.substring(0, text.length - 1); // 把X去掉
text = Tag.mapCn2En[text] || text;
tags.push(text);
});
@ -117,7 +117,7 @@ Tag.renderReadOnlyTags = function(tags) {
// 添加tag
// tag = {classes:"label label-red", text:"红色"}
// tag = life
Tag.appendTag = function(tag) {
Tag.appendTag = function(tag, save) {
var isColor = false;
var classes, text;
@ -140,21 +140,25 @@ Tag.appendTag = function(tag) {
classes = "label label-default";
}
}
var rawText = text;
if(LEA.locale == "zh") {
text = Tag.mapEn2Cn[text] || text;
rawText = Tag.mapCn2En[rawText] || rawText;
}
tag = tt('<span class="?">?<i title="' + getMsg("delete") + '">X</i></span>', classes, text);
tag = tt('<span class="?" data-tag="?">?<i title="' + getMsg("delete") + '">X</i></span>', classes, text, text);
// 避免重复
var isExists = false;
$("#tags").children().each(function() {
if (isColor) {
var tagHtml = $("<div></div>").append($(this).clone()).html();
if (tagHtml == tag) {
$(this).remove();
isExists = true;
}
} else if (text + "X" == $(this).text()) {
$(this).remove();
isExists = true;
}
});
@ -165,6 +169,22 @@ Tag.appendTag = function(tag) {
if (!isColor) {
reRenderTags();
}
// 笔记已污染
if(save) {
Note.curNoteIsDirtied();
// 如果之前不存, 则添加之
if(!isExists) {
Note.curChangedSaveIt(true, function() {
ajaxPost("/tag/updateTag", {tag: rawText}, function(ret) {
if(reIsOk(ret)) {
Tag.addTagNav(ret.Item);
}
});
});
}
}
}
// 为了颜色间隔, add, delete时调用
@ -181,23 +201,59 @@ function reRenderTags() {
i++;
}
});
}
};
// 删除tag
Tag.removeTag = function($target) {
var tag = $target.data('tag');
$target.remove();
reRenderTags();
if(LEA.locale == "zh") {
tag = Tag.mapCn2En[tag] || tag;
}
Note.curChangedSaveIt(true, function() {
ajaxPost("/tag/updateTag", {tag: tag}, function(ret) {
if(reIsOk(ret)) {
Tag.addTagNav(ret.Item);
}
});
});
};
//-----------
// 左侧nav en -> cn
Tag.tags = [];
Tag.renderTagNav = function(tags) {
var me = this;
tags = tags || [];
Tag.tags = tags;
$("#tagNav").html('');
for(var i in tags) {
var tag = tags[i];
if(tag == "red" || tag == "blue" || tag == "yellow" || tag == "green") {
continue;
var noteTag = tags[i];
var tag = noteTag.Tag;
var text = tag;
if(LEA.locale == "zh") {
var text = Tag.mapEn2Cn[tag] || text;
}
var text = Tag.mapEn2Cn[tag] || tag;
var classes = Tag.classes[tag] || "label label-default";
$("#tagNav").append(tt('<li data-tag="?"><a> <span class="?">?</span></li>', text, classes, text));
$("#tagNav").append(tt('<li data-tag="?"><a> <span class="?">?</span> <span class="tag-delete">X</span></li>', tag, classes, text));
}
}
};
// 添加的标签重新render到左边, 放在第一个位置
// 重新render
Tag.addTagNav = function(newTag) {
var me = this;
for(var i in me.tags) {
var noteTag = me.tags[i];
if(noteTag.Tag == newTag.Tag) {
me.tags.splice(i, 1);
break;
}
}
me.tags.unshift(newTag);
me.renderTagNav(me.tags);
};
// 事件
$(function() {
@ -245,7 +301,7 @@ $(function() {
Tag.appendTag({
classes : a.attr("class"),
text : a.text()
});
}, true);
});
// 这是个问题, 为什么? 捕获不了事件?, input的blur造成
/*
@ -260,14 +316,29 @@ $(function() {
*/
$("#tags").on("click", "i", function() {
$(this).parent().remove();
reRenderTags();
Tag.removeTag($(this).parent());
});
//----------
//
function deleteTag() {
$li = $(this).closest('li');
var tag = $.trim($li.data("tag"));
if(confirm("Are you sure ?")) {
ajaxPost("/tag/deleteTag", {tag: tag}, function(re) {
if(reIsOk(re)) {
var item = re.Item; // 被删除的
Note.deleteNoteTag(item, tag);
$li.remove();
}
});
};
}
//-------------
// nav 标签搜索
function searchTag() {
var tag = $.trim($(this).data("tag"));
var $li = $(this).closest('li');
var tag = $.trim($li.data("tag"));
// tag = Tag.mapCn2En[tag] || tag;
// 学习changeNotebook
@ -279,7 +350,9 @@ $(function() {
// 也会把curNoteId清空
Note.clearAll();
$("#tagSearch").html($(this).html()).show();
$("#tagSearch").html($li.html()).show();
$("#tagSearch .tag-delete").remove();
showLoading();
ajaxGet("/note/searchNoteByTags", {tags: [tag]}, function(notes) {
hideLoading();
@ -295,6 +368,8 @@ $(function() {
}
});
}
$("#myTag .folderBody").on("click", "li", searchTag);
$("#minTagNav").on("click", "li", searchTag);
$("#myTag .folderBody").on("click", "li .label", searchTag);
// $("#minTagNav").on("click", "li", searchTag);
$("#myTag .folderBody").on("click", "li .tag-delete", deleteTag);
});