2011-12-13 12:30:09 +00:00
|
|
|
/*\
|
|
|
|
title: js/WikiStore.js
|
|
|
|
|
|
|
|
\*/
|
2011-12-12 10:52:04 +00:00
|
|
|
(function(){
|
|
|
|
|
2011-12-09 16:34:02 +00:00
|
|
|
/*jslint node: true */
|
2011-11-30 17:27:00 +00:00
|
|
|
"use strict";
|
|
|
|
|
2011-12-05 16:50:25 +00:00
|
|
|
var Tiddler = require("./Tiddler.js").Tiddler,
|
2012-01-03 11:09:59 +00:00
|
|
|
utils = require("./Utils.js"),
|
2011-12-05 16:50:25 +00:00
|
|
|
util = require("util");
|
2011-11-22 14:29:29 +00:00
|
|
|
|
2011-12-28 17:16:56 +00:00
|
|
|
/* Creates a new WikiStore object
|
|
|
|
|
|
|
|
Available options are:
|
|
|
|
shadowStore: An existing WikiStore to use for shadow tiddler storage. Pass null to prevent a default shadow store from being created
|
|
|
|
*/
|
2011-12-11 18:28:09 +00:00
|
|
|
var WikiStore = function WikiStore(options) {
|
2011-12-28 17:16:56 +00:00
|
|
|
options = options || {};
|
2011-11-22 14:29:29 +00:00
|
|
|
this.tiddlers = {};
|
2011-12-28 17:16:56 +00:00
|
|
|
this.textProcessors = {};
|
|
|
|
this.tiddlerSerializers = {};
|
|
|
|
this.tiddlerDeserializers = {};
|
2011-12-28 22:07:17 +00:00
|
|
|
this.sandbox = options.sandbox;
|
2011-12-11 18:28:09 +00:00
|
|
|
this.shadows = options.shadowStore !== undefined ? options.shadowStore : new WikiStore({
|
2011-12-28 17:16:56 +00:00
|
|
|
shadowStore: null
|
2011-12-11 18:28:09 +00:00
|
|
|
});
|
2011-11-22 14:29:29 +00:00
|
|
|
};
|
|
|
|
|
2011-12-28 17:16:56 +00:00
|
|
|
WikiStore.prototype.registerTextProcessor = function(type,processor) {
|
|
|
|
this.textProcessors[type] = processor;
|
|
|
|
};
|
|
|
|
|
|
|
|
WikiStore.prototype.registerTiddlerSerializer = function(extension,mimeType,serializer) {
|
|
|
|
this.tiddlerSerializers[extension] = serializer;
|
|
|
|
this.tiddlerSerializers[mimeType] = serializer;
|
|
|
|
};
|
|
|
|
|
|
|
|
WikiStore.prototype.registerTiddlerDeserializer = function(extension,mimeType,deserializer) {
|
|
|
|
this.tiddlerDeserializers[extension] = deserializer;
|
|
|
|
this.tiddlerDeserializers[mimeType] = deserializer;
|
2011-12-01 10:19:21 +00:00
|
|
|
};
|
2011-11-22 14:29:29 +00:00
|
|
|
|
2011-12-06 18:29:53 +00:00
|
|
|
WikiStore.prototype.getTiddler = function(title) {
|
2011-11-22 14:29:29 +00:00
|
|
|
var t = this.tiddlers[title];
|
2011-12-03 17:02:34 +00:00
|
|
|
if(t instanceof Tiddler) {
|
|
|
|
return t;
|
|
|
|
} else if(this.shadows) {
|
|
|
|
return this.shadows.getTiddler(title);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2011-12-01 10:19:21 +00:00
|
|
|
};
|
2011-11-22 14:29:29 +00:00
|
|
|
|
2011-12-06 18:29:53 +00:00
|
|
|
WikiStore.prototype.getTiddlerText = function(title) {
|
2011-12-03 17:02:34 +00:00
|
|
|
var t = this.getTiddler(title);
|
2011-12-01 15:07:10 +00:00
|
|
|
return t instanceof Tiddler ? t.fields.text : null;
|
|
|
|
};
|
|
|
|
|
2011-12-06 18:29:53 +00:00
|
|
|
WikiStore.prototype.deleteTiddler = function(title) {
|
2011-11-22 14:29:29 +00:00
|
|
|
delete this.tiddlers[title];
|
2011-12-01 10:19:21 +00:00
|
|
|
};
|
2011-11-22 14:29:29 +00:00
|
|
|
|
2011-12-06 18:29:53 +00:00
|
|
|
WikiStore.prototype.tiddlerExists = function(title) {
|
2011-12-16 10:48:36 +00:00
|
|
|
var exists = this.tiddlers[title] instanceof Tiddler;
|
|
|
|
if(exists) {
|
|
|
|
return true;
|
|
|
|
} else if (this.shadows) {
|
|
|
|
return this.shadows.tiddlerExists(title);
|
|
|
|
}
|
|
|
|
return ;
|
2011-12-02 14:40:18 +00:00
|
|
|
};
|
2011-12-01 15:07:10 +00:00
|
|
|
|
2011-12-06 18:29:53 +00:00
|
|
|
WikiStore.prototype.addTiddler = function(tiddler) {
|
2011-12-01 15:07:10 +00:00
|
|
|
this.tiddlers[tiddler.fields.title] = tiddler;
|
2011-12-01 10:19:21 +00:00
|
|
|
};
|
2011-11-22 14:29:29 +00:00
|
|
|
|
2011-12-12 08:59:28 +00:00
|
|
|
WikiStore.prototype.forEachTiddler = function(/* [sortField,[excludeTag,]]callback */) {
|
|
|
|
var a = 0,
|
|
|
|
sortField = arguments.length > 1 ? arguments[a++] : null,
|
|
|
|
excludeTag = arguments.length > 2 ? arguments[a++] : null,
|
|
|
|
callback = arguments[a++],
|
|
|
|
t,tiddlers = [],tiddler;
|
|
|
|
if(sortField) {
|
|
|
|
for(t in this.tiddlers) {
|
|
|
|
tiddlers.push(this.tiddlers[t]);
|
|
|
|
}
|
|
|
|
tiddlers.sort(function (a,b) {
|
|
|
|
var aa = a.fields[sortField] || 0,
|
|
|
|
bb = b.fields[sortField] || 0;
|
|
|
|
if(aa < bb) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
if(aa > bb) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
for(t=0; t<tiddlers.length; t++) {
|
|
|
|
if(!tiddlers[t].hasTag(excludeTag)) {
|
|
|
|
callback.call(this,tiddlers[t].fields.title,tiddlers[t]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for(t in this.tiddlers) {
|
|
|
|
tiddler = this.tiddlers[t];
|
|
|
|
if(tiddler instanceof Tiddler && !tiddler.hasTag(excludeTag))
|
|
|
|
callback.call(this,t,tiddler);
|
|
|
|
}
|
2011-11-22 14:29:29 +00:00
|
|
|
}
|
2011-12-01 10:19:21 +00:00
|
|
|
};
|
2011-11-22 14:29:29 +00:00
|
|
|
|
2011-12-14 18:25:40 +00:00
|
|
|
WikiStore.prototype.getTitles = function(sortField,excludeTag) {
|
2011-12-28 16:10:30 +00:00
|
|
|
sortField = sortField || "title";
|
2011-12-14 18:25:40 +00:00
|
|
|
var tiddlers = [];
|
|
|
|
this.forEachTiddler(sortField,excludeTag,function(title,tiddler) {
|
|
|
|
tiddlers.push(title);
|
|
|
|
});
|
|
|
|
return tiddlers;
|
|
|
|
};
|
|
|
|
|
2011-12-28 16:10:30 +00:00
|
|
|
WikiStore.prototype.getMissingTitles = function() {
|
|
|
|
return [];
|
|
|
|
};
|
|
|
|
|
|
|
|
WikiStore.prototype.getOrphanTitles = function() {
|
|
|
|
return [];
|
|
|
|
};
|
|
|
|
|
|
|
|
WikiStore.prototype.getShadowTitles = function() {
|
|
|
|
return this.shadows ? this.shadows.getTitles() : [];
|
|
|
|
};
|
|
|
|
|
2011-12-28 17:16:56 +00:00
|
|
|
WikiStore.prototype.serializeTiddler = function(type,tiddler) {
|
|
|
|
var serializer = this.tiddlerSerializers[type];
|
|
|
|
if(serializer) {
|
|
|
|
return serializer(tiddler);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
WikiStore.prototype.deserializeTiddlers = function(type,text,srcFields) {
|
|
|
|
var fields = {},
|
|
|
|
deserializer = this.tiddlerDeserializers[type],
|
|
|
|
t;
|
|
|
|
if(srcFields) {
|
|
|
|
for(t in srcFields) {
|
|
|
|
fields[t] = srcFields[t];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(deserializer) {
|
|
|
|
return deserializer(text,fields);
|
|
|
|
} else {
|
|
|
|
// Return a raw tiddler for unknown types
|
|
|
|
fields.text = text;
|
|
|
|
return [fields];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-03 11:09:59 +00:00
|
|
|
WikiStore.prototype.getFormattedTiddlerField = function(title,field,format,template) {
|
|
|
|
format = format || "text";
|
|
|
|
var tiddler = this.getTiddler(title);
|
|
|
|
if(tiddler && tiddler.fields[field]) {
|
|
|
|
switch(format) {
|
|
|
|
case "text":
|
|
|
|
return utils.htmlEncode(tiddler.fields[field]);
|
|
|
|
case "link":
|
|
|
|
// xxx: Attribute encoding is wrong
|
|
|
|
return "<a href='" + utils.htmlEncode(tiddler.fields[field]) + "'>" + utils.htmlEncode(tiddler.fields[field]) + "</a>";
|
|
|
|
case "wikified":
|
|
|
|
return this.renderTiddler("text/html",tiddler.fields.title);
|
|
|
|
case "date":
|
|
|
|
template = template || "DD MMM YYYY";
|
|
|
|
return utils.htmlEncode(utils.formatDateString(tiddler.fields[field],template));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
};
|
|
|
|
|
|
|
|
WikiStore.prototype.classesForLink = function(target) {
|
|
|
|
return this.tiddlerExists(target) ? "class=\"linkResolves\"" : "";
|
|
|
|
};
|
|
|
|
|
|
|
|
WikiStore.prototype.listTiddlers = function(type,template,emptyMessage) {
|
|
|
|
return "<span>Listing!</span>";
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
argOptions: {defaultName:"type"},
|
|
|
|
handler: function(macroNode,args,title) {
|
|
|
|
var type = args.getValueByName("type","all"),
|
|
|
|
template = args.getValueByName("template",null),
|
|
|
|
templateType = "text/x-tiddlywiki", templateText = "<<view title link>>",
|
|
|
|
emptyMessage = args.getValueByName("emptyMessage",null);
|
|
|
|
// Get the template to use
|
|
|
|
template = template ? this.store.getTiddler(template) : null;
|
|
|
|
if(template) {
|
|
|
|
templateType = template.fields.type;
|
|
|
|
templateText = template.fields.text;
|
|
|
|
}
|
|
|
|
// Get the handler and the tiddlers
|
|
|
|
var handler = WikiTextRenderer.macros.list.types[type];
|
|
|
|
handler = handler || WikiTextRenderer.macros.list.types.all;
|
|
|
|
var tiddlers = handler.call(this);
|
|
|
|
// Render them as a list
|
|
|
|
var ul = {type: "ul", children: []};
|
|
|
|
for(var t=0; t<tiddlers.length; t++) {
|
|
|
|
var li = {
|
|
|
|
type: "li",
|
|
|
|
children: [ {
|
|
|
|
type: "context",
|
|
|
|
tiddler: tiddlers[t],
|
|
|
|
children: []
|
|
|
|
} ]
|
|
|
|
};
|
|
|
|
li.children[0].children = this.store.parseText(templateType,templateText).children;
|
|
|
|
ul.children.push(li);
|
|
|
|
}
|
|
|
|
if(ul.children.length > 0) {
|
|
|
|
macroNode.output.push(ul);
|
|
|
|
this.executeMacros(macroNode.output,title);
|
|
|
|
} else if (emptyMessage) {
|
|
|
|
macroNode.output.push({type: "text", value: emptyMessage});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
types: {
|
|
|
|
all: function() {
|
|
|
|
return this.store.getTitles("title","excludeLists");
|
|
|
|
},
|
|
|
|
missing: function() {
|
|
|
|
return this.store.getMissingTitles();
|
|
|
|
},
|
|
|
|
orphans: function() {
|
|
|
|
return this.store.getOrphanTitles();
|
|
|
|
},
|
|
|
|
shadowed: function() {
|
|
|
|
return this.store.getShadowTitles();
|
|
|
|
},
|
|
|
|
touched: function() {
|
|
|
|
// Server syncing isn't implemented yet
|
|
|
|
return [];
|
|
|
|
},
|
|
|
|
filter: function() {
|
|
|
|
// Filters aren't implemented yet
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-12-28 17:16:56 +00:00
|
|
|
WikiStore.prototype.parseText = function(type,text) {
|
|
|
|
var processor = this.textProcessors[type];
|
|
|
|
if(!processor) {
|
|
|
|
processor = this.textProcessors["text/x-tiddlywiki"];
|
|
|
|
}
|
|
|
|
if(processor) {
|
|
|
|
return processor.parse(text);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-12-11 18:28:09 +00:00
|
|
|
WikiStore.prototype.parseTiddler = function(title) {
|
|
|
|
var tiddler = this.getTiddler(title);
|
|
|
|
if(tiddler) {
|
2011-12-28 17:16:56 +00:00
|
|
|
return this.parseText(tiddler.fields.type,tiddler.fields.text);
|
2011-12-11 18:28:09 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2011-12-12 10:52:04 +00:00
|
|
|
};
|
2011-12-11 18:28:09 +00:00
|
|
|
|
2011-12-14 14:11:11 +00:00
|
|
|
/*
|
|
|
|
Render a tiddler to a particular MIME type. Optionally render it with a different tiddler as the context. This option is used to render a tiddler through a template as store.renderTiddler("text/html",tiddler,template)
|
|
|
|
*/
|
|
|
|
WikiStore.prototype.renderTiddler = function(type,title,asTitle) {
|
2011-12-14 15:45:42 +00:00
|
|
|
var parser = this.parseTiddler(title),
|
|
|
|
asTitleExists = asTitle ? this.tiddlerExists(asTitle) : true;
|
|
|
|
if(parser && asTitleExists) {
|
2011-12-14 14:11:11 +00:00
|
|
|
return parser.render(type,parser.children,this,asTitle ? asTitle : title);
|
2011-12-11 18:28:09 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2011-12-12 10:52:04 +00:00
|
|
|
};
|
2011-12-11 18:28:09 +00:00
|
|
|
|
2012-01-03 11:09:59 +00:00
|
|
|
WikiStore.prototype.compileTiddler = function(type,title,asTitle) {
|
|
|
|
var parser = this.parseTiddler(title),
|
|
|
|
asTitleExists = asTitle ? this.tiddlerExists(asTitle) : true;
|
|
|
|
if(parser && asTitleExists) {
|
|
|
|
return parser.compile(type,parser.children,this,asTitle ? asTitle : title);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
WikiStore.prototype.installMacros = function() {
|
|
|
|
this.macros = {
|
|
|
|
echo: {
|
|
|
|
params: {
|
|
|
|
text: {byPos: 0, type: "text", optional: false}
|
|
|
|
},
|
|
|
|
code: {
|
|
|
|
"text/html": this.sandbox.parse("return '<p>' + utils.htmlEncode(params.text) + '</p>';"),
|
|
|
|
"text/plain": this.sandbox.parse("return params.text;")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
view: {
|
|
|
|
params: {
|
|
|
|
field: {byPos: 0, type: "text", optional: false},
|
|
|
|
format: {byPos: 1, type: "text", optional: true},
|
|
|
|
template: {byPos: 2, type: "text", optional: true}
|
|
|
|
},
|
|
|
|
code: {
|
|
|
|
"text/html": this.sandbox.parse("return '<span>' + store.getFormattedTiddlerField(tiddler.fields.title,params.field,params.format,params.template) + '</span>';"),
|
|
|
|
"text/plain": this.sandbox.parse("return store.getFormattedTiddlerField(tiddler.fields.title,params.field,params.format,params.template);")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
list: {
|
|
|
|
params: {
|
|
|
|
type: {byName: "default", type: "text", optional: false},
|
|
|
|
template: {byName: true, type: "tiddler", optional: true},
|
|
|
|
emptyMessage: {byName: true, type: "text", optional: true}
|
|
|
|
},
|
|
|
|
code: {
|
|
|
|
"text/html": this.sandbox.parse("return '<span>' + store.listTiddlers(params.type,params.template,params.emptyMessage) + '</span>';"),
|
|
|
|
"text/plain": this.sandbox.parse("return store.listTiddlers(params.type,params.template,params.emptyMessage);")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2011-12-06 18:29:53 +00:00
|
|
|
exports.WikiStore = WikiStore;
|
2011-12-12 10:52:04 +00:00
|
|
|
|
|
|
|
})();
|