/
object.js
265 lines (210 loc) · 8.77 KB
/
object.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import { getHighlightHtml } from '../../common/highlight/index';
import _ from 'lodash';
import './editor/object.less';
import objectTemplate from './editor/object.html';
import format_html from './templates/object_format.html';
import image_html from './templates/object_image.html';
import link_html from './templates/object_link.html';
import text_html from './templates/object_text.html';
import empty_html from './templates/object_empty.html';
import { lodashOopMixin } from '../../common/lodash-mixins/oop';
import { lodashGetPluckMixin } from '../../common/lodash-mixins/get_pluck';
lodashOopMixin(_);
lodashGetPluckMixin(_);
const vis_template = _.template(format_html);
const image_template = _.template(image_html);
const link_template = _.template(link_html);
const text_template = _.template(text_html);
const empty_template = _.template(empty_html);
const DEFAULT_VALUES = {
label: null, // Optional data label
path: null, // Dot notated location of the value within the object, relative to basePath
type: 'text',
filtered: true, // To enable the filtering on cell click
filterField: null, // If the data is analyzed, and there is a keyword subfield we can use for the filter
height: null, // Image dimension in px
width: null, // Image dimension in px
limit: null // If presenting an array, this is the max we will show
};
export function createObjectFormat(FieldFormat) {
class ObjectFormat extends FieldFormat {
constructor(params) {
super(params);
}
static id = 'ist-object';
static title = 'Object';
static fieldType = [
'string',
'unknown'
];
getParamDefaults() {
return {
fieldType: null, // populated by editor, see controller
basePath: null, // If multiple fields should be grouped, this is the common parent
limit: null, // // If basePath is an array, this is the max we will show
fields: [_.cloneDeep(DEFAULT_VALUES)]
};
}
_getFieldModels(value, field, hit, basePath, objectFields) {
let filtered = false;
const fields = [];
// Apply each field configured for the formatter to the value
_.forEach(objectFields, _.bind(function (objectField) {
let label = '';
let fieldPath = '';
if (objectField.label) label = objectField.label + ': ';
if (objectField.path) fieldPath = objectField.path;
if (objectField.filtered) filtered = objectField.filtered;
// Get the value from the field path
let fieldValues = _.getPluck(value, fieldPath);
if (objectField.type === 'text') {
// We generate a nice comma delimited list, like the built in String does.
if (_.isArray(fieldValues)) {
if (objectField.limit) {
fieldValues = _.slice(fieldValues, 0, objectField.limit);
}
fieldValues = _(fieldValues)
.map(item => (_.isObject(item) ? JSON.stringify(item) : item))
.join(', ');
}
else if (_.isObject(fieldValues)) {
fieldValues = JSON.stringify(fieldValues);
}
}
if (!_.isArray(fieldValues)) {
fieldValues = [fieldValues];
}
// If we have a limit on lists, impose it now
if (objectField.limit) {
fieldValues = _.slice(fieldValues, 0, objectField.limit);
}
const fullPath = this._getFullPath(basePath, field, objectField.path, null);
const filterPath = this._getFullPath(basePath, field, objectField.path, objectField.filterField);
const valueModels = [];
_.forEach(fieldValues, _.bind(function (fieldValue) {
const valueModel = {
value: fieldValue,
display: _.escape(fieldValue)
};
if (hit && hit.highlight && hit.highlight[fullPath]) {
valueModel.display = getHighlightHtml(valueModel.display, hit.highlight[fullPath]);
}
else if (hit && hit.highlight && hit.highlight[filterPath]) {
valueModel.display = getHighlightHtml(valueModel.display, hit.highlight[filterPath]);
}
valueModels.push(valueModel);
}, this));
const fieldHtml = this._fieldToHtml({
label: label,
formatType: objectField.type,
values: valueModels,
width: objectField.width,
height: objectField.height,
filterField: objectField.filterField
});
fields.push({
formatType: objectField.type,
label: label,
html: fieldHtml
});
}, this));
return { filtered: filtered, fields: fields };
}
_fieldToHtml(fieldModel) {
let html = null;
switch (fieldModel.formatType) {
case 'image':
html = image_template({ field: fieldModel });
break;
case 'link':
html = link_template({ field: fieldModel });
break;
case 'text':
default:
html = text_template({ field: fieldModel });
break;
}
return html;
}
_getFullPath(basePath, field, valuePath, filterField) {
const parts = [field.name];
if (basePath) {
parts.push(basePath);
}
parts.push(valuePath);
if (filterField) {
parts.push(filterField);
}
return parts.join('.');
}
asPrettyString(val) {
if (val === null || val === undefined) return ' - ';
switch (typeof val) {
case 'string': return val;
case 'object': return JSON.stringify(val, null, ' ');
default: return '' + val;
}
}
}
ObjectFormat.prototype._convert = {
text(val) {
return this.asPrettyString(val);
},
html(val, field, hit) {
const basePath = this.param('basePath');
const objectFields = this.param('fields');
const limit = this.param('limit');
if (basePath) {
val = _.get(val, basePath);
}
if (!_.isArray(val)) {
val = [val];
}
// Filter out any null or empty entries
val = $.grep(val, function (n) { return n === 0 || n });
// If we have a limit on this list, impose it now
if (limit) {
val = _.slice(val, 0, limit);
}
if (val.length > 0) {
const htmlSnippets = [];
_.forEach(val, _.bind(function (value) {
if (value) {
const fieldModels = this._getFieldModels(value, field, hit, basePath, objectFields);
htmlSnippets.push(vis_template({ filtered: fieldModels.filtered,
fields: fieldModels.fields,
uid: Math.floor((Math.random() * 1000000) + 1) }));
}
}, this));
return htmlSnippets.join('\n');
}
else {
return empty_template();
}
}
};
return ObjectFormat;
}
export function objectEditor() {
return {
formatId: 'ist-object',
template: objectTemplate,
controllerAs: 'object',
controller: function ($scope) {
this.formatTypes = [
{ id: 'text', name: 'Text' },
{ id: 'link', name: 'Link' },
{ id: 'image', name: 'Image' }
];
$scope.$watch('editor.field.type', type => {
$scope.editor.formatParams.fieldType = type;
});
$scope.addField = function () {
$scope.editor.formatParams.fields.push(_.cloneDeep(DEFAULT_VALUES));
};
$scope.removeField = function (index) {
$scope.editor.formatParams.fields.splice(index, 1);
};
}
};
}