1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Remember formatting choices for dbg msg elements

This commit is contained in:
Nick O'Leary 2017-05-11 17:00:49 +01:00
parent c75dc3cc36
commit d9ef32d7e8
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9

View File

@ -71,7 +71,7 @@ RED.utils = (function() {
} }
var pinnedPaths = {}; var pinnedPaths = {};
var formattedPaths = {};
function addMessageControls(obj,sourceId,key,msg,rootPath,strippedKey) { function addMessageControls(obj,sourceId,key,msg,rootPath,strippedKey) {
if (!pinnedPaths.hasOwnProperty(sourceId)) { if (!pinnedPaths.hasOwnProperty(sourceId)) {
@ -134,6 +134,56 @@ RED.utils = (function() {
return false; return false;
} }
function formatNumber(element,obj,sourceId,path,cycle) {
var format = (formattedPaths[sourceId] && formattedPaths[sourceId][path]) || "dec";
if (cycle) {
if (format === 'dec') {
if ((obj.toString().length===13) && (obj<=2147483647000)) {
format = 'dateMS';
} else if ((obj.toString().length===10) && (obj<=2147483647)) {
format = 'dateS';
} else {
format = 'hex'
}
} else if (format === 'dateMS' || format == 'dateS') {
format = 'hex';
} else {
format = 'dec';
}
formattedPaths[sourceId] = formattedPaths[sourceId]||{};
formattedPaths[sourceId][path] = format;
}
if (format === 'dec') {
element.text(""+obj);
} else if (format === 'dateMS') {
element.text((new Date(obj)).toISOString());
} else if (format === 'dateS') {
element.text((new Date(obj*1000)).toISOString());
} else if (format === 'hex') {
element.text("0x"+(obj).toString(16));
}
}
function formatBuffer(element,button,sourceId,path,cycle) {
var format = (formattedPaths[sourceId] && formattedPaths[sourceId][path]) || "raw";
if (cycle) {
if (format === 'raw') {
format = 'string';
} else {
format = 'raw';
}
formattedPaths[sourceId] = formattedPaths[sourceId]||{};
formattedPaths[sourceId][path] = format;
}
if (format === 'raw') {
button.text('raw');
element.removeClass('debug-message-buffer-string').addClass('debug-message-buffer-raw');
} else if (format === 'string') {
button.text('string');
element.addClass('debug-message-buffer-string').removeClass('debug-message-buffer-raw');
}
}
function buildMessageElement(obj,key,typeHint,hideKey,path,sourceId,rootPath,expandPaths) { function buildMessageElement(obj,key,typeHint,hideKey,path,sourceId,rootPath,expandPaths) {
var i; var i;
var e; var e;
@ -203,23 +253,17 @@ RED.utils = (function() {
} }
} else if (typeof obj === 'number') { } else if (typeof obj === 'number') {
e = $('<span class="debug-message-type-number"></span>').text(""+obj).appendTo(entryObj); e = $('<span class="debug-message-type-number"></span>').appendTo(entryObj);
if (Number.isInteger(obj) && (obj >= 0)) { // if it's a +ve integer if (Number.isInteger(obj) && (obj >= 0)) { // if it's a +ve integer
e.addClass("debug-message-type-number-toggle"); e.addClass("debug-message-type-number-toggle");
e.click(function(evt) { e.click(function(evt) {
var format = $(this).data('format') || "date";
if (format === 'dec') {
$(this).text(""+obj).data('format','date');
} else if ((format === 'date') && (obj.toString().length===13) && (obj<=2147483647000)) {
$(this).text((new Date(obj)).toISOString()).data('format','hex');
} else if ((format === 'date') && (obj.toString().length===10) && (obj<=2147483647)) {
$(this).text((new Date(obj*1000)).toISOString()).data('format','hex');
} else {
$(this).text("0x"+(obj).toString(16)).data('format','dec');
}
evt.preventDefault(); evt.preventDefault();
formatNumber($(this), obj, sourceId, path, true);
}); });
} }
formatNumber(e,obj,sourceId,path,false);
} else if (isArray) { } else if (isArray) {
element.addClass('collapsed'); element.addClass('collapsed');
@ -250,6 +294,28 @@ RED.utils = (function() {
$('<i class="fa fa-caret-right debug-message-object-handle"></i> ').prependTo(header); $('<i class="fa fa-caret-right debug-message-object-handle"></i> ').prependTo(header);
var arrayRows = $('<div class="debug-message-array-rows"></div>').appendTo(element); var arrayRows = $('<div class="debug-message-array-rows"></div>').appendTo(element);
element.addClass('debug-message-buffer-raw'); element.addClass('debug-message-buffer-raw');
if (key) {
headerHead = $('<span class="debug-message-type-meta f"></span>').html(typeHint||(type+'['+originalLength+']')).appendTo(entryObj);
} else {
headerHead = $('<span class="debug-message-object-header"></span>').appendTo(entryObj);
$('<span>[ </span>').appendTo(headerHead);
var arrayLength = Math.min(originalLength,10);
for (i=0;i<arrayLength;i++) {
buildMessageSummaryValue(data[i]).appendTo(headerHead);
if (i < arrayLength-1) {
$('<span>, </span>').appendTo(headerHead);
}
}
if (originalLength > arrayLength) {
$('<span> &hellip;</span>').appendTo(headerHead);
}
if (arrayLength === 0) {
$('<span class="debug-message-type-meta">empty</span>').appendTo(headerHead);
}
$('<span> ]</span>').appendTo(headerHead);
}
makeExpandable(header,function() { makeExpandable(header,function() {
if (!key) { if (!key) {
headerHead = $('<span class="debug-message-type-meta debug-message-object-type-header"></span>').html(typeHint||(type+'['+originalLength+']')).appendTo(header); headerHead = $('<span class="debug-message-type-meta debug-message-object-type-header"></span>').html(typeHint||(type+'['+originalLength+']')).appendTo(header);
@ -257,7 +323,6 @@ RED.utils = (function() {
if (type === 'buffer') { if (type === 'buffer') {
var stringRow = $('<div class="debug-message-string-rows"></div>').appendTo(element); var stringRow = $('<div class="debug-message-string-rows"></div>').appendTo(element);
var sr = $('<div class="debug-message-object-entry collapsed"></div>').appendTo(stringRow); var sr = $('<div class="debug-message-object-entry collapsed"></div>').appendTo(stringRow);
var stringEncoding = ""; var stringEncoding = "";
try { try {
stringEncoding = String.fromCharCode.apply(null, new Uint16Array(data)) stringEncoding = String.fromCharCode.apply(null, new Uint16Array(data))
@ -266,17 +331,13 @@ RED.utils = (function() {
} }
$('<pre class="debug-message-type-string"></pre>').text(stringEncoding).appendTo(sr); $('<pre class="debug-message-type-string"></pre>').text(stringEncoding).appendTo(sr);
var bufferOpts = $('<span class="debug-message-buffer-opts"></span>').appendTo(headerHead); var bufferOpts = $('<span class="debug-message-buffer-opts"></span>').appendTo(headerHead);
$('<a href="#"></a>').addClass('selected').html('raw').appendTo(bufferOpts).click(function(e) { var switchFormat = $('<a href="#"></a>').addClass('selected').html('raw').appendTo(bufferOpts).click(function(e) {
if ($(this).text() === 'raw') {
$(this).text('string');
element.addClass('debug-message-buffer-string').removeClass('debug-message-buffer-raw');
} else {
$(this).text('raw');
element.removeClass('debug-message-buffer-string').addClass('debug-message-buffer-raw');
}
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
}) formatBuffer(element,$(this),sourceId,path,true);
});
formatBuffer(element,switchFormat,sourceId,path,false);
} }
var row; var row;
if (fullLength <= 10) { if (fullLength <= 10) {
@ -309,27 +370,6 @@ RED.utils = (function() {
} }
},checkExpanded(strippedKey,expandPaths)); },checkExpanded(strippedKey,expandPaths));
} }
if (key) {
headerHead = $('<span class="debug-message-type-meta f"></span>').html(typeHint||(type+'['+originalLength+']')).appendTo(entryObj);
} else {
headerHead = $('<span class="debug-message-object-header"></span>').appendTo(entryObj);
$('<span>[ </span>').appendTo(headerHead);
var arrayLength = Math.min(originalLength,10);
for (i=0;i<arrayLength;i++) {
buildMessageSummaryValue(data[i]).appendTo(headerHead);
if (i < arrayLength-1) {
$('<span>, </span>').appendTo(headerHead);
}
}
if (originalLength > arrayLength) {
$('<span> &hellip;</span>').appendTo(headerHead);
}
if (arrayLength === 0) {
$('<span class="debug-message-type-meta">empty</span>').appendTo(headerHead);
}
$('<span> ]</span>').appendTo(headerHead);
}
} else if (typeof obj === 'object') { } else if (typeof obj === 'object') {
element.addClass('collapsed'); element.addClass('collapsed');
var keys = Object.keys(obj); var keys = Object.keys(obj);