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",
|
|
|
|
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() {
|
|
|
|
if(!this.tiddlerTitle) {
|
2012-03-03 18:35:52 +00:00
|
|
|
return Renderer.ErrorNode("Missing tiddler");
|
2012-01-23 16:42:12 +00:00
|
|
|
} else {
|
2012-02-21 21:57:30 +00:00
|
|
|
var tiddler = this.store.getTiddler(this.tiddlerTitle),
|
|
|
|
v = tiddler[this.params.field],
|
2012-02-16 20:38:10 +00:00
|
|
|
content,
|
|
|
|
t,
|
2012-02-19 17:20:16 +00:00
|
|
|
contentClone = [],
|
2012-02-21 21:57:30 +00:00
|
|
|
parents = this.parents;
|
2012-01-23 16:42:12 +00:00
|
|
|
if(v !== undefined) {
|
2012-02-21 21:57:30 +00:00
|
|
|
switch(this.params.format) {
|
2012-01-23 16:42:12 +00:00
|
|
|
case "link":
|
2012-02-16 20:38:10 +00:00
|
|
|
var link = Renderer.MacroNode("link",
|
|
|
|
{target: v},
|
|
|
|
[Renderer.TextNode(v)],
|
2012-02-21 21:57:30 +00:00
|
|
|
this.store);
|
2012-02-19 17:20:16 +00:00
|
|
|
link.execute(parents,tiddler);
|
2012-02-16 20:38:10 +00:00
|
|
|
return [link];
|
2012-01-23 16:42:12 +00:00
|
|
|
case "wikified":
|
2012-02-21 21:57:30 +00:00
|
|
|
if(this.params.field === "text") {
|
2012-02-19 17:20:16 +00:00
|
|
|
if(parents.indexOf(tiddler.title) === -1) {
|
2012-03-03 18:39:13 +00:00
|
|
|
content = this.store.parseTiddler(tiddler.title).nodes;
|
2012-02-19 17:20:16 +00:00
|
|
|
} else {
|
2012-03-03 18:35:52 +00:00
|
|
|
content = [Renderer.ErrorNode("Tiddler recursion error in <<view>> macro")];
|
2012-02-19 17:20:16 +00:00
|
|
|
}
|
|
|
|
parents = parents.slice(0);
|
|
|
|
parents.push(tiddler.title);
|
2012-02-11 18:19:34 +00:00
|
|
|
} else {
|
2012-03-03 18:39:13 +00:00
|
|
|
content = this.store.parseText("text/x-tiddlywiki",v).nodes;
|
2012-02-11 18:19:34 +00:00
|
|
|
}
|
2012-02-16 20:38:10 +00:00
|
|
|
for(t=0; t<content.length; t++) {
|
|
|
|
contentClone.push(content[t].clone());
|
|
|
|
}
|
|
|
|
for(t=0; t<contentClone.length; t++) {
|
2012-02-19 17:20:16 +00:00
|
|
|
contentClone[t].execute(parents,tiddler);
|
2012-02-16 20:38:10 +00:00
|
|
|
}
|
|
|
|
return contentClone;
|
2012-01-23 16:42:12 +00:00
|
|
|
case "date":
|
2012-02-21 21:57:30 +00:00
|
|
|
var template = this.params.template || "DD MMM YYYY";
|
2012-02-16 20:38:10 +00:00
|
|
|
return [Renderer.TextNode(utils.formatDateString(v,template))];
|
2012-01-23 16:42:12 +00:00
|
|
|
default: // "text"
|
2012-02-16 20:38:10 +00:00
|
|
|
return [Renderer.TextNode(v)];
|
2012-01-23 16:42:12 +00:00
|
|
|
}
|
2012-01-07 17:33:42 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-17 14:11:25 +00:00
|
|
|
return [];
|
2012-01-07 17:33:42 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|
|
|
|
|