mongoose-ui-forms
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -7,16 +7,33 @@ 'use strict'; | ||
// todo: TBD add options: isTabsVisible on default page | ||
module.exports = function (schema) { | ||
var _schema = utils.getPaths(schema, {}); | ||
var paths = utils.getPaths(schema, {}); | ||
var _schema = utils.toUiFormsSchema(paths); | ||
var tabs = _(paths).map(function (p) { | ||
if (!p.tabName) { | ||
return false; | ||
} | ||
function toUiFormsSchema(){ | ||
return {name: p.tabName, order: p.tabOrder}; | ||
}) | ||
.compact() | ||
.unique('name') | ||
.sortBy('order') | ||
.value(); | ||
function toUiFormsSchema() { | ||
return _schema; | ||
} | ||
function getUiFormsProjection(){ | ||
function getUiFormsProjection() { | ||
return _.pluck(_schema, 'field').join(' '); | ||
} | ||
Object.defineProperties(schema.statics, { | ||
UiFormsTabs: {value: tabs, enumerable: true}, | ||
UiFormsSchema: {value: _schema, enumerable: true} | ||
}); | ||
schema.statics.getUiFormsProjection = getUiFormsProjection; | ||
schema.statics.getUiFormsSchema = toUiFormsSchema; | ||
schema._uiGridProjection = toUiFormsSchema(); | ||
}; |
107
lib/utils.js
@@ -8,2 +8,3 @@ 'use strict'; | ||
var options = _.cloneDeep(opts); | ||
Object.defineProperty(options, '_type', { value:options.type}); | ||
// todo: add support of html 5 input types | ||
@@ -37,4 +38,11 @@ //email//tel//number//search//range//url//color//date//datetime//time//month//week | ||
// _id - field | ||
if (options.type === ObjectId && !options.ref){ | ||
options.type = 'text'; | ||
options.editable = false; | ||
options.visible = false; | ||
} | ||
// this is direct reference type | ||
if (options.type === ObjectId){ | ||
if (options.type === ObjectId && options.ref){ | ||
// todo: use options.uiForm.type | ||
@@ -45,12 +53,32 @@ // todo: check default type for ObjectId||ref field | ||
// array of refs | ||
if (Array.isArray(options.type) && options.type[0] && !!options.type[0].ref){ | ||
options.ref = options.type[0].ref; | ||
options.type = 'arrayRef'; | ||
} | ||
// array | ||
if (Array.isArray(options.type)){ | ||
// Array | ||
options.type = 'array'; | ||
options.hideLabels = true; | ||
} | ||
if (options.ref && !options.uiForm){ | ||
console.warn('add uiForm to path: %s', options.ref); | ||
} | ||
if (options.auto === true){ | ||
options.visible = options.visible || false; | ||
delete options.auto; | ||
} | ||
_.merge(this, options, options.uiForm); | ||
if (this.auto === true){ | ||
this.visible = this.visible || false; | ||
delete this.auto; | ||
if (this.tabName){ | ||
this.visible = false; | ||
} | ||
if (this.visible !== false){ | ||
this.visible = true; | ||
} | ||
} | ||
@@ -61,3 +89,3 @@ | ||
function toUiGridColumnDef(paths, parent) { | ||
MongooseUtils.prototype.toUiFormsSchema = function toUiFormsSchema(paths, parent) { | ||
if (parent && !Array.isArray(parent)) { | ||
@@ -67,12 +95,16 @@ parent = [parent]; | ||
var mix = _.map(paths, function (uiGridOptions, path) { | ||
if (uiGridOptions instanceof UiFormOptions) { | ||
var mix = _.map(paths, function (uiFormOptions, path) { | ||
if (uiFormOptions instanceof UiFormOptions) { | ||
// todo: could be errors | ||
if (uiFormOptions.array){ | ||
uiFormOptions.array = toUiFormsSchema(uiFormOptions.array); | ||
} | ||
if (parent && parent.length) { | ||
return _.merge(uiGridOptions, {field: [parent.join('.'), path].join('.')}); | ||
return _.merge(uiFormOptions, {field: [parent.join('.'), path].join('.')}); | ||
} | ||
return _.merge(uiGridOptions, {field: path}); | ||
return _.merge(uiFormOptions, {field: path}); | ||
} | ||
return toUiGridColumnDef(uiGridOptions, path); | ||
return toUiFormsSchema(uiFormOptions, path); | ||
}); | ||
@@ -95,12 +127,39 @@ | ||
var self = this; | ||
var paths = _.reduce(schema.paths, function (result, pathSchema, path) { | ||
//flatten mixed type or ignore mixed type without fields | ||
//field: { type: {} } | ||
var _paths = _.reduce(schema.paths, function (result, schema, path){ | ||
if (schema.constructor.name === 'Mixed'){ | ||
if (!schema.options.type || Object.keys(schema.options.type).length === 0){ | ||
return result; | ||
} | ||
// flatten | ||
flatten(result, schema.options.type, path, schema.options.uiForm); | ||
return result; | ||
} | ||
result[path] = schema; | ||
return result; | ||
}, {}) | ||
//// Nested schema OR Array can no be a column | ||
//if ((pathSchema.schema && self.include(pathSchema.schema)) || | ||
// ( Array.isArray(pathSchema.options.type) && | ||
// (!!pathSchema.options.type[0] && self.include({options: pathSchema.options.type[0]})))) { | ||
// var msg = 'Nested schema or Array can not be a column in ui-grid! Path: ' + path; | ||
// throw new Error(msg); | ||
//} | ||
function flatten(result, paths, parent, parentOptions){ | ||
parent = parent || []; | ||
if (!Array.isArray(parent)){ | ||
parent = [parent]; | ||
} | ||
_.each(paths, function (schema, path){ | ||
if (typeof schema === 'function' ){ | ||
result[parent.concat(path).join('.')] = {options: _.extend({type: schema}, {uiForm: parentOptions})}; | ||
return; | ||
} | ||
if (typeof schema.type === 'function'){ | ||
result[parent.concat(path).join('.')] = {options: _.extend(schema, {uiForm: parentOptions})}; | ||
return; | ||
} | ||
flatten(result, paths[path], parent.concat(path), _.extend({}, parentOptions, schema.options && schema.options.uiForm)); | ||
}); | ||
} | ||
var paths = _.reduce(_paths, function (result, pathSchema, path) { | ||
var valIncl = self.include(pathSchema); | ||
@@ -127,3 +186,3 @@ | ||
if (valIncl) { | ||
if (!result[path] && valIncl) { | ||
result[path] = valIncl; | ||
@@ -135,9 +194,3 @@ } | ||
// include _id field by default | ||
if (!paths._id) { | ||
paths._id = new UiFormOptions({visible: false, order: 0}); | ||
} | ||
var columnDef = toUiGridColumnDef(paths); | ||
return _.sortBy(columnDef, function(def){return def.order;}); | ||
return paths; | ||
}; | ||
@@ -144,0 +197,0 @@ |
{ | ||
"name": "mongoose-ui-forms", | ||
"version": "0.0.1", | ||
"main": "lib/index.js", | ||
"version": "0.0.2", | ||
"main": "./lib/index.js", | ||
"description": "Mongoose plugin to map mongoose schema to ui-forms schema", | ||
@@ -6,0 +6,0 @@ "homepage": "https://github.com/valorkin/mongoose-ui-forms", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10624
205