Skip to content

Commit b8323f0

Browse files
committed
[FIX] Fixed rendering in custom dashbaords
1 parent 808ca0e commit b8323f0

1 file changed

Lines changed: 68 additions & 19 deletions

File tree

source/app/static/assets/js/iris/custom_dashboards/custom_dashboards.js

Lines changed: 68 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,17 @@
351351
operator: 'eq',
352352
value: ''
353353
};
354-
return Object.assign(defaults, overrides);
354+
const filter = Object.assign({}, defaults, overrides);
355+
if (Array.isArray(filter.value)) {
356+
filter.value = filter.value.slice();
357+
} else if (filter.value && typeof filter.value === 'object' && !(filter.value instanceof Date)) {
358+
try {
359+
filter.value = JSON.parse(JSON.stringify(filter.value));
360+
} catch (err) {
361+
filter.value = Object.assign({}, filter.value);
362+
}
363+
}
364+
return filter;
355365
}
356366

357367
function createEmptyCustomOption(overrides = {}) {
@@ -360,7 +370,17 @@
360370
key: '',
361371
value: ''
362372
};
363-
return Object.assign(defaults, overrides);
373+
const option = Object.assign({}, defaults, overrides);
374+
if (Array.isArray(option.value)) {
375+
option.value = option.value.slice();
376+
} else if (option.value && typeof option.value === 'object' && !(option.value instanceof Date)) {
377+
try {
378+
option.value = JSON.parse(JSON.stringify(option.value));
379+
} catch (err) {
380+
option.value = Object.assign({}, option.value);
381+
}
382+
}
383+
return option;
364384
}
365385

366386
function createEmptyWidget(overrides = {}) {
@@ -463,7 +483,7 @@
463483
table: filter.table,
464484
column: filter.column,
465485
operator: filter.operator,
466-
value: filter.value
486+
value: coerceFilterValue(filter.value, filter.operator)
467487
}))
468488
: [];
469489

@@ -516,22 +536,36 @@
516536
return value;
517537
}
518538

519-
function coerceFilterValue(rawValue) {
539+
function coerceFilterValue(rawValue, operator) {
520540
if (rawValue === undefined || rawValue === null) {
521541
return null;
522542
}
523543
if (typeof rawValue === 'number' || typeof rawValue === 'boolean') {
524544
return rawValue;
525545
}
546+
if (Array.isArray(rawValue)) {
547+
return rawValue.slice();
548+
}
549+
if (rawValue instanceof Date) {
550+
return new Date(rawValue.getTime());
551+
}
552+
if (typeof rawValue === 'object') {
553+
try {
554+
return JSON.parse(JSON.stringify(rawValue));
555+
} catch (err) {
556+
return Object.assign({}, rawValue);
557+
}
558+
}
526559
const value = String(rawValue).trim();
527560
if (!value) {
528561
return '';
529562
}
530563
if (value === 'true' || value === 'false') {
531564
return value === 'true';
532565
}
533-
if (!Number.isNaN(Number(value))) {
534-
return Number(value);
566+
const numericValue = Number(value);
567+
if (!Number.isNaN(numericValue) && String(numericValue) === value) {
568+
return numericValue;
535569
}
536570
if ((value.startsWith('[') && value.endsWith(']')) || (value.startsWith('{') && value.endsWith('}'))) {
537571
try {
@@ -540,27 +574,34 @@
540574
return value;
541575
}
542576
}
577+
if ((operator === 'in' || operator === 'nin') && value.indexOf(',') !== -1) {
578+
const parts = value.split(',').map((item) => item.trim()).filter((item) => item !== '');
579+
if (parts.length > 1) {
580+
return parts;
581+
}
582+
}
543583
return value;
544584
}
545585

546-
function formatValueForInput(rawValue) {
547-
if (rawValue === undefined || rawValue === null) {
586+
function formatValueForInput(rawValue, operator) {
587+
const normalized = coerceFilterValue(rawValue, operator);
588+
if (normalized === undefined || normalized === null) {
548589
return '';
549590
}
550-
if (typeof rawValue === 'string') {
551-
return rawValue;
591+
if (typeof normalized === 'string') {
592+
return normalized;
552593
}
553-
if (rawValue instanceof Date) {
554-
return rawValue.toISOString();
594+
if (normalized instanceof Date) {
595+
return normalized.toISOString();
555596
}
556-
if (Array.isArray(rawValue) || (typeof rawValue === 'object' && rawValue !== null)) {
597+
if (Array.isArray(normalized) || (typeof normalized === 'object' && normalized !== null)) {
557598
try {
558-
return JSON.stringify(rawValue);
599+
return JSON.stringify(normalized);
559600
} catch (err) {
560-
return String(rawValue);
601+
return String(normalized);
561602
}
562603
}
563-
return String(rawValue);
604+
return String(normalized);
564605
}
565606

566607
function coerceCustomOptionValue(rawValue) {
@@ -609,7 +650,7 @@
609650
table: filter.table,
610651
column: filter.column,
611652
operator: filter.operator,
612-
value: coerceFilterValue(filter.value)
653+
value: coerceFilterValue(filter.value, filter.operator)
613654
})),
614655
group_by: widget.groupBy.map((group) => formatTableColumn(group.table, group.column)).filter((value) => value && value.indexOf('.') !== -1),
615656
time_bucket: widget.timeBucket ? widget.timeBucket : undefined,
@@ -1157,7 +1198,7 @@
11571198
valueInput.attr('data-section-id', sectionId);
11581199
valueInput.attr('data-widget-id', widgetId);
11591200
valueInput.attr('data-filter-id', filter.id);
1160-
valueInput.val(formatValueForInput(filter.value));
1201+
valueInput.val(formatValueForInput(filter.value, filter.operator));
11611202
valueGroup.append(valueInput);
11621203

11631204
const removeGroup = $('<div class="form-group col-lg-1 text-right"></div>');
@@ -2139,6 +2180,8 @@
21392180
return;
21402181
}
21412182
filter.operator = $(this).val();
2183+
filter.value = coerceFilterValue(filter.value, filter.operator);
2184+
rerenderWidget(sectionId, widgetId);
21422185
});
21432186

21442187
$(document).on('input', '.builder-filter-value', function () {
@@ -2149,7 +2192,13 @@
21492192
if (!filter) {
21502193
return;
21512194
}
2152-
filter.value = $(this).val();
2195+
filter.value = coerceFilterValue($(this).val(), filter.operator);
2196+
});
2197+
2198+
$(document).on('blur', '.builder-filter-value', function () {
2199+
const sectionId = $(this).data('section-id');
2200+
const widgetId = $(this).data('widget-id');
2201+
rerenderWidget(sectionId, widgetId);
21532202
});
21542203

21552204
$(document).on('change', '.builder-option-display', function () {

0 commit comments

Comments
 (0)