main.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*!
  2. * jQuery.textcomplete
  3. *
  4. * Repository: https://github.com/yuku-t/jquery-textcomplete
  5. * License: MIT (https://github.com/yuku-t/jquery-textcomplete/blob/master/LICENSE)
  6. * Author: Yuku Takahashi
  7. */
  8. if (typeof jQuery === 'undefined') {
  9. throw new Error('jQuery.textcomplete requires jQuery');
  10. }
  11. +function ($) {
  12. 'use strict';
  13. var warn = function (message) {
  14. if (console.warn) { console.warn(message); }
  15. };
  16. var id = 1;
  17. $.fn.textcomplete = function (strategies, option) {
  18. var args = Array.prototype.slice.call(arguments);
  19. return this.each(function () {
  20. var self = this;
  21. var $this = $(this);
  22. var completer = $this.data('textComplete');
  23. if (!completer) {
  24. option || (option = {});
  25. option._oid = id++; // unique object id
  26. completer = new $.fn.textcomplete.Completer(this, option);
  27. $this.data('textComplete', completer);
  28. }
  29. if (typeof strategies === 'string') {
  30. if (!completer) return;
  31. args.shift()
  32. completer[strategies].apply(completer, args);
  33. if (strategies === 'destroy') {
  34. $this.removeData('textComplete');
  35. }
  36. } else {
  37. // For backward compatibility.
  38. // TODO: Remove at v0.4
  39. $.each(strategies, function (obj) {
  40. $.each(['header', 'footer', 'placement', 'maxCount'], function (name) {
  41. if (obj[name]) {
  42. completer.option[name] = obj[name];
  43. warn(name + 'as a strategy param is deprecated. Use option.');
  44. delete obj[name];
  45. }
  46. });
  47. });
  48. completer.register($.fn.textcomplete.Strategy.parse(strategies, {
  49. el: self,
  50. $el: $this
  51. }));
  52. }
  53. });
  54. };
  55. }(jQuery);