jquery.delayedinput.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. /*
  17. * jHue Delayed Input plugin
  18. * use it with
  19. * $("#element").jHueDelayedInput( __FUNCTION_YOU_WANT_TO_CALL__, __TIMEOUT_IN_MS__ [optional])
  20. */
  21. ;
  22. (function ($, window, document, undefined) {
  23. var pluginName = "jHueDelayedInput",
  24. defaults = {
  25. fn: null,
  26. timeout: 300
  27. };
  28. function Plugin(element, options) {
  29. this.element = element;
  30. this.options = $.extend({}, defaults, options);
  31. this._defaults = defaults;
  32. this._name = pluginName;
  33. this.init();
  34. }
  35. Plugin.prototype.setOptions = function (options) {
  36. this.options = $.extend({}, defaults, options);
  37. };
  38. Plugin.prototype.init = function () {
  39. var _this = this;
  40. var _timeout = -1;
  41. if (_this.options.fn != null) {
  42. $(_this.element).on("keyup", function () {
  43. window.clearTimeout(_timeout);
  44. _timeout = window.setTimeout(_this.options.fn, _this.options.timeout);
  45. });
  46. }
  47. };
  48. $.fn[pluginName] = function (fn, timeout) {
  49. var _options = {
  50. fn: fn,
  51. timeout: timeout
  52. }
  53. return this.each(function () {
  54. if (!$.data(this, 'plugin_' + pluginName)) {
  55. $.data(this, 'plugin_' + pluginName, new Plugin(this, _options));
  56. }
  57. else {
  58. $.data(this, 'plugin_' + pluginName).setOptions(_options);
  59. }
  60. });
  61. }
  62. })(jQuery, window, document);