-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnote.js
164 lines (152 loc) · 5.33 KB
/
note.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* Created by JetBrains WebStorm.
* User: tjk && sankyu
* Date: 12-9-3
* Time: 下午5:48
* To change this template use File | Settings | File Templates.
*/
(function(){
function Note(){
this.id = 0;
this.title = "";
this.text = "";
this.time = "";
this.toJson = function(){
return {
"id" : this.id,
"title" : this.title,
"text" : this.text,
"time" : this.time
}
}
}
//var maxId = 1;
var h5note = {
maxId : 1,
bindEditContent:function(){
var editArea = document.querySelectorAll("#editContent");
editArea.addEventListener("change",function(){
alert("change event");
$(".title").text("change event");
});
editArea.addEventListener("DOMCharacterDataModified",function(){
alert("DOMCharacterDataModified event");
$(".title").text("DOMCharacterDataModified event");
});
editArea.addEventListener("focus",function(){
alert("focus event");
$(".title").text("focus event");
});
},
//切换
tiggler:function(event){
var $article = $(".page");
if($article.hasClass("showMenu")){
$article.removeClass("showMenu");
}else{
$article.addClass("showMenu");
}
},
updateLocalMaxId:function(){
var temp = localStorage.getItem("maxId");
if(temp != null){
h5note.maxId = parseInt(temp);
}else{
h5note.maxId = 1;
}
},
plugMaxId:function(){
var temp = localStorage.getItem("maxId");
if(temp != null){
h5note.maxId = parseInt(temp) + 1;
}
localStorage.setItem("maxId",h5note.maxId);
},
//初始化
init:function(){
$("#main").width(window.outerWidth);
$("#main").height(window.outerHeight);
$("#main #menu section").height(window.outerHeight - 46);
var headerH = $(".page header").height();
$("#editContent").height($("#main").height() - headerH);
h5note.updateLocalMaxId();
h5note.getNotes();
$("#tiggler").on("click",function(){
h5note.tiggler();
h5note.getNotes();
});
$("#save").on("click",function(){
if($(this).text() == "新建"){
$(this).text("保存");
$("#editContent").html("");
$("#editContent").attr("data-id","");
$("#editContent").focus();
}else{
console.log("开始保存笔记!");
h5note.addNote();
$(this).text("新增");
console.log("笔记保存成功!");
}
});
$("#clear").on("click",function(){
localStorage.clear();
h5note.getNotes();
});
setTimeout(function(){window.scrollTo(0,0)},100);
},
//读取所有本地存储的数据
getNotes:function(){
var length = localStorage.length;
var list = new Array();
var temphtml = "",n;
for(var i=0;i<length;i++){
var key = localStorage.key(i);
if(!isNaN(key)){
console.log(key);
n = JSON.parse(localStorage.getItem(key));
temphtml += "<article data-id='" + n.id + "'>" + n.title +"</article>";
}
}
document.getElementById("noteNav").innerHTML = temphtml;
$("#noteNav article").on("click",function(){
h5note.showNote($(this).attr("data-id"));
var $article = $(".page");
if($article.hasClass("showMenu")){
$article.removeClass("showMenu");
}else{
$article.addClass("showMenu");
}
$("#save").text("保存");
});
},
//创建一条笔记到本地存储
addNote:function(){
var n = new Note();
text = document.getElementById("editContent").innerHTML;
text2 = document.getElementById("editContent").innerText;
n.text = text;
n.title = text2.substring(0,15);
n.time = new Date().toString();
var dataid = $("#editContent").attr("data-id");
if(dataid != ""){
n.id = dataid;
localStorage.setItem(dataid,JSON.stringify(n.toJson()));
}else{
h5note.plugMaxId();
n.id = h5note.maxId;
localStorage.setItem(h5note.maxId,JSON.stringify(n.toJson()));
}
},
//读取单条笔记
getNote:function(id){
var object = JSON.parse(localStorage.getItem(id));
return object;
},
showNote:function(id){
var n = h5note.getNote(id);
$("#editContent").html(n.text);
$("#editContent").attr("data-id",id);
}
};
h5note.init();
})();