Search.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. 'use strict';
  2. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. /**
  5. * --------------------------------------------
  6. * AdminLTE Search.js
  7. * License MIT
  8. * --------------------------------------------
  9. */
  10. var Search = function ($) {
  11. /**
  12. * Constants
  13. * ====================================================
  14. */
  15. var NAME = 'Search';
  16. var DATA_KEY = 'lte.search';
  17. var EVENT_KEY = '.' + DATA_KEY;
  18. var JQUERY_NO_CONFLICT = $.fn[NAME];
  19. var Event = {
  20. LOAD_DATA_API: 'load' + EVENT_KEY
  21. };
  22. var Selector = {
  23. LI: '.nav-item',
  24. LINK: '.nav-link',
  25. OPEN: '.menu-open',
  26. ACTIVE: '.active',
  27. TREEVIEW_MENU: '[data-widget="treeview"]',
  28. NAV_TREEVIEW: '.nav-treeview',
  29. DATA_WIDGET: '[data-widget="search"]'
  30. };
  31. var ClassName = {
  32. LI: 'nav-item',
  33. LINK: 'nav-link',
  34. NAV_TREEVIEW: 'nav-treeview',
  35. OPEN: 'menu-open'
  36. };
  37. var Default = {
  38. target: '',
  39. case_sensitive: false
  40. };
  41. /**
  42. * Class Definition
  43. * ====================================================
  44. */
  45. var Search = function () {
  46. function Search(element, config) {
  47. _classCallCheck(this, Search);
  48. this._config = config;
  49. this._element = element;
  50. this._open_menus = null;
  51. }
  52. // Public
  53. _createClass(Search, [{
  54. key: 'init',
  55. value: function init() {
  56. if (this._config.target === '') {
  57. this._config.target = this._element.closest(Selector.TREEVIEW_MENU);
  58. } else {
  59. this._config.target = $(this._config.target);
  60. }
  61. // Set treeview original state
  62. this._open_menus = this._config.target.find(Selector.OPEN);
  63. // Prevent form submission
  64. this._element.parents('form').first().submit(function (event) {
  65. event.preventDefault();
  66. });
  67. // Setup search function
  68. this._element.keyup(function (event) {
  69. event.preventDefault();
  70. var value = $(event.currentTarget).val();
  71. if (!this._config.case_sensitive) {
  72. value = value.toLowerCase();
  73. }
  74. this.search(value);
  75. }.bind(this));
  76. }
  77. }, {
  78. key: 'search',
  79. value: function search(value) {
  80. var _that = this;
  81. // If the value is back to null
  82. if (!value) {
  83. // Close all treeviews
  84. this._config.target.find(Selector.LI).css('display', 'block').find(Selector.NAV_TREEVIEW).css('display', 'none');
  85. // Open the originally opened treeviews
  86. this._config.target.find(Selector.OPEN).find(Selector.NAV_TREEVIEW).css('display', 'block');
  87. this._open_menus.each(function () {
  88. if (!$(this).hasClass(ClassName.OPEN)) {
  89. $(this).addClass(Selector.OPEN).css('display', 'block');
  90. $(this).children(Selector.NAV_TREEVIEW).css('display', 'block');
  91. }
  92. });
  93. return;
  94. }
  95. // Search through the tree elements
  96. this._config.target.find(Selector.LI).each(function () {
  97. var text = $(this).children(Selector.LINK).first().text();
  98. if (!_that._config.case_sensitive) {
  99. text = text.toLowerCase();
  100. }
  101. if (text.search(value) == -1) {
  102. // No results
  103. $(this).css('display', 'none');
  104. } else {
  105. // Found the result
  106. // Make the parent LI visible
  107. $(this).parents(Selector.LI).css('display', 'block');
  108. // Make the parent nav-treeview visible
  109. $(this).parents(Selector.NAV_TREEVIEW).addClass('menu-open').css('display', 'block');
  110. // Make this element visible
  111. $(this).css('display', 'block');
  112. }
  113. });
  114. }
  115. // Static
  116. }], [{
  117. key: '_jQueryInterface',
  118. value: function _jQueryInterface(config) {
  119. return this.each(function () {
  120. var data = $(this).data(DATA_KEY);
  121. var _config = $.extend({}, Default, $(this).data());
  122. if (!data) {
  123. data = new Search($(this), _config);
  124. $(this).data(DATA_KEY, data);
  125. }
  126. if (config === 'init') {
  127. data[config]();
  128. }
  129. });
  130. }
  131. }]);
  132. return Search;
  133. }();
  134. /**
  135. * Data API
  136. * ====================================================
  137. */
  138. $(window).on(Event.LOAD_DATA_API, function () {
  139. $(Selector.DATA_WIDGET).each(function () {
  140. Search._jQueryInterface.call($(this), 'init');
  141. });
  142. });
  143. /**
  144. * jQuery API
  145. * ====================================================
  146. */
  147. $.fn[NAME] = Search._jQueryInterface;
  148. $.fn[NAME].Constructor = Search;
  149. $.fn[NAME].noConflict = function () {
  150. $.fn[NAME] = JQUERY_NO_CONFLICT;
  151. return Search._jQueryInterface;
  152. };
  153. return Search;
  154. }(jQuery);
  155. //# sourceMappingURL=Search.js.map