2022-09-24 13:07:42 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/widgets/genesis.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: widget
|
|
|
|
|
|
|
|
Genesis widget for dynamically creating widgets
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var Widget = require("$:/core/modules/widgets/widget.js").widget;
|
|
|
|
|
|
|
|
var GenesisWidget = function(parseTreeNode,options) {
|
|
|
|
this.initialise(parseTreeNode,options);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Inherit from the base widget class
|
|
|
|
*/
|
|
|
|
GenesisWidget.prototype = new Widget();
|
|
|
|
|
|
|
|
/*
|
|
|
|
Render this widget into the DOM
|
|
|
|
*/
|
|
|
|
GenesisWidget.prototype.render = function(parent,nextSibling) {
|
|
|
|
this.parentDomNode = parent;
|
|
|
|
this.computeAttributes({filterFn: function(name) {
|
|
|
|
// Only compute our own attributes which start with a single dollar
|
|
|
|
return name.charAt(0) === "$" && name.charAt(1) !== "$";
|
|
|
|
}});
|
|
|
|
this.execute();
|
|
|
|
this.renderChildren(parent,nextSibling);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Compute the internal state of the widget
|
|
|
|
*/
|
|
|
|
GenesisWidget.prototype.execute = function() {
|
|
|
|
var self = this;
|
|
|
|
// Collect attributes
|
2022-12-30 21:42:07 +00:00
|
|
|
this.genesisType = this.getAttribute("$type");
|
2022-09-24 13:07:42 +00:00
|
|
|
this.genesisRemappable = this.getAttribute("$remappable","yes") === "yes";
|
|
|
|
this.genesisNames = this.getAttribute("$names","");
|
|
|
|
this.genesisValues = this.getAttribute("$values","");
|
2023-02-10 10:17:32 +00:00
|
|
|
this.genesisIsBlock = this.getAttribute("$mode",this.parseTreeNode.isBlock && "block") === "block";
|
2022-12-30 21:42:07 +00:00
|
|
|
// Do not create a child widget if the $type attribute is missing or blank
|
|
|
|
if(!this.genesisType) {
|
|
|
|
this.makeChildWidgets(this.parseTreeNode.children);
|
|
|
|
return;
|
|
|
|
}
|
2022-09-24 13:07:42 +00:00
|
|
|
// Construct parse tree
|
|
|
|
var isElementWidget = this.genesisType.charAt(0) !== "$",
|
|
|
|
nodeType = isElementWidget ? "element" : this.genesisType.substr(1),
|
|
|
|
nodeTag = isElementWidget ? this.genesisType : undefined;
|
|
|
|
var parseTreeNodes = [{
|
|
|
|
type: nodeType,
|
|
|
|
tag: nodeTag,
|
|
|
|
attributes: {},
|
|
|
|
orderedAttributes: [],
|
2023-02-10 10:17:32 +00:00
|
|
|
isBlock: this.genesisIsBlock,
|
2022-09-24 13:07:42 +00:00
|
|
|
children: this.parseTreeNode.children || [],
|
|
|
|
isNotRemappable: !this.genesisRemappable
|
|
|
|
}];
|
2024-10-30 14:51:04 +00:00
|
|
|
// Apply attributes in $names/$values
|
|
|
|
this.attributeNames = [];
|
|
|
|
this.attributeValues = [];
|
|
|
|
if(this.genesisNames && this.genesisValues) {
|
|
|
|
this.attributeNames = this.wiki.filterTiddlers(self.genesisNames,this);
|
|
|
|
this.attributeValues = this.wiki.filterTiddlers(self.genesisValues,this);
|
|
|
|
$tw.utils.each(this.attributeNames,function(varname,index) {
|
|
|
|
$tw.utils.addAttributeToParseTreeNode(parseTreeNodes[0],varname,self.attributeValues[index] || "");
|
|
|
|
});
|
|
|
|
}
|
2022-09-24 13:07:42 +00:00
|
|
|
// Apply explicit attributes
|
|
|
|
$tw.utils.each($tw.utils.getOrderedAttributesFromParseTreeNode(this.parseTreeNode),function(attribute) {
|
|
|
|
var name = attribute.name;
|
|
|
|
if(name.charAt(0) === "$") {
|
|
|
|
if(name.charAt(1) === "$") {
|
|
|
|
// Double $$ is changed to a single $
|
|
|
|
name = name.substr(1);
|
|
|
|
} else {
|
|
|
|
// Single dollar is ignored
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$tw.utils.addAttributeToParseTreeNode(parseTreeNodes[0],$tw.utils.extend({},attribute,{name: name}));
|
|
|
|
});
|
|
|
|
// Construct the child widgets
|
|
|
|
this.makeChildWidgets(parseTreeNodes);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
|
|
|
|
*/
|
|
|
|
GenesisWidget.prototype.refresh = function(changedTiddlers) {
|
|
|
|
var changedAttributes = this.computeAttributes(),
|
|
|
|
filterNames = this.getAttribute("$names",""),
|
|
|
|
filterValues = this.getAttribute("$values",""),
|
|
|
|
attributeNames = this.wiki.filterTiddlers(filterNames,this),
|
|
|
|
attributeValues = this.wiki.filterTiddlers(filterValues,this);
|
|
|
|
if($tw.utils.count(changedAttributes) > 0 || !$tw.utils.isArrayEqual(this.attributeNames,attributeNames) || !$tw.utils.isArrayEqual(this.attributeValues,attributeValues)) {
|
|
|
|
this.refreshSelf();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return this.refreshChildren(changedTiddlers);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.genesis = GenesisWidget;
|
|
|
|
|
|
|
|
})();
|