Skip to content

Commit

Permalink
the add definition dialog is now a component - much nicer
Browse files Browse the repository at this point in the history
  • Loading branch information
EricWittmann committed Mar 3, 2017
1 parent 0381112 commit d03e4f7
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 135 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<!-- Add Definition Dialog -->
<div>
<div bsModal #addDefinitionModal="bs-modal" class="modal fade" id="addDefinitionModal" tabindex="-1" role="dialog"
aria-labelledby="addDefinitionModalLabel" role="dialog" aria-hidden="true" *ngIf="isOpen()"
(onShown)="addDefinitionInput.focus()" (onHidden)="close()">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true" (click)="addDefinitionModal.hide()">
<span class="pficon pficon-close"></span>
</button>
<h4 class="modal-title" id="addDefinitionModalLabel">Add Definition</h4>
</div>
<div class="modal-body">
<p>Enter a new definition name below and then click Add.</p>
<form id="adddef-form" class="form-horizontal" #addDefinitionForm="ngForm" data-dismiss="modal">
<div class="form-group">
<label class="col-sm-2 control-label" for="definitionName">Name</label>
<div class="col-sm-10">
<input #addDefinitionInput name="definitionName" type="text" id="definitionName" class="form-control"
placeholder="Enter a Definition Name"
required [(ngModel)]="name">
</div>
</div>
<div class="section example-section">
<div class="section-header">
<a class="collapsed" data-toggle="collapse" data-target="#example-section-body">
<span class="section-label">FROM EXAMPLE (optional)</span>
</a>
</div>
<div class="section-body collapse" id="example-section-body">
<div class="explanation">
Input a JSON formatted sample below and we'll do our best to initialize the Definition's schema
based on the example.
</div>
<div class="example">
<ace-editor #exampleEditor
[theme]="'eclipse'"
[mode]="'json'"
[durationBeforeCallback]="350"
(textChanged)="setExampleDefinition($event)"
style="position: relative; height: 200px; border: 1px solid #ccc; width: 100%"></ace-editor>
<button class="btn btn-default btn-xs" type="button"
[disabled]="!isExampleDefinitionFormattable()" (click)="formatExampleDefinition()">
<span class="fa fa-fw fa-indent"></span>
<span>Format</span>
</button>
</div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal" (click)="add()"
[disabled]="!addDefinitionForm.form.valid || !isExampleDefinitionValid()">Add</button>
<button type="button" class="btn btn-default" data-dismiss="modal" (click)="cancel()">Cancel</button>
</div>
</div>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.example-section .example button {
margin-top: 5px;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/**
* @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 {Component, Output, EventEmitter, ViewChild, ViewChildren, QueryList} from "@angular/core";
import {ModalDirective} from "ng2-bootstrap";
import {AceEditorDirective} from "ng2-ace-editor";


@Component({
moduleId: module.id,
selector: "add-definition-dialog",
templateUrl: "add-definition.component.html",
styleUrls: ["add-definition.component.css"]
})
export class AddDefinitionDialogComponent {

@Output() onAdd: EventEmitter<any> = new EventEmitter<any>();

@ViewChildren("addDefinitionModal") addDefinitionModal: QueryList<ModalDirective>;
@ViewChildren("exampleEditor") exampleEditor: QueryList<AceEditorDirective>;

protected _isOpen: boolean = false;

protected name: string = "";

protected example: string = "";
protected exampleValid: boolean = true;
protected exampleFormattable: boolean = false;

/**
* Called to open the dialog.
*/
public open(): void {
console.info("Opening the Add Definition dialog!");
this._isOpen = true;
this.addDefinitionModal.changes.subscribe( thing => {
if (this.addDefinitionModal.first) {
this.addDefinitionModal.first.show();
}
});
}

/**
* Called to close the dialog.
*/
public close(): void {
this._isOpen = false;
this.name = "";
this.example = "";
this.exampleValid = false;
this.exampleFormattable = false;
}

/**
* Called when the user clicks "add".
*/
protected add(): void {
let data: any = {
name: this.name,
example: this.example
};
this.onAdd.emit(data);
this.cancel();
}

/**
* Called when the user clicks "cancel".
*/
protected cancel(): void {
this.addDefinitionModal.first.hide();
}

/**
* Returns true if the dialog is open.
* @return {boolean}
*/
public isOpen(): boolean {
return this._isOpen;
}

/**
* Called when the user changes the example definition in the Add Definition modal dialog.
* @param definition
*/
public setExampleDefinition(definition: string): void {
this.example = definition;
if (this.example === "") {
this.exampleValid = true;
this.exampleFormattable = false;
} else {
try {
JSON.parse(this.example);
this.exampleValid = true;
this.exampleFormattable = true;
} catch (e) {
this.exampleValid = false;
this.exampleFormattable = false;
}
}
}

/**
* Returns true if it's possible to format the example definition (it must be non-null and
* syntactically valid).
* @return {boolean}
*/
public isExampleDefinitionFormattable(): boolean {
return this.exampleFormattable;
}

/**
* Returns true if the example definition added by the user in the Add Definition modal
* dialog is valid (syntactically).
* @return {boolean}
*/
public isExampleDefinitionValid(): boolean {
return this.exampleValid;
}

/**
* Called to format the example definition.
*/
public formatExampleDefinition(): void {
if (this.exampleEditor.first) {
let jsObj: any = JSON.parse(this.example);
let nsrcStr: string = JSON.stringify(jsObj, null, 4);
this.exampleEditor.first.setText(nsrcStr);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ <h2>{{ document().info.title }}</h2>
<span class="section-label">Definitions</span>
</a>
<button class="btn btn-sm btn-default pull-right"
(click)="addDefinitionModal.show()"><span class="fa fa-plus"></span></button>
(click)="addDefinitionDialog.open()"><span class="fa fa-plus"></span></button>
</div>
<div class="section-body collapse in" id="definition-section-body">
<div definition-item class="api-definition" *ngFor="let defName of definitionNames()" [name]="defName"
Expand All @@ -66,7 +66,7 @@ <h2>{{ document().info.title }}</h2>
<span>Reusable types are useful!</span>
<span>&nbsp;</span>
<button class="btn btn-xs btn-default pull-right" style="margin-top: 0px"
(click)="addDefinitionModal.show()">Add Now</button>
(click)="addDefinitionDialog.open()">Add Now</button>
</div>
</div>
</div>
Expand Down Expand Up @@ -106,64 +106,7 @@ <h2>{{ document().info.title }}</h2>
(onDeselect)="deselectDefinition()" *ngIf="selectedType === 'definition'"></definition-form>
</div>

<!-- Add Definition Dialog -->
<div bsModal #addDefinitionModal="bs-modal" class="modal fade" id="addDefinitionModal" tabindex="-1" role="dialog" aria-labelledby="addDefinitionModalLabel"
role="dialog" aria-hidden="true" (onShown)="initAddDefinition(); addDefinitionInput.focus(); addDefinitionInput.select()">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true" (click)="addDefinitionModal.hide()">
<span class="pficon pficon-close"></span>
</button>
<h4 class="modal-title" id="addDefinitionModalLabel">Add Definition</h4>
</div>
<div class="modal-body">
<p>Enter a new definition name below and then click Add.</p>
<form id="adddef-form" class="form-horizontal" #addDefinitionForm="ngForm" data-dismiss="modal">
<div class="form-group">
<label class="col-sm-2 control-label" for="definitionName">Name</label>
<div class="col-sm-10">
<input #addDefinitionInput name="definitionName" type="text" id="definitionName" class="form-control"
placeholder="Enter a Definition Name"
required [(ngModel)]="modals.addDefinition.definitionName">
</div>
</div>
<div class="section example-section">
<div class="section-header">
<a data-toggle="collapse" data-target="#example-section-body">
<span class="section-label">FROM EXAMPLE (optional)</span>
</a>
</div>
<div class="section-body collapse in" id="example-section-body">
<div class="explanation">
Input a JSON formatted sample below and we'll do our best to initialize the Definition's schema
based on the example.
</div>
<div class="example">
<ace-editor #exampleEditor
[theme]="'eclipse'"
[mode]="'json'"
[durationBeforeCallback]="350"
(textChanged)="setExampleDefinition($event)"
style="position: relative; height: 200px; border: 1px solid #ccc; width: 100%"></ace-editor>
<button class="btn btn-default btn-xs" style="margin-top: 10px;" type="button"
[disabled]="!isExampleDefinitionFormattable()" (click)="formatExampleDefinition()">
<span class="fa fa-fw fa-indent"></span>
<span>Format</span>
</button>
</div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal" (click)="addDefinition()"
[disabled]="!addDefinitionForm.form.valid || !isExampleDefinitionValid()">Add</button>
<button type="button" class="btn btn-default" data-dismiss="modal" (click)="addDefinitionModal.hide()">Cancel</button>
</div>
</div>
</div>
</div>
<add-definition-dialog #addDefinitionDialog (onAdd)="addDefinition($event)"></add-definition-dialog>

</div>

Expand Down
Loading

0 comments on commit d03e4f7

Please sign in to comment.