From 57ceffd67c72e54ea520109b58d5863db95d8b98 Mon Sep 17 00:00:00 2001 From: Jermolene Date: Wed, 23 Dec 2015 12:19:47 +0000 Subject: [PATCH] Merge #1909 from @nameanyone --- core/modules/filters/days.js | 49 +++++++++++++++++++ core/modules/filters/recent.js | 35 ------------- core/modules/utils/utils.js | 11 +++++ .../tiddlers/staging/filters/days.tid | 16 ++++++ .../filters/examples/{recent.tid => days.tid} | 10 ++-- .../tiddlers/staging/filters/recent.tid | 15 ------ 6 files changed, 81 insertions(+), 55 deletions(-) create mode 100644 core/modules/filters/days.js delete mode 100644 core/modules/filters/recent.js create mode 100644 editions/prerelease/tiddlers/staging/filters/days.tid rename editions/prerelease/tiddlers/staging/filters/examples/{recent.tid => days.tid} (52%) delete mode 100644 editions/prerelease/tiddlers/staging/filters/recent.tid diff --git a/core/modules/filters/days.js b/core/modules/filters/days.js new file mode 100644 index 000000000..39cd375f7 --- /dev/null +++ b/core/modules/filters/days.js @@ -0,0 +1,49 @@ +/*\ +title: $:/core/modules/filters/days.js +type: application/javascript +module-type: filteroperator + +Filter operator that selects tiddlers with a specified date field within a specified date interval. + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +/* +Export our filter function +*/ +exports.days = function(source,operator,options) { + var results = [], + fieldName = operator.suffix || "modified", + dayInterval = (parseInt(operator.operand,10)||0), + dayIntervalSign = $tw.utils.sign(dayInterval), + targetTimeStamp = (new Date()).setHours(0,0,0,0) + 1000*60*60*24*dayInterval, + isWithinDays = function(dateField) { + var sign = $tw.utils.sign(targetTimeStamp - (new Date(dateField)).setHours(0,0,0,0)); + return sign === 0 || sign === dayIntervalSign; + }; + + if(operator.prefix === "!") { + source(function(tiddler,title) { + if(tiddler && tiddler.fields[fieldName]) { + if(!isWithinDays($tw.utils.parseDate(tiddler.fields[fieldName]))) { + results.push(title); + } + } + }); + } else { + source(function(tiddler,title) { + if(tiddler && tiddler.fields[fieldName]) { + if(isWithinDays($tw.utils.parseDate(tiddler.fields[fieldName]))) { + results.push(title); + } + } + }); + } + return results; +}; + +})(); diff --git a/core/modules/filters/recent.js b/core/modules/filters/recent.js deleted file mode 100644 index 897847d00..000000000 --- a/core/modules/filters/recent.js +++ /dev/null @@ -1,35 +0,0 @@ -/*\ -title: $:/core/modules/filters/recent.js -type: application/javascript -module-type: filteroperator - -Filter operator that selects tiddlers with a specified date field within the last N days. - -\*/ -(function(){ - -/*jslint node: true, browser: true */ -/*global $tw: false */ -"use strict"; - -/* -Export our filter function -*/ -exports.recent = function(source,operator,options) { - var results = [], - fieldName = operator.suffix || "modified", - targetTimeStamp = (new Date()).setHours(0,0,0,0) - 1000*60*60*24*(parseInt(operator.operand,10) || 0), - isRecent = function(dateField) { - return targetTimeStamp <= (new Date(dateField)).setHours(0,0,0,0); - }; - source(function(tiddler,title) { - if(tiddler && tiddler.fields[fieldName]) { - if(isRecent($tw.utils.parseDate(tiddler.fields[fieldName]))) { - results.push(title); - } - } - }); - return results; -}; - -})(); diff --git a/core/modules/utils/utils.js b/core/modules/utils/utils.js index b490c45f0..90015609d 100644 --- a/core/modules/utils/utils.js +++ b/core/modules/utils/utils.js @@ -643,4 +643,15 @@ exports.tagToCssSelector = function(tagName) { }; +/* +IE does not have sign function +*/ +exports.sign = Math.sign || function(x) { + x = +x; // convert to a number + if (x === 0 || isNaN(x)) { + return x; + } + return x > 0 ? 1 : -1; +}; + })(); diff --git a/editions/prerelease/tiddlers/staging/filters/days.tid b/editions/prerelease/tiddlers/staging/filters/days.tid new file mode 100644 index 000000000..732bb3aab --- /dev/null +++ b/editions/prerelease/tiddlers/staging/filters/days.tid @@ -0,0 +1,16 @@ +tags: [[Filter Operators]] [[Date Operators]] [[Negatable Operators]] +title: days Operator +type: text/vnd.tiddlywiki +caption: days +op-input: a [[selection of titles|Title Selection]] +op-neg-output: those input tiddlers in which field <<.place F>> is more than <<.place D>> days in the { future | past }, where <<.place D>> is { positive | negative } +op-output: those input tiddlers in which field <<.place F>> is <<.place D>> days in the { future | past } or any time { before | after } that, including { past | future }, where <<.place D>> is { positive | negative } +op-parameter: a number of days, defaulting to 0 +op-parameter-name: D +op-purpose: filter the input by date +op-suffix: the name of a [[date field|Date Fields]], defaulting to <<.field modified>> +op-suffix-name: F + +Select tiddlers where a specified date field (default "modified") is withing a specified date range. Time portion is ignored. + +<<.operator-examples "days">> diff --git a/editions/prerelease/tiddlers/staging/filters/examples/recent.tid b/editions/prerelease/tiddlers/staging/filters/examples/days.tid similarity index 52% rename from editions/prerelease/tiddlers/staging/filters/examples/recent.tid rename to editions/prerelease/tiddlers/staging/filters/examples/days.tid index 9f366823d..c4cc2bf16 100644 --- a/editions/prerelease/tiddlers/staging/filters/examples/recent.tid +++ b/editions/prerelease/tiddlers/staging/filters/examples/days.tid @@ -1,9 +1,9 @@ -tags: [[recent Operator]] [[Operator Examples]] -title: recent Operator (Examples) +tags: [[Operator Examples]] [[days Operator]] +title: days Operator (Examples) type: text/vnd.tiddlywiki -<<.operator-example 1 "[recent[1]]" "tiddlers modified yesterday and today">> -<<.operator-example 2 "[recent:created[14]]" "tiddlers created within the last 14 days">> +<<.operator-example 1 "[days[-14]]" "tiddlers modified within the last 14 days">> +<<.operator-example 2 "[!days:created[-800]]" "tiddlers created more than 800 days ago">> The filter can be used to highlight new items in a list. For example: <$macrocall $name="wikitext-example-without-html" src= @@ -11,7 +11,7 @@ The filter can be used to highlight new items in a list. For example: diff --git a/editions/prerelease/tiddlers/staging/filters/recent.tid b/editions/prerelease/tiddlers/staging/filters/recent.tid deleted file mode 100644 index 4adeb8c96..000000000 --- a/editions/prerelease/tiddlers/staging/filters/recent.tid +++ /dev/null @@ -1,15 +0,0 @@ -tags: [[Filter Operators]] [[Date Operators]] -title: recent Operator -type: text/vnd.tiddlywiki -caption: recent -op-purpose: filter the input by age -op-input: a [[selection of titles|Title Selection]] -op-suffix: the name of a [[date field|Date Fields]], defaulting to <<.field modified>> -op-suffix-name: F -op-parameter: a number of days, defaulting to 0 -op-parameter-name: D -op-output: those input tiddlers in which field <<.place F>> is at most <<.place D>> days old, ignoring time - -Select tiddlers where a specified date field (default "modified") is within the last N days (default 0, meaning today). - -<<.operator-examples "recent">>