-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
executable file
·295 lines (261 loc) · 8.56 KB
/
script.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* Copyright 2015 Jules and Theo Zimmermann
All rights reserved
**/
"use strict"
// localStorage getters and setters
var store = function() {
// local attributes
var versionNb = localStorage.versionNb || 0;
var editNb = localStorage["editNb(" + versionNb + ")"] || 0;
const initialInfo = 'Espace à remplir par un texte initial.\n\nPour sauvegarder un nouveau texte, cliquer sur "Proposer la modification" puis entrer "edit".\nLa première fois, il faudra choisir un mot de passe qui sera demandé les fois suivantes.'
var getEditNb = function() {
return editNb;
};
var getEditName = function(i) {
return localStorage["editName(" + versionNb + "," + i + ")"];
};
var getEditText = function(i) {
return localStorage["editText(" + versionNb + "," + i + ")"];
};
var getText = function() {
var inStore = localStorage["text(" + versionNb + ")"];
return (typeof(inStore) === "undefined") ? initialInfo : inStore;
};
var addEdit = function(name, text) {
editNb++;
localStorage["editNb(" + versionNb + ")"] = editNb;
localStorage["editText(" + versionNb + "," + editNb + ")"] = text;
localStorage["editName(" + versionNb + "," + editNb + ")"] = name;
};
var setText = function(value) {
// every time the main text changes, the version changes
versionNb++;
localStorage.versionNb = versionNb;
editNb = 0;
localStorage["text(" + versionNb + ")"] = value;
};
var setPassword = function(value) {
localStorage.password = CryptoJS.MD5(value);
};
var checkPassword = function(value) {
// stored value is a string and hashed value is an object
// thus we need to check equality modulo conversion
return localStorage.password == CryptoJS.MD5(value);
};
var noPassword = function() {
return typeof(localStorage.password) === "undefined";
}
var clear = function() {
localStorage.clear();
versionNb = 0;
editNb = 0;
};
return {
getEditNb: getEditNb,
getEditName: getEditName,
getEditText: getEditText,
getText: getText,
addEdit: addEdit,
setText: setText,
setPassword: setPassword,
checkPassword: checkPassword,
noPassword: noPassword,
clear: clear
};
}();
var alert = function(message, width) {
width = width ? width : 300;
$("<div>" + message + "</div>").dialog({
resizable: false,
width: width,
modal: true,
buttons: {
Ok: function() {
$(this).remove();
}
}
});
};
var confirm = function(message, callback) {
$("<div>" + message + "</div>").dialog({
resizable: false,
modal: true,
buttons: {
Continuer: function() {
$(this).remove();
callback();
},
Annuler: function() {
$(this).remove();
}
}
});
};
var prompt = function(message, callback, password) {
var type = password ? "password" : "text";
var dialog = $('<div><p><label for="prompt">' + message + '</label><p><input id="prompt" type="' + type + '" autocomplete="off"></div>').dialog({
autoOpen: false,
resizable: false,
modal: true,
buttons: {
Continuer: function() {
var value = $("#prompt").val();
$(this).remove();
callback(value);
},
Annuler: function() {
$(this).remove();
callback(null);
}
}
});
$("#prompt").keypress(function(e) {
if (e.which === 13) {
var value = $("#prompt").val();
dialog.remove();
callback(value);
}
});
dialog.dialog("open");
};
var passwordOk = function(toSet, callback) {
if (store.noPassword()) {
if (toSet) {
prompt("Choisir un mot de passe :", function(value) {
if (value === null) {
alert("<p>Opération annulée !");
}
else {
store.setPassword(value);
callback();
}
}, true);
}
else {
callback();
}
}
else {
prompt("Mot de passe ?", function(password) {
if (password === null) {
alert("<p>Opération annulée !");
}
else if (store.checkPassword(password)) {
callback();
}
else {
alert("<p>Mot de passe incorrect !");
}
}, true);
}
}
$(document).ready(function() {
var text = $("#text");
var edits = $("#edits");
// make a diff_match_patch object once and for all
var dmp = new diff_match_patch();
var textUnchanged = function() {
var textarea = $("#text textarea");
return textarea.length === 0 || textarea.val() === store.getText();
};
var showEdit = function(i) {
edits.append('<p><button class="edit" id="edit' + i + '">' + store.getEditName(i) + '</button></p>');
$("#edit" + i).click(function() {
// active -> unactive / unactive -> active
// if was active
if ($(this).hasClass("active")) {
// unactivate button
$(this).removeClass("active");
showText();
return;
}
var that = $(this);
var activate = function() {
// unactivate other active buttons
$("button.active").removeClass("active");
that.addClass("active");
var texti = store.getEditText(i);
if (typeof(texti) !== "undefined") {
var diffs = dmp.diff_main(store.getText(), texti);
dmp.diff_cleanupSemantic(diffs);
var html = dmp.diff_prettyHtml(diffs);
text.html("<p>" + html + "</p>");
}
else {
text.html("<p>Erreur !</p>");
}
};
if (textUnchanged()) {
activate();
}
else {
confirm("<p>Attention : afficher la proposition de modification annulera la modification en cours.", activate);
}
});
};
var saveEdit = function(name) {
// add date and hour to edit name
var date = new Date();
// French date
var localeDate = date.toLocaleString();
localeDate = localeDate.substring(0, localeDate.length - 3);
localeDate = localeDate.replace(" ", " à ").replace(":", "h");
store.addEdit(name + " (le " + localeDate + ")", $("#text textarea").val());
};
// Show initial text
var showText = function() {
text.html("<textarea>" + store.getText() + "</textarea>");
};
showText();
var saveText = function() {
store.setText($("#text textarea").val());
}
// Show initial edits
for (var i = 1; i <= store.getEditNb(); i++) {
showEdit(i);
}
// New edits are made possible
$("#propose").click(function() {
if (textUnchanged()) {
alert("<p>Pas de modification en cours !");
return;
}
prompt("Description de la proposition de modification :", function(name) {
if (name === null || name === "" || name === "reset" && $("textarea").val() !== "" || name === "edit" && $("textarea").val() === "") {
alert("<p>La modification n'a pas été sauvegardée.")
}
else if (name === "reset") {
passwordOk(false, function() {
store.clear();
showText();
$(".edit").remove();
});
}
else if (name === "edit") {
passwordOk(true, function() {
saveText();
showText();
$(".edit").remove();
});
}
else {
saveEdit(name);
showEdit(store.getEditNb());
showText();
}
});
});
// Menu
$("#help").click(function(e) {
e.preventDefault();
alert("<h2>Pour les élèves :</h2><p>Le texte principal est modifiable. On sauvegarde les changements en cliquant sur \"Proposer la modification\" et en lui donnant un nom.<p>Toutes les propositions de modification sont listées dans la colonne de droite. Un clic permet de les afficher, un autre clic de revenir au texte non modifié. Le texte n'est pas modifiable lors de l'affichage d'une proposition modification.<h2>Pour les profs :</h2><p>Les opérations suivantes sont protégées par un mot de passe.<p>Pour sauvegarder une nouvelle version du texte, il faut cliquer sur \"Proposer la modification\" puis entrer \"edit\".<p>Pour remettre l'outil à zéro (attention danger !), il faut supprimer le texte, cliquer sur \"Proposer la modification\" puis entrer \"reset\".", 500);
});
$("#about").click(function(e) {
e.preventDefault();
alert("<p>Les Petites Plumes est un site réalisé pour les élèves du Vallon de l'Oriol par Jules et Théo Zimmermann.<p>C'est un outil simplifié, issu d'<a target=\"_blank\" href=\"http://unkilodeplumes.fr\">unkilodeplumes</a>, le roman collaboratif.");
});
$("#contact").click(function(e) {
e.preventDefault();
alert("<p>Nous sommes ouverts à toutes les remarques, questions, etc. Notre adresse e-mail:<p><a href=\"mailto:[email protected]\">[email protected]</a>.");
});
});