From 6aaca0ab38a3de0770cd9c885086e25845f325d8 Mon Sep 17 00:00:00 2001 From: Daniel Florian Date: Wed, 25 Oct 2017 16:50:57 -0500 Subject: [PATCH] - created an enum for a property definition control type - moved the getControlType() function from the property definition model into the PropertyControlType enum - changed html switch to use the enum --- .../property-control-type.enum.ts | 74 +++++++++++++++++++ .../property-definition.model.ts | 18 ----- .../property-form-property.component.html | 10 +-- .../property-form-property.component.ts | 3 + ngapp/tslint.json | 1 + 5 files changed, 83 insertions(+), 23 deletions(-) create mode 100644 ngapp/src/app/shared/property-form/property-control-type.enum.ts diff --git a/ngapp/src/app/shared/property-form/property-control-type.enum.ts b/ngapp/src/app/shared/property-form/property-control-type.enum.ts new file mode 100644 index 00000000..f3f9c1a4 --- /dev/null +++ b/ngapp/src/app/shared/property-form/property-control-type.enum.ts @@ -0,0 +1,74 @@ +/** + * @license + * Copyright 2017 JBoss Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { PropertyDefinition } from "@shared/property-form/property-definition.model"; + +/** + * An enumeration of control types for a property. + */ + +export enum PropertyControlType { + + /** + * Render the property as a checkbox. + */ + CHECKBOX, + + /** + * Render the property as a dropdown. + */ + DROPDOWN, + + /** + * Render the property as a masked textbox. + */ + PASSWORD, + + /** + * Render the property as a textbox. + */ + TEXT + +} + +/** + * Namespace to allow methods on the enum. + */ +export namespace PropertyControlType { + + /** + * @param {PropertyDefinition} propDefn the property whose control type is being requested + * @returns {PropertyControlType} the control type to render the property value + */ + export function toControlType( propDefn: PropertyDefinition< any > ): PropertyControlType { + if ( propDefn.isConstrainedToAllowedValues() ) { + return PropertyControlType.DROPDOWN; + } + + if ( propDefn.getTypeClassName() === "java.lang.Boolean" ) { + return PropertyControlType.CHECKBOX; + } + + if ( propDefn.isMasked() || propDefn.getId() === "password" ) { + return PropertyControlType.PASSWORD; + } + + // defaults to a text control + return PropertyControlType.TEXT; + } + +} diff --git a/ngapp/src/app/shared/property-form/property-definition.model.ts b/ngapp/src/app/shared/property-form/property-definition.model.ts index 011cce13..60c8bf55 100644 --- a/ngapp/src/app/shared/property-form/property-definition.model.ts +++ b/ngapp/src/app/shared/property-form/property-definition.model.ts @@ -94,24 +94,6 @@ export class PropertyDefinition { return this.typeClassName; } - /** - * @returns {string} the type of control for this property definition - */ - public getControlType(): string { - const className = this.getTypeClassName(); - if (this.isConstrainedToAllowedValues()) { - return "dropdown"; - } else if (className === "java.lang.String") { - if (this.isMasked() || this.getId() === "password") { - return "maskedTextbox"; - } - return "textbox"; - } else if (className === "java.lang.Boolean") { - return "checkbox"; - } - return "textbox"; - } - /** * @returns {boolean} 'true' if required */ diff --git a/ngapp/src/app/shared/property-form/property-form-property/property-form-property.component.html b/ngapp/src/app/shared/property-form/property-form-property/property-form-property.component.html index 96702d1d..6bf149fb 100644 --- a/ngapp/src/app/shared/property-form/property-form-property/property-form-property.component.html +++ b/ngapp/src/app/shared/property-form/property-form-property/property-form-property.component.html @@ -1,18 +1,18 @@
-
+
- - - - diff --git a/ngapp/src/app/shared/property-form/property-form-property/property-form-property.component.ts b/ngapp/src/app/shared/property-form/property-form-property/property-form-property.component.ts index b3a9fda1..5fdcc192 100644 --- a/ngapp/src/app/shared/property-form/property-form-property/property-form-property.component.ts +++ b/ngapp/src/app/shared/property-form/property-form-property/property-form-property.component.ts @@ -19,6 +19,7 @@ import { Component, Input } from "@angular/core"; import { AbstractControl, FormGroup } from "@angular/forms"; import { PropertyDefinition } from "@shared/property-form/property-definition.model"; +import { PropertyControlType } from "@shared/property-form/property-control-type.enum"; @Component({ selector: "app-form-property", @@ -27,6 +28,8 @@ import { PropertyDefinition } from "@shared/property-form/property-definition.mo export class PropertyFormPropertyComponent { + public controlType = PropertyControlType; // need local ref of enum for html to use + @Input() public property: PropertyDefinition; @Input() public form: FormGroup; diff --git a/ngapp/tslint.json b/ngapp/tslint.json index 2845ac89..b9fedaaa 100644 --- a/ngapp/tslint.json +++ b/ngapp/tslint.json @@ -91,6 +91,7 @@ "ignore-params" ], "no-misused-new": true, + "no-namespace": false, "no-non-null-assertion": true, "no-parameter-properties": true, "no-shadowed-variable": true,