mirror of
https://bitbucket.org/bellord13/kit.git
synced 2026-11-13 19:06:11 +03:00
164 lines
6.1 KiB
JavaScript
164 lines
6.1 KiB
JavaScript
/** @module logger */
|
|
__PluginManager__.register('Logger', function(module, exports, require, global){
|
|
|
|
var support = require('support');
|
|
|
|
var lightMap = [
|
|
{class: '', keys: []},
|
|
{class: 'text-danger', keys: []},
|
|
{class: 'text-secondary', keys: []},
|
|
];
|
|
var getLightClass = function(key) {
|
|
key = ''+key;
|
|
var addClass = '', ar;
|
|
for(var i=0; i < lightMap.length; i++){
|
|
ar = lightMap[i]['keys'];
|
|
addClass = ar.includes(key)?lightMap[i]['class']:addClass;
|
|
}
|
|
return addClass;
|
|
};
|
|
var collectionTemplater = '<div class="kit-logger kit-logger--fixed"></div>';
|
|
// function dump(value) {
|
|
// var rArray = [];
|
|
// if(value === null) {
|
|
// rArray[0] = 'null';
|
|
// } else {
|
|
// rArray[0] = String(typeof value);
|
|
// rArray[1] = emptyPHP(value)?'empty':'';
|
|
// }
|
|
// return rArray.join(' ');
|
|
// };
|
|
function getRowNode(level, key, value, opClass = ''){
|
|
if(opClass) {
|
|
opClass = ' kit-logger-row--'+opClass;
|
|
}
|
|
var i = 0, lvlTemplate = '';
|
|
key = String(key);
|
|
value = String(value);
|
|
lvlTemplate = level?'style="--logger-lvl:'+level+';" ':'';
|
|
var result = '<logger-item '+lvlTemplate+'class="'+getLightClass(key)+opClass+'"><logger-item__key>'+key+'</logger-item__key><logger-item__value>'+value+'</logger-item__value></logger-item>';
|
|
|
|
return support.makeNode(result);
|
|
}
|
|
function getItemObjectNode(id = ''){
|
|
var entity = '', css = '', style = '';
|
|
if(id) {
|
|
css = '<style>input#'+id+':checked ~ [data-entity-id="'+id+'"] {display: block !important;}</style>';
|
|
entity = ' data-entity-id="'+id+'"';
|
|
style = ' style="display: none"'
|
|
}
|
|
return support.makeNode('<logger-item-object'+entity+style+'>'+css+'</logger-item-object>');
|
|
}
|
|
|
|
function args2array(arguments) {
|
|
var args = [];
|
|
for (var i = 0; i < arguments.length; i++) {
|
|
args.push(arguments[i]);
|
|
}
|
|
return args;
|
|
}
|
|
|
|
/**
|
|
* @class Logger
|
|
* @memberOf kit
|
|
*/
|
|
var Logger = function() {
|
|
this.collection = undefined;
|
|
};
|
|
/**
|
|
* @param node {HTMLElement}
|
|
*/
|
|
Logger.prototype.setCollection = function (node) {
|
|
/**
|
|
* @type {HTMLElement}
|
|
*/
|
|
this.collection = node || document.getElementById('kit-logger') || document.querySelector('.kit-logger') || support.makeNode(collectionTemplater);
|
|
|
|
if((this.collection instanceof HTMLElement) && !this.collection.parentElement) {
|
|
document.body.appendChild(this.collection)
|
|
}
|
|
};
|
|
|
|
Logger.prototype.__row = function(message, lvl = 0, opClass = '') {
|
|
if (!this.collection) {
|
|
this.setCollection();
|
|
}
|
|
var log = this.collection;
|
|
|
|
function setObjectValue(log, lvl, key, value, opClass, depth = 3, openDepth = 1, isRoot = true) {
|
|
var rowChild, isObj = support.isObject(value), id = isObj?support.uniqueId():'',
|
|
val = (isObj&&depth)?'<label for="'+id+'">'+value+'</label>':value, inp, j,
|
|
len = isObj?Object.keys(value).length:0;
|
|
|
|
if(!isRoot || (isRoot && !isObj)) {
|
|
log.appendChild(getRowNode(lvl, key, '' + val + (isObj?(' ['+len+']'):''), opClass));
|
|
}
|
|
if(isObj&&depth) {
|
|
!isRoot && ++lvl;
|
|
depth = Math.max(0, depth - 1);
|
|
log.appendChild(inp = support.makeNode('<input id="'+id+'" type="checkbox" style="display: none;">'));
|
|
inp.checked = !!openDepth;
|
|
openDepth = Math.max(0, openDepth - 1);
|
|
log.appendChild(rowChild = getItemObjectNode(id));
|
|
for(j in value) {
|
|
if(value.hasOwnProperty(j))
|
|
setObjectValue(rowChild, lvl, j, value[j], opClass, depth, openDepth, false);
|
|
}
|
|
}
|
|
}
|
|
|
|
setObjectValue(log, lvl, '', message, opClass, 6, 3);
|
|
|
|
log.scrollTop = log.scrollHeight;
|
|
};
|
|
|
|
Logger.prototype.__log = function(lvl, type, message, context){
|
|
lvl = Number(lvl);
|
|
var opClass = '';
|
|
if(arguments.length >= 2) opClass = type;
|
|
if(arguments.length >= 3)
|
|
this.__row(message, lvl, opClass);
|
|
if(arguments.length >= 4)
|
|
this.__row(context, lvl+1);
|
|
};
|
|
|
|
Logger.prototype.emergency = function (message, context) {
|
|
this.__log.apply(this, [0, 'emergency'].concat(args2array(arguments)));
|
|
};
|
|
Logger.prototype.alert = function (message, context) {
|
|
this.__log.apply(this, [0, 'alert'].concat(args2array(arguments)));
|
|
};
|
|
Logger.prototype.critical = function (message, context) {
|
|
this.__log.apply(this, [0, 'critical'].concat(args2array(arguments)));
|
|
};
|
|
Logger.prototype.error = function (message, context) {
|
|
this.__log.apply(this, [0, 'error'].concat(args2array(arguments)));
|
|
};
|
|
Logger.prototype.warning = function (message, context) {
|
|
this.__log.apply(this, [0, 'warning'].concat(args2array(arguments)));
|
|
};
|
|
Logger.prototype.notice = function (message, context) {
|
|
this.__log.apply(this, [0, 'notice'].concat(args2array(arguments)));
|
|
};
|
|
Logger.prototype.info = function (message, context) {
|
|
this.__log.apply(this, [0, 'info'].concat(args2array(arguments)));
|
|
};
|
|
Logger.prototype.debug = function (message, context) {
|
|
this.__log.apply(this, [0, 'debug'].concat(args2array(arguments)));
|
|
};
|
|
Logger.prototype.success = function (message, context) {
|
|
this.__log.apply(this, [0, 'success'].concat(args2array(arguments)));
|
|
};
|
|
Logger.prototype.log = function (lvl = 0, message = '', context, type) {
|
|
lvl = Number(lvl);
|
|
var opClass = '';
|
|
if(arguments.length >= 4) opClass = type;
|
|
if(arguments.length >= 2)
|
|
this.__row(message, lvl, opClass);
|
|
if(arguments.length >= 3)
|
|
this.__row(context, lvl+1, opClass);
|
|
};
|
|
|
|
return module.exports = Logger;
|
|
});
|