ramblings/oxygen/js-options/options.js

69 lines
2.3 KiB
JavaScript

/*
* Main features
*/
Options = function (authorAccess, namespace) {
this.namespace = namespace;
this.authorAccess = authorAccess;
this.optionsStorage = this.authorAccess.getOptionsStorage();
// Consider namespaces as a SYSTEM option
if (namespace != Options.SYSTEM) {
this.systemOptions = new Options(authorAccess, Options.SYSTEM);
var namespaces = JSON.parse(this.systemOptions.getOption(Options.NAMESPACES, '{}'));
namespaces[this.namespace] = (new Date()).toISOString();
this.systemOptions.setOption(Options.NAMESPACES, JSON.stringify(namespaces))
}
}
Options.SYSTEM = '__system__';
Options.NAMESPACES = '__namespaces__';
Options.SYSTEMIDS = '__systemIds__';
Options.prototype.getOption = function (key, defaultValue) {
this.optionsStorage.setOptionsDoctypePrefix(this.namespace);
return this.optionsStorage.getOption(key, defaultValue);
}
Options.prototype.setOption = function (key, value) {
this.optionsStorage.setOptionsDoctypePrefix(Options.SYSTEM);
var options = JSON.parse(this.optionsStorage.getOption(this.namespace, '{}'));
options[key] = (new Date()).toISOString();
this.optionsStorage.setOption(this.namespace, JSON.stringify(options));
this.optionsStorage.setOptionsDoctypePrefix(this.namespace);
this.optionsStorage.setOption(key, value);
}
Options.prototype.getOptions = function () {
var results = {
};
this.optionsStorage.setOptionsDoctypePrefix(Options.SYSTEM);
var options = JSON.parse(this.optionsStorage.getOption(this.namespace, '{}'));
for (var key in options) {
results[key] = this.getOption(key, '');
}
return results;
}
/*
* Per file extensions
*
*/
Options.prototype.setSystemId = function(systemID) {
if (systemID === undefined) {
this.systemId = String(this.authorAccess.getDocumentController().getAuthorDocumentNode().getSystemID());
} else {
this.systemId = String(systemID);
}
var md = java.security.MessageDigest.getInstance("SHA-1");
var digestBytes = md.digest(java.lang.String(this.systemId).getBytes(java.nio.charset.Charset.forName("UTF-8")));
this.systemIdDigest = javax.xml.bind.DatatypeConverter.printHexBinary(digestBytes);
var systemIds = JSON.parse(this.systemOptions.getOption(Options.SYSTEMIDS, '{}'));
systemIds[this.systemIdDigest] = {
date: (new Date()).toISOString(),
systemId: this.systemId
}
this.systemOptions.setOption(Options.SYSTEMIDS, JSON.stringify(systemIds));
}