salix/front/core/components/json-value/index.js

57 lines
1.5 KiB
JavaScript

import ngModule from '../../module';
import Component from 'core/lib/component';
import './style.scss';
const maxStrLen = 25;
/**
* Displays pretty JSON value.
*
* @property {*} value The value
*/
export default class Controller extends Component {
get value() {
return this._value;
}
set value(value) {
this._value = value;
const span = this.element;
const formattedValue = this.formatValue(value);
span.textContent = formattedValue;
span.title = typeof value == 'string' && value.length > maxStrLen ? value : '';
span.className = `js-${value == null ? 'null' : typeof value}`;
}
formatValue(value) {
if (value == null) return '∅';
switch (typeof value) {
case 'boolean':
return value ? '✓' : '✗';
case 'string':
return value.length <= maxStrLen
? value
: value.substring(0, maxStrLen) + '...';
case 'object':
if (value instanceof Date) {
const hasZeroTime =
value.getHours() === 0 &&
value.getMinutes() === 0 &&
value.getSeconds() === 0;
const format = hasZeroTime ? 'dd/MM/yyyy' : 'dd/MM/yyyy HH:mm:ss';
return this.$filter('date')(value, format);
} else
return value;
default:
return value;
}
}
}
ngModule.vnComponent('vnJsonValue', {
controller: Controller,
bindings: {
value: '<?'
}
});