-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce new generic filter tracker
The new filter tracker is a generic JS mechanism for tracking changes to the results of filters and changes to the tiddlers identified by those results. The rationale for the new mechanism is that it is a generalisation of code that is already present in the keyboard manager. The intention is to refactor the keyboard manager to use the filter tracker in due course
- Loading branch information
Showing
6 changed files
with
134 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/*\ | ||
title: $:/core/modules/filter-tracker.js | ||
type: application/javascript | ||
module-type: global | ||
Class to track the results of a filter string | ||
\*/ | ||
(function(){ | ||
|
||
/*jslint node: true, browser: true */ | ||
/*global $tw: false */ | ||
"use strict"; | ||
|
||
function FilterTracker(wiki) { | ||
this.wiki = wiki; | ||
this.trackers = []; | ||
this.wiki.addEventListener("change",this.handleChangeEvent.bind(this)); | ||
} | ||
|
||
FilterTracker.prototype.handleChangeEvent = function(changes) { | ||
this.processTrackers(); | ||
this.processChanges(changes); | ||
}; | ||
|
||
/* | ||
Add a tracker to the filter tracker | ||
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 | ||
*/ | ||
FilterTracker.prototype.track = function(filterString,fnEnter,fnLeave,fnChange) { | ||
// Add the tracker details | ||
var index = this.trackers.length; | ||
this.trackers.push({ | ||
filterString: filterString, | ||
fnEnter: fnEnter, | ||
fnLeave: fnLeave, | ||
fnChange: fnChange, | ||
previousResults: [], // Results from the previous time the tracker was processed | ||
resultValues: {} // Map by title to the value returned by fnEnter | ||
}); | ||
// Process the tracker | ||
this.processTracker(index); | ||
}; | ||
|
||
FilterTracker.prototype.processTrackers = function() { | ||
for(var t=0; t<this.trackers.length; t++) { | ||
this.processTracker(t); | ||
} | ||
}; | ||
|
||
FilterTracker.prototype.processTracker = function(index) { | ||
var tracker = this.trackers[index], | ||
results = this.wiki.filterTiddlers(tracker.filterString); | ||
// Process the results | ||
$tw.utils.each(results,function(title) { | ||
if(tracker.previousResults.indexOf(title) === -1 && !tracker.resultValues[title]) { | ||
tracker.resultValues[title] = tracker.fnEnter(title) || true; | ||
} | ||
}); | ||
$tw.utils.each(tracker.previousResults,function(title) { | ||
if(results.indexOf(title) === -1 && tracker.resultValues[title]) { | ||
tracker.fnLeave(title,tracker.resultValues[title]); | ||
delete tracker.resultValues[title]; | ||
} | ||
}); | ||
// Update the previous results | ||
tracker.previousResults = results; | ||
}; | ||
|
||
FilterTracker.prototype.processChanges = function(changes) { | ||
for(var t=0; t<this.trackers.length; t++) { | ||
var tracker = this.trackers[t]; | ||
$tw.utils.each(changes,function(change,title) { | ||
if(tracker.previousResults.indexOf(title) !== -1) { | ||
// Call the change function and if it doesn't return a value then keep the old value | ||
tracker.resultValues[title] = tracker.fnChange(title,tracker.resultValues[title]) || tracker.resultValues[title]; | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
exports.FilterTracker = FilterTracker; | ||
|
||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
editions/tw5.com/tiddlers/mechanisms/MediaQueryTrackerMechanism.tid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
title: MediaQueryTrackerMechanism | ||
tags: Mechanisms | ||
|
||
The media query tracker mechanism allows you to define [[custom CSS media queries|https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_media_queries/Using_media_queries]] to be bound to a specified [[info|InfoMechanism]] tiddler. The info tiddler will be dynamically update to reflect the current state of the media query. | ||
<<.from-version "5.3.7">> The media query tracker mechanism allows you to define [[custom CSS media queries|https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_media_queries/Using_media_queries]] to be bound to a specified [[info|InfoMechanism]] tiddler. The info tiddler will be dynamically update to reflect the current state of the media query. | ||
|
||
Adding or modifying a tiddler tagged $:/tags/MediaQueryTracker will only take effect when the wiki is reloaded | ||
Adding or modifying a tiddler tagged $:/tags/MediaQueryTracker takes effect immediately. | ||
|
||
The media queries are always applied against the main window. This is relevant for viewport related media queries such as `min-width` which will always respect the main window and ignore the sizes of any external windows. |