/public/wuwei.js

http://github.com/mtravers/wuwei · JavaScript · 87 lines · 56 code · 15 blank · 16 comment · 12 complexity · f056b879751a27690f76c6a2d669c7a3 MD5 · raw file

  1. // functions to support the wuwei Lisp library
  2. function add_spinner(after) {
  3. var s_id = after + '_spin';
  4. Element.insert($(after), {after: "<img src='/wupub/images/spinner.gif', id='" + s_id + "'>"});
  5. }
  6. function remove_spinner (after) {
  7. var s_id = after + '_spin';
  8. var elt = $(s_id);
  9. if (elt != null) { // spinner may have been removed by an update
  10. Element.remove(elt);
  11. }
  12. }
  13. // autocomplete support
  14. function setupAutocomplete(input, continuation) {
  15. var input = $(input);
  16. input.continuation = continuation;
  17. // on blur, if autocomplete hasn't happend, send the string to the continuation in case it can do something (+++ should be an option)
  18. // Event.observe($(input), 'blur', function (event) {
  19. // if (input.continuation != null) {
  20. // window.setTimeout(function () { // yes this is horrible, but this has to happen AFTER the click handler or you get the wrong thing
  21. // new Ajax.Request(continuation, {asynchronous:true, evalScripts:true, parameters: {value_string: $(input).value, id: $(input).id} });
  22. // input.continuation = null;
  23. // },150);
  24. // }
  25. // });
  26. }
  27. function postAutocomplete(text, li) {
  28. var continuation = text.continuation;
  29. var value = li.id;
  30. var value_string = li.innerHTML;
  31. var id = text.id
  32. if (continuation != null) {
  33. new Ajax.Request(continuation, {asynchronous:true, evalScripts:true, parameters: {value: value, value_string: value_string, id: id} });
  34. // text.continuation = null;
  35. }
  36. }
  37. // inplace editor support
  38. // stupid that this isn't in the thing to being with.
  39. if (Ajax.InPlaceEditor != null) {
  40. Ajax.InPlaceEditorWithEmptyText = Class.create(Ajax.InPlaceEditor, {
  41. initialize : function($super, element, url, options) {
  42. if (!options.emptyText) options.emptyText = "click to edit…";
  43. if (!options.emptyClassName) options.emptyClassName = "inplaceeditor-empty";
  44. $super(element, url, options);
  45. this.checkEmpty();
  46. },
  47. checkEmpty : function() {
  48. if (this.element.innerHTML.length == 0 && this.options.emptyText) {
  49. this.element.appendChild(
  50. new Element('span', { className : this.options.emptyClassName }).update(this.options.emptyText)
  51. );
  52. }
  53. },
  54. getText : function($super) {
  55. if (empty_span = this.element.select('.' + this.options.emptyClassName).first()) {
  56. empty_span.remove();
  57. }
  58. return $super();
  59. },
  60. onComplete : function($super, transport) {
  61. this.checkEmpty();
  62. return $super(transport);
  63. },
  64. wrapUp: function(transport) {
  65. this.leaveEditMode();
  66. this.checkEmpty();
  67. // Can't use triggerCallback due to backward compatibility: requires
  68. // binding + direct element
  69. this._boundComplete(transport, this.element);
  70. }
  71. });
  72. }