DotaNoobs main site.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

298 lines
8.5 KiB

  1. /*! UIkit 2.5.0 | http://www.getuikit.com | (c) 2014 YOOtheme | MIT License */
  2. (function(addon) {
  3. if (typeof define == "function" && define.amd) { // AMD
  4. define("uikit-autocomplete", ["uikit"], function(){
  5. return jQuery.UIkit.autocomplete || addon(window, window.jQuery, window.jQuery.UIkit);
  6. });
  7. }
  8. if(window && window.jQuery && window.jQuery.UIkit) {
  9. addon(window, window.jQuery, window.jQuery.UIkit);
  10. }
  11. })(function(global, $, UI){
  12. var Autocomplete = function(element, options) {
  13. var $this = this, $element = $(element);
  14. if($element.data("autocomplete")) return;
  15. this.options = $.extend({}, Autocomplete.defaults, options);
  16. this.element = $element;
  17. this.dropdown = $element.find('.uk-dropdown');
  18. this.template = $element.find('script[type="text/autocomplete"]').html();
  19. this.template = UI.Utils.template(this.template || this.options.template);
  20. this.input = $element.find("input:first").attr("autocomplete", "off");
  21. this.element.data("autocomplete", this);
  22. if (!this.dropdown.length) {
  23. this.dropdown = $('<div class="uk-dropdown"></div>').appendTo($element);
  24. }
  25. if (this.options.flipDropdown) {
  26. this.dropdown.addClass('uk-dropdown-flip');
  27. }
  28. this.init();
  29. };
  30. $.extend(Autocomplete.prototype, {
  31. visible : false,
  32. value : null,
  33. selected : null,
  34. init: function() {
  35. var $this = this,
  36. select = false,
  37. trigger = UI.Utils.debounce(function(e) {
  38. if(select) {
  39. return (select = false);
  40. }
  41. $this.trigger();
  42. }, this.options.delay);
  43. this.input.on({
  44. "keydown": function(e) {
  45. if (e && e.which && !e.shiftKey) {
  46. switch (e.which) {
  47. case 13: // enter
  48. e.preventDefault();
  49. select = true;
  50. $this.select();
  51. break;
  52. case 38: // up
  53. e.preventDefault();
  54. $this.pick('prev');
  55. break;
  56. case 40: // down
  57. e.preventDefault();
  58. $this.pick('next');
  59. break;
  60. case 27:
  61. case 9: // esc, tab
  62. $this.hide();
  63. break;
  64. default:
  65. break;
  66. }
  67. }
  68. },
  69. "keyup": trigger,
  70. "blur": function(e) {
  71. setTimeout(function() { $this.hide(); }, 200);
  72. }
  73. });
  74. this.dropdown.on("click", ".uk-autocomplete-results > *", function(){
  75. $this.select();
  76. });
  77. this.dropdown.on("mouseover", ".uk-autocomplete-results > *", function(){
  78. $this.pick($(this));
  79. });
  80. },
  81. trigger: function() {
  82. var $this = this, old = this.value;
  83. this.value = this.input.val();
  84. if (this.value.length < this.options.minLength) return this.hide();
  85. if (this.value != old) {
  86. $this.request();
  87. }
  88. return this;
  89. },
  90. pick: function(item) {
  91. var items = this.dropdown.find('.uk-autocomplete-results').children(':not(.'+this.options.skipClass+')'),
  92. selected = false;
  93. if (typeof item !== "string" && !item.hasClass(this.options.skipClass)) {
  94. selected = item;
  95. } else if (item == 'next' || item == 'prev') {
  96. if (this.selected) {
  97. var index = items.index(this.selected);
  98. if (item == 'next') {
  99. selected = items.eq(index + 1 < items.length ? index + 1 : 0);
  100. } else {
  101. selected = items.eq(index - 1 < 0 ? items.length - 1 : index - 1);
  102. }
  103. } else {
  104. selected = items[(item == 'next') ? 'first' : 'last']();
  105. }
  106. }
  107. if (selected && selected.length) {
  108. this.selected = selected;
  109. items.removeClass(this.options.hoverClass);
  110. this.selected.addClass(this.options.hoverClass);
  111. }
  112. },
  113. select: function() {
  114. if(!this.selected) return;
  115. var data = this.selected.data();
  116. this.element.trigger("autocomplete-select", [data, this]);
  117. if (data.value) {
  118. this.input.val(data.value);
  119. }
  120. this.hide();
  121. },
  122. show: function() {
  123. if (this.visible) return;
  124. this.visible = true;
  125. this.element.addClass("uk-open");
  126. return this;
  127. },
  128. hide: function() {
  129. if (!this.visible) return;
  130. this.visible = false;
  131. this.element.removeClass("uk-open");
  132. return this;
  133. },
  134. request: function() {
  135. var $this = this,
  136. release = function(data) {
  137. if(data) {
  138. $this.render(data);
  139. }
  140. $this.element.removeClass($this.options.loadingClass);
  141. };
  142. this.element.addClass(this.options.loadingClass);
  143. if (this.options.source) {
  144. var source = this.options.source;
  145. switch(typeof(this.options.source)) {
  146. case 'function':
  147. this.options.source.apply(this, [release]);
  148. break;
  149. case 'object':
  150. if(source.length) {
  151. var items = [];
  152. source.forEach(function(item){
  153. if(item.value && item.value.toLowerCase().indexOf($this.value.toLowerCase())!=-1) {
  154. items.push(item);
  155. }
  156. });
  157. release(items);
  158. }
  159. break;
  160. case 'string':
  161. var params ={};
  162. params[this.options.param] = this.value;
  163. $.ajax({
  164. url: this.options.source,
  165. data: params,
  166. type: this.options.method,
  167. dataType: 'json',
  168. complete: function(xhr) {
  169. release(xhr.responseJSON || []);
  170. }
  171. });
  172. break;
  173. default:
  174. release(null);
  175. }
  176. } else {
  177. this.element.removeClass($this.options.loadingClass);
  178. }
  179. },
  180. render: function(data) {
  181. var $this = this;
  182. this.dropdown.empty();
  183. this.selected = false;
  184. if (this.options.renderer) {
  185. this.options.renderer.apply(this, [data]);
  186. } else if(data && data.length) {
  187. this.dropdown.append(this.template({"items":data}));
  188. this.show();
  189. }
  190. return this;
  191. }
  192. });
  193. Autocomplete.defaults = {
  194. minLength: 3,
  195. param: 'search',
  196. method: 'post',
  197. delay: 300,
  198. loadingClass: 'uk-loading',
  199. flipDropdown: false,
  200. skipClass: 'uk-skip',
  201. hoverClass: 'uk-active',
  202. source: null,
  203. renderer: null,
  204. // template
  205. template: '<ul class="uk-nav uk-nav-autocomplete uk-autocomplete-results">{{~items}}<li data-value="{{$item.value}}"><a>{{$item.value}}</a></li>{{/items}}</ul>'
  206. };
  207. UI["autocomplete"] = Autocomplete;
  208. // init code
  209. $(document).on("focus.autocomplete.uikit", "[data-uk-autocomplete]", function(e) {
  210. var ele = $(this);
  211. if (!ele.data("autocomplete")) {
  212. var obj = new Autocomplete(ele, UI.Utils.options(ele.attr("data-uk-autocomplete")));
  213. }
  214. });
  215. return Autocomplete;
  216. });