2011-12-13 12:30:09 +00:00
|
|
|
/*\
|
|
|
|
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.
|
2011-11-30 18:39:39 +00:00
|
|
|
|
2011-12-13 12:30:09 +00:00
|
|
|
\*/
|
|
|
|
(function(){
|
2011-11-22 14:29:29 +00:00
|
|
|
|
2011-12-09 16:34:02 +00:00
|
|
|
/*jslint node: true */
|
2011-11-30 17:27:00 +00:00
|
|
|
"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 */) {
|
2012-01-17 13:01:55 +00:00
|
|
|
var fields = {}, // Keep the fields private, later we'll expose getters for them
|
|
|
|
tags, // Keep the tags separately because they're the only Array field
|
|
|
|
f,t,c,arg,src;
|
|
|
|
// Accumulate the supplied fields
|
|
|
|
for(c=0; c<arguments.length; c++) {
|
|
|
|
arg = arguments[c];
|
|
|
|
src = null;
|
2011-11-30 18:39:39 +00:00
|
|
|
if(arg instanceof Tiddler) {
|
2012-01-25 12:48:12 +00:00
|
|
|
src = arg.getFields();
|
2011-11-30 18:39:39 +00:00
|
|
|
} else {
|
|
|
|
src = arg;
|
|
|
|
}
|
2012-01-17 13:01:55 +00:00
|
|
|
for(t in src) {
|
2012-04-10 13:37:52 +00:00
|
|
|
if(src[t] === undefined) {
|
|
|
|
// If we get a field that's undefined, delete any previous field value
|
|
|
|
delete fields[t];
|
|
|
|
} else {
|
|
|
|
f = Tiddler.parseTiddlerField(t,src[t]);
|
|
|
|
if(f !== null) {
|
|
|
|
fields[t] = f;
|
|
|
|
}
|
2011-11-30 18:39:39 +00:00
|
|
|
}
|
2011-11-22 14:29:29 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-17 13:01:55 +00:00
|
|
|
// Pull out the tags
|
|
|
|
if(fields.tags) {
|
|
|
|
tags = fields.tags;
|
|
|
|
delete fields.tags;
|
|
|
|
}
|
|
|
|
// Expose the fields as read only properties
|
|
|
|
for(f in fields) {
|
|
|
|
Object.defineProperty(this,f,{value: fields[f], writeable: false});
|
|
|
|
}
|
|
|
|
// Expose the tags as a getter
|
|
|
|
Object.defineProperty(this,"tags",{get: function() {return tags ? tags.slice(0) : [];}});
|
2012-04-05 11:20:06 +00:00
|
|
|
|
|
|
|
// Get a copy of the fields hashmap
|
2012-01-17 13:01:55 +00:00
|
|
|
this.getFields = function() {
|
|
|
|
var r = {};
|
|
|
|
for(var f in fields) {
|
2012-01-17 14:36:27 +00:00
|
|
|
r[f] = fields[f];
|
2012-01-17 13:01:55 +00:00
|
|
|
}
|
|
|
|
if(tags) {
|
2012-01-17 14:36:27 +00:00
|
|
|
r.tags = tags.slice(0);
|
2012-01-17 13:01:55 +00:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
};
|
2012-04-05 11:20:06 +00:00
|
|
|
// Return a field as a string
|
|
|
|
this.getFieldString = function(field) {
|
|
|
|
var result, t;
|
|
|
|
if(field === "tags") {
|
|
|
|
if(tags) {
|
|
|
|
result = [];
|
|
|
|
for(t=0; t<tags.length; t++) {
|
|
|
|
if(tags[t].indexOf(" ") !== -1) {
|
|
|
|
result.push("[[" + tags[t] + "]]");
|
|
|
|
} else {
|
|
|
|
result.push(tags[t]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result.join(" ");
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(fields.hasOwnProperty(field)) {
|
|
|
|
if(field === "created" || field === "modified") {
|
|
|
|
return utils.convertToYYYYMMDDHHMM(fields[field]);
|
|
|
|
} else {
|
|
|
|
return fields[field];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Get all the tiddler fields as an array of {name:string,value:string} objects
|
|
|
|
this.getFieldStrings = function() {
|
|
|
|
var result = [],
|
|
|
|
fieldOrder = "title creator modifier created modified tags type".split(" "),
|
|
|
|
t,v;
|
|
|
|
for(t=0; t<fieldOrder.length; t++) {
|
|
|
|
v = this.getFieldString(fieldOrder[t]);
|
|
|
|
if(v !== null) {
|
|
|
|
result.push({name: fieldOrder[t], value: v});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(t in fields) {
|
|
|
|
if(fieldOrder.indexOf(t) === -1 && t !== "text") {
|
|
|
|
result.push({name: t, value: fields[t]});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
2012-01-17 13:01:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Tiddler.prototype.hasTag = function(tag) {
|
|
|
|
return this.tags.indexOf(tag) !== -1;
|
2011-11-30 18:39:39 +00:00
|
|
|
};
|
|
|
|
|
2011-12-13 12:30:09 +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
|
|
|
};
|
2011-12-13 12:30:09 +00:00
|
|
|
|
|
|
|
Tiddler.isStandardField = function(name) {
|
|
|
|
return name in Tiddler.standardFields;
|
|
|
|
};
|
|
|
|
|
2012-01-17 13:01:55 +00:00
|
|
|
Tiddler.compareTiddlerFields = function(a,b,sortField) {
|
|
|
|
var aa = a[sortField] || 0,
|
|
|
|
bb = b[sortField] || 0;
|
|
|
|
if(aa < bb) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
if(aa > bb) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
2011-12-08 16:19:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-17 13:01:55 +00:00
|
|
|
Tiddler.parseTiddlerField = function(name,value) {
|
2011-12-05 16:50:25 +00:00
|
|
|
var type = Tiddler.specialTiddlerFields[name];
|
2011-11-30 18:39:39 +00:00
|
|
|
if(type) {
|
2011-12-05 16:50:25 +00:00
|
|
|
return Tiddler.specialTiddlerFieldParsers[type](value);
|
2011-11-30 18:39:39 +00:00
|
|
|
} else if (typeof value === "string") {
|
|
|
|
return value;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// These are the non-string fields
|
2011-12-05 16:50:25 +00:00
|
|
|
Tiddler.specialTiddlerFields = {
|
2011-11-30 18:39:39 +00:00
|
|
|
"created": "date",
|
|
|
|
"modified": "date",
|
|
|
|
"tags": "array"
|
|
|
|
};
|
|
|
|
|
2011-12-05 16:50:25 +00:00
|
|
|
Tiddler.specialTiddlerFieldParsers = {
|
2011-11-30 18:39:39 +00:00
|
|
|
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();
|
2011-11-30 18:39:39 +00:00
|
|
|
} 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-30 18:39:39 +00:00
|
|
|
};
|
2011-11-22 14:29:29 +00:00
|
|
|
|
|
|
|
exports.Tiddler = Tiddler;
|
2011-12-12 10:52:04 +00:00
|
|
|
|
|
|
|
})();
|