1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-23 18:17:20 +00:00

Refactored the Tiddler class

Specifically, made the constructor more flexible
This commit is contained in:
Jeremy Ruston 2011-11-30 18:39:39 +00:00
parent 13230fd34a
commit 02137055ec

View File

@ -8,29 +8,82 @@ and 'created' fields are dates. All other fields are strings.
The Tiddler object exposes the following API
new Tiddler(fields) - create a Tiddler given a hashmap of field values
new Tiddler(tiddler,fields) - create a Tiddler from an existing tiddler with a hashmap of modified field values
new Tiddler(src) - create a Tiddler given a hashmap of field values or a tiddler to clone
new Tiddler(src1,src2) - create a Tiddler with the union of the fields from the
sources, with the rightmost taking priority
Tiddler.fields - hashmap of tiddler fields
The hashmap(s) can specify the "modified" and "created" fields as strings in YYYYMMDDHHMMSSMMM
format or as JavaScript date objects. The "tags" field can be given as a JavaScript array of strings or
as a TiddlyWiki quoted string (eg, "one [[two three]]").
*/
"use strict";
var Tiddler = function(/* tiddler,fields */) {
var tiddler, fields, c = 0, t;
if(arguments[c] instanceof Tiddler) {
tiddler = arguments[c++];
}
fields = arguments[c++];
this.fields = {};
if(tiddler instanceof Tiddler) {
for(t in tiddler.fields) {
this.fields[t] = tiddler.fields[t]; // Should copy arrays by value
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.parseField(t,src[t]);
if(f !== null) {
this.fields[t] = f;
}
}
}
for(t in fields) {
this.fields[t] = fields[t]; // Should copy arrays by value
};
Tiddler.prototype.parseField = function(name,value) {
var type = Tiddler.specialFields[name];
if(type) {
return Tiddler.specialParsers[type](value);
} else if (typeof value === "string") {
return value;
} else {
return null;
}
}
};
// These are the non-string fields
Tiddler.specialFields = {
"created": "date",
"modified": "date",
"tags": "array"
};
Tiddler.specialParsers = {
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") {
var parser = new ArgParser(value,{noNames: true});
return parser.getValuesByName("","");
} 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;
}
}
};
exports.Tiddler = Tiddler;