2012-12-13 21:34:31 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/widgets/view.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: widget
|
|
|
|
|
|
|
|
The view widget displays a tiddler field.
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*
|
|
|
|
Define the "text" viewer here so that it is always available
|
|
|
|
*/
|
|
|
|
var TextViewer = function(viewWidget,tiddler,field,value) {
|
|
|
|
this.viewWidget = viewWidget;
|
|
|
|
this.tiddler = tiddler;
|
|
|
|
this.field = field;
|
|
|
|
this.value = value;
|
|
|
|
};
|
|
|
|
|
|
|
|
TextViewer.prototype.render = function() {
|
|
|
|
// Get the value as a string
|
|
|
|
if(this.field !== "text" && this.tiddler) {
|
|
|
|
this.value = this.tiddler.getFieldString(this.field);
|
|
|
|
}
|
|
|
|
var value = "";
|
|
|
|
if(this.value !== undefined && this.value !== null) {
|
|
|
|
value = this.value;
|
|
|
|
}
|
2013-01-03 16:27:55 +00:00
|
|
|
// Set the element details
|
|
|
|
this.viewWidget.tag = "span";
|
|
|
|
this.viewWidget.attributes = {};
|
|
|
|
this.viewWidget.children = this.viewWidget.renderer.renderTree.createRenderers(this.viewWidget.renderer.renderContext,[{
|
2012-12-13 21:34:31 +00:00
|
|
|
type: "text",
|
|
|
|
text: value
|
|
|
|
}]);
|
|
|
|
};
|
|
|
|
|
2013-01-01 17:51:02 +00:00
|
|
|
var ViewWidget = function(renderer) {
|
2012-12-13 21:34:31 +00:00
|
|
|
// Save state
|
|
|
|
this.renderer = renderer;
|
|
|
|
// Initialise the field viewers if they've not been done already
|
2013-01-03 16:27:55 +00:00
|
|
|
if(!this.fieldViewers) {
|
|
|
|
ViewWidget.prototype.fieldViewers = {text: TextViewer}; // Start with the built-in text viewer
|
|
|
|
$tw.modules.applyMethods("fieldviewer",this.fieldViewers);
|
2012-12-13 21:34:31 +00:00
|
|
|
}
|
|
|
|
// Generate child nodes
|
2013-01-03 16:27:55 +00:00
|
|
|
this.generate();
|
2012-12-13 21:34:31 +00:00
|
|
|
};
|
|
|
|
|
2013-01-03 16:27:55 +00:00
|
|
|
ViewWidget.prototype.generate = function() {
|
2012-12-13 21:34:31 +00:00
|
|
|
// Get parameters from our attributes
|
2013-05-15 16:32:17 +00:00
|
|
|
this.tiddlerTitle = this.renderer.getAttribute("tiddler",this.renderer.tiddlerTitle);
|
2013-06-02 22:20:08 +00:00
|
|
|
this.fieldName = this.renderer.getAttribute("field",this.renderer.renderTree.getContextVariable(this.renderer,"field","text"));
|
2012-12-13 21:34:31 +00:00
|
|
|
this.format = this.renderer.getAttribute("format","text");
|
|
|
|
// Get the value to display
|
|
|
|
var tiddler = this.renderer.renderTree.wiki.getTiddler(this.tiddlerTitle),
|
|
|
|
value;
|
|
|
|
if(tiddler) {
|
|
|
|
if(this.fieldName === "text") {
|
|
|
|
// Calling getTiddlerText() triggers lazy loading of skinny tiddlers
|
|
|
|
value = this.renderer.renderTree.wiki.getTiddlerText(this.tiddlerTitle);
|
|
|
|
} else {
|
|
|
|
value = tiddler.fields[this.fieldName];
|
|
|
|
}
|
|
|
|
} else { // Use a special value if the tiddler is missing
|
|
|
|
switch(this.fieldName) {
|
|
|
|
case "title":
|
|
|
|
value = this.tiddlerTitle;
|
|
|
|
break;
|
|
|
|
case "modified":
|
|
|
|
case "created":
|
|
|
|
value = new Date();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
value = "";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Choose the viewer to use
|
2013-01-03 16:27:55 +00:00
|
|
|
var Viewer = this.fieldViewers.text;
|
|
|
|
if($tw.utils.hop(this.fieldViewers,this.format)) {
|
|
|
|
Viewer = this.fieldViewers[this.format];
|
2012-12-13 21:34:31 +00:00
|
|
|
}
|
|
|
|
this.viewer = new Viewer(this,tiddler,this.fieldName,value);
|
2013-01-03 16:27:55 +00:00
|
|
|
// Ask the viewer to create the widget element
|
|
|
|
this.viewer.render();
|
2012-12-13 21:34:31 +00:00
|
|
|
};
|
|
|
|
|
2013-01-01 17:51:02 +00:00
|
|
|
ViewWidget.prototype.refreshInDom = function(changedAttributes,changedTiddlers) {
|
2012-12-13 21:34:31 +00:00
|
|
|
// Check if any of our attributes have changed, or if a tiddler we're interested in has changed
|
|
|
|
if(changedAttributes.tiddler || changedAttributes.field || changedAttributes.format || (this.tiddlerTitle && changedTiddlers[this.tiddlerTitle])) {
|
2013-01-03 16:27:55 +00:00
|
|
|
// Regenerate and rerender the widget and replace the existing DOM node
|
|
|
|
this.generate();
|
|
|
|
var oldDomNode = this.renderer.domNode,
|
|
|
|
newDomNode = this.renderer.renderInDom();
|
|
|
|
oldDomNode.parentNode.replaceChild(newDomNode,oldDomNode);
|
2012-12-13 21:34:31 +00:00
|
|
|
} else {
|
|
|
|
// We don't need to refresh ourselves, so just refresh any child nodes
|
|
|
|
$tw.utils.each(this.children,function(node) {
|
|
|
|
if(node.refreshInDom) {
|
|
|
|
node.refreshInDom(changedTiddlers);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-03-19 18:59:00 +00:00
|
|
|
ViewWidget.prototype.postRenderInDom = function() {
|
|
|
|
if(this.viewer && this.viewer.postRenderInDom) {
|
|
|
|
this.viewer.postRenderInDom();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-01 17:51:02 +00:00
|
|
|
exports.view = ViewWidget;
|
|
|
|
|
2012-12-13 21:34:31 +00:00
|
|
|
})();
|