2012-01-07 17:33:42 +00:00
|
|
|
/*\
|
|
|
|
title: js/macros/view.js
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true */
|
|
|
|
"use strict";
|
|
|
|
|
2012-02-16 20:38:10 +00:00
|
|
|
var Renderer = require("../Renderer.js").Renderer,
|
2012-02-17 14:11:25 +00:00
|
|
|
Dependencies = require("../Dependencies.js").Dependencies,
|
2012-02-16 20:38:10 +00:00
|
|
|
utils = require("../Utils.js");
|
2012-01-07 17:33:42 +00:00
|
|
|
|
|
|
|
exports.macro = {
|
|
|
|
name: "view",
|
2012-03-30 12:49:26 +00:00
|
|
|
dependentOnContextTiddler: true,
|
2012-01-07 17:33:42 +00:00
|
|
|
params: {
|
2012-02-17 17:32:32 +00:00
|
|
|
field: {byPos: 0, type: "text"},
|
|
|
|
format: {byPos: 1, type: "text"},
|
|
|
|
template: {byPos: 2, type: "text"}
|
2012-01-07 17:33:42 +00:00
|
|
|
},
|
2012-02-21 21:57:30 +00:00
|
|
|
execute: function() {
|
2012-03-29 12:44:45 +00:00
|
|
|
var tiddler = this.store.getTiddler(this.tiddlerTitle),
|
|
|
|
field = this.hasParameter("field") ? this.params.field : "title",
|
|
|
|
value,
|
|
|
|
content,
|
|
|
|
t,
|
|
|
|
contentClone = [],
|
|
|
|
parents = this.parents;
|
|
|
|
if(tiddler) {
|
|
|
|
value = tiddler[field];
|
2012-01-23 16:42:12 +00:00
|
|
|
} else {
|
2012-03-29 12:44:45 +00:00
|
|
|
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;
|
2012-01-07 17:33:42 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-29 12:44:45 +00:00
|
|
|
switch(this.params.format) {
|
|
|
|
case "link":
|
2012-03-29 13:30:22 +00:00
|
|
|
if(value === undefined) {
|
|
|
|
return [];
|
|
|
|
} else {
|
|
|
|
var link = Renderer.MacroNode("link",
|
|
|
|
{target: value},
|
|
|
|
[Renderer.TextNode(value)],
|
|
|
|
this.store);
|
|
|
|
link.execute(parents,this.tiddlerTitle);
|
|
|
|
return [link];
|
|
|
|
}
|
|
|
|
break;
|
2012-03-29 12:44:45 +00:00
|
|
|
case "wikified":
|
|
|
|
if(tiddler && this.params.field === "text") {
|
|
|
|
if(parents.indexOf(tiddler.title) !== -1) {
|
|
|
|
content = [Renderer.ErrorNode("Tiddler recursion error in <<view>> macro")];
|
|
|
|
} else {
|
|
|
|
content = this.store.parseTiddler(tiddler.title).nodes;
|
|
|
|
}
|
|
|
|
parents = parents.slice(0);
|
|
|
|
parents.push(tiddler.title);
|
|
|
|
} else {
|
|
|
|
content = this.store.parseText("text/x-tiddlywiki",value).nodes;
|
|
|
|
}
|
|
|
|
for(t=0; t<content.length; t++) {
|
|
|
|
contentClone.push(content[t].clone());
|
|
|
|
}
|
|
|
|
for(t=0; t<contentClone.length; t++) {
|
2012-03-29 13:30:22 +00:00
|
|
|
contentClone[t].execute(parents,this.tiddlerTitle);
|
2012-03-29 12:44:45 +00:00
|
|
|
}
|
|
|
|
return contentClone;
|
|
|
|
case "date":
|
|
|
|
var template = this.params.template || "DD MMM YYYY";
|
|
|
|
if(value === undefined) {
|
|
|
|
return [];
|
|
|
|
} else {
|
|
|
|
return [Renderer.TextNode(utils.formatDateString(value,template))];
|
|
|
|
}
|
2012-03-29 13:30:22 +00:00
|
|
|
break;
|
2012-03-29 12:44:45 +00:00
|
|
|
default: // "text"
|
|
|
|
return [Renderer.TextNode(value)];
|
|
|
|
}
|
2012-02-17 14:11:25 +00:00
|
|
|
return [];
|
2012-01-07 17:33:42 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|
|
|
|
|