-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.js
More file actions
204 lines (150 loc) · 3.3 KB
/
page.js
File metadata and controls
204 lines (150 loc) · 3.3 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
var _ = require('underscore'),
keystone = require('../../'),
utils = keystone.utils,
Type = require('./type');
/**
* Page Class
*
* @param {String} key
* @param {Object} options
* @api public
*/
function Page(key, options) {
if (!(this instanceof Page)) {
return new Page(key, options);
}
this.options = utils.options({
// ...
}, options);
this.key = key;
this.fields = {};
}
Object.defineProperty(Page.prototype, 'name', {
get: function() {
return this.get('name') || this.set('name', utils.keyToLabel(this.key));
}
});
/**
* Sets page options
*
* ####Example:
*
* page.set('test', value) // sets the 'test' option to `value`
*
* @param {String} key
* @param {String} value
* @api public
*/
Page.prototype.set = function(key, value) {
if (arguments.length === 1) {
return this.options[key];
}
this.options[key] = value;
return value;
};
/**
* Gets page options
*
* ####Example:
*
* page.get('test') // returns the 'test' value
*
* @param {String} key
* @method get
* @api public
*/
Page.prototype.get = Page.prototype.set;
/**
* Adds one or more fields to the page
*
* @api public
*/
Page.prototype.add = function(fields) {
// TODO: nested paths
if (!utils.isObject(fields)) {
throw new Error('keystone.content.Page.add() Error: fields must be an object.');
}
_.each(fields, function(options, path) {
if ('function' === typeof options) {
options = { type: options };
}
if ('function' !== typeof options.type) {
throw new Error('Page fields must be specified with a type function');
}
if (options.type.prototype.__proto__ !== Type.prototype) {
// Convert native field types to their default Keystone counterpart
if (options.type === String) {
options.type = keystone.content.Types.Text;
}
// TODO: More types
// else if (options.type == Number)
// options.type = Field.Types.Number;
// else if (options.type == Boolean)
// options.type = Field.Types.Boolean;
// else if (options.type == Date)
// options.type = Field.Types.Datetime;
else {
throw new Error('Unrecognised field constructor: ' + options.type);
}
}
this.fields[path] = new options.type(path, options);
}, this);
return this;
};
/**
* Registers the page with Keystone.
*
* ####Example:
*
* var homePage = new keystone.content.Page('home');
* // ...
* homePage.register();
*
* // later...
* var homePage = keystone.content.page('home');
*
* @api public
*/
Page.prototype.register = function() {
return keystone.content.page(this.key, this);
};
/**
* Populates a data structure based on defined fields
*
* @api public
*/
Page.prototype.populate = function(data) {
if (!utils.isObject(data)) {
data = {};
}
// TODO: implement schema
return data;
};
/**
* Validates a data structure based on defined fields
*
* @api public
*/
Page.prototype.validate = function(data) {
if (!_.isObject(data)) {
data = {};
}
// TODO: implement schema
return data;
};
/**
* Cleans a data structure so only the defined fields are present
*
* @api public
*/
Page.prototype.clean = function(data) {
if (!_.isObject(data)) {
data = {};
}
// TODO: implement schema
return data;
};
/*!
* Export class
*/
exports = module.exports = Page;