diff --git a/core/modules/background-actions.js b/core/modules/background-actions.js index 630361a8a..00caab5d1 100644 --- a/core/modules/background-actions.js +++ b/core/modules/background-actions.js @@ -9,57 +9,52 @@ Class to dispatch actions when filters change "use strict"; -function BackgroundActionDispatcher(filterTracker,wiki) { - var self = this; - this.filterTracker = filterTracker; - this.wiki = wiki; - this.nextTrackedFilterId = 1; - this.trackedFilters = Object.create(null); // Hashmap by id - // Track the filter for the background actions - this.filterTracker.track({ - filterString: "[all[tiddlers+shadows]tag[$:/tags/BackgroundAction]!is[draft]]", - fnEnter: function fnEnter(title) { - return self.trackFilter(title); - }, - fnLeave: function fnLeave(title,enterValue) { - self.untrackFilter(enterValue); - }, - fnChange: function fnChange(title,enterValue) { - self.untrackFilter(enterValue); - return self.trackFilter(title); - }, - fnProcess: function fnProcess(changes) { - self.process(changes); - } - }); -} +class BackgroundActionDispatcher { + constructor(filterTracker, wiki) { + this.filterTracker = filterTracker; + this.wiki = wiki; + this.nextTrackedFilterId = 1; + this.trackedFilters = Object.create(null); // Hashmap by id + // Track the filter for the background actions + this.filterTracker.track({ + filterString: "[all[tiddlers+shadows]tag[$:/tags/BackgroundAction]!is[draft]]", + fnEnter: (title) => this.trackFilter(title), + fnLeave: (title, enterValue) => this.untrackFilter(enterValue), + fnChange: (title, enterValue) => { + this.untrackFilter(enterValue); + return this.trackFilter(title); + }, + fnProcess: (changes) => this.process(changes) + }); + } -BackgroundActionDispatcher.prototype.trackFilter = function(title) { - var tiddler = this.wiki.getTiddler(title), - id = this.nextTrackedFilterId++, - tracker = new BackgroundActionTracker({ + trackFilter(title) { + const tiddler = this.wiki.getTiddler(title); + const id = this.nextTrackedFilterId++; + const tracker = new BackgroundActionTracker({ wiki: this.wiki, title: title, trackFilter: tiddler.fields["track-filter"], actions: tiddler.fields.text }); - this.trackedFilters[id] = tracker; - return id; -}; - -BackgroundActionDispatcher.prototype.untrackFilter = function(enterValue) { - var tracker = this.trackedFilters[enterValue]; - if(tracker) { - tracker.destroy(); + this.trackedFilters[id] = tracker; + return id; } - delete this.trackedFilters[enterValue]; -}; -BackgroundActionDispatcher.prototype.process = function(changes) { - for(var id in this.trackedFilters) { - this.trackedFilters[id].process(changes); + untrackFilter(enterValue) { + const tracker = this.trackedFilters[enterValue]; + if(tracker) { + tracker.destroy(); + } + delete this.trackedFilters[enterValue]; } -}; + + process(changes) { + for(const id in this.trackedFilters) { + this.trackedFilters[id].process(changes); + } + } +} /* Represents an individual tracked filter. Options include: @@ -68,57 +63,54 @@ title: title of the tiddler being tracked trackFilter: filter string to track changes actions: actions to be executed when the filter changes */ -function BackgroundActionTracker(options) { - var self = this; - this.wiki = options.wiki; - this.title = options.title; - this.trackFilter = options.trackFilter; - this.actions = options.actions; - this.filterTracker = new $tw.FilterTracker(this.wiki); - this.hasChanged = false; - this.trackerID = this.filterTracker.track({ - filterString: this.trackFilter, - fnEnter: function(title) { - self.hasChanged = true; - }, - fnLeave: function(title,enterValue) { - self.hasChanged = true; - }, - fnProcess: function(changes) { - if(self.hasChanged) { - self.hasChanged = false; - console.log("Processing background action",self.title); - var tiddler = self.wiki.getTiddler(self.title), - doActions = true; - if(tiddler && tiddler.fields.platforms) { - doActions = false; - var platforms = $tw.utils.parseStringArray(tiddler.fields.platforms); - if(($tw.browser && platforms.indexOf("browser") !== -1) || ($tw.node && platforms.indexOf("node") !== -1)) { - doActions = true; +class BackgroundActionTracker { + constructor(options) { + this.wiki = options.wiki; + this.title = options.title; + this.trackFilter = options.trackFilter; + this.actions = options.actions; + this.filterTracker = new $tw.FilterTracker(this.wiki); + this.hasChanged = false; + this.trackerID = this.filterTracker.track({ + filterString: this.trackFilter, + fnEnter: (title) => { this.hasChanged = true; }, + fnLeave: (title, enterValue) => { this.hasChanged = true; }, + fnProcess: (changes) => { + if(this.hasChanged) { + this.hasChanged = false; + console.log("Processing background action", this.title); + const tiddler = this.wiki.getTiddler(this.title); + let doActions = true; + if(tiddler && tiddler.fields.platforms) { + doActions = false; + const platforms = $tw.utils.parseStringArray(tiddler.fields.platforms); + if(($tw.browser && platforms.indexOf("browser") !== -1) || ($tw.node && platforms.indexOf("node") !== -1)) { + doActions = true; + } + } + if(doActions) { + this.wiki.invokeActionString( + this.actions, + null, + { + currentTiddler: this.title + },{ + parentWidget: $tw.rootWidget + } + ); } } - if(doActions) { - self.wiki.invokeActionString( - self.actions, - null, - { - currentTiddler: self.title - },{ - parentWidget: $tw.rootWidget - } - ); - } } - } - }); + }); + } + + process(changes) { + this.filterTracker.handleChangeEvent(changes); + } + + destroy() { + this.filterTracker.untrack(this.trackerID); + } } -BackgroundActionTracker.prototype.process = function(changes) { - this.filterTracker.handleChangeEvent(changes); -}; - -BackgroundActionTracker.prototype.destroy = function() { - this.filterTracker.untrack(this.trackerID); -}; - exports.BackgroundActionDispatcher = BackgroundActionDispatcher; diff --git a/core/modules/filter-tracker.js b/core/modules/filter-tracker.js index 584975e6d..161be3733 100644 --- a/core/modules/filter-tracker.js +++ b/core/modules/filter-tracker.js @@ -9,95 +9,97 @@ Class to track the results of a filter string "use strict"; -function FilterTracker(wiki) { - this.wiki = wiki; - this.trackers = []; - this.nextTrackerId = 1; -} +class FilterTracker { + constructor(wiki) { + this.wiki = wiki; + this.trackers = []; + this.nextTrackerId = 1; + } -FilterTracker.prototype.handleChangeEvent = function(changes) { - this.processTrackers(); - this.processChanges(changes); -}; + handleChangeEvent(changes) { + this.processTrackers(); + this.processChanges(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. 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(options) { - // Add the tracker details - var tracker = { - id: this.nextTrackerId++, - 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 - }; - this.trackers.push(tracker); - // Process the tracker - this.processTracker(this.trackers.length - 1); - return tracker.id; -}; + /* + 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) + */ + track(options) { + // Add the tracker details + const tracker = { + id: this.nextTrackerId++, + 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 + }; + this.trackers.push(tracker); + // Process the tracker + this.processTracker(this.trackers.length - 1); + return tracker.id; + } -FilterTracker.prototype.untrack = function(id) { - for(var t=0; t