Continuing the discussion from @done and subtasks:
This script toggles a provided tag/value for selected items. In addition to toggling value for the selected items it also:
-
Sets all descendants of the selected items to match the new value.
-
If all siblings of the selected items have the same value then the parent is given that same value, recursively.
In “maybe” more easy to understand terms. As the script is setup now it toggles the @done tag. If you toggle an item such that all items in the list become @done, then the parent is also marked @done.
var TaskPaper = Application('TaskPaper')
function TPContextGetCurrentPath(editor, options) {
var selection = editor.selection;
var selectedItems = selection.selectedItemsCommonAncestors;
editor.outline.groupUndoAndChanges(function() {
var attributeName = 'data-' + options.tag
var value = options.value;
selectedItems.forEach(function(each) {
if (each.hasAttribute(attributeName)) {
value = null;
}
});
selectedItems.forEach(function(each) {
each.setAttribute(attributeName, value);
each.descendants.forEach(function(eachDescendant) {
eachDescendant.setAttribute(attributeName, value);
});
parent = each.parent;
while (parent) {
var siblingsMatch = true;
parent.children.forEach(function(each) {
if (each.getAttribute(attributeName) != value) {
siblingsMatch = false;
}
});
if (siblingsMatch) {
parent.setAttribute(attributeName, value);
} else {
parent.setAttribute(attributeName, null);
}
parent = parent.parent;
}
});
});
editor.moveSelectionToItems(selection);
}
var path = TaskPaper.documents[0].evaluate({
script: TPContextGetCurrentPath.toString(),
withOptions: { tag: 'done', value: '' }
});