mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-09 19:39:57 +00:00
74 lines
1.6 KiB
JavaScript
74 lines
1.6 KiB
JavaScript
/*\
|
|
title: $:/core/modules/macros/view.js
|
|
type: application/javascript
|
|
module-type: macro
|
|
|
|
View macro
|
|
|
|
\*/
|
|
(function(){
|
|
|
|
/*jslint node: true, browser: true */
|
|
/*global $tw: false */
|
|
"use strict";
|
|
|
|
exports.info = {
|
|
name: "view",
|
|
dependentOnContextTiddler: true,
|
|
params: {
|
|
field: {byPos: 0, type: "text"},
|
|
format: {byPos: 1, type: "text"},
|
|
template: {byPos: 2, type: "text"}
|
|
}
|
|
};
|
|
|
|
exports.executeMacro = function() {
|
|
var tiddler = this.wiki.getTiddler(this.tiddlerTitle),
|
|
field = this.hasParameter("field") ? this.params.field : "title",
|
|
value;
|
|
// Get the value to display
|
|
if(tiddler) {
|
|
if(field === "text") {
|
|
value = this.wiki.getTiddlerText(this.tiddlerTitle);
|
|
} else {
|
|
value = tiddler.fields[field];
|
|
}
|
|
} else { // Use a special value if the tiddler is missing
|
|
switch(field) {
|
|
case "text":
|
|
value = "";
|
|
break;
|
|
case "title":
|
|
value = this.tiddlerTitle;
|
|
break;
|
|
case "modified":
|
|
case "created":
|
|
value = new Date();
|
|
break;
|
|
default:
|
|
value = "Missing tiddler '" + this.tiddlerTitle + "'";
|
|
break;
|
|
}
|
|
}
|
|
// Figure out which viewer to use
|
|
// TODO: Tiddler field modules should be able to specify a field type from which the viewer is derived
|
|
var Viewer;
|
|
if(this.params.format) {
|
|
Viewer = this.wiki.macros.view.fieldviewers[this.params.format];
|
|
}
|
|
if(!Viewer) {
|
|
Viewer = this.wiki.macros.view.fieldviewers.text;
|
|
}
|
|
this.viewer = new Viewer(this,tiddler,field,value);
|
|
// Call the viewer to generate the content
|
|
return this.viewer.render();
|
|
};
|
|
|
|
exports.postRenderInDom = function() {
|
|
if(this.viewer.postRenderInDom) {
|
|
this.viewer.postRenderInDom();
|
|
}
|
|
};
|
|
|
|
})();
|