dashSearchView.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. define([
  2. 'angular',
  3. 'jquery'
  4. ],
  5. function (angular, $) {
  6. 'use strict';
  7. angular
  8. .module('grafana.directives')
  9. .directive('dashSearchView', function($compile, $timeout) {
  10. return {
  11. restrict: 'A',
  12. link: function(scope, elem) {
  13. var editorScope;
  14. function hookUpHideWhenClickedOutside() {
  15. $timeout(function() {
  16. $(document).bind('click.hide-search', function(evt) {
  17. // some items can be inside container
  18. // but then removed
  19. if ($(evt.target).parents().length === 0) {
  20. return;
  21. }
  22. if ($(evt.target).parents('.search-container').length === 0) {
  23. if (editorScope) {
  24. editorScope.dismiss();
  25. }
  26. }
  27. });
  28. });
  29. }
  30. function showSearch() {
  31. if (editorScope) {
  32. editorScope.dismiss();
  33. return;
  34. }
  35. editorScope = scope.$new();
  36. editorScope.dismiss = function() {
  37. editorScope.$destroy();
  38. elem.empty();
  39. elem.unbind();
  40. editorScope = null;
  41. $(document).unbind('click.hide-search');
  42. };
  43. var view = $('<div class="search-container" ng-include="\'app/partials/search.html\'"></div>');
  44. elem.append(view);
  45. $compile(elem.contents())(editorScope);
  46. hookUpHideWhenClickedOutside();
  47. }
  48. function hideSearch() {
  49. if (editorScope) {
  50. editorScope.dismiss();
  51. }
  52. }
  53. scope.onAppEvent('show-dash-search', showSearch);
  54. scope.onAppEvent('hide-dash-search', hideSearch);
  55. }
  56. };
  57. });
  58. });