Browse Source

Autosave function

pecesama 16 years ago
parent
commit
7ed00a70aa
5 changed files with 2142 additions and 3 deletions
  1. 42 0
      admin/form.autosave.php
  2. 11 1
      admin/index.php
  3. 97 0
      admin/scripts/form.autosave.js
  4. 1985 0
      admin/scripts/prototype.js
  5. 7 2
      notice.txt

+ 42 - 0
admin/form.autosave.php View File

@@ -0,0 +1,42 @@
1
+<?php
2
+
3
+session_start();
4
+
5
+function isAjax() { 
6
+	return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && 
7
+	$_SERVER ['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'; 
8
+}
9
+
10
+function saveForm() {
11
+	$type = getMethod();
12
+	$id = ($type=='GET') ? $_GET['autosaveid'] : $_POST['autosaveid'];
13
+	$_SESSION[$id] = $_SERVER['QUERY_STRING'];
14
+	echo date('H:i | d/m/y',time());
15
+}
16
+
17
+function loadForm() {
18
+	$type = getMethod();
19
+	$id = ($type=='GET') ? $_GET['autosaveid'] : $_POST['autosaveid'];
20
+	if(isset($_SESSION[$id]))
21
+		echo $_SESSION[$id];
22
+}
23
+
24
+function isLoad() {
25
+	$type = getMethod();
26
+	if($type=='GET' and isset($_GET['autosave'])) return true;
27
+	elseif(isset($_POST['autosave'])) return true;
28
+	return false;
29
+}
30
+
31
+function getMethod() {
32
+	return $_SERVER['REQUEST_METHOD'];
33
+}
34
+
35
+if(isAjax()) {
36
+	if(isLoad()) loadForm();
37
+	else saveForm();
38
+}
39
+
40
+exit;
41
+
42
+?>

+ 11 - 1
admin/index.php View File

@@ -99,6 +99,8 @@ if ($user->isAdmin()) {
99 99
 		<script language="javascript" type="text/javascript" src="<?php echo $conf->urlGelato;?>/admin/scripts/tools.js"></script>
100 100
 		<script language="javascript" type="text/javascript" src="<?php echo $conf->urlGelato;?>/admin/scripts/mootools.js"></script>
101 101
 		<script language="javascript" type="text/javascript" src="<?php echo $conf->urlGelato;?>/admin/scripts/slimbox.js"></script>
102
+		<script language="javascript" type="text/javascript" src="<?php echo $conf->urlGelato;?>/admin/scripts/prototype.js"></script>
103
+		<script language="javascript" type="text/javascript" src="<?php echo $conf->urlGelato;?>/admin/scripts/form.autosave.js"></script>
102 104
 		<script language="javascript" type="text/javascript">
103 105
 		<!--
104 106
 			function exit(el, path) {
@@ -196,8 +198,9 @@ if ($user->isAdmin()) {
196 198
 						}
197 199
 					}					
198 200
 ?>					
199
-					<form action="index.php" method="post" <?php echo (isset($_GET["new"]) && $_GET["new"]=="photo") ? "enctype=\"multipart/form-data\"" : ""?> name="frmAdd" class="newpost">
201
+					<form action="index.php" method="post" <?php echo (isset($_GET["new"]) && $_GET["new"]=="photo") ? "enctype=\"multipart/form-data\"" : ""?> name="frmAdd" id="autosave" class="newpost">
200 202
 						<fieldset>
203
+						<div id="update" style="display:none;" class="exito"></div><br />
201 204
 <?php
202 205
 							if ($isEdition) {
203 206
 ?>
@@ -302,6 +305,13 @@ if ($user->isAdmin()) {
302 305
 								</p>
303 306
 						</fieldset>
304 307
 					</form>
308
+					<div id="serialize"></div>
309
+					<script type="text/javascript" language="javascript" charset="utf-8">
310
+					// <![CDATA[
311
+						//id: formulario, id: info, options
312
+						new Form.autoSave('autosave','update',{});
313
+					// ]]>
314
+					</script>
305 315
 					
306 316
 					<div class="footer-box">&nbsp;</div>
307 317
 				</div>

+ 97 - 0
admin/scripts/form.autosave.js View File

@@ -0,0 +1,97 @@
1
+/*  Autocompleter.editor, version 1.0: http://icebeat.bitacoras.com
2
+ *  (c) 2005 Daniel Mota aka IceBeat <daniel.mota@gmail.com>
3
+ *
4
+/*--------------------------------------------------------------------------*/
5
+Form.autoSave = Class.create();
6
+Form.autoSave.prototype = {
7
+	/*	
8
+	*	Inicializamos la clase y preparamos parametros
9
+	*/
10
+  initialize: function(element, update, options) {
11
+    this.element = $(element);
12
+    this.update = $(update);
13
+    this.options = $H({
14
+			url: 'form.autosave.php',
15
+			frequency: 30,
16
+			method: 'get',
17
+			msg: {
18
+			  loading:  'Filling form',
19
+			  loaded:   'Form filled',
20
+			  sending:  'Sending form',
21
+			  update:   'Form saved at: '
22
+			}
23
+		}).merge(options);
24
+		this.msg = this.options.msg;
25
+		this.message(this.msg.loading);
26
+    new Ajax.Request(this.options.url, { method: this.options.method, parameters:"autosave=true&autosaveid="+this.element.id, onSuccess: this.autoFill.bind(this) });
27
+  },
28
+  message: function(msg) {
29
+    Element.show(this.update);
30
+    this.update.innerHTML = msg;
31
+  },
32
+  send: function(element,value) {
33
+    this.message(this.msg.sending);
34
+    new Ajax.Request(this.options.url, { method: this.options.method, parameters:"autosaveid="+element.id+'&'+value, onSuccess: this.updateForm.bind(this) });
35
+  },
36
+  updateForm: function(resp) {
37
+    if(resp.responseText) {
38
+      this.message(this.msg.update+resp.responseText);
39
+    }
40
+  },
41
+  autoFill: function(resp) {
42
+    if(resp.responseText) {
43
+      this.message(this.msg.loaded);
44
+      Form.Unserialize(this.element,resp.responseText);
45
+    } else {
46
+      Element.hide(this.update);
47
+    }
48
+    new Form.Observer(this.element,this.options.frequency,this.send.bind(this));
49
+  }
50
+};
51
+
52
+Form.Unserialize = function(form,queryComponents) {
53
+  var elements = Form.getElements($(form));
54
+  var queryComponents = queryComponents.toQueryParams();
55
+  for (var i = 0; i < elements.length; i++) {
56
+    var element = elements[i];
57
+    var name = element.name;
58
+    if(queryComponents[name]) {
59
+      var method = element.tagName.toLowerCase();
60
+      var value = decodeURIComponent(queryComponents[name]);
61
+      Form.Element.Unserializers[method](element,value);
62
+    }
63
+  }
64
+};
65
+
66
+Form.Element.Unserializers = {
67
+  input: function(element,value) {
68
+    switch (element.type.toLowerCase()) {
69
+      case 'submit':
70
+      case 'hidden':
71
+      case 'password':
72
+      case 'text':
73
+        return Form.Element.Unserializers.textarea(element,value);
74
+      case 'checkbox':
75
+      case 'radio':
76
+        return Form.Element.Unserializers.inputSelector(element,value);
77
+    }
78
+    return false;
79
+  },
80
+
81
+  inputSelector: function(element,value) {
82
+    if (element.value == value)
83
+        element.checked = 'checked';
84
+  },
85
+
86
+  textarea: function(element,value) {
87
+    element.value = value;
88
+  },
89
+
90
+  select: function(element,value) {
91
+    for (var i = 0; i < element.length; i++) {
92
+      var opt = element.options[i];
93
+      if(opt.value == value || opt.text == value) 
94
+        opt.selected = 'selected';
95
+    }
96
+  }
97
+}

File diff suppressed because it is too large
+ 1985 - 0
admin/scripts/prototype.js


+ 7 - 2
notice.txt View File

@@ -1,10 +1,11 @@
1
-== CREDITS==
1
+== CREDITS==
2 2
 Gelato developers and designers (in chronological order):
3 3
 
4 4
 Pedro Santana [ http://www.pecesama.net/weblog/ ]
5 5
 Jorge Condomí [ http://www.raven.com.ar/ ]
6 6
 Víctor de la Rocha [ http://blog.mis-algoritmos.com/ ]
7 7
 Victor Bracco [ http://www.vbracco.com.ar/blog/ ]
8
+Matt Heitzenroder [ http://www.globaljourney.net/ ]
8 9
 
9 10
 = COPYRIGHT NOTICES =
10 11
 This product includes code and libraries developed by third parties, which are governed by different licenses.  These components, and their licenses, are listed below.
@@ -48,4 +49,8 @@ Textile class available under GNU General Public License.
48 49
 = PHP-gettext =
49 50
 Copyright (c) 2003 Danilo Segan, http://savannah.nongnu.org/projects/php-gettext/
50 51
 Copyright (c) 2005 Nico Kaiser
51
-PHP-gettext available under GNU General Public License.
52
+PHP-gettext available under GNU General Public License.
53
+
54
+= form.autosave =
55
+Copyright (c) Daniel Mota Leiva, http://icebeat.bitacoras.com/app/autosave/
56
+form.autosave available under Creative Commons Attribution-ShareAlike License.