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.

93 lines
3.6 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-search", ["uikit"], function(){
  5. return jQuery.UIkit.search || 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. "use strict";
  13. var Search = function(element, options) {
  14. var $element = $(element), $this = this;
  15. if ($element.data("search")) return;
  16. this.autocomplete = new UI.autocomplete($element, $.extend({}, Search.defaults, options));
  17. this.autocomplete.dropdown.addClass('uk-dropdown-search');
  18. this.autocomplete.input.on("keyup", function(){
  19. $element[this.value ? "addClass":"removeClass"]("uk-active");
  20. }).closest("form").on("reset", function(){
  21. this.value="";
  22. $element.removeClass("uk-active");
  23. });
  24. $element.on('autocomplete-select', function(e, data) {
  25. if (data.url) {
  26. location.href = data.url;
  27. } else if(data.moreresults) {
  28. $this.autocomplete.input.closest('form').submit();
  29. }
  30. });
  31. $element.data("search", this);
  32. };
  33. Search.defaults = {
  34. msgResultsHeader : 'Search Results',
  35. msgMoreResults : 'More Results',
  36. msgNoResults : 'No results found',
  37. template : '<ul class="uk-nav uk-nav-search uk-autocomplete-results">\
  38. {{#msgResultsHeader}}<li class="uk-nav-header uk-skip">{{msgResultsHeader}}</li>{{/msgResultsHeader}}\
  39. {{#items && items.length}}\
  40. {{~items}}\
  41. <li data-url="{{!$item.url}}">\
  42. <a href="{{!$item.url}}">\
  43. {{$item.title}}\
  44. {{#$item.text}}<div>{{{$item.text}}}</div>{{/$item.text}}\
  45. </a>\
  46. </li>\
  47. {{/items}}\
  48. {{#msgMoreResults}}\
  49. <li class="uk-nav-divider uk-skip"></li>\
  50. <li class="uk-search-moreresults" data-moreresults="true"><a href="javascript:jQuery(this).closest(\'form\').submit();">{{msgMoreResults}}</a></li>\
  51. {{/msgMoreResults}}\
  52. {{/end}}\
  53. {{^items.length}}\
  54. {{#msgNoResults}}<li class="uk-skip"><a>{{msgNoResults}}</a></li>{{/msgNoResults}}\
  55. {{/end}}\
  56. </ul>',
  57. renderer: function(data) {
  58. var $this = this, opts = this.options;
  59. this.dropdown.append(this.template({"items":data.results || [], "msgResultsHeader":opts.msgResultsHeader, "msgMoreResults": opts.msgMoreResults, "msgNoResults": opts.msgNoResults}));
  60. this.show();
  61. }
  62. };
  63. UI["search"] = Search;
  64. // init code
  65. $(document).on("focus.search.uikit", "[data-uk-search]", function(e) {
  66. var ele = $(this);
  67. if (!ele.data("search")) {
  68. var obj = new Search(ele, UI.Utils.options(ele.attr("data-uk-search")));
  69. }
  70. });
  71. return Search;
  72. });