/modules/mod_base/lib/js/modules/z.feedback.js

https://code.google.com/p/zotonic/ · JavaScript · 95 lines · 65 code · 6 blank · 24 comment · 21 complexity · d90aca46570cb9e4a6099e1887a9b8a1 MD5 · raw file

  1. /* feedback js
  2. ----------------------------------------------------------
  3. @package: Zotonic 2011
  4. @Author: Marc Worrell <marc@worrell.nl>
  5. Copyright 2011 Marc Worrell
  6. Licensed under the Apache License, Version 2.0 (the "License");
  7. you may not use this file except in compliance with the License.
  8. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. ---------------------------------------------------------- */
  16. $.widget("ui.feedback",
  17. {
  18. _init: function()
  19. {
  20. var self = this;
  21. z_ensure_id(this.element);
  22. $('#'+this.options.trigger)
  23. .bind('keyup', function() { self.update(); })
  24. .bind('change', function() { self.update(); });
  25. },
  26. update: function()
  27. {
  28. if (this.input_updater != undefined)
  29. {
  30. clearInterval(this.input_updater);
  31. this.input_updater = undefined;
  32. }
  33. var self = this;
  34. this.input_updater = setInterval(function()
  35. {
  36. // Fetch the update
  37. var args = [];
  38. var trigger = $('#'+self.options.trigger);
  39. if (trigger.length == 0) {
  40. clearInterval(self.input_updater);
  41. self.input_updater = undefined;
  42. } else {
  43. if (trigger.prop('tagName').toLowerCase() == 'form') {
  44. args = trigger.formToArray();
  45. } else {
  46. var v = $.fieldValue(trigger, true);
  47. if (v && v.constructor == Array) {
  48. for(var j=0, jmax=v.length; j < jmax; j++)
  49. args.push({name: "triggervalue", value: v[j]});
  50. }
  51. else if (v !== null && typeof v != 'undefined')
  52. args.push({name: "triggervalue", value: v});
  53. }
  54. // Stop when there is an 'in-flight' update with the same args
  55. if (self.element.hasClass('loading')) {
  56. if (self.last_args == $.param(args)) {
  57. clearInterval(self.input_updater);
  58. self.input_updater = undefined;
  59. }
  60. } else {
  61. self.element.addClass('loading');
  62. self.last_args = $.param(args);
  63. clearInterval(self.input_updater);
  64. // Form changed, post it to the server
  65. var notify_args = {};
  66. for (var i=0; i<args.length; i++) {
  67. notify_args[args[i].name] = args[i].value;
  68. }
  69. notify_args.z_trigger_id = self.options.trigger;
  70. notify_args.z_target_id = $(self.element).attr('id');
  71. notify_args.z_delegate = self.options.delegate;
  72. z_notify("feedback", notify_args);
  73. }
  74. }
  75. }, self.options.timeout);
  76. }
  77. });
  78. $.ui.feedback.defaults = {
  79. delegate: undefined,
  80. trigger: undefined,
  81. template: undefined,
  82. timeout: 400
  83. }