|
@@ -1,11 +1,7 @@
|
|
|
-const app = require('../../app')
|
|
|
|
|
-const { pascal, camel, param, title } = require('change-case')
|
|
|
|
|
|
|
+const { pascal, title, camel, param } = require('change-case')
|
|
|
const plural = require('plural')
|
|
const plural = require('plural')
|
|
|
-const list = require('./list')
|
|
|
|
|
-const details = require('./details')
|
|
|
|
|
-const trash = require('./trash')
|
|
|
|
|
|
|
|
|
|
-/** @define CrudPagesOptions
|
|
|
|
|
|
|
+/** @define CrudOptions
|
|
|
* @property {string} titleName Type name in Title Case. This is used for labels like "New {Title Name}"
|
|
* @property {string} titleName Type name in Title Case. This is used for labels like "New {Title Name}"
|
|
|
* @property {string} titlePlural Plural type name in Title Case. This is used for labels like "View All {Title Plurals}"
|
|
* @property {string} titlePlural Plural type name in Title Case. This is used for labels like "View All {Title Plurals}"
|
|
|
* @property {string} pascalName Type name in PascalCase. This is used for Class definitions like "app{PascalName}Page"
|
|
* @property {string} pascalName Type name in PascalCase. This is used for Class definitions like "app{PascalName}Page"
|
|
@@ -28,10 +24,9 @@ const trash = require('./trash')
|
|
|
* @property {boolean} inList Default: true. Includes column in list page.
|
|
* @property {boolean} inList Default: true. Includes column in list page.
|
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
-/**
|
|
|
|
|
- * @param {CrudPagesOptions} opts
|
|
|
|
|
- */
|
|
|
|
|
-const crudPages = (opts) => {
|
|
|
|
|
|
|
+
|
|
|
|
|
+const defaults = (opts) => {
|
|
|
|
|
+ opts = Object.assign({}, opts)
|
|
|
if (!opts.pascalName) opts.pascalName = pascal(opts.titleName || opts.camelName || opts.paramName || '')
|
|
if (!opts.pascalName) opts.pascalName = pascal(opts.titleName || opts.camelName || opts.paramName || '')
|
|
|
if (!opts.pascalName) throw new Error('pascalName is required')
|
|
if (!opts.pascalName) throw new Error('pascalName is required')
|
|
|
if (opts.pascalName !== pascal(opts.pascalName)) throw new Error('pascalName should be PascalCased')
|
|
if (opts.pascalName !== pascal(opts.pascalName)) throw new Error('pascalName should be PascalCased')
|
|
@@ -49,6 +44,7 @@ const crudPages = (opts) => {
|
|
|
if (opts.paramPlural !== param(opts.paramPlural)) throw new Error('paramPlural should be param-cased')
|
|
if (opts.paramPlural !== param(opts.paramPlural)) throw new Error('paramPlural should be param-cased')
|
|
|
if (!opts.apiPrefix) opts.apiPrefix = `/api/${opts.paramPlural}`
|
|
if (!opts.apiPrefix) opts.apiPrefix = `/api/${opts.paramPlural}`
|
|
|
if (!opts.appPrefix) opts.appPrefix = `/${opts.paramPlural}`
|
|
if (!opts.appPrefix) opts.appPrefix = `/${opts.paramPlural}`
|
|
|
|
|
+ if (!opts.routeParam) opts.routeParam = `${opts.camelName}Id`
|
|
|
if (!opts.listComponentName) opts.listComponentName = `app${opts.pascalPlural}List`
|
|
if (!opts.listComponentName) opts.listComponentName = `app${opts.pascalPlural}List`
|
|
|
if (opts.listComponentName !== camel(opts.listComponentName)) throw new Error('listComponentName should be camelCased')
|
|
if (opts.listComponentName !== camel(opts.listComponentName)) throw new Error('listComponentName should be camelCased')
|
|
|
if (!opts.listComponentTag) opts.listComponentTag = `app-${opts.paramName}-list`
|
|
if (!opts.listComponentTag) opts.listComponentTag = `app-${opts.paramName}-list`
|
|
@@ -57,24 +53,21 @@ const crudPages = (opts) => {
|
|
|
if (opts.listPageComponentName !== camel(opts.listPageComponentName)) throw new Error('listPageComponentName should be camelCased')
|
|
if (opts.listPageComponentName !== camel(opts.listPageComponentName)) throw new Error('listPageComponentName should be camelCased')
|
|
|
if (!opts.listPageComponentTag) opts.listPageComponentTag = `app-${opts.paramPlural}-page`
|
|
if (!opts.listPageComponentTag) opts.listPageComponentTag = `app-${opts.paramPlural}-page`
|
|
|
if (opts.listPageComponentTag !== param(opts.listPageComponentTag)) throw new Error('listPageComponentTag should be param-cased')
|
|
if (opts.listPageComponentTag !== param(opts.listPageComponentTag)) throw new Error('listPageComponentTag should be param-cased')
|
|
|
-
|
|
|
|
|
- if (!opts.columns) throw new Error('Columns are required')
|
|
|
|
|
- opts.columns.forEach(col => {
|
|
|
|
|
- if (!col.camelName) col.camelName = camel(opts.titleName)
|
|
|
|
|
- if (!col.camelName) throw new Error('camelName is required')
|
|
|
|
|
- if (col.camelName !== camel(col.camelName)) throw new Error('column.camelName should be camelCased')
|
|
|
|
|
- if (!col.titleName) col.titleName = title(col.camelName)
|
|
|
|
|
- if (col.type === undefined) col.type = 'text'
|
|
|
|
|
- if (col.inList === undefined) col.inList = true
|
|
|
|
|
- })
|
|
|
|
|
|
|
|
|
|
- list(opts)
|
|
|
|
|
- details(opts)
|
|
|
|
|
- trash(opts)
|
|
|
|
|
|
|
+ if (opts.columns) {
|
|
|
|
|
+ opts.columns = opts.columns.map(col => {
|
|
|
|
|
+ col = Object.assign({}, col)
|
|
|
|
|
+ if (!col.camelName) col.camelName = camel(opts.titleName)
|
|
|
|
|
+ if (!col.camelName) throw new Error('camelName is required')
|
|
|
|
|
+ if (col.camelName !== camel(col.camelName)) throw new Error('column.camelName should be camelCased')
|
|
|
|
|
+ if (!col.titleName) col.titleName = title(col.camelName)
|
|
|
|
|
+ if (col.type === undefined) col.type = 'text'
|
|
|
|
|
+ if (col.inList === undefined) col.inList = true
|
|
|
|
|
+ return col
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ return opts;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
- // TODO: Create Read Update Delete pages...
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-module.exports = crudPages
|
|
|
|
|
|
|
+module.exports = defaults
|