dashSearchView.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. scope.onAppEvent('show-dash-search', showSearch);
  49. }
  50. };
  51. });
  52. });