1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-09-19 10:49:43 +00:00
TiddlyWiki5/core/modules/widgets/data.js

183 lines
4.7 KiB
JavaScript
Raw Normal View History

Add <$testcase> widget (#7817) * Initial Commit * Add note to preview build * Fix whitespace and indenting Thanks @pmario * Fix crash with unset $tiddler attribute on <$data> widget Thanks @CodaCodr * Don't duplicate "description" field in test cases * Use different background colours for nested testcase widgets * Extend the testcase widget to run tests * Add testcases to control panel * Add a view template body template to render testcase tiddlers * Test edition should display testcases * Whitespace fixes * Make testcase tiddler tempalte link to itself * Styling tweaks * Docs improvements * Styling tweaks * Run the new tw5.com testcases in the test edition * Update data widget to display its content in JSON * Add testcase convenience procedure * Clearer testcases for data widget, and docs tweaks * Don't expect our intentionally failing test to pass * Extend testcase default template so that the display format can be chosen It is selected by setting the variable "displayFormat" * DataWidget docs typo * Fix data widget not refreshing * Links in testcase output switch to the tab containing that tiddler Thanks to @btheado for the suggestion * Docs update for 648855e8a50b1ee3bef3120b64b5713b69190c9b * Wording tweak * Add support for narrative tiddlers in test cases * Documentation improvements * Cleanup comments * Remove obsolete code comments * Simplify template * Docs update * Rename $:/core/ui/testcases/DefaultTemplate/SourceTabs from $:/core/ui/testcases/DefaultTemplate/Source * Use the view template body for failing tests * Don't reference the geospatial plugin * "Test case" should be two words * Fix handling of currentTiddler variable Fixes problem reported by @btheado in https://github.com/Jermolene/TiddlyWiki5/pull/7817#issuecomment-2103704468 * Prepare for merging
2024-05-20 10:30:30 +00:00
/*\
title: $:/core/modules/widgets/data.js
type: application/javascript
module-type: widget
Widget to dynamically represent one or more tiddlers
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
var Widget = require("$:/core/modules/widgets/widget.js").widget;
var DataWidget = function(parseTreeNode,options) {
this.dataWidgetTag = parseTreeNode.type;
this.initialise(parseTreeNode,options);
};
/*
Inherit from the base widget class
*/
DataWidget.prototype = new Widget();
/*
Render this widget into the DOM
*/
DataWidget.prototype.render = function(parent,nextSibling) {
this.parentDomNode = parent;
this.computeAttributes();
this.execute();
2024-05-31 11:43:21 +00:00
this.dataPayload = this.computeDataTiddlerValues(); // Array of $tw.Tiddler objects
this.domNode = this.document.createTextNode(this.readDataTiddlerValuesAsJson());
parent.insertBefore(this.domNode,nextSibling);
this.domNodes.push(this.domNode);
Add <$testcase> widget (#7817) * Initial Commit * Add note to preview build * Fix whitespace and indenting Thanks @pmario * Fix crash with unset $tiddler attribute on <$data> widget Thanks @CodaCodr * Don't duplicate "description" field in test cases * Use different background colours for nested testcase widgets * Extend the testcase widget to run tests * Add testcases to control panel * Add a view template body template to render testcase tiddlers * Test edition should display testcases * Whitespace fixes * Make testcase tiddler tempalte link to itself * Styling tweaks * Docs improvements * Styling tweaks * Run the new tw5.com testcases in the test edition * Update data widget to display its content in JSON * Add testcase convenience procedure * Clearer testcases for data widget, and docs tweaks * Don't expect our intentionally failing test to pass * Extend testcase default template so that the display format can be chosen It is selected by setting the variable "displayFormat" * DataWidget docs typo * Fix data widget not refreshing * Links in testcase output switch to the tab containing that tiddler Thanks to @btheado for the suggestion * Docs update for 648855e8a50b1ee3bef3120b64b5713b69190c9b * Wording tweak * Add support for narrative tiddlers in test cases * Documentation improvements * Cleanup comments * Remove obsolete code comments * Simplify template * Docs update * Rename $:/core/ui/testcases/DefaultTemplate/SourceTabs from $:/core/ui/testcases/DefaultTemplate/Source * Use the view template body for failing tests * Don't reference the geospatial plugin * "Test case" should be two words * Fix handling of currentTiddler variable Fixes problem reported by @btheado in https://github.com/Jermolene/TiddlyWiki5/pull/7817#issuecomment-2103704468 * Prepare for merging
2024-05-20 10:30:30 +00:00
};
/*
Compute the internal state of the widget
*/
DataWidget.prototype.execute = function() {
2024-05-31 11:43:21 +00:00
// Nothing to do here
Add <$testcase> widget (#7817) * Initial Commit * Add note to preview build * Fix whitespace and indenting Thanks @pmario * Fix crash with unset $tiddler attribute on <$data> widget Thanks @CodaCodr * Don't duplicate "description" field in test cases * Use different background colours for nested testcase widgets * Extend the testcase widget to run tests * Add testcases to control panel * Add a view template body template to render testcase tiddlers * Test edition should display testcases * Whitespace fixes * Make testcase tiddler tempalte link to itself * Styling tweaks * Docs improvements * Styling tweaks * Run the new tw5.com testcases in the test edition * Update data widget to display its content in JSON * Add testcase convenience procedure * Clearer testcases for data widget, and docs tweaks * Don't expect our intentionally failing test to pass * Extend testcase default template so that the display format can be chosen It is selected by setting the variable "displayFormat" * DataWidget docs typo * Fix data widget not refreshing * Links in testcase output switch to the tab containing that tiddler Thanks to @btheado for the suggestion * Docs update for 648855e8a50b1ee3bef3120b64b5713b69190c9b * Wording tweak * Add support for narrative tiddlers in test cases * Documentation improvements * Cleanup comments * Remove obsolete code comments * Simplify template * Docs update * Rename $:/core/ui/testcases/DefaultTemplate/SourceTabs from $:/core/ui/testcases/DefaultTemplate/Source * Use the view template body for failing tests * Don't reference the geospatial plugin * "Test case" should be two words * Fix handling of currentTiddler variable Fixes problem reported by @btheado in https://github.com/Jermolene/TiddlyWiki5/pull/7817#issuecomment-2103704468 * Prepare for merging
2024-05-20 10:30:30 +00:00
};
/*
2024-05-31 11:43:21 +00:00
Read the tiddler value(s) from a data widget as an array of tiddler field objects (not $tw.Tiddler objects)
Add <$testcase> widget (#7817) * Initial Commit * Add note to preview build * Fix whitespace and indenting Thanks @pmario * Fix crash with unset $tiddler attribute on <$data> widget Thanks @CodaCodr * Don't duplicate "description" field in test cases * Use different background colours for nested testcase widgets * Extend the testcase widget to run tests * Add testcases to control panel * Add a view template body template to render testcase tiddlers * Test edition should display testcases * Whitespace fixes * Make testcase tiddler tempalte link to itself * Styling tweaks * Docs improvements * Styling tweaks * Run the new tw5.com testcases in the test edition * Update data widget to display its content in JSON * Add testcase convenience procedure * Clearer testcases for data widget, and docs tweaks * Don't expect our intentionally failing test to pass * Extend testcase default template so that the display format can be chosen It is selected by setting the variable "displayFormat" * DataWidget docs typo * Fix data widget not refreshing * Links in testcase output switch to the tab containing that tiddler Thanks to @btheado for the suggestion * Docs update for 648855e8a50b1ee3bef3120b64b5713b69190c9b * Wording tweak * Add support for narrative tiddlers in test cases * Documentation improvements * Cleanup comments * Remove obsolete code comments * Simplify template * Docs update * Rename $:/core/ui/testcases/DefaultTemplate/SourceTabs from $:/core/ui/testcases/DefaultTemplate/Source * Use the view template body for failing tests * Don't reference the geospatial plugin * "Test case" should be two words * Fix handling of currentTiddler variable Fixes problem reported by @btheado in https://github.com/Jermolene/TiddlyWiki5/pull/7817#issuecomment-2103704468 * Prepare for merging
2024-05-20 10:30:30 +00:00
*/
DataWidget.prototype.readDataTiddlerValues = function() {
2024-05-31 11:43:21 +00:00
var results = [];
$tw.utils.each(this.dataPayload,function(tiddler,index) {
results.push(tiddler.getFieldStrings());
});
return results;
};
/*
Read the tiddler value(s) from a data widget as an array of tiddler field objects (not $tw.Tiddler objects)
*/
DataWidget.prototype.readDataTiddlerValuesAsJson = function() {
return JSON.stringify(this.readDataTiddlerValues(),null,4);
};
/*
Compute list of tiddlers from a data widget
*/
DataWidget.prototype.computeDataTiddlerValues = function() {
Add <$testcase> widget (#7817) * Initial Commit * Add note to preview build * Fix whitespace and indenting Thanks @pmario * Fix crash with unset $tiddler attribute on <$data> widget Thanks @CodaCodr * Don't duplicate "description" field in test cases * Use different background colours for nested testcase widgets * Extend the testcase widget to run tests * Add testcases to control panel * Add a view template body template to render testcase tiddlers * Test edition should display testcases * Whitespace fixes * Make testcase tiddler tempalte link to itself * Styling tweaks * Docs improvements * Styling tweaks * Run the new tw5.com testcases in the test edition * Update data widget to display its content in JSON * Add testcase convenience procedure * Clearer testcases for data widget, and docs tweaks * Don't expect our intentionally failing test to pass * Extend testcase default template so that the display format can be chosen It is selected by setting the variable "displayFormat" * DataWidget docs typo * Fix data widget not refreshing * Links in testcase output switch to the tab containing that tiddler Thanks to @btheado for the suggestion * Docs update for 648855e8a50b1ee3bef3120b64b5713b69190c9b * Wording tweak * Add support for narrative tiddlers in test cases * Documentation improvements * Cleanup comments * Remove obsolete code comments * Simplify template * Docs update * Rename $:/core/ui/testcases/DefaultTemplate/SourceTabs from $:/core/ui/testcases/DefaultTemplate/Source * Use the view template body for failing tests * Don't reference the geospatial plugin * "Test case" should be two words * Fix handling of currentTiddler variable Fixes problem reported by @btheado in https://github.com/Jermolene/TiddlyWiki5/pull/7817#issuecomment-2103704468 * Prepare for merging
2024-05-20 10:30:30 +00:00
var self = this;
// Read any attributes not prefixed with $
2024-05-31 11:43:21 +00:00
var item = Object.create(null);
Add <$testcase> widget (#7817) * Initial Commit * Add note to preview build * Fix whitespace and indenting Thanks @pmario * Fix crash with unset $tiddler attribute on <$data> widget Thanks @CodaCodr * Don't duplicate "description" field in test cases * Use different background colours for nested testcase widgets * Extend the testcase widget to run tests * Add testcases to control panel * Add a view template body template to render testcase tiddlers * Test edition should display testcases * Whitespace fixes * Make testcase tiddler tempalte link to itself * Styling tweaks * Docs improvements * Styling tweaks * Run the new tw5.com testcases in the test edition * Update data widget to display its content in JSON * Add testcase convenience procedure * Clearer testcases for data widget, and docs tweaks * Don't expect our intentionally failing test to pass * Extend testcase default template so that the display format can be chosen It is selected by setting the variable "displayFormat" * DataWidget docs typo * Fix data widget not refreshing * Links in testcase output switch to the tab containing that tiddler Thanks to @btheado for the suggestion * Docs update for 648855e8a50b1ee3bef3120b64b5713b69190c9b * Wording tweak * Add support for narrative tiddlers in test cases * Documentation improvements * Cleanup comments * Remove obsolete code comments * Simplify template * Docs update * Rename $:/core/ui/testcases/DefaultTemplate/SourceTabs from $:/core/ui/testcases/DefaultTemplate/Source * Use the view template body for failing tests * Don't reference the geospatial plugin * "Test case" should be two words * Fix handling of currentTiddler variable Fixes problem reported by @btheado in https://github.com/Jermolene/TiddlyWiki5/pull/7817#issuecomment-2103704468 * Prepare for merging
2024-05-20 10:30:30 +00:00
$tw.utils.each(this.attributes,function(value,name) {
if(name.charAt(0) !== "$") {
item[name] = value;
}
});
// Deal with $tiddler, $filter or $compound-tiddler attributes
var tiddlers = [],title;
if(this.hasAttribute("$tiddler")) {
title = this.getAttribute("$tiddler");
if(title) {
var tiddler = this.wiki.getTiddler(title);
if(tiddler) {
tiddlers.push(tiddler);
}
}
}
if(this.hasAttribute("$filter")) {
var filter = this.getAttribute("$filter");
if(filter) {
var titles = this.wiki.filterTiddlers(filter);
$tw.utils.each(titles,function(title) {
var tiddler = self.wiki.getTiddler(title);
tiddlers.push(tiddler);
});
}
}
if(this.hasAttribute("$compound-tiddler")) {
title = this.getAttribute("$compound-tiddler");
if(title) {
tiddlers.push.apply(tiddlers,this.extractCompoundTiddler(title));
}
}
2024-05-31 11:43:21 +00:00
// Return the literal item if none of the special attributes were used
if(!this.hasAttribute("$tiddler") && !this.hasAttribute("$filter") && !this.hasAttribute("$compound-tiddler")) {
Add <$testcase> widget (#7817) * Initial Commit * Add note to preview build * Fix whitespace and indenting Thanks @pmario * Fix crash with unset $tiddler attribute on <$data> widget Thanks @CodaCodr * Don't duplicate "description" field in test cases * Use different background colours for nested testcase widgets * Extend the testcase widget to run tests * Add testcases to control panel * Add a view template body template to render testcase tiddlers * Test edition should display testcases * Whitespace fixes * Make testcase tiddler tempalte link to itself * Styling tweaks * Docs improvements * Styling tweaks * Run the new tw5.com testcases in the test edition * Update data widget to display its content in JSON * Add testcase convenience procedure * Clearer testcases for data widget, and docs tweaks * Don't expect our intentionally failing test to pass * Extend testcase default template so that the display format can be chosen It is selected by setting the variable "displayFormat" * DataWidget docs typo * Fix data widget not refreshing * Links in testcase output switch to the tab containing that tiddler Thanks to @btheado for the suggestion * Docs update for 648855e8a50b1ee3bef3120b64b5713b69190c9b * Wording tweak * Add support for narrative tiddlers in test cases * Documentation improvements * Cleanup comments * Remove obsolete code comments * Simplify template * Docs update * Rename $:/core/ui/testcases/DefaultTemplate/SourceTabs from $:/core/ui/testcases/DefaultTemplate/Source * Use the view template body for failing tests * Don't reference the geospatial plugin * "Test case" should be two words * Fix handling of currentTiddler variable Fixes problem reported by @btheado in https://github.com/Jermolene/TiddlyWiki5/pull/7817#issuecomment-2103704468 * Prepare for merging
2024-05-20 10:30:30 +00:00
if(Object.keys(item).length > 0 && !!item.title) {
2024-05-31 11:43:21 +00:00
return [new $tw.Tiddler(item)];
Add <$testcase> widget (#7817) * Initial Commit * Add note to preview build * Fix whitespace and indenting Thanks @pmario * Fix crash with unset $tiddler attribute on <$data> widget Thanks @CodaCodr * Don't duplicate "description" field in test cases * Use different background colours for nested testcase widgets * Extend the testcase widget to run tests * Add testcases to control panel * Add a view template body template to render testcase tiddlers * Test edition should display testcases * Whitespace fixes * Make testcase tiddler tempalte link to itself * Styling tweaks * Docs improvements * Styling tweaks * Run the new tw5.com testcases in the test edition * Update data widget to display its content in JSON * Add testcase convenience procedure * Clearer testcases for data widget, and docs tweaks * Don't expect our intentionally failing test to pass * Extend testcase default template so that the display format can be chosen It is selected by setting the variable "displayFormat" * DataWidget docs typo * Fix data widget not refreshing * Links in testcase output switch to the tab containing that tiddler Thanks to @btheado for the suggestion * Docs update for 648855e8a50b1ee3bef3120b64b5713b69190c9b * Wording tweak * Add support for narrative tiddlers in test cases * Documentation improvements * Cleanup comments * Remove obsolete code comments * Simplify template * Docs update * Rename $:/core/ui/testcases/DefaultTemplate/SourceTabs from $:/core/ui/testcases/DefaultTemplate/Source * Use the view template body for failing tests * Don't reference the geospatial plugin * "Test case" should be two words * Fix handling of currentTiddler variable Fixes problem reported by @btheado in https://github.com/Jermolene/TiddlyWiki5/pull/7817#issuecomment-2103704468 * Prepare for merging
2024-05-20 10:30:30 +00:00
} else {
return [];
}
} else {
2024-05-31 11:43:21 +00:00
// Apply the item fields to each of the tiddlers
delete item.title; // Do not overwrite the title
if(Object.keys(item).length > 0) {
$tw.utils.each(tiddlers,function(tiddler,index) {
tiddlers[index] = new $tw.Tiddler(tiddler,item);
});
}
return tiddlers;
Add <$testcase> widget (#7817) * Initial Commit * Add note to preview build * Fix whitespace and indenting Thanks @pmario * Fix crash with unset $tiddler attribute on <$data> widget Thanks @CodaCodr * Don't duplicate "description" field in test cases * Use different background colours for nested testcase widgets * Extend the testcase widget to run tests * Add testcases to control panel * Add a view template body template to render testcase tiddlers * Test edition should display testcases * Whitespace fixes * Make testcase tiddler tempalte link to itself * Styling tweaks * Docs improvements * Styling tweaks * Run the new tw5.com testcases in the test edition * Update data widget to display its content in JSON * Add testcase convenience procedure * Clearer testcases for data widget, and docs tweaks * Don't expect our intentionally failing test to pass * Extend testcase default template so that the display format can be chosen It is selected by setting the variable "displayFormat" * DataWidget docs typo * Fix data widget not refreshing * Links in testcase output switch to the tab containing that tiddler Thanks to @btheado for the suggestion * Docs update for 648855e8a50b1ee3bef3120b64b5713b69190c9b * Wording tweak * Add support for narrative tiddlers in test cases * Documentation improvements * Cleanup comments * Remove obsolete code comments * Simplify template * Docs update * Rename $:/core/ui/testcases/DefaultTemplate/SourceTabs from $:/core/ui/testcases/DefaultTemplate/Source * Use the view template body for failing tests * Don't reference the geospatial plugin * "Test case" should be two words * Fix handling of currentTiddler variable Fixes problem reported by @btheado in https://github.com/Jermolene/TiddlyWiki5/pull/7817#issuecomment-2103704468 * Prepare for merging
2024-05-20 10:30:30 +00:00
}
};
/*
Helper to extract tiddlers from text/vnd.tiddlywiki-multiple tiddlers
*/
DataWidget.prototype.extractCompoundTiddler = function(title) {
var tiddler = this.wiki.getTiddler(title);
if(tiddler && tiddler.fields.type === "text/vnd.tiddlywiki-multiple") {
var text = tiddler.fields.text || "",
rawTiddlers = text.split(/\r?\n\+\r?\n/),
tiddlers = [];
$tw.utils.each(rawTiddlers,function(rawTiddler) {
var fields = Object.create(null),
split = rawTiddler.split(/\r?\n\r?\n/mg);
if(split.length >= 1) {
fields = $tw.utils.parseFields(split[0],fields);
}
if(split.length >= 2) {
fields.text = split.slice(1).join("\n\n");
}
tiddlers.push(new $tw.Tiddler(fields));
});
return tiddlers;
} else {
return [];
}
};
/*
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
*/
DataWidget.prototype.refresh = function(changedTiddlers) {
2024-05-31 11:43:21 +00:00
var changedAttributes = this.computeAttributes();
var newPayload = this.computeDataTiddlerValues();
if(hasPayloadChanged(this.dataPayload,newPayload)) {
this.dataPayload = newPayload;
this.domNode.textContent = this.readDataTiddlerValuesAsJson();
return true;
} else {
return false;
}
Add <$testcase> widget (#7817) * Initial Commit * Add note to preview build * Fix whitespace and indenting Thanks @pmario * Fix crash with unset $tiddler attribute on <$data> widget Thanks @CodaCodr * Don't duplicate "description" field in test cases * Use different background colours for nested testcase widgets * Extend the testcase widget to run tests * Add testcases to control panel * Add a view template body template to render testcase tiddlers * Test edition should display testcases * Whitespace fixes * Make testcase tiddler tempalte link to itself * Styling tweaks * Docs improvements * Styling tweaks * Run the new tw5.com testcases in the test edition * Update data widget to display its content in JSON * Add testcase convenience procedure * Clearer testcases for data widget, and docs tweaks * Don't expect our intentionally failing test to pass * Extend testcase default template so that the display format can be chosen It is selected by setting the variable "displayFormat" * DataWidget docs typo * Fix data widget not refreshing * Links in testcase output switch to the tab containing that tiddler Thanks to @btheado for the suggestion * Docs update for 648855e8a50b1ee3bef3120b64b5713b69190c9b * Wording tweak * Add support for narrative tiddlers in test cases * Documentation improvements * Cleanup comments * Remove obsolete code comments * Simplify template * Docs update * Rename $:/core/ui/testcases/DefaultTemplate/SourceTabs from $:/core/ui/testcases/DefaultTemplate/Source * Use the view template body for failing tests * Don't reference the geospatial plugin * "Test case" should be two words * Fix handling of currentTiddler variable Fixes problem reported by @btheado in https://github.com/Jermolene/TiddlyWiki5/pull/7817#issuecomment-2103704468 * Prepare for merging
2024-05-20 10:30:30 +00:00
};
2024-05-31 11:43:21 +00:00
/*
Compare two arrays of tiddlers and return true if they are different
*/
function hasPayloadChanged(a,b) {
if(a.length === b.length) {
for(var t=0; t<a.length; t++) {
if(!(a[t].isEqual(b[t]))) {
return true;
}
}
return false;
} else {
return true;
}
}
Add <$testcase> widget (#7817) * Initial Commit * Add note to preview build * Fix whitespace and indenting Thanks @pmario * Fix crash with unset $tiddler attribute on <$data> widget Thanks @CodaCodr * Don't duplicate "description" field in test cases * Use different background colours for nested testcase widgets * Extend the testcase widget to run tests * Add testcases to control panel * Add a view template body template to render testcase tiddlers * Test edition should display testcases * Whitespace fixes * Make testcase tiddler tempalte link to itself * Styling tweaks * Docs improvements * Styling tweaks * Run the new tw5.com testcases in the test edition * Update data widget to display its content in JSON * Add testcase convenience procedure * Clearer testcases for data widget, and docs tweaks * Don't expect our intentionally failing test to pass * Extend testcase default template so that the display format can be chosen It is selected by setting the variable "displayFormat" * DataWidget docs typo * Fix data widget not refreshing * Links in testcase output switch to the tab containing that tiddler Thanks to @btheado for the suggestion * Docs update for 648855e8a50b1ee3bef3120b64b5713b69190c9b * Wording tweak * Add support for narrative tiddlers in test cases * Documentation improvements * Cleanup comments * Remove obsolete code comments * Simplify template * Docs update * Rename $:/core/ui/testcases/DefaultTemplate/SourceTabs from $:/core/ui/testcases/DefaultTemplate/Source * Use the view template body for failing tests * Don't reference the geospatial plugin * "Test case" should be two words * Fix handling of currentTiddler variable Fixes problem reported by @btheado in https://github.com/Jermolene/TiddlyWiki5/pull/7817#issuecomment-2103704468 * Prepare for merging
2024-05-20 10:30:30 +00:00
exports.data = DataWidget;
})();