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
|
|
|
|
*/
|
|
|
|
|
2014-02-17 11:28:48 +00:00
|
|
|
/*
|
|
|
|
Retrieve the value of the widget. Options are:
|
|
|
|
asString: Optionally return the value as a string
|
|
|
|
*/
|
|
|
|
ViewWidget.prototype.getValue = function(options) {
|
|
|
|
options = options || {};
|
|
|
|
var value = options.asString ? "" : undefined;
|
|
|
|
if(this.viewIndex) {
|
|
|
|
value = this.wiki.extractTiddlerDataItem(this.viewTitle,this.viewIndex);
|
|
|
|
} else {
|
|
|
|
var tiddler = this.wiki.getTiddler(this.viewTitle);
|
|
|
|
if(tiddler) {
|
|
|
|
if(this.viewField === "text") {
|
|
|
|
// Calling getTiddlerText() triggers lazy loading of skinny tiddlers
|
|
|
|
value = this.wiki.getTiddlerText(this.viewTitle);
|
2013-10-14 11:59:39 +00:00
|
|
|
} else {
|
2014-02-17 11:28:48 +00:00
|
|
|
if($tw.utils.hop(tiddler.fields,this.viewField)) {
|
|
|
|
if(options.asString) {
|
|
|
|
value = tiddler.getFieldString(this.viewField);
|
|
|
|
} else {
|
|
|
|
value = tiddler.fields[this.viewField];
|
|
|
|
}
|
|
|
|
}
|
2013-10-14 11:59:39 +00:00
|
|
|
}
|
2013-10-14 20:02:03 +00:00
|
|
|
} else {
|
2014-02-17 11:28:48 +00:00
|
|
|
if(this.viewField === "title") {
|
|
|
|
value = this.viewTitle;
|
|
|
|
}
|
2013-10-14 20:02:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
|
|
|
|
ViewWidget.prototype.getValueAsText = function() {
|
2014-02-17 11:28:48 +00:00
|
|
|
return this.getValue({asString: true});
|
2013-10-12 16:05:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ViewWidget.prototype.getValueAsHtmlWikified = function() {
|
2013-11-16 22:50:10 +00:00
|
|
|
return this.wiki.renderText("text/html","text/vnd.tiddlywiki",this.getValueAsText(),{parentWidget: 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) {
|
2014-01-03 18:34:27 +00:00
|
|
|
format = format || "YYYY MM DD 0hh:0mm";
|
2013-12-03 10:09:58 +00:00
|
|
|
var value = $tw.utils.parseDate(this.getValue());
|
|
|
|
if(value && $tw.utils.isDate(value) && value.toString() !== "Invalid Date") {
|
2013-11-13 19:41:54 +00:00
|
|
|
return $tw.utils.formatDateString(value,format);
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
2013-10-12 16:05:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ViewWidget.prototype.getValueAsRelativeDate = function(format) {
|
2013-12-30 13:08:48 +00:00
|
|
|
var value = $tw.utils.parseDate(this.getValue());
|
|
|
|
if(value && $tw.utils.isDate(value) && value.toString() !== "Invalid Date") {
|
2013-10-14 11:59:39 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
})();
|