diff --git a/core/modules/filter-tracker.js b/core/modules/filter-tracker.js index 3b2d8ae0f..36744b2cf 100644 --- a/core/modules/filter-tracker.js +++ b/core/modules/filter-tracker.js @@ -16,7 +16,6 @@ function FilterTracker(wiki) { this.wiki = wiki; this.trackers = []; this.nextTrackerId = 1; - this.wiki.addEventListener("change",this.handleChangeEvent.bind(this)); } FilterTracker.prototype.handleChangeEvent = function(changes) { @@ -25,20 +24,22 @@ FilterTracker.prototype.handleChangeEvent = function(changes) { }; /* -Add a tracker to the filter tracker. Returns null if any of the parameters are invalid, or a tracker id if the tracker was added successfully +Add a tracker to the filter tracker. Returns null if any of the parameters are invalid, or a tracker id if the tracker was added successfully. Options include: filterString: the filter string to track fnEnter: function to call when a title enters the filter results. Called even if the tiddler does not actually exist. Called as (title), and should return a truthy value that is stored in the tracker as the "enterValue" fnLeave: function to call when a title leaves the filter results. Called as (title,enterValue) fnChange: function to call when a tiddler changes in the filter results. Only called for filter results that identify a tiddler or shadow tiddler. Called as (title,enterValue), and may optionally return a replacement enterValue +fnProcess: function to call each time the tracker is processed, after any enter, leave or change functions are called. Called as (changes) */ -FilterTracker.prototype.track = function(filterString,fnEnter,fnLeave,fnChange) { +FilterTracker.prototype.track = function(options) { // Add the tracker details var tracker = { id: this.nextTrackerId++, - filterString: filterString, - fnEnter: fnEnter, - fnLeave: fnLeave, - fnChange: fnChange, + filterString: options.filterString, + fnEnter: options.fnEnter, + fnLeave: options.fnLeave, + fnChange: options.fnChange, + fnProcess: options.fnProcess, previousResults: [], // Results from the previous time the tracker was processed resultValues: {} // Map by title to the value returned by fnEnter }; @@ -96,6 +97,9 @@ FilterTracker.prototype.processChanges = function(changes) { tracker.resultValues[title] = tracker.fnChange(title,tracker.resultValues[title]) || tracker.resultValues[title]; } }); + if(tracker.fnProcess) { + tracker.fnProcess(changes); + } } }; diff --git a/core/modules/info/mediaquerytracker.js b/core/modules/info/mediaquerytracker.js index f6f0568ea..fdb1bc0f0 100644 --- a/core/modules/info/mediaquerytracker.js +++ b/core/modules/info/mediaquerytracker.js @@ -59,7 +59,12 @@ exports.getInfoTiddlerFields = function(updateInfoTiddlersCallback) { untrack(enterValue); return track(title); } - $tw.filterTracker.track("[all[tiddlers+shadows]tag[$:/tags/MediaQueryTracker]!is[draft]]",fnEnter,fnLeave,fnChange); + $tw.filterTracker.track({ + filterString: "[all[tiddlers+shadows]tag[$:/tags/MediaQueryTracker]!is[draft]]", + fnEnter: fnEnter, + fnLeave: fnLeave, + fnChange: fnChange + }); } return []; }; diff --git a/core/modules/startup/load-modules.js b/core/modules/startup/load-modules.js index 4d580a97e..ec48c7225 100644 --- a/core/modules/startup/load-modules.js +++ b/core/modules/startup/load-modules.js @@ -46,6 +46,9 @@ exports.startup = function() { $tw.perf = new $tw.Performance($tw.wiki.getTiddlerText(PERFORMANCE_INSTRUMENTATION_CONFIG_TITLE,"no") === "yes"); // Kick off the filter tracker $tw.filterTracker = new $tw.FilterTracker($tw.wiki); + $tw.wiki.addEventListener("change",function(changes) { + $tw.filterTracker.handleChangeEvent(changes); + }); }; })();