From 95ef1c45804b33c921190e713fc439044c82dbea Mon Sep 17 00:00:00 2001 From: Jermolene Date: Mon, 10 Jul 2017 14:43:43 +0100 Subject: [PATCH] Fix linkcatcher recursion problem MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Using action-navigate within the “actions” attribute of the linkcatcher widget would otherwise trigger the navigation handler recursively. --- core/modules/widgets/linkcatcher.js | 38 ++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/core/modules/widgets/linkcatcher.js b/core/modules/widgets/linkcatcher.js index 0e5b93e71..ea0629d6e 100644 --- a/core/modules/widgets/linkcatcher.js +++ b/core/modules/widgets/linkcatcher.js @@ -48,6 +48,8 @@ LinkCatcherWidget.prototype.execute = function() { this.catchActions = this.getAttribute("actions"); // Construct the child widgets this.makeChildWidgets(); + // When executing actions we avoid trapping navigate events, so that we don't trigger ourselves recursively + this.executingActions = false; }; /* @@ -67,23 +69,35 @@ LinkCatcherWidget.prototype.refresh = function(changedTiddlers) { Handle a tm-navigate event */ LinkCatcherWidget.prototype.handleNavigateEvent = function(event) { - if(this.catchTo) { - this.wiki.setTextReference(this.catchTo,event.navigateTo,this.getVariable("currentTiddler")); - } - if(this.catchMessage && this.parentWidget) { + if(!this.executingActions) { + // Execute the actions + if(this.catchTo) { + this.wiki.setTextReference(this.catchTo,event.navigateTo,this.getVariable("currentTiddler")); + } + if(this.catchMessage && this.parentWidget) { + this.parentWidget.dispatchEvent({ + type: this.catchMessage, + param: event.navigateTo, + navigateTo: event.navigateTo + }); + } + if(this.catchSet) { + var tiddler = this.wiki.getTiddler(this.catchSet); + this.wiki.addTiddler(new $tw.Tiddler(tiddler,{title: this.catchSet, text: this.catchSetTo})); + } + if(this.catchActions) { + this.executingActions = true; + this.invokeActionString(this.catchActions,this,event,{navigateTo: event.navigateTo}); + this.executingActions = false; + } + } else { + // This is a navigate event generated by the actions of this linkcatcher, so we don't trap it again, but just pass it to the parent this.parentWidget.dispatchEvent({ - type: this.catchMessage, + type: "tm-navigate", param: event.navigateTo, navigateTo: event.navigateTo }); } - if(this.catchSet) { - var tiddler = this.wiki.getTiddler(this.catchSet); - this.wiki.addTiddler(new $tw.Tiddler(tiddler,{title: this.catchSet, text: this.catchSetTo})); - } - if(this.catchActions) { - this.invokeActionString(this.catchActions,this,event,{navigateTo: event.navigateTo}); - } return false; };