A tumblelog CMS built on AJAX, PHP and MySQL.

form.autosave.js 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. Form.Unserialize = function(form,queryComponents) {
  52. var elements = Form.getElements($(form));
  53. var queryComponents = queryComponents.toQueryParams();
  54. for (var i = 0; i < elements.length; i++) {
  55. var element = elements[i];
  56. var name = element.name;
  57. if(queryComponents[name]) {
  58. var method = element.tagName.toLowerCase();
  59. var value = decodeURIComponent(queryComponents[name]);
  60. Form.Element.Unserializers[method](element,value);
  61. }
  62. }
  63. };
  64. Form.Element.Unserializers = {
  65. input: function(element,value) {
  66. switch (element.type.toLowerCase()) {
  67. case 'submit':
  68. case 'hidden':
  69. case 'password':
  70. case 'text':
  71. return Form.Element.Unserializers.textarea(element,value);
  72. case 'checkbox':
  73. case 'radio':
  74. return Form.Element.Unserializers.inputSelector(element,value);
  75. }
  76. return false;
  77. },
  78. inputSelector: function(element,value) {
  79. if (element.value == value)
  80. element.checked = 'checked';
  81. },
  82. textarea: function(element,value) {
  83. element.value = value;
  84. },
  85. select: function(element,value) {
  86. for (var i = 0; i < element.length; i++) {
  87. var opt = element.options[i];
  88. if(opt.value == value || opt.text == value)
  89. opt.selected = 'selected';
  90. }
  91. }
  92. }