2017-03-23 16:27:59 +09:00
|
|
|
|
import Users from 'meteor/vulcan:users';
|
2018-06-28 12:31:35 +02:00
|
|
|
|
import merge from 'lodash/merge';
|
|
|
|
|
import find from 'lodash/find';
|
|
|
|
|
import isObjectLike from 'lodash/isObjectLike';
|
2016-11-27 19:12:54 +09:00
|
|
|
|
|
2016-03-30 19:08:06 +09:00
|
|
|
|
// add support for nested properties
|
2018-03-08 11:28:29 +09:00
|
|
|
|
export const deepValue = function(obj, path){
|
2016-11-15 09:02:30 +01:00
|
|
|
|
const pathArray = path.split('.');
|
|
|
|
|
|
|
|
|
|
for (var i=0; i < pathArray.length; i++) {
|
2016-11-15 10:17:27 +01:00
|
|
|
|
obj = obj[pathArray[i]];
|
2016-11-15 09:02:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-30 19:08:06 +09:00
|
|
|
|
return obj;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// see http://stackoverflow.com/questions/19098797/fastest-way-to-flatten-un-flatten-nested-json-objects
|
2018-03-08 11:28:29 +09:00
|
|
|
|
export const flatten = function(data) {
|
2016-03-30 19:08:06 +09:00
|
|
|
|
var result = {};
|
|
|
|
|
function recurse (cur, prop) {
|
|
|
|
|
|
|
|
|
|
if (Object.prototype.toString.call(cur) !== "[object Object]") {
|
|
|
|
|
result[prop] = cur;
|
|
|
|
|
} else if (Array.isArray(cur)) {
|
|
|
|
|
for(var i=0, l=cur.length; i<l; i++)
|
|
|
|
|
recurse(cur[i], prop + "[" + i + "]");
|
|
|
|
|
if (l == 0)
|
|
|
|
|
result[prop] = [];
|
|
|
|
|
} else {
|
|
|
|
|
var isEmpty = true;
|
|
|
|
|
for (var p in cur) {
|
|
|
|
|
isEmpty = false;
|
|
|
|
|
recurse(cur[p], prop ? prop+"."+p : p);
|
|
|
|
|
}
|
|
|
|
|
if (isEmpty && prop)
|
|
|
|
|
result[prop] = {};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
recurse(data, "");
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-13 16:05:46 +09:00
|
|
|
|
/**
|
|
|
|
|
* @method Mongo.Collection.getInsertableFields
|
|
|
|
|
* Get an array of all fields editable by a specific user for a given collection
|
|
|
|
|
* @param {Object} user – the user for which to check field permissions
|
|
|
|
|
*/
|
2018-03-08 11:28:29 +09:00
|
|
|
|
export const getInsertableFields = function (schema, user) {
|
2016-06-13 16:05:46 +09:00
|
|
|
|
const fields = _.filter(_.keys(schema), function (fieldName) {
|
|
|
|
|
var field = schema[fieldName];
|
2018-06-21 14:27:20 +02:00
|
|
|
|
return Users.canCreateField(user, field);
|
2016-06-13 16:05:46 +09:00
|
|
|
|
});
|
|
|
|
|
return fields;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @method Mongo.Collection.getEditableFields
|
2016-11-23 17:22:29 +09:00
|
|
|
|
* Get an array of all fields editable by a specific user for a given collection (and optionally document)
|
2016-06-13 16:05:46 +09:00
|
|
|
|
* @param {Object} user – the user for which to check field permissions
|
|
|
|
|
*/
|
2018-03-08 11:28:29 +09:00
|
|
|
|
export const getEditableFields = function (schema, user, document) {
|
2016-06-13 16:05:46 +09:00
|
|
|
|
const fields = _.filter(_.keys(schema), function (fieldName) {
|
|
|
|
|
var field = schema[fieldName];
|
2018-06-21 14:27:20 +02:00
|
|
|
|
return Users.canUpdateField(user, field, document);
|
2016-06-13 16:05:46 +09:00
|
|
|
|
});
|
|
|
|
|
return fields;
|
|
|
|
|
};
|
|
|
|
|
|
2018-06-28 12:31:35 +02:00
|
|
|
|
export const isEmptyValue = value => (typeof value === 'undefined' || value === '' || Array.isArray(value) && value.length === 0);
|
|
|
|
|
|
|
|
|
|
export const mergeValue = ({
|
|
|
|
|
locale,
|
|
|
|
|
currentValue,
|
|
|
|
|
documentValue,
|
|
|
|
|
emptyValue,
|
|
|
|
|
datatype,
|
|
|
|
|
}) => {
|
|
|
|
|
if (locale) {
|
|
|
|
|
// note: intl fields are of type Object but should be treated as Strings
|
|
|
|
|
return currentValue || documentValue || emptyValue;
|
|
|
|
|
} else if (Array.isArray(currentValue) && find(datatype, ['type', Array])) {
|
|
|
|
|
// for object and arrays, use lodash's merge
|
|
|
|
|
// if field type is array, use [] as merge seed to force result to be an array as well
|
|
|
|
|
return merge([], documentValue, currentValue);
|
|
|
|
|
} else if (isObjectLike(currentValue) && find(datatype, ['type', Object])) {
|
|
|
|
|
return merge({}, documentValue, currentValue);
|
|
|
|
|
}
|
2018-06-29 12:55:55 +02:00
|
|
|
|
return undefined;
|
2018-06-28 12:31:35 +02:00
|
|
|
|
};
|