Skip to content

Commit

Permalink
Merge pull request #11 from dionmaicon/add_types_to_model_templates
Browse files Browse the repository at this point in the history
Type Hidden Fields add
  • Loading branch information
dionmaicon committed Dec 15, 2019
2 parents 25b2e2c + 374675d commit 2472dec
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 56 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
/nbproject/private/
coverage
/nbproject/private/
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,10 @@ const model = {
price: {
type: "currency"
},
hidden_fields: ["price", "ISBN"]
fields: {
type: "hiddenFields",
options: ["price", "ISBN"]
}
};
module.exports = { model, resource };
Expand Down
55 changes: 31 additions & 24 deletions js/bootstrap/form.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable */
const capitalize = require("../libs/capitalize");
const pluralize = require("pluralize");
const Types = require("../types");

const Form = class {
constructor(name, model, resource) {
Expand All @@ -26,12 +27,15 @@ const Form = class {
let countFiles = 0;
//Template Struct
for (let property in this.model) {
if (property.includes("hidden_fields")) continue;


if (this.model.hasOwnProperty(property)) {

if (this.model[property].type == Types.HIDDEN_FIELDS) continue;

templateStruct += `\t<div class="form-group">\n\t\t<label for="${property}">${property}</label>\n`;

if (this.model[property].type == "select") {
if (this.model[property].type == Types.SELECT) {
templateStruct += `
<multiselect
v-model="${this.modelName}.${property}"
Expand All @@ -41,28 +45,28 @@ const Form = class {
:show-labels="false"
placeholder="Pick a value"
/>\n`;
} else if (this.model[property].type == "textarea") {
} else if (this.model[property].type == Types.TEXTAREA) {
templateStruct += `\t<textarea id="${property}" style="width: 100%" v-model="${this.modelName}.${property}" rows="10">You text here...</textarea>\n\n`;
} else if (this.model[property].type == "radio") {
templateStruct += "<br />";
for (let option of this.model[property].options) {
templateStruct += `\t<input type="radio" id="${option.id}" value="${option.value}" v-model="${this.modelName}.${property}">\n`;
templateStruct += `\t<label for="${option.id}">${option.value}</label><br>\n\n`;
}
} else if (this.model[property].type == "checkbox") {
} else if (this.model[property].type == Types.CHECKBOX) {
for (let option of this.model[property].options) {
templateStruct += `\t<input type="checkbox" id="${option.id}" value="${option.value}" v-model="${this.modelName}.${property}">\n`;
templateStruct += `\t<label for="${option.id}">${option.value}</label><br>\n\n`;
}
} else if (this.model[property].type == "file") {
} else if (this.model[property].type == Types.FILE) {
templateStruct += `\t<input type="file" id="${property}" v-on:change="${property}OnFileChange">\n`;
countFiles++;
} else if (
this.model[property].type == "oneToOne" ||
this.model[property].type == "oneToMany"
this.model[property].type == Types.ONE_TO_ONE ||
this.model[property].type == Types.ONE_TO_MANY
) {
templateStruct += `\t`;
if (this.model[property].type == "oneToOne") {
if (this.model[property].type == Types.ONE_TO_ONE) {
templateStruct += `
<multiselect
v-model="${this.modelName}.${property}"
Expand All @@ -89,15 +93,15 @@ const Form = class {
>
</multiselect>\n`;
}
} else if (this.model[property].type == "html") {
} else if (this.model[property].type == Types.HTML) {
templateStruct += `\t
<quill-editor
v-model="${this.modelName}.${property}"
ref="myQuillEditor"
:options="{}"
/>
\n`;
} else if (this.model[property].type == "currency") {
} else if (this.model[property].type == Types.CURRENCY) {
templateStruct += `\t
<money
id="value"
Expand Down Expand Up @@ -233,9 +237,10 @@ const Form = class {

for (let property in this.model) {
if (this.model.hasOwnProperty(property)) {
if (property.includes("hidden_fields")) continue; //If contains word hide continue loop for next iteration

if (this.model[property].type == "html") {
if (this.model[property].type == Types.HIDDEN_FIELDS) continue;

if (this.model[property].type == Types.HTML) {
if (dataImport == "") {
dataImport += `
import "quill/dist/quill.core.css";
Expand All @@ -254,25 +259,26 @@ const Form = class {

for (let property in this.model) {
if (this.model.hasOwnProperty(property)) {
if (property.includes("hidden_fields")) continue;

if (this.model[property].type == "select") {
if (this.model[property].type == Types.HIDDEN_FIELDS) continue;

if (this.model[property].type == Types.SELECT) {
relationsScript += `${property}: ${JSON.stringify(
this.model[property].options
)},\n`;
dataScript += `${property}: '',\n`;
} else if (this.model[property].type == "checkbox") {
} else if (this.model[property].type == Types.CHECKBOX) {
dataScript += `${property}: [],\n`;
} else if (this.model[property].type == "oneToOne") {
} else if (this.model[property].type == Types.ONE_TO_ONE) {
relationsScript += `${property}: [],\n`;
dataScript += `${property}: {},\n`;
} else if (this.model[property].type == "oneToMany") {
} else if (this.model[property].type == Types.ONE_TO_MANY) {
relationsScript += `${property}: [],\n`;
dataScript += `${property}: [],\n`;
} else if (
this.model[property].type == "radio" ||
this.model[property].type == "textarea" ||
this.model[property].type == "file"
this.model[property].type == Types.RADIO ||
this.model[property].type == Types.TEXTAREA ||
this.model[property].type == Types.FILE
) {
dataScript += `${property}: '',\n`;
} else {
Expand All @@ -286,11 +292,12 @@ const Form = class {

for (let property in this.model) {
if (this.model.hasOwnProperty(property)) {
if (property.includes("hidden_fields")) continue;

if (this.model[property].type == Types.HIDDEN_FIELDS) continue;

if (
this.model[property].type == "oneToOne" ||
this.model[property].type == "oneToMany"
this.model[property].type == Types.ONE_TO_ONE ||
this.model[property].type == Types.ONE_TO_MANY
) {
let capitalizedRelationName = capitalize(this.model[property].model);
let pluralizedAndCapitalizedRelationName = pluralize(
Expand All @@ -314,7 +321,7 @@ const Form = class {
if (countFiles > 0) {
for (let property in this.model) {
if (this.model.hasOwnProperty(property)) {
if (this.model[property].type == "file") {
if (this.model[property].type == Types.FILE) {
methodsScript += `
${property}OnFileChange(e){
let files = e.target.files || e.dataTransfer.files;
Expand Down
43 changes: 19 additions & 24 deletions js/bootstrap/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const capitalize = require("../libs/capitalize");
const pluralize = require("pluralize");
const Types = require("../types");

const Index = class {
constructor(name, model, resource) {
Expand Down Expand Up @@ -108,7 +109,6 @@ const Index = class {
${this.modelName}List: [],
mainList:[],
showParentPage: true,
columns: hide_columns,
sort: {
key: null
},
Expand Down Expand Up @@ -289,34 +289,29 @@ const Index = class {
`;

let hide = this.model["hidden_fields"];
let hiddenFields = [];
for (let property in this.model) {
if (!this.model.hasOwnProperty(property)) continue;

if (hide != null) {
templateHTMLStart = templateHTMLStart.replace(
"hide_columns",
JSON.stringify(this.model["hidden_fields"])
);
} else {
templateHTMLStart = templateHTMLStart.replace("hide_columns", "[]");
if (this.model[property].type == Types.HIDDEN_FIELDS) {
hiddenFields = this.model[property].options;
break;
}
}

for (var property in this.model) {
if (this.model.hasOwnProperty(property)) {
if (property.includes("hidden_fields")) continue;

if (hide) {
if (hide.includes(property)) continue;
}
for (let property in this.model) {
if (!this.model.hasOwnProperty(property)) continue;

if (
this.model[property].type === "oneToMany" ||
this.model[property].type === "oneToOne"
)
continue;
if (
this.model[property].type === Types.ONE_TO_ONE ||
this.model[property].type === Types.ONE_TO_MANY ||
this.model[property].type === Types.HIDDEN_FIELDS ||
hiddenFields.includes(property)
)
continue;

templateStrucTableHead += `<th @click="sortBy('${property}')"> ${property} <i style="float: right" class="fa fa-sort"> </i></th>\n`;
templateStrucTableBody += `<td>{{${this.modelName}.${property}}}</td>\n`;
}
templateStrucTableHead += `<th @click="sortBy('${property}')"> ${property} <i style="float: right" class="fa fa-sort"> </i></th>\n`;
templateStrucTableBody += `<td>{{${this.modelName}.${property}}}</td>\n`;
}

templateStrucTableBody += `
Expand Down
7 changes: 4 additions & 3 deletions js/bootstrap/view.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable */
const capitalize = require("../libs/capitalize");
const Types = require("../types");

const View = class {
constructor(name, model, resource) {
Expand Down Expand Up @@ -117,11 +118,11 @@ const View = class {
let viewStruct = "";
for (var property in this.model) {
if (this.model.hasOwnProperty(property)) {
if (property.includes("hidden_fields")) continue;
if (this.model[property].type === Types.HIDDEN_FIELDS) continue;

if (
this.model[property].type === "oneToMany" ||
this.model[property].type === "oneToOne"
this.model[property].type === Types.ONE_TO_MANY ||
this.model[property].type === Types.ONE_TO_ONE
) {
viewStruct += `
<li v-if="${this.modelName}.${property}" class="list-group-item">
Expand Down
9 changes: 8 additions & 1 deletion js/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const HTML = "html";
const RADIO = "radio";
const CHECKBOX = "checkbox";
const FILE = "file";
const HIDDEN_FIELDS = "hiddenFields";

const HTML5_TYPES = [
"color",
Expand Down Expand Up @@ -78,6 +79,10 @@ const Types = class {
return FILE;
}

static get HIDDEN_FIELDS() {
return HIDDEN_FIELDS;
}

static get validTypes() {
return HTML5_TYPES.concat([
ONE_TO_ONE,
Expand All @@ -90,7 +95,8 @@ const Types = class {
HTML,
RADIO,
CHECKBOX,
FILE
FILE,
HIDDEN_FIELDS
]);
}

Expand Down Expand Up @@ -128,6 +134,7 @@ const Types = class {
case Types.RADIO:
case Types.CHECKBOX:
case Types.SELECT:
case Types.HIDDEN_FIELDS:
if (!object.options) {
response.message += `Model type: ${prop} needs options property.\n`;
response.success = false;
Expand Down
8 changes: 7 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { exec } = require("child_process");

const Crud = require("./js/crud.js");
const Init = require("./js/init.js");
const Types = require("./js/types.js");

const config = {
pathRoutes: path.join(process.cwd(), "src/routes"),
Expand Down Expand Up @@ -55,7 +56,12 @@ const createFiles = (name, file) => {
console.error(`Template model "${name}" has sintax error.`);
process.exit(-1);
}
createTemplates(name, model, resource);
let valid = Types.modelIsValid(model);
if (valid.success == true) {
createTemplates(name, model, resource);
} else {
console.log(valid.message);
}
});
};

Expand Down
6 changes: 5 additions & 1 deletion test/types.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ const model = {
sponsor: {
type: Types.SELECT,
options: ["Patron", "Vue-Crudgen"]
},
fields: {
type: Types.HIDDEN_FIELDS,
options: ["birth", "price"]
}
};

Expand All @@ -49,7 +53,7 @@ test("Check if types are valid", () => {
}
}
}
expect(count).toBe(8);
expect(count).toBe(9);
});

test("Check if Types Models is invalid", () => {
Expand Down

0 comments on commit 2472dec

Please sign in to comment.