-
Notifications
You must be signed in to change notification settings - Fork 1
/
behavior.js
120 lines (108 loc) · 3.88 KB
/
behavior.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
function removeClass(element, name){
var classes = element.className.split(" ");
if(classes.indexOf(name) != -1)
{
classes.splice(classes.indexOf(name),1);
element.className = classes.join(" ");
}
}
function addClass(element, name){
var classes = element.className.split(" ");
if(classes.indexOf(name) != -1) return;
classes.push(name);
element.className = classes.join(" ");
}
function sort(obj, sortFunction){
sorted = [];
for(key in obj){
sorted.push(obj[key]);
}
sorted.sort(sortFunction);
return sorted;
};
function autosave (area){
if (area.hasDefault == true || area.domElement.value == "") return;
var count = area.wordCount();
document.getElementById("message").textContent = count + " word" +(count > 1? 's':'');
document.getElementById("autosave").textContent = "Autosaved at "+new Date().toTimeString();
area.makeSnippet().save();
}
function refreshHistory (history, area){
var next = document.getElementById("history-next");
document.getElementById("past").removeChild(next);
var collection = document.getElementById("past").getElementsByTagName("p");
for(var i = 0; i < collection.length;){
collection[i].parentNode.removeChild(collection[i]);
}
history.entries.forEach(function(snippet){
var text = document.createElement("p");
text.textContent = snippet.shortContent();
text.snippet = snippet;
text.addEventListener("click",function(){
area.loadSnippet(this.snippet);
var count = area.wordCount();
document.getElementById("message").textContent = count + " word" +(count > 1? 's':'');
});
var text_delete = document.createElement("img");
text_delete.src = "img/del.png";
addClass(text_delete, "icon");
text_delete.addEventListener("click", function(e){
this.parentNode.snippet.remove();
history.load(true);
setTimeout(function(){refreshHistory(history,area);},0);
e.stopPropagation();
});
text.appendChild(text_delete);
document.getElementById("past").appendChild(text);
});
document.getElementById("past").appendChild(next);
}
window.onload = function(){
document.getElementById("today").textContent = new Date().toLocaleDateString();
var area = new WritingArea(document.getElementById('maintext'));
area.domElement.addEventListener("keypress", function(e){
if(area.hasDefault){
area.hasDefault = false;
area.clear();
area.setPlaceholder(false);
}
});
area.domElement.addEventListener("change", function(e){
var count = area.wordCount();
document.getElementById("message").textContent = count + " word" +(count > 1? 's':'')
});
document.getElementById('donewriting').addEventListener("click", function(){
var snip = area.makeSnippet();
snip.save();
area.clear();
area.currentSnippet = null;
history.load(true);
refreshHistory(history,area);
});
var history = new History();
history.load();
refreshHistory(history,area);
if(history.entries.length == 10){
addClass(document.getElementById("history-next"),"button-visible");
document.getElementById("history-next").addEventListener("click", function(){
history.next();
if(history.entries.length < 10 || history.window.max >= history.totalCount) removeClass(document.getElementById("history-next"),"button-visible");
addClass(document.getElementById("history-previous"),"button-visible");
refreshHistory(history,area);
});
document.getElementById("history-previous").addEventListener("click", function(){
history.previous();
if(history.window.min == 0) removeClass(document.getElementById("history-previous"),"button-visible");
addClass(document.getElementById("history-next"),"button-visible");
refreshHistory(history,area);
});
}
document.getElementById("createNew").addEventListener("click", function(){
area.clear();
area.currentSnippet = null;
document.getElementById("message").innerHTML = " ";
});
setInterval(function(){
return autosave(area);
}, 50000);
}