diff options
| author | Jules Laplace <jules@okfoc.us> | 2016-09-06 14:14:46 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2016-09-06 14:14:46 -0400 |
| commit | bd1b0a856d02f817b80cf9a30fea9b6b85ff24c8 (patch) | |
| tree | edc78ebe9006682b196282f591a2af4ecded5666 /themes/okadmin/public | |
| parent | 636866c735a1b480625ad38029e54ec932e28fb1 (diff) | |
library functions for interpreting CMS data
Diffstat (limited to 'themes/okadmin/public')
| -rw-r--r-- | themes/okadmin/public/js/okcms.js | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/themes/okadmin/public/js/okcms.js b/themes/okadmin/public/js/okcms.js new file mode 100644 index 0000000..5a91a13 --- /dev/null +++ b/themes/okadmin/public/js/okcms.js @@ -0,0 +1,166 @@ +var OKCMS = (function(){ + + var root = window.location.pathname.split("/")[1] + var dbPath = [ "", root, "index.json" ].join("/") + + var OKCMS = function(opt){ + this.opt = Object.assign({ + ready: function(){}, + }, opt || {}) + this.fetch() + } + OKCMS.prototype.fetch = function(){ + var request = $.ajax({ + url: dbPath, + type: "get", + success: this.load.bind(this) + }) + } + OKCMS.prototype.load = function(data){ + this.data = data + // data.meta + // data.resources by type + // data.resources[types].type + // data.resources[types].spec + // data.resources[types].groupBy + // data.resources[types].descending + // data.resources[types].data[] + // data.resources[types].data[0][groupBy][group] + var resources = this.resources = {} + this.types = Object.keys(data.resources) + this.types.forEach(function(type){ + var resource = data.resources[type] + resources[resource.type] = resource + resource.lookup = {} + if (resource.groupBy) { + var group_lookup = resource.data[0][resource.groupBy] + var groups = Object.keys(group_lookup) + resource.index = [] + groups.forEach(function(group){ + var list = group_lookup[group] + list.forEach(function(record){ + resource.lookup[record.id] = record + resource.index.push(record) + }) + }) + } + else { + resource.data.forEach(function(record){ + resource.lookup[record.id] = record + }) + } + }) + this.opt.ready && this.opt.ready() + } + OKCMS.prototype.getResource = function(type){ + var resource = this.resources[type] + if (! resource) throw new Error ("Resource not found: " + type) + return resource + } + OKCMS.prototype.index = function(type){ + var resource = this.getResource(type) + return resource.index + } + OKCMS.prototype.groups = function(type){ + var resource = this.getResource(type) + return resource.data[0][resource.groupBy] + } + OKCMS.prototype.keys = function(type){ + var index = this.index(type) + var keys = index.map(function(rec){ + return rec.id + }) + return keys + } + OKCMS.prototype.spec = function(type){ + var resource = this.getResource(type) + return resource.spec + } + OKCMS.prototype.get = function(type, id){ + var resource = this.getResource(type) + if (id in resource.index) { + return resource.index[id] + } + throw new Error ("Key not found: " + type + ":" + id) + } + OKCMS.prototype.new = function(type){ + var resource = this.resource(type) + var spec = this.spec(type) + var record = {} + Object.keys(spec).forEach(function(key){ + var field = spec[key] + switch (field.type) { + case 'foreign-key': + record[key] = "" + break + + case 'enum': + record[key] = field.values[0] + break + + case 'meta': + if (key === "__index") { + record[key] = resource.index.length + } + else if (key === "dateCreated") { + record[key] = (new Date ()).toUTCString() + } + else { + record[key] = "" // ??? + } + break + + case 'date': + record[key] = new Date + break + + case 'flag': + record[key] = false + break + + case 'link-list': + case 'gallery': + case 'media': + record[key] = [] + break + + case 'image': + case 'video': + record[key] = {} + break + + case 'number': + record[key] = 0 + break + + case 'string': + case 'text': + default: + record[key] = "" + break + } + }) + return record + } + OKCMS.prototype.enum_keys = function(type, field){ + var spec = this.spec(type) + var field = spec[field] + if (! field) throw new Error ("No such field: " + field) + if (field.type == "enum") { + return field.options + } + else if (field.type == "foreign-key") { + return this.keys(field.key) + } + } + OKCMS.prototype.save = function(type, data){ + } + OKCMS.prototype.create = function(type, data){ + } + OKCMS.prototype.update = function(type, data){ + var id = data.id + } + OKCMS.prototype.destroy = function(type, id){ + } + +})()
\ No newline at end of file |
