-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
155 lines (134 loc) · 4.63 KB
/
Copy pathmain.js
File metadata and controls
155 lines (134 loc) · 4.63 KB
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
/*
* Autosave v1.0 - by Omid Mufeed
* Examples/Docs: www.omidmufeed.com
* Date: 01 April 2014
*
* Open Source
* Reference: typewatch code piece by Christian C. Salvadó (http://codingspot.com/)
*/
var typewatch = (function () {
var timer = 0;
return function (callback, ms) {
clearTimeout(timer);
timer = setTimeout(callback, ms);
}
})();
$(document).ready(function () {
Initialise();
});
//Initialises the autosave in the input containers
function Initialise() {
$(".InputContainer").each(function () {
var objInputContainer = $(this);
//Initilise the word count
UpdateWordCount(objInputContainer);
var timer = $.timer(function () {
if (timer.isActive) {
timer.stop();
$("#IsActive").html(timer.isActive);
}
});
//Save every 20 seconds
timer.set({ time: 20000, autostart: false });
//Activate on keyup
objInputContainer.find(".InputBody").keyup(function () {
if (!timer.isActive) {
//Add some delay before starting to save again. This would help the save trigger very often.
setTimeout(function () {
objInputContainer.find(".InputState_Saved").fadeOut(10);
timer.play();
$("#IsActive").html(timer.isActive);
}, 500);
}
typewatch(function () {
//executed only 2s after the last keyup event.
if (timer.isActive) {
timer.stop();
$("#IsActive").html(timer.isActive);
Save(objInputContainer);
}
}, 2000);
//Update word count on keyup
UpdateWordCount(objInputContainer);
});
//Deactivate
objInputContainer.find(".InputBody").blur(function () {
timer.stop();
$("#IsActive").html(timer.isActive);
});
//Save on change
objInputContainer.find(".InputBody").change(function () {
if (timer.isActive) {
timer.stop();
$("#IsActive").html(timer.isActive);
Save(objInputContainer);
}
});
//Execute autosave before leaving the page
$(window).unload(function () {
timer.stop();
Save(objInputContainer);
});
});
}
// The magic happens here...
function Save(objInputContainer) {
var blnReturn = false;
if (objInputContainer) {
Saving(objInputContainer);
//Save the text here
var fnSave = new Function("return " + objInputContainer.find("#SaveFunction").val());
//If the save fails, try it 3 times.
var intAttemptCount = 0;
while (!blnReturn && intAttemptCount < 3)
{
if (fnSave()) blnReturn = true;
else intAttemptCount++;
}
if (blnReturn)
{
UpdateLog(objInputContainer);
Saved(objInputContainer);
}
else {
Error(objInputContainer);
}
}
return blnReturn;
}
function UpdateLog(objInputContainer){
//Update the log
var dtmSaved = new Date();
$("#AutosaveLog").append("<p>" + objInputContainer.attr("id") + " saved at " + dtmSaved.getHours() + ":" + dtmSaved.getMinutes() + ":" + dtmSaved.getSeconds() + "<br />");
$("#AutosaveLog").append(objInputContainer.find(".InputBody").val() + "</p><hr />");
}
var regex = /\s+/gi;
function UpdateWordCount(objInputContainer) {
objInputContainer.find(".WordCount").html(objInputContainer.find(".InputBody").val().length > 0 ? objInputContainer.find(".InputBody").val().trim().replace(regex, ' ').split(' ').length : 0);
}
function Saving(objInputContainer) {
objInputContainer.find(".InputState_Error").hide();
objInputContainer.find(".InputState_Saved").hide();
objInputContainer.find(".InputState_Saving").fadeIn(5).delay(1000);
}
function Saved(objInputContainer) {
objInputContainer.find(".InputState_Error").hide();
objInputContainer.find(".InputState_Saving").fadeOut(5);
objInputContainer.find(".InputState_Saved").delay(2000).fadeIn(5);
}
function Error(objInputContainer) {
objInputContainer.find(".InputState_Saved").hide();
objInputContainer.find(".InputState_Saving").fadeOut(5);
objInputContainer.find(".InputState_Error").delay(2000).fadeIn(5);
}
function SaveTitle(){
//TODO make you ajax calls here
//Return 1 if the save operations is successful and 0 if failed.
return 1;
}
function SaveBody()
{
//TODO make you ajax calls here
//Return 1 if the save operations is successful and 0 if failed.
return 1;
}