[SPIP] +2.1.12
[velocampus/web/www.git] / www / prive / javascript / jquery.autosave.js
diff --git a/www/prive/javascript/jquery.autosave.js b/www/prive/javascript/jquery.autosave.js
new file mode 100644 (file)
index 0000000..933b636
--- /dev/null
@@ -0,0 +1,45 @@
+/**
+ * autosave plugin
+ *
+ * Copyright (c) 2009 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(opt,{
+                       confirm: false,
+                       confirmstring: 'Sauvegarder ?'
+               });
+               $(window)
+               .bind('unload',function() {
+                       $('form.autosavechanged')
+                       .each(function(){
+                               if (!opt.confirm || confirm(opt.confirmstring)) {
+                                       var contenu = $(this).serialize();
+                                       $.post('spip.php', {
+                                               'action': 'session',
+                                               'var': 'autosave_' + $('input[name=autosave]', this).val(),
+                                               'val': contenu
+                                       });
+                               }
+                       });
+               });
+               return this
+               .bind('change keyup', function() {
+                       $(this).addClass('autosavechanged');
+               })
+               .bind('submit',function() {
+                       $(this).removeClass('autosavechanged');
+               });
+       }
+})(jQuery);
+