From 483fd941f57a81bfb86ff7f92a62ff2186cd8ff3 Mon Sep 17 00:00:00 2001 From: saqimtiaz Date: Fri, 20 Nov 2020 15:08:18 +0100 Subject: [PATCH] Extend action-log and subclass it as log widget (#5078) * Extended action-log and subclassed it as log widget * Do not rename LogWidget class * Removed unneeded variable declaration --- core/modules/widgets/action-log.js | 47 ++++++++++++++++++++++++++++-- core/modules/widgets/log.js | 30 +++++++++++++++++++ 2 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 core/modules/widgets/log.js diff --git a/core/modules/widgets/action-log.js b/core/modules/widgets/action-log.js index e3027a8e9..216eb1167 100644 --- a/core/modules/widgets/action-log.js +++ b/core/modules/widgets/action-log.js @@ -28,23 +28,66 @@ Render this widget into the DOM */ LogWidget.prototype.render = function(parent,nextSibling) { this.computeAttributes(); + this.execute(); }; +LogWidget.prototype.execute = function(){ + this.message = this.getAttribute("$$message","debug"); + this.logAll = this.getAttribute("$$all","no") === "yes" ? true : false; + this.filter = this.getAttribute("$$filter"); +} + /* Refresh the widget by ensuring our attributes are up to date */ LogWidget.prototype.refresh = function(changedTiddlers) { - return this.refreshChildren(changedTiddlers); + this.refreshSelf(); + return true; }; /* Invoke the action associated with this widget */ LogWidget.prototype.invokeAction = function(triggeringWidget,event) { - $tw.utils.logTable(this.attributes,["attribute name","value"]); + this.log(); return true; // Action was invoked }; +LogWidget.prototype.log = function() { + var data = {}, + dataCount, + allVars = {}, + filteredVars; + + $tw.utils.each(this.attributes,function(attribute,name) { + if(name.substring(0,2) !== "$$") { + data[name] = attribute; + } + }); + + for(var v in this.variables) { + allVars[v] = this.getVariable(v,{defaultValue:""}); + } + if(this.filter) { + filteredVars = this.wiki.compileFilter(this.filter).call(this.wiki,this.wiki.makeTiddlerIterator(allVars)); + $tw.utils.each(filteredVars,function(name) { + data[name] = allVars[name]; + }); + } + dataCount = $tw.utils.count(data); + + console.group(this.message); + if(dataCount > 0) { + $tw.utils.logTable(data,["name","value"]); + } + if(this.logAll || !dataCount) { + console.groupCollapsed("All variables"); + $tw.utils.logTable(allVars,["name","value"]); + console.groupEnd(); + } + console.groupEnd(); +} + exports["action-log"] = LogWidget; })(); diff --git a/core/modules/widgets/log.js b/core/modules/widgets/log.js new file mode 100644 index 000000000..2615547ad --- /dev/null +++ b/core/modules/widgets/log.js @@ -0,0 +1,30 @@ +/*\ +title: $:/core/modules/widgets/log.js +type: application/javascript +module-type: widget-subclass + +Widget to log debug messages + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +exports.baseClass = "action-log"; + +exports.name = "log"; + +exports.constructor = function(parseTreeNode,options) { + this.initialise(parseTreeNode,options); +} + +exports.prototype = {}; + +exports.prototype.render = function(event) { + Object.getPrototypeOf(Object.getPrototypeOf(this)).render.call(this,event); + Object.getPrototypeOf(Object.getPrototypeOf(this)).log.call(this); +} + +})(); \ No newline at end of file