summaryrefslogtreecommitdiff
path: root/themes/okadmin/public/js/okcms.js
blob: fe0ed69e9af18ab08a7ea41fd777576586819460 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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){
    if (! data.title) {
      throw new Error ("Title field is empty")
    }
    if (! data.id) {
      data.id = slugify( data.title )
    }
  }
  OKCMS.prototype.update = function(type, data){
    var id = data.id
  }
  OKCMS.prototype.destroy = function(type, id){
  }

  function slugify (s){
    return (s || "").toLowerCase().replace(/\s/g,"-").replace(/[^-_a-zA-Z0-9]/g, '-').replace(/-+/g, "-")
  }

})()