TiddlyWiki5/js/Tiddler.js

120 wiersze
2.8 KiB
JavaScript
Czysty Zwykły widok Historia

/*\
title: js/Tiddler.js
2011-11-22 14:29:29 +00:00
2012-01-05 11:31:40 +00:00
Tiddlers are an immutable dictionary of name:value pairs called fields. Values can be a string,
an array of strings, or a JavaScript date object.
2011-11-22 14:29:29 +00:00
2012-01-05 11:31:40 +00:00
The only field that is required is the `title` field, but useful tiddlers also have a `text`
field, and some or all of the standard fields `modified`, `modifier`, `created`, `creator`,
`tags` and `type`.
2011-11-22 14:29:29 +00:00
2012-01-05 11:31:40 +00:00
Hardcoded in the system is the knowledge that the 'tags' field is a string array, and that
the 'modified' and 'created' fields are dates. All other fields are strings.
\*/
(function(){
2011-11-22 14:29:29 +00:00
/*jslint node: true */
"use strict";
2011-11-30 19:35:01 +00:00
var utils = require("./Utils.js"),
2012-01-06 17:53:37 +00:00
ArgParser = require("./ArgParser.js").ArgParser;
2011-11-30 19:35:01 +00:00
2011-11-22 14:29:29 +00:00
var Tiddler = function(/* tiddler,fields */) {
this.parseTree = null; // Caches the parse tree for the tiddler
this.renderers = {}; // Caches rendering functions for this tiddler (indexed by MIME type)
this.renditions = {}; // Caches the renditions produced by those functions (indexed by MIME type)
2011-11-22 14:29:29 +00:00
this.fields = {};
for(var c=0; c<arguments.length; c++) {
var arg = arguments[c],
src = null;
if(arg instanceof Tiddler) {
src = arg.fields;
} else {
src = arg;
}
for(var t in src) {
var f = this.parseTiddlerField(t,src[t]);
if(f !== null) {
this.fields[t] = f;
}
2011-11-22 14:29:29 +00:00
}
}
};
Tiddler.standardFields = {
title: { type: "string"},
modifier: { type: "string"},
modified: { type: "date"},
creator: { type: "string"},
created: { type: "date"},
tags: { type: "tags"},
type: { type: "string"},
text: { type: "string"}
2011-12-14 14:11:11 +00:00
};
Tiddler.isStandardField = function(name) {
return name in Tiddler.standardFields;
};
2011-12-08 16:19:19 +00:00
Tiddler.prototype.hasTag = function(tag) {
if(this.tags) {
for(var t=0; t<this.tags.length; t++) {
if(this.tags[t] === tag) {
return true;
}
}
}
return false;
};
Tiddler.prototype.parseTiddlerField = function(name,value) {
var type = Tiddler.specialTiddlerFields[name];
if(type) {
return Tiddler.specialTiddlerFieldParsers[type](value);
} else if (typeof value === "string") {
return value;
} else {
return null;
}
};
// These are the non-string fields
Tiddler.specialTiddlerFields = {
"created": "date",
"modified": "date",
"tags": "array"
};
Tiddler.specialTiddlerFieldParsers = {
date: function(value) {
if(typeof value === "string") {
return utils.convertFromYYYYMMDDHHMMSSMMM(value);
} else if (value instanceof Date) {
return value;
} else {
return null;
}
},
array: function(value) {
if(typeof value === "string") {
2012-01-03 11:08:56 +00:00
var parser = new ArgParser(value,{noNames: true, allowEval: false});
return parser.getStringValues();
} else if (value instanceof Array) {
var result = [];
for(var t=0; t<value.length; t++) {
if(typeof value[t] === "string") {
result.push(value[t]);
}
}
return result;
} else {
return null;
}
2011-11-22 14:29:29 +00:00
}
};
2011-11-22 14:29:29 +00:00
exports.Tiddler = Tiddler;
})();