2013-10-12 16:05:13 +00:00
|
|
|
/*\
|
2013-11-08 08:47:00 +00:00
|
|
|
title: $:/core/modules/widgets/view.js
|
2013-10-12 16:05:13 +00:00
|
|
|
type: application/javascript
|
2013-11-08 08:47:00 +00:00
|
|
|
module-type: widget
|
2013-10-12 16:05:13 +00:00
|
|
|
|
|
|
|
View widget
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
2013-11-08 08:47:00 +00:00
|
|
|
var Widget = require("$:/core/modules/widgets/widget.js").widget;
|
2013-10-12 16:05:13 +00:00
|
|
|
|
|
|
|
var ViewWidget = function(parseTreeNode,options) {
|
|
|
|
this.initialise(parseTreeNode,options);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Inherit from the base widget class
|
|
|
|
*/
|
|
|
|
ViewWidget.prototype = new Widget();
|
|
|
|
|
|
|
|
/*
|
|
|
|
Render this widget into the DOM
|
|
|
|
*/
|
|
|
|
ViewWidget.prototype.render = function(parent,nextSibling) {
|
|
|
|
this.parentDomNode = parent;
|
|
|
|
this.computeAttributes();
|
|
|
|
this.execute();
|
2013-11-09 17:30:06 +00:00
|
|
|
if(this.text) {
|
|
|
|
var textNode = this.document.createTextNode(this.text);
|
|
|
|
parent.insertBefore(textNode,nextSibling);
|
|
|
|
this.domNodes.push(textNode);
|
|
|
|
} else {
|
|
|
|
this.makeChildWidgets();
|
|
|
|
this.renderChildren(parent,nextSibling);
|
|
|
|
}
|
2013-10-12 16:05:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Compute the internal state of the widget
|
|
|
|
*/
|
|
|
|
ViewWidget.prototype.execute = function() {
|
|
|
|
// Get parameters from our attributes
|
2013-10-30 13:36:44 +00:00
|
|
|
this.viewTitle = this.getAttribute("tiddler",this.getVariable("currentTiddler"));
|
2013-10-12 16:05:13 +00:00
|
|
|
this.viewField = this.getAttribute("field","text");
|
|
|
|
this.viewIndex = this.getAttribute("index");
|
|
|
|
this.viewFormat = this.getAttribute("format","text");
|
2013-10-13 21:59:14 +00:00
|
|
|
this.viewTemplate = this.getAttribute("template","");
|
2013-10-12 16:05:13 +00:00
|
|
|
switch(this.viewFormat) {
|
|
|
|
case "htmlwikified":
|
|
|
|
this.text = this.getValueAsHtmlWikified();
|
|
|
|
break;
|
|
|
|
case "htmlencoded":
|
|
|
|
this.text = this.getValueAsHtmlEncoded();
|
|
|
|
break;
|
2013-11-12 20:29:14 +00:00
|
|
|
case "urlencoded":
|
|
|
|
this.text = this.getValueAsUrlEncoded();
|
|
|
|
break;
|
|
|
|
case "doubleurlencoded":
|
|
|
|
this.text = this.getValueAsDoubleUrlEncoded();
|
|
|
|
break;
|
2013-10-12 16:05:13 +00:00
|
|
|
case "date":
|
2013-10-13 21:59:14 +00:00
|
|
|
this.text = this.getValueAsDate(this.viewTemplate);
|
2013-10-12 16:05:13 +00:00
|
|
|
break;
|
|
|
|
case "relativedate":
|
|
|
|
this.text = this.getValueAsRelativeDate();
|
|
|
|
break;
|
2013-10-25 11:32:57 +00:00
|
|
|
case "stripcomments":
|
|
|
|
this.text = this.getValueAsStrippedComments();
|
|
|
|
break;
|
2013-10-25 22:00:43 +00:00
|
|
|
case "jsencoded":
|
|
|
|
this.text = this.getValueAsJsEncoded();
|
|
|
|
break;
|
2013-10-12 16:05:13 +00:00
|
|
|
default: // "text"
|
|
|
|
this.text = this.getValueAsText();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
The various formatter functions are baked into this widget for the moment. Eventually they will be replaced by macro functions
|
|
|
|
*/
|
|
|
|
|
2013-10-14 20:02:03 +00:00
|
|
|
ViewWidget.prototype.getValue = function() {
|
2013-10-12 16:05:13 +00:00
|
|
|
// Get the value to display
|
2013-10-14 20:02:03 +00:00
|
|
|
var value,
|
2013-10-12 16:05:13 +00:00
|
|
|
tiddler = this.wiki.getTiddler(this.viewTitle);
|
|
|
|
if(tiddler) {
|
|
|
|
if(this.viewField === "text") {
|
|
|
|
// Calling getTiddlerText() triggers lazy loading of skinny tiddlers
|
2013-10-14 20:02:03 +00:00
|
|
|
value = this.wiki.getTiddlerText(this.viewTitle);
|
2013-10-12 16:05:13 +00:00
|
|
|
} else {
|
2013-10-14 11:59:39 +00:00
|
|
|
if($tw.utils.hop(tiddler.fields,this.viewField)) {
|
2013-10-14 20:02:03 +00:00
|
|
|
value = tiddler.fields[this.viewField];
|
2013-10-14 11:59:39 +00:00
|
|
|
} else {
|
2013-10-14 20:02:03 +00:00
|
|
|
value = "";
|
2013-10-14 11:59:39 +00:00
|
|
|
}
|
2013-10-12 16:05:13 +00:00
|
|
|
}
|
2013-10-14 20:02:03 +00:00
|
|
|
} else {
|
|
|
|
if(this.viewField === "title") {
|
|
|
|
value = this.viewTitle;
|
|
|
|
} else {
|
|
|
|
value = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
|
|
|
|
ViewWidget.prototype.getValueAsText = function() {
|
|
|
|
// Get the value to display
|
|
|
|
var text,
|
|
|
|
tiddler = this.wiki.getTiddler(this.viewTitle);
|
|
|
|
if(tiddler) {
|
|
|
|
if(this.viewField === "text") {
|
|
|
|
// Calling getTiddlerText() triggers lazy loading of skinny tiddlers
|
|
|
|
text = this.wiki.getTiddlerText(this.viewTitle);
|
|
|
|
} else {
|
|
|
|
text = tiddler.getFieldString(this.viewField);
|
|
|
|
}
|
2013-10-12 16:05:13 +00:00
|
|
|
} else { // Use a special value if the tiddler is missing
|
2013-10-14 20:02:03 +00:00
|
|
|
if(this.viewField === "title") {
|
|
|
|
text = this.viewTitle;
|
|
|
|
} else {
|
|
|
|
text = "";
|
2013-10-12 16:05:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
};
|
|
|
|
|
|
|
|
ViewWidget.prototype.getValueAsHtmlWikified = function() {
|
2013-11-08 08:51:14 +00:00
|
|
|
return this.wiki.renderText("text/html","text/vnd.tiddlywiki",this.getValueAsText(),this);
|
2013-10-12 16:05:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ViewWidget.prototype.getValueAsHtmlEncoded = function() {
|
|
|
|
return $tw.utils.htmlEncode(this.getValueAsText());
|
|
|
|
};
|
|
|
|
|
2013-11-12 20:29:14 +00:00
|
|
|
ViewWidget.prototype.getValueAsUrlEncoded = function() {
|
|
|
|
return encodeURIComponent(this.getValueAsText());
|
|
|
|
};
|
|
|
|
|
|
|
|
ViewWidget.prototype.getValueAsDoubleUrlEncoded = function() {
|
|
|
|
return encodeURIComponent(encodeURIComponent(this.getValueAsText()));
|
|
|
|
};
|
|
|
|
|
2013-10-12 16:05:13 +00:00
|
|
|
ViewWidget.prototype.getValueAsDate = function(format) {
|
2013-10-14 20:02:03 +00:00
|
|
|
return $tw.utils.formatDateString(this.getValue(),format);
|
2013-10-12 16:05:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ViewWidget.prototype.getValueAsRelativeDate = function(format) {
|
2013-10-14 20:02:03 +00:00
|
|
|
var value = this.getValue();
|
2013-10-14 11:59:39 +00:00
|
|
|
if(value) {
|
|
|
|
return $tw.utils.getRelativeDate((new Date()) - (new Date(value))).description;
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
2013-10-12 16:05:13 +00:00
|
|
|
};
|
|
|
|
|
2013-10-25 11:32:57 +00:00
|
|
|
ViewWidget.prototype.getValueAsStrippedComments = function() {
|
|
|
|
var lines = this.getValueAsText().split("\n"),
|
|
|
|
out = [];
|
|
|
|
for(var line=0; line<lines.length; line++) {
|
|
|
|
var text = lines[line];
|
|
|
|
if(!/^\s*\/\/#/.test(text)) {
|
|
|
|
out.push(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out.join("\n");
|
|
|
|
};
|
|
|
|
|
2013-10-25 22:00:43 +00:00
|
|
|
ViewWidget.prototype.getValueAsJsEncoded = function() {
|
|
|
|
return $tw.utils.stringify(this.getValueAsText());
|
|
|
|
};
|
|
|
|
|
2013-10-12 16:05:13 +00:00
|
|
|
/*
|
|
|
|
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
|
|
|
|
*/
|
|
|
|
ViewWidget.prototype.refresh = function(changedTiddlers) {
|
|
|
|
var changedAttributes = this.computeAttributes();
|
2013-10-30 13:36:44 +00:00
|
|
|
if(changedAttributes.tiddler || changedAttributes.field || changedAttributes.index || changedAttributes.template || changedAttributes.format || changedTiddlers[this.viewTitle]) {
|
2013-10-12 16:05:13 +00:00
|
|
|
this.refreshSelf();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.view = ViewWidget;
|
|
|
|
|
|
|
|
})();
|