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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231 | 1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
12×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
55×
2×
53×
53×
53×
53×
53×
32×
21×
1×
37×
37×
37×
1×
15×
15×
15×
1×
14×
15×
1×
1×
1×
3×
3×
3×
1×
2×
2×
1×
2×
1×
3×
1×
| var _ = require('lodash');
var FieldType = require('../Type');
var util = require('util');
var utils = require('keystone-utils');
var displayName = require('display-name');
/**
* Name FieldType Constructor
* @extends Field
* @api public
*/
function name (list, path, options) {
this._fixedSize = 'full';
options.default = { first: '', last: '' };
options.nofilter = true; // TODO: remove this when 0.4 is merged
name.super_.call(this, list, path, options);
}
util.inherits(name, FieldType);
/**
* Registers the field on the List's Mongoose Schema.
*
* Adds String properties for .first and .last name, and a virtual
* with get() and set() methods for .full
*
* @api public
*/
name.prototype.addToSchema = function () {
var schema = this.list.schema;
var paths = this.paths = {
first: this._path.append('.first'),
last: this._path.append('.last'),
full: this._path.append('.full'),
};
schema.nested[this.path] = true;
schema.add({
first: String,
last: String,
}, this.path + '.');
schema.virtual(paths.full).get(function () {
return displayName(this.get(paths.first), this.get(paths.last));
});
schema.virtual(paths.full).set(function (value) {
Iif (typeof value !== 'string') {
this.set(paths.first, undefined);
this.set(paths.last, undefined);
return;
}
var split = value.split(' ');
this.set(paths.first, split.shift());
this.set(paths.last, split.join(' ') || undefined);
});
this.bindUnderscoreMethods();
};
/**
* Gets the string to use for sorting by this field
*/
name.prototype.getSortString = function (options) {
if (options.invert) {
return '-' + this.paths.first + ' -' + this.paths.last;
}
return this.paths.first + ' ' + this.paths.last;
};
/**
* Add filters to a query
*
* TODO: this filter will conflict with any other $or filter, including filters
* on other "name" type fields; need to work out a better way to implement.
*/
name.prototype.addFilterToQuery = function (filter, query) {
query = query || {};
if (filter.mode === 'exactly' && !filter.value) {
query[this.paths.first] = query[this.paths.last] = filter.inverted ? { $nin: ['', null] } : { $in: ['', null] };
return;
}
var value = utils.escapeRegExp(filter.value);
if (filter.mode === 'startsWith') {
value = '^' + value;
} else if (filter.mode === 'endsWith') {
value = value + '$';
} else if (filter.mode === 'exactly') {
value = '^' + value + '$';
}
value = new RegExp(value, filter.caseSensitive ? '' : 'i');
if (filter.inverted) {
query[this.paths.first] = query[this.paths.last] = { $not: value };
} else {
var first = {}; first[this.paths.first] = value;
var last = {}; last[this.paths.last] = value;
var $or = [first, last];
if (query.$and) {
query.$and.push({ $or: $or });
} else if (query.$or) {
query.$and = [{ $or: query.$or }, { $or: $or }];
delete query.$or;
} else {
query.$or = $or;
}
}
return query;
};
/**
* Formats the field value
*/
name.prototype.format = function (item) {
return item.get(this.paths.full);
};
/**
* Get the value from a data object; may be simple or a pair of fields
*/
name.prototype.getInputFromData = function (data) {
// this.getValueFromData throws an error if we pass name: null
if (data[this.path] === null) {
return null;
}
var first = this.getValueFromData(data, '_first');
if (first === undefined) first = this.getValueFromData(data, '.first');
var last = this.getValueFromData(data, '_last');
if (last === undefined) last = this.getValueFromData(data, '.last');
if (first !== undefined || last !== undefined) {
return {
first: first,
last: last,
};
}
return this.getValueFromData(data);
};
/**
* Validates that a value for this field has been provided in a data object
*/
name.prototype.validateInput = function (data, callback) {
var value = this.getInputFromData(data);
var result = value === undefined
|| value === null
|| typeof value === 'string'
|| (typeof value === 'object' && (
typeof value.first === 'string'
|| value.first === null
|| typeof value.last === 'string'
|| value.last === null)
);
utils.defer(callback, result);
};
/**
* Validates that input has been provided
*/
name.prototype.validateRequiredInput = function (item, data, callback) {
var value = this.getInputFromData(data);
var result;
if (value === null) {
result = false;
} else {
result = (
typeof value === 'string' && value.length
|| typeof value === 'object' && (
typeof value.first === 'string' && value.first.length
|| typeof value.last === 'string' && value.last.length)
|| (item.get(this.paths.full)
|| item.get(this.paths.first)
|| item.get(this.paths.last)) && (
value === undefined
|| (value.first === undefined
&& value.last === undefined))
) ? true : false;
}
utils.defer(callback, result);
};
/**
* Validates that a value for this field has been provided in a data object
*
* Deprecated
*/
name.prototype.inputIsValid = function (data, required, item) {
// Input is valid if none was provided, but the item has data
if (!(this.path in data || this.paths.first in data || this.paths.last in data || this.paths.full in data) && item && item.get(this.paths.full)) return true;
// Input is valid if the field is not required
if (!required) return true;
// Otherwise check for valid strings in the provided data,
// which may be nested or use flattened paths.
if (_.isObject(data[this.path])) {
return (data[this.path].full || data[this.path].first || data[this.path].last) ? true : false;
} else {
return (data[this.paths.full] || data[this.paths.first] || data[this.paths.last]) ? true : false;
}
};
/**
* Detects whether the field has been modified
*
* @api public
*/
name.prototype.isModified = function (item) {
return item.isModified(this.paths.first) || item.isModified(this.paths.last);
};
/**
* Updates the value for this field in the item from a data object
*
* @api public
*/
name.prototype.updateItem = function (item, data, callback) {
var paths = this.paths;
var value = this.getInputFromData(data);
if (typeof value === 'string' || value === null) {
item.set(paths.full, value);
} else Eif (typeof value === 'object') {
if (typeof value.first === 'string' || value.first === null) {
item.set(paths.first, value.first);
}
if (typeof value.last === 'string' || value.last === null) {
item.set(paths.last, value.last);
}
}
process.nextTick(callback);
};
/* Export Field Type */
module.exports = name;
|