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"));
|
2014-10-22 13:12:49 +00:00
|
|
|
this.viewSubtiddler = this.getAttribute("subtiddler");
|
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","");
|
2017-08-24 19:58:08 +00:00
|
|
|
this.viewMode = this.getAttribute("mode","block");
|
2013-10-12 16:05:13 +00:00
|
|
|
switch(this.viewFormat) {
|
|
|
|
case "htmlwikified":
|
2017-08-24 19:58:08 +00:00
|
|
|
this.text = this.getValueAsHtmlWikified(this.viewMode);
|
2013-10-12 16:05:13 +00:00
|
|
|
break;
|
2016-06-23 15:28:59 +00:00
|
|
|
case "plainwikified":
|
2017-08-24 19:58:08 +00:00
|
|
|
this.text = this.getValueAsPlainWikified(this.viewMode);
|
2016-06-23 15:28:59 +00:00
|
|
|
break;
|
2015-09-11 12:33:20 +00:00
|
|
|
case "htmlencodedplainwikified":
|
2017-08-24 19:58:08 +00:00
|
|
|
this.text = this.getValueAsHtmlEncodedPlainWikified(this.viewMode);
|
2015-09-11 12:33:20 +00:00
|
|
|
break;
|
2013-10-12 16:05:13 +00:00
|
|
|
case "htmlencoded":
|
|
|
|
this.text = this.getValueAsHtmlEncoded();
|
|
|
|
break;
|
2021-04-02 08:32:32 +00:00
|
|
|
case "htmltextencoded":
|
|
|
|
this.text = this.getValueAsHtmlTextEncoded();
|
|
|
|
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 {
|
2014-10-22 13:12:49 +00:00
|
|
|
var tiddler;
|
|
|
|
if(this.viewSubtiddler) {
|
2021-05-30 18:20:17 +00:00
|
|
|
tiddler = this.wiki.getSubTiddler(this.viewTitle,this.viewSubtiddler);
|
2014-10-22 13:12:49 +00:00
|
|
|
} else {
|
|
|
|
tiddler = this.wiki.getTiddler(this.viewTitle);
|
|
|
|
}
|
2014-02-17 11:28:48 +00:00
|
|
|
if(tiddler) {
|
2014-10-22 13:12:49 +00:00
|
|
|
if(this.viewField === "text" && !this.viewSubtiddler) {
|
2014-02-17 11:28:48 +00:00
|
|
|
// 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 {
|
2021-05-30 18:20:17 +00:00
|
|
|
value = tiddler.fields[this.viewField];
|
2014-02-17 11:28:48 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
};
|
|
|
|
|
2017-08-24 19:58:08 +00:00
|
|
|
ViewWidget.prototype.getValueAsHtmlWikified = function(mode) {
|
|
|
|
return this.wiki.renderText("text/html","text/vnd.tiddlywiki",this.getValueAsText(),{
|
|
|
|
parseAsInline: mode !== "block",
|
|
|
|
parentWidget: this
|
|
|
|
});
|
2013-10-12 16:05:13 +00:00
|
|
|
};
|
|
|
|
|
2017-08-24 19:58:08 +00:00
|
|
|
ViewWidget.prototype.getValueAsPlainWikified = function(mode) {
|
|
|
|
return this.wiki.renderText("text/plain","text/vnd.tiddlywiki",this.getValueAsText(),{
|
|
|
|
parseAsInline: mode !== "block",
|
|
|
|
parentWidget: this
|
|
|
|
});
|
2016-06-23 15:28:59 +00:00
|
|
|
};
|
|
|
|
|
2017-08-24 19:58:08 +00:00
|
|
|
ViewWidget.prototype.getValueAsHtmlEncodedPlainWikified = function(mode) {
|
|
|
|
return $tw.utils.htmlEncode(this.wiki.renderText("text/plain","text/vnd.tiddlywiki",this.getValueAsText(),{
|
|
|
|
parseAsInline: mode !== "block",
|
|
|
|
parentWidget: this
|
|
|
|
}));
|
2015-09-11 12:33:20 +00:00
|
|
|
};
|
|
|
|
|
2013-10-12 16:05:13 +00:00
|
|
|
ViewWidget.prototype.getValueAsHtmlEncoded = function() {
|
|
|
|
return $tw.utils.htmlEncode(this.getValueAsText());
|
|
|
|
};
|
|
|
|
|
2021-04-02 08:32:32 +00:00
|
|
|
ViewWidget.prototype.getValueAsHtmlTextEncoded = function() {
|
|
|
|
return $tw.utils.htmlTextEncode(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 {
|
2021-05-30 18:20:17 +00:00
|
|
|
return false;
|
2013-10-12 16:05:13 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.view = ViewWidget;
|
|
|
|
|
|
|
|
})();
|