var OKCMS = (function(){ var adminRoot = window.location.pathname.split("/")[1] var adminDB = [ "", adminRoot, "index.json" ].join("/") var OKCMS = function(opt){ this.opt = Object.assign({ ready: function(){}, }, opt || {}) this.fetch() } OKCMS.prototype.fetch = function(){ $.ajax({ url: adminDB, 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, cb){ if (! data.id) { this.create(type, data, cb) } else { this.update(type, data, cb) } } OKCMS.prototype.create = function(type, data, cb){ var resource = this.getResource(type) if (! data.title) { throw new Error ("Title field is empty") } if (! data.id) { data.id = slugify( data.title ) } $.ajax({ url: [ "", adminRoot, type ].join("/"), type: "POST", success: function(){ cb && cb() } }) } OKCMS.prototype.update = function(type, data, cb){ var resource = this.getResource(type) var id = data.id // /admin/:type/:id/ $.ajax({ url: [ "", adminRoot, type, id ].join("/"), type: "PUT", data: data, success: function(){ cb && cb() } }) } OKCMS.prototype.destroy = function(type, id, cb){ $.ajax({ url: [ "", adminRoot, type, id ].join("/"), type: "DELETE", success: function(){ cb && cb() } }) } function slugify (s){ return (s || "").toLowerCase().replace(/\s/g,"-").replace(/[^-_a-zA-Z0-9]/g, '-').replace(/-+/g, "-") } })()