forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualize.js
200 lines (161 loc) · 5.76 KB
/
visualize.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import 'ui/visualize/spy';
import 'ui/visualize/visualize.less';
import 'ui/visualize/visualize_legend';
import $ from 'jquery';
import _ from 'lodash';
import RegistryVisTypesProvider from 'ui/registry/vis_types';
import uiModules from 'ui/modules';
import visualizeTemplate from 'ui/visualize/visualize.html';
import 'angular-sanitize';
import {
isTermSizeZeroError,
} from '../elasticsearch_errors';
uiModules
.get('kibana/directive', ['ngSanitize'])
.directive('visualize', function (Notifier, SavedVis, indexPatterns, Private, config, $timeout) {
let visTypes = Private(RegistryVisTypesProvider);
let notify = new Notifier({
location: 'Visualize'
});
return {
restrict: 'E',
scope : {
showSpyPanel: '=?',
vis: '=',
uiState: '=?',
searchSource: '=?',
editableVis: '=?',
esResp: '=?',
},
template: visualizeTemplate,
link: function ($scope, $el, attr) {
const minVisChartHeight = 180;
if (_.isUndefined($scope.showSpyPanel)) {
$scope.showSpyPanel = true;
}
function getter(selector) {
return function () {
let $sel = $el.find(selector);
if ($sel.size()) return $sel;
};
}
let getVisEl = getter('.visualize-chart');
let getVisContainer = getter('.vis-container');
// Show no results message when isZeroHits is true and it requires search
$scope.showNoResultsMessage = function () {
let requiresSearch = _.get($scope, 'vis.type.requiresSearch');
let isZeroHits = _.get($scope,'esResp.hits.total') === 0;
let shouldShowMessage = !_.get($scope, 'vis.params.handleNoResults');
return Boolean(requiresSearch && isZeroHits && shouldShowMessage);
};
const legendPositionToVisContainerClassMap = {
top: 'vis-container--legend-top',
bottom: 'vis-container--legend-bottom',
left: 'vis-container--legend-left',
right: 'vis-container--legend-right',
};
$scope.getVisContainerClasses = function () {
return legendPositionToVisContainerClassMap[$scope.vis.params.legendPosition];
};
$scope.spy = {};
$scope.spy.mode = ($scope.uiState) ? $scope.uiState.get('spy.mode', {}) : {};
let applyClassNames = function () {
let $visEl = getVisContainer();
const $spyEl = getter('.visualize-spy-container')();
let fullSpy = ($scope.spy.mode && ($scope.spy.mode.fill || $scope.fullScreenSpy));
$visEl.toggleClass('spy-only', Boolean(fullSpy));
if ($spyEl) {
$spyEl.toggleClass('only', Boolean(fullSpy));
}
$timeout(function () {
if (shouldHaveFullSpy()) {
$visEl.addClass('spy-only');
if ($spyEl) {
$spyEl.addClass('only');
}
};
}, 0);
};
// we need to wait for some watchers to fire at least once
// before we are "ready", this manages that
let prereq = (function () {
let fns = [];
return function register(fn) {
fns.push(fn);
return function () {
fn.apply(this, arguments);
if (fns.length) {
_.pull(fns, fn);
if (!fns.length) {
$scope.$root.$broadcast('ready:vis');
}
}
};
};
}());
let loadingDelay = config.get('visualization:loadingDelay');
$scope.loadingStyle = {
'-webkit-transition-delay': loadingDelay,
'transition-delay': loadingDelay
};
function shouldHaveFullSpy() {
let $visEl = getVisEl();
if (!$visEl) return;
return ($visEl.height() < minVisChartHeight)
&& _.get($scope.spy, 'mode.fill')
&& _.get($scope.spy, 'mode.name');
}
// spy watchers
$scope.$watch('fullScreenSpy', applyClassNames);
$scope.$watchCollection('spy.mode', function () {
$scope.fullScreenSpy = shouldHaveFullSpy();
applyClassNames();
});
$scope.$watch('vis', prereq(function (vis, oldVis) {
let $visEl = getVisEl();
if (!$visEl) return;
if (!attr.editableVis) {
$scope.editableVis = vis;
}
if (oldVis) $scope.renderbot = null;
if (vis) $scope.renderbot = vis.type.createRenderbot(vis, $visEl, $scope.uiState);
}));
$scope.$watchCollection('vis.params', prereq(function () {
if ($scope.renderbot) $scope.renderbot.updateParams();
}));
$scope.$watch('searchSource', prereq(function (searchSource) {
if (!searchSource || attr.esResp) return;
// TODO: we need to have some way to clean up result requests
searchSource.onResults().then(function onResults(resp) {
if ($scope.searchSource !== searchSource) return;
$scope.esResp = resp;
return searchSource.onResults().then(onResults);
}).catch(notify.fatal);
searchSource.onError(e => {
if (isTermSizeZeroError(e)) {
return notify.error(
`Your visualization ('${$scope.vis.title}') has an error: it has a term ` +
`aggregation with a size of 0. Please set it to a number greater than 0 to resolve ` +
`the error.`
);
}
notify.error(e);
}).catch(notify.fatal);
}));
$scope.$watch('esResp', prereq(function (resp, prevResp) {
if (!resp) return;
$scope.renderbot.render(resp);
}));
$scope.$watch('renderbot', function (newRenderbot, oldRenderbot) {
if (oldRenderbot && newRenderbot !== oldRenderbot) {
oldRenderbot.destroy();
}
});
$scope.$on('$destroy', function () {
if ($scope.renderbot) {
$scope.renderbot.destroy();
}
});
}
};
});