2012-04-30 11:23:03 +00:00
|
|
|
/*\
|
2012-05-03 20:47:16 +00:00
|
|
|
title: $:/core/modules/macros/view.js
|
2012-04-30 11:23:03 +00:00
|
|
|
type: application/javascript
|
|
|
|
module-type: macro
|
|
|
|
|
2012-05-19 17:23:14 +00:00
|
|
|
View macro
|
|
|
|
|
2012-04-30 11:23:03 +00:00
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
2012-05-04 17:49:04 +00:00
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
2012-04-30 11:23:03 +00:00
|
|
|
"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,
|
|
|
|
children,
|
|
|
|
t,
|
|
|
|
childrenClone = [],
|
|
|
|
parents = this.parents;
|
|
|
|
if(tiddler) {
|
|
|
|
value = tiddler.fields[field];
|
|
|
|
} else {
|
|
|
|
switch(field) {
|
|
|
|
case "text":
|
|
|
|
value = "The tiddler '" + this.tiddlerTitle + "' does not exist";
|
|
|
|
break;
|
|
|
|
case "title":
|
|
|
|
value = this.tiddlerTitle;
|
|
|
|
break;
|
|
|
|
case "modified":
|
|
|
|
case "created":
|
|
|
|
value = new Date();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
value = "Missing tiddler '" + this.tiddlerTitle + "'";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch(this.params.format) {
|
|
|
|
case "link":
|
|
|
|
if(value === undefined) {
|
2012-06-11 16:36:28 +00:00
|
|
|
return $tw.Tree.Text("");
|
2012-04-30 11:23:03 +00:00
|
|
|
} else {
|
2012-06-19 15:46:50 +00:00
|
|
|
var link = $tw.Tree.Element("span",{},[$tw.Tree.Macro("link",{
|
2012-05-28 14:51:52 +00:00
|
|
|
srcParams: {to: value},
|
|
|
|
content: [$tw.Tree.Text(value)],
|
2012-06-09 21:25:13 +00:00
|
|
|
isBlock: this.isBlock,
|
2012-05-28 14:51:52 +00:00
|
|
|
wiki: this.wiki
|
2012-06-19 15:46:50 +00:00
|
|
|
})]);
|
2012-04-30 11:23:03 +00:00
|
|
|
link.execute(parents,this.tiddlerTitle);
|
2012-06-09 17:50:01 +00:00
|
|
|
return link;
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "wikified":
|
|
|
|
if(tiddler && this.params.field === "text") {
|
|
|
|
if(parents.indexOf(tiddler.fields.title) !== -1) {
|
|
|
|
children = [$tw.Tree.errorNode("Tiddler recursion error in <<view>> macro")];
|
|
|
|
} else {
|
|
|
|
children = this.wiki.parseTiddler(tiddler.fields.title).tree;
|
|
|
|
}
|
|
|
|
parents = parents.slice(0);
|
|
|
|
parents.push(tiddler.fields.title);
|
|
|
|
} else {
|
|
|
|
children = this.wiki.parseText("text/x-tiddlywiki",value).tree;
|
|
|
|
}
|
|
|
|
for(t=0; t<children.length; t++) {
|
|
|
|
childrenClone.push(children[t].clone());
|
|
|
|
}
|
|
|
|
for(t=0; t<childrenClone.length; t++) {
|
|
|
|
childrenClone[t].execute(parents,this.tiddlerTitle);
|
|
|
|
}
|
2012-06-09 17:36:32 +00:00
|
|
|
return $tw.Tree.Element(this.isBlock ? "div" : "span",{},childrenClone);
|
2012-04-30 11:23:03 +00:00
|
|
|
case "date":
|
|
|
|
var template = this.params.template || "DD MMM YYYY";
|
|
|
|
if(value === undefined) {
|
2012-06-11 16:36:28 +00:00
|
|
|
return $tw.Tree.Text("");
|
2012-04-30 11:23:03 +00:00
|
|
|
} else {
|
2012-06-09 17:36:32 +00:00
|
|
|
return $tw.Tree.Text($tw.utils.formatDateString(value,template));
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: // "text"
|
|
|
|
// Get the stringified version of the field value
|
|
|
|
if(field !== "text" && tiddler) {
|
|
|
|
value = tiddler.getFieldString(field);
|
|
|
|
}
|
|
|
|
if(value === undefined || value === null) {
|
2012-06-11 16:36:28 +00:00
|
|
|
return $tw.Tree.Text("");
|
2012-04-30 11:23:03 +00:00
|
|
|
} else {
|
2012-06-09 17:36:32 +00:00
|
|
|
return $tw.Tree.Text(value);
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-11 16:36:28 +00:00
|
|
|
return $tw.Tree.Text("");
|
2012-04-30 11:23:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
})();
|
|
|
|
|