mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-09-19 18:59:42 +00:00
a51191d334
* Support specifying insertbefore position title as parameter
Closes: #6133
* Update insertbefore calls with new variable parameter syntax
See-also: 96b52606a
(Support specifying insertbefore position title as parameter, 2022-02-21)
42 lines
937 B
JavaScript
42 lines
937 B
JavaScript
/*\
|
|
title: $:/core/modules/filters/insertbefore.js
|
|
type: application/javascript
|
|
module-type: filteroperator
|
|
|
|
Insert an item before another item in a list
|
|
|
|
\*/
|
|
(function(){
|
|
|
|
/*jslint node: true, browser: true */
|
|
/*global $tw: false */
|
|
"use strict";
|
|
|
|
/*
|
|
Order a list
|
|
*/
|
|
exports.insertbefore = function(source,operator,options) {
|
|
var results = [];
|
|
source(function(tiddler,title) {
|
|
results.push(title);
|
|
});
|
|
var target = operator.operands[1] || (options.widget && options.widget.getVariable(operator.suffix || "currentTiddler"));
|
|
if(target !== operator.operand) {
|
|
// Remove the entry from the list if it is present
|
|
var pos = results.indexOf(operator.operand);
|
|
if(pos !== -1) {
|
|
results.splice(pos,1);
|
|
}
|
|
// Insert the entry before the target marker
|
|
pos = results.indexOf(target);
|
|
if(pos !== -1) {
|
|
results.splice(pos,0,operator.operand);
|
|
} else {
|
|
results.push(operator.operand);
|
|
}
|
|
}
|
|
return results;
|
|
};
|
|
|
|
})();
|