/
row.js
162 lines (131 loc) · 5.42 KB
/
row.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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
// copy of ui/public/directives/row.js
// overridden to add the option for row expansion.
import $ from 'jquery';
import _ from 'lodash';
import AggConfigResult from 'ui/vis/agg_config_result';
import { FilterBarClickHandlerProvider } from 'ui/filter_bar/filter_bar_click_handler';
import { uiModules } from 'ui/modules';
const module = uiModules.get('apps/ml');
module.directive('mlRows', function ($compile, getAppState, Private) {
const filterBarClickHandler = Private(FilterBarClickHandlerProvider);
return {
restrict: 'A',
link: function ($scope, $el, attr) {
function addCell($tr, contents) {
let $cell = $(document.createElement('td'));
// TODO: It would be better to actually check the type of the field, but we don't have
// access to it here. This may become a problem with the switch to BigNumber
if (_.isNumber(contents)) {
$cell.addClass('numeric-value');
}
const createAggConfigResultCell = function (aggConfigResult) {
const $resultCell = $(document.createElement('td'));
const $state = getAppState();
const clickHandler = filterBarClickHandler($state);
$resultCell.scope = $scope.$new();
$resultCell.addClass('cell-hover');
$resultCell.attr('ng-click', 'clickHandler($event)');
$resultCell.scope.clickHandler = function (event) {
if ($(event.target).is('a')) return; // Don't add filter if a link was clicked
clickHandler({ point: { aggConfigResult: aggConfigResult } });
};
return $compile($resultCell)($cell.scope);
};
if (contents instanceof AggConfigResult) {
if (contents.type === 'bucket' && contents.aggConfig.field() && contents.aggConfig.field().filterable) {
$cell = createAggConfigResultCell(contents);
}
contents = contents.toString('html');
}
if (_.isObject(contents)) {
if (contents.attr) {
$cell.attr(contents.attr);
}
if (contents.class) {
$cell.addClass(contents.class);
}
if (contents.scope) {
$cell = $compile($cell.html(contents.markup))(contents.scope);
} else {
$cell.html(contents.markup);
}
} else {
if (contents === '') {
$cell.html(' ');
} else {
$cell.html(contents);
}
}
$tr.append($cell);
}
$scope.$watchMulti([
attr.mlRows,
attr.mlRowsMin
], function (vals) {
let rows = vals[0];
const min = vals[1];
$el.empty();
if (!_.isArray(rows)) {
rows = [];
}
if (isFinite(min) && rows.length < min) {
// clone the rows so that we can add elements to it without upsetting the original
rows = _.clone(rows);
}
rows.forEach(function (row) {
if (row.length) {
const rowScope = row[0].scope;
const $tr = $(document.createElement('tr')).appendTo($el);
if (rowScope &&
rowScope.mouseenterRow !== undefined && typeof rowScope.mouseenterRow === 'function') {
// Add mousenter and mouseleave events to the row
$tr.attr({ 'ng-mouseenter': 'mouseenterRow($event)' });
$tr.attr({ 'ng-mouseleave': 'mouseleaveRow($event)' });
$compile($tr)(rowScope);
}
row.forEach(function (cell) {
addCell($tr, cell);
});
if (rowScope &&
rowScope.expandable &&
rowScope.expandElement && // the tag name of the element which contains the expanded row's contents
row.join('') !== '') { // empty rows are passed in as an array of empty cols, i.e., ['','','']
if (rowScope.open === undefined) {
rowScope.open = false;
}
if (rowScope.rowInitialised === undefined) {
rowScope.rowInitialised = false;
}
rowScope.toggleRow = function () {
this.open = !this.open;
if (this.initRow && this.rowInitialised === false) {
this.rowInitialised = true;
this.initRow();
}
};
const $trExpand = $(document.createElement('tr')).appendTo($el);
$trExpand.attr('ng-show', 'open');
$trExpand.addClass('row-expand');
const $td = $(document.createElement('td')).appendTo($trExpand);
$td.attr('colspan', row.length);
const expEl = rowScope.expandElement;
const $exp = $(document.createElement(expEl)).appendTo($td);
// if expand element already exits and has child elements,
// copy them to the new expand element
if (rowScope.$expandElement && rowScope.$expandElement.children().length) {
$exp.append(rowScope.$expandElement.children()[0]);
}
$compile($trExpand)(rowScope);
rowScope.$expandElement = $exp;
}
}
});
});
}
};
});