Skip to content
This repository has been archived by the owner on Nov 22, 2019. It is now read-only.

Commit

Permalink
- created an enum for a property definition control type
Browse files Browse the repository at this point in the history
- moved the getControlType() function from the property definition model into the PropertyControlType enum
- changed html switch to use the enum
  • Loading branch information
elvisisking committed Oct 25, 2017
1 parent 98580ea commit 6aaca0a
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 23 deletions.
74 changes: 74 additions & 0 deletions 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<any>} 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;
}

}
18 changes: 0 additions & 18 deletions ngapp/src/app/shared/property-form/property-definition.model.ts
Expand Up @@ -94,24 +94,6 @@ export class PropertyDefinition<T> {
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
*/
Expand Down
@@ -1,18 +1,18 @@
<div [ngClass]="isValid ? 'form-group' : 'form-group has-error'" [formGroup]="form">
<label class="col-md-2 control-label" [attr.for]="property.getId()" data-toggle="tooltip" [title]="property.getDescription()">{{ property.getDisplayName() }}</label>

<div class="col-md-10" [ngSwitch]="property.getControlType()">
<div class="col-md-10" [ngSwitch]="controlType.toControlType(property)">

<input *ngSwitchCase="'textbox'" class="form-control" [formControlName]="property.getId()"
<input *ngSwitchCase="controlType.TEXT" class="form-control" [formControlName]="property.getId()"
[id]="property.getId()" type="text" title="A text value">

<input *ngSwitchCase="'maskedTextbox'" class="form-control" [formControlName]="property.getId()"
<input *ngSwitchCase="controlType.PASSWORD" class="form-control" [formControlName]="property.getId()"
[id]="property.getId()" type="password" title="A text value">

<input *ngSwitchCase="'checkbox'" [formControlName]="property.getId()"
<input *ngSwitchCase="controlType.CHECKBOX" [formControlName]="property.getId()"
[id]="property.getId()" type="checkbox" title="A boolean value">

<select [id]="property.getId()" *ngSwitchCase="'dropdown'" [(ngModel)]="property.theDefaultValue" class="form-control"
<select [id]="property.getId()" *ngSwitchCase="controlType.DROPDOWN" [(ngModel)]="property.theDefaultValue" class="form-control"
[formControlName]="property.getId()" title="A value with allowed values">
<option *ngFor="let allowedValue of property.getAllowedValues()" [selected]="allowedValue === property.theDefaultValue" [value]="allowedValue">{{ allowedValue }}</option>
</select>
Expand Down
Expand Up @@ -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",
Expand All @@ -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<any>;
@Input() public form: FormGroup;

Expand Down
1 change: 1 addition & 0 deletions ngapp/tslint.json
Expand Up @@ -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,
Expand Down

0 comments on commit 6aaca0a

Please sign in to comment.