mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-23 18:17:20 +00:00
Add a relative date field viewer
With a bit of attendant refactoring
This commit is contained in:
parent
ecd933dd25
commit
c6add09e54
@ -48,14 +48,22 @@ exports.executeMacro = function() {
|
||||
}
|
||||
// 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(tiddler && this.params.format) {
|
||||
var viewer = this.wiki.macros.view.fieldviewers[this.params.format];
|
||||
Viewer = this.wiki.macros.view.fieldviewers[this.params.format];
|
||||
}
|
||||
if(!viewer) {
|
||||
viewer = this.wiki.macros.view.fieldviewers["text"];
|
||||
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 viewer(tiddler,field,value,this);
|
||||
return this.viewer.render();
|
||||
};
|
||||
|
||||
exports.postRenderInDom = function() {
|
||||
if(this.viewer.postRenderInDom) {
|
||||
this.viewer.postRenderInDom();
|
||||
}
|
||||
};
|
||||
|
||||
})();
|
||||
|
@ -12,15 +12,22 @@ A viewer for viewing tiddler fields as a date
|
||||
/*global $tw: false */
|
||||
"use strict";
|
||||
|
||||
function renderValue(tiddler,field,value,viewMacro) {
|
||||
var template = viewMacro.params.template || "DD MMM YYYY";
|
||||
if(value === undefined) {
|
||||
var DateViewer = function(viewMacro,tiddler,field,value) {
|
||||
this.viewMacro = viewMacro;
|
||||
this.tiddler = tiddler;
|
||||
this.field = field;
|
||||
this.value = value;
|
||||
};
|
||||
|
||||
DateViewer.prototype.render = function() {
|
||||
var template = this.viewMacro.params.template || "DD MMM YYYY";
|
||||
if(this.value === undefined) {
|
||||
return $tw.Tree.Text("");
|
||||
} else {
|
||||
return $tw.Tree.Text($tw.utils.formatDateString(value,template));
|
||||
return $tw.Tree.Text($tw.utils.formatDateString(this.value,template));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
exports["date"] = renderValue;
|
||||
exports["date"] = DateViewer;
|
||||
|
||||
})();
|
||||
|
@ -12,21 +12,28 @@ A viewer for viewing tiddler fields as a link
|
||||
/*global $tw: false */
|
||||
"use strict";
|
||||
|
||||
function renderValue(tiddler,field,value,viewMacro) {
|
||||
if(value === undefined) {
|
||||
var LinkViewer = function(viewMacro,tiddler,field,value) {
|
||||
this.viewMacro = viewMacro;
|
||||
this.tiddler = tiddler;
|
||||
this.field = field;
|
||||
this.value = value;
|
||||
};
|
||||
|
||||
LinkViewer.prototype.render = function() {
|
||||
if(this.value === undefined) {
|
||||
return $tw.Tree.Text("");
|
||||
} else {
|
||||
var link = $tw.Tree.Macro("link",{
|
||||
srcParams: {to: value},
|
||||
content: [$tw.Tree.Text(value)],
|
||||
isBlock: viewMacro.isBlock,
|
||||
wiki: viewMacro.wiki
|
||||
srcParams: {to: this.value},
|
||||
content: [$tw.Tree.Text(this.value)],
|
||||
isBlock: this.viewMacro.isBlock,
|
||||
wiki: this.viewMacro.wiki
|
||||
});
|
||||
link.execute(viewMacro.parents,viewMacro.tiddlerTitle);
|
||||
link.execute(this.viewMacro.parents,this.viewMacro.tiddlerTitle);
|
||||
return link;
|
||||
}
|
||||
}
|
||||
|
||||
exports["link"] = renderValue;
|
||||
exports["link"] = LinkViewer;
|
||||
|
||||
})();
|
||||
|
@ -12,16 +12,62 @@ A viewer for viewing tiddler fields as a relative date
|
||||
/*global $tw: false */
|
||||
"use strict";
|
||||
|
||||
function renderValue(tiddler,field,value,viewMacro) {
|
||||
if(value === undefined) {
|
||||
var RelativeDateViewer = function(viewMacro,tiddler,field,value) {
|
||||
this.viewMacro = viewMacro;
|
||||
this.tiddler = tiddler;
|
||||
this.field = field;
|
||||
this.value = value;
|
||||
};
|
||||
|
||||
RelativeDateViewer.prototype.render = function() {
|
||||
if(this.value === undefined) {
|
||||
return $tw.Tree.Text("");
|
||||
} else {
|
||||
return $tw.Tree.Text(
|
||||
$tw.utils.getRelativeDate((new Date()) - value).description
|
||||
);
|
||||
this.relativeDate = $tw.utils.getRelativeDate((new Date()) - this.value);
|
||||
return $tw.Tree.Element(this.viewMacro.isBlock ? "div" : "span",{},[
|
||||
$tw.Tree.Text(
|
||||
this.relativeDate.description
|
||||
)
|
||||
]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
exports["relativedate"] = renderValue;
|
||||
/*
|
||||
Trigger the timer when the relative date is put into the DOM
|
||||
*/
|
||||
RelativeDateViewer.prototype.postRenderInDom = function() {
|
||||
if(this.relativeDate) {
|
||||
this.setTimer();
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
Trigger the timer for the next update of the relative date
|
||||
*/
|
||||
RelativeDateViewer.prototype.setTimer = function() {
|
||||
var self = this;
|
||||
if(this.relativeDate.updatePeriod < 24 * 60 * 60 * 1000) {
|
||||
window.setTimeout(function() {
|
||||
// Only call the update function if the dom node is still in the document
|
||||
if($tw.utils.domContains(document,self.viewMacro.child.domNode)) {
|
||||
self.update.call(self);
|
||||
}
|
||||
},this.relativeDate.updatePeriod);
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
Update the relative date display, and trigger the timer for the next update
|
||||
*/
|
||||
RelativeDateViewer.prototype.update = function() {
|
||||
while(this.viewMacro.child.domNode.hasChildNodes()) {
|
||||
this.viewMacro.child.domNode.removeChild(this.viewMacro.child.domNode.firstChild);
|
||||
}
|
||||
this.relativeDate = $tw.utils.getRelativeDate((new Date()) - this.value);
|
||||
this.viewMacro.child.domNode.appendChild(document.createTextNode(this.relativeDate.description));
|
||||
this.setTimer();
|
||||
};
|
||||
|
||||
exports["relativedate"] = RelativeDateViewer;
|
||||
|
||||
})();
|
||||
|
@ -12,19 +12,26 @@ A viewer for viewing tiddler fields as plain text
|
||||
/*global $tw: false */
|
||||
"use strict";
|
||||
|
||||
function renderValue(tiddler,field,value,viewMacro) {
|
||||
var TextViewer = function(viewMacro,tiddler,field,value) {
|
||||
this.viewMacro = viewMacro;
|
||||
this.tiddler = tiddler;
|
||||
this.field = field;
|
||||
this.value = value;
|
||||
};
|
||||
|
||||
TextViewer.prototype.render = function() {
|
||||
// Get the value as a string
|
||||
if(field !== "text" && tiddler) {
|
||||
value = tiddler.getFieldString(field);
|
||||
if(this.field !== "text" && this.tiddler) {
|
||||
this.value = this.tiddler.getFieldString(this.field);
|
||||
}
|
||||
// Return the text
|
||||
if(value === undefined || value === null) {
|
||||
if(this.value === undefined || this.value === null) {
|
||||
return $tw.Tree.Text("");
|
||||
} else {
|
||||
return $tw.Tree.Text(value);
|
||||
return $tw.Tree.Text(this.value);
|
||||
}
|
||||
}
|
||||
|
||||
exports["text"] = renderValue;
|
||||
exports["text"] = TextViewer;
|
||||
|
||||
})();
|
||||
|
@ -12,20 +12,27 @@ A viewer that transcludes the tiddler whose title is specified in the viewed fie
|
||||
/*global $tw: false */
|
||||
"use strict";
|
||||
|
||||
function renderValue(tiddler,field,value,viewMacro) {
|
||||
if(tiddler && viewMacro.params.field && (viewMacro.params.field in tiddler.fields)) {
|
||||
var children = viewMacro.wiki.parseTiddler(tiddler.fields[viewMacro.params.field]).tree,
|
||||
var TranscludeViewer = function(viewMacro,tiddler,field,value) {
|
||||
this.viewMacro = viewMacro;
|
||||
this.tiddler = tiddler;
|
||||
this.field = field;
|
||||
this.value = value;
|
||||
};
|
||||
|
||||
TranscludeViewer.prototype.render = function() {
|
||||
if(this.tiddler && this.viewMacro.params.field && (this.viewMacro.params.field in this.tiddler.fields)) {
|
||||
var children = this.viewMacro.wiki.parseTiddler(this.tiddler.fields[this.viewMacro.params.field]).tree,
|
||||
childrenClone = [],t;
|
||||
for(t=0; t<children.length; t++) {
|
||||
childrenClone.push(children[t].clone());
|
||||
}
|
||||
for(t=0; t<childrenClone.length; t++) {
|
||||
childrenClone[t].execute(viewMacro.parents,viewMacro.tiddlerTitle);
|
||||
childrenClone[t].execute(this.viewMacro.parents,this.viewMacro.tiddlerTitle);
|
||||
}
|
||||
return $tw.Tree.Element(viewMacro.isBlock ? "div" : "span",{},childrenClone);
|
||||
return $tw.Tree.Element(this.viewMacro.isBlock ? "div" : "span",{},childrenClone);
|
||||
}
|
||||
}
|
||||
|
||||
exports["transclude"] = renderValue;
|
||||
exports["transclude"] = TranscludeViewer;
|
||||
|
||||
})();
|
||||
|
@ -12,31 +12,38 @@ A viewer for viewing tiddler fields as wikified text
|
||||
/*global $tw: false */
|
||||
"use strict";
|
||||
|
||||
function renderValue(tiddler,field,value,viewMacro) {
|
||||
var WikifiedViewer = function(viewMacro,tiddler,field,value) {
|
||||
this.viewMacro = viewMacro;
|
||||
this.tiddler = tiddler;
|
||||
this.field = field;
|
||||
this.value = value;
|
||||
};
|
||||
|
||||
WikifiedViewer.prototype.render = function() {
|
||||
// Check for recursion
|
||||
var parents = viewMacro.parents,
|
||||
var parents = this.viewMacro.parents,
|
||||
children,t,childrenClone = [];
|
||||
if(tiddler && viewMacro.params.field === "text") {
|
||||
if(parents.indexOf(tiddler.fields.title) !== -1) {
|
||||
if(this.tiddler && this.viewMacro.params.field === "text") {
|
||||
if(parents.indexOf(this.tiddler.fields.title) !== -1) {
|
||||
children = [$tw.Tree.errorNode("Tiddler recursion error in <<view>> macro")];
|
||||
} else {
|
||||
children = viewMacro.wiki.parseTiddler(tiddler.fields.title).tree;
|
||||
children = this.viewMacro.wiki.parseTiddler(this.tiddler.fields.title).tree;
|
||||
}
|
||||
parents = parents.slice(0);
|
||||
parents.push(tiddler.fields.title);
|
||||
parents.push(this.tiddler.fields.title);
|
||||
} else {
|
||||
children = viewMacro.wiki.parseText("text/x-tiddlywiki",value).tree;
|
||||
children = this.viewMacro.wiki.parseText("text/x-tiddlywiki",value).tree;
|
||||
}
|
||||
// Clone and execute the parsed wikitext
|
||||
for(t=0; t<children.length; t++) {
|
||||
childrenClone.push(children[t].clone());
|
||||
}
|
||||
for(t=0; t<childrenClone.length; t++) {
|
||||
childrenClone[t].execute(parents,viewMacro.tiddlerTitle);
|
||||
childrenClone[t].execute(parents,this.viewMacro.tiddlerTitle);
|
||||
}
|
||||
return $tw.Tree.Element(viewMacro.isBlock ? "div" : "span",{},childrenClone);
|
||||
return $tw.Tree.Element(this.viewMacro.isBlock ? "div" : "span",{},childrenClone);
|
||||
}
|
||||
|
||||
exports["wikified"] = renderValue;
|
||||
exports["wikified"] = WikifiedViewer;
|
||||
|
||||
})();
|
||||
|
Loading…
Reference in New Issue
Block a user