-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.autosave.js
57 lines (54 loc) · 1.45 KB
/
jquery.autosave.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
/**
* autosave plugin
*
* Copyright (c) 2009-2016 Fil (fil@rezo.net)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
/*
* Usage: $("form").autosave({options...});
* to use with SPIP's action/session.php
*/
(function($){
$.fn.autosave = function(opt) {
opt = $.extend({
url: window.location,
confirm: false,
confirmstring: 'Sauvegarder ?'
},opt);
var save_changed = function(){
$('form.autosavechanged')
.each(function(){
if (!opt.confirm || confirm(opt.confirmstring)) {
var contenu = $(this).serialize();
// ajoutons un timestamp
var d=new Date();
contenu = contenu + "&__timestamp=" + Math.round(d.getTime()/1000);
$.post(opt.url, {
'action': 'session',
'var': 'autosave_' + $('input[name=autosave]', this).val(),
'val': contenu
});
}
}).removeClass('autosavechanged');
}
$(window)
.bind('unload',save_changed);
return this
.bind('keyup', function() {
$(this).addClass('autosavechanged');
})
.bind('change', function() {
$(this).addClass('autosavechanged');
save_changed();
})
.bind('submit',function() {
save_changed();
/* trop agressif : exemple du submit previsu forum, ou des submit suivant/precedent d'un cvt multipage
on sauvegarde toujours, et le serveur videra quand il faudra */
/*$(this).removeClass('autosavechanged')*/;
});
}
})(jQuery);