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 | 1×
1×
1×
1×
1×
72×
37×
37×
37×
37×
37×
37×
37×
37×
37×
37×
37×
37×
37×
37×
37×
37×
37×
37×
37×
37×
37×
37×
37×
37×
37×
37×
37×
37×
37×
2×
37×
10×
34×
34×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
| var _ = require('lodash');
var keystone = require('../');
var schemaPlugins = require('./schemaPlugins');
var utils = require('keystone-utils');
/**
* List Class
*
* @param {String} key
* @param {Object} options
*/
function List (key, options) {
if (!(this instanceof List)) return new List(key, options);
var defaultOptions = {
schema: {
collection: keystone.prefixModel(key),
},
noedit: false,
nocreate: false,
nodelete: false,
autocreate: false,
sortable: false,
hidden: false,
track: false,
inherits: false,
perPage: 100,
searchFields: '__name__',
defaultSort: '__default__',
defaultColumns: '__name__',
};
// initialFields values are initialised on demand by the getter
var initialFields;
// Inherit default options from parent list if it exists
Iif (options && options.inherits) {
if (options.inherits.options && options.inherits.options.inherits) {
throw new Error('Inherited Lists may not contain any inheritance');
}
defaultOptions = utils.options(defaultOptions, options.inherits.options);
}
this.options = utils.options(defaultOptions, options);
// init properties
this.key = key;
this.path = this.get('path') || utils.keyToPath(key, true);
this.schema = new keystone.mongoose.Schema({}, this.options.schema);
this.schemaFields = [];
this.uiElements = [];
this.underscoreMethods = {};
this.fields = {};
this.fieldsArray = [];
this.fieldTypes = {};
this.relationshipFields = [];
this.relationships = {};
this.mappings = {
name: null,
createdBy: null,
createdOn: null,
modifiedBy: null,
modifiedOn: null,
};
var self = this;
// init mappings
_.forEach(this.options.map, function (val, key) { self.map(key, val); });
// define property getters
Object.defineProperty(this, 'label', { get: function () {
return this.get('label') || this.set('label', utils.plural(utils.keyToLabel(key)));
} });
Object.defineProperty(this, 'singular', { get: function () {
return this.get('singular') || this.set('singular', utils.singular(this.label));
} });
Object.defineProperty(this, 'plural', { get: function () {
return this.get('plural') || this.set('plural', utils.plural(this.singular));
} });
Object.defineProperty(this, 'namePath', { get: function () {
return this.mappings.name || '_id';
} });
Object.defineProperty(this, 'nameField', { get: function () {
return this.fields[this.mappings.name];
} });
Object.defineProperty(this, 'nameIsVirtual', { get: function () {
return this.model.schema.virtuals[this.mappings.name] ? true : false;
} });
Object.defineProperty(this, 'nameIsEditable', { get: function () {
return (this.fields[this.mappings.name] && this.fields[this.mappings.name].type === 'text') ? !this.fields[this.mappings.name].noedit : false;
} });
Object.defineProperty(this, 'nameIsInitial', { get: function () {
return (this.fields[this.mappings.name] && this.fields[this.mappings.name].options.initial === undefined);
} });
Object.defineProperty(this, 'initialFields', { get: function () {
return initialFields || (initialFields = _.filter(this.fields, function (i) { return i.initial; }));
} });
Iif (this.get('sortable')) {
schemaPlugins.sortable.apply(this);
}
if (this.get('autokey')) {
schemaPlugins.autokey.apply(this);
}
if (this.get('track')) {
schemaPlugins.track.apply(this);
}
Iif (this.get('history')) {
schemaPlugins.history.apply(this);
}
Iif (this.get('inherits')) {
var parentFields = this.get('inherits').schemaFields;
this.add.apply(this, parentFields);
}
}
// Search Fields
Object.defineProperty(List.prototype, 'searchFields', {
get: function () {
if (!this._searchFields) {
this._searchFields = this.expandPaths(this.get('searchFields'));
}
return this._searchFields;
}, set: function (value) {
this.set('searchFields', value);
delete this._searchFields;
},
});
// Default Sort Field
Object.defineProperty(List.prototype, 'defaultSort', {
get: function () {
var ds = this.get('defaultSort');
return (ds === '__default__') ? (this.get('sortable') ? 'sortOrder' : this.namePath) : ds;
}, set: function (value) {
this.set('defaultSort', value);
},
});
// Default Column Fields
Object.defineProperty(List.prototype, 'defaultColumns', {
get: function () {
if (!this._defaultColumns) {
this._defaultColumns = this.expandColumns(this.get('defaultColumns'));
}
return this._defaultColumns;
}, set: function (value) {
this.set('defaultColumns', value);
delete this._defaultColumns;
},
});
// Add prototype methods
List.prototype.add = require('./list/add');
List.prototype.addFiltersToQuery = require('./list/addFiltersToQuery');
List.prototype.addSearchToQuery = require('./list/addSearchToQuery');
List.prototype.automap = require('./list/automap');
List.prototype.expandColumns = require('./list/expandColumns');
List.prototype.expandPaths = require('./list/expandPaths');
List.prototype.expandSort = require('./list/expandSort');
List.prototype.field = require('./list/field');
List.prototype.get = List.prototype.set = require('./list/set');
List.prototype.getAdminURL = require('./list/getAdminURL');
List.prototype.getCSV = require('./list/getCSV');
List.prototype.getData = require('./list/getData');
List.prototype.getDocumentName = require('./list/getDocumentName');
List.prototype.getOptions = require('./list/getOptions');
List.prototype.getPages = require('./list/getPages');
List.prototype.getSearchFilters = require('./list/getSearchFilters');
List.prototype.getUniqueValue = require('./list/getUniqueValue');
List.prototype.isReserved = require('./list/isReserved');
List.prototype.map = require('./list/map');
List.prototype.paginate = require('./list/paginate');
List.prototype.processFilters = require('./list/processFilters');
List.prototype.register = require('./list/register');
List.prototype.relationship = require('./list/relationship');
List.prototype.selectColumns = require('./list/selectColumns');
List.prototype.updateItem = require('./list/updateItem');
List.prototype.underscoreMethod = require('./list/underscoreMethod');
List.prototype.validateInput = require('./list/validateInput');
/*!
* Export class
*/
module.exports = List;
|