src/app/streams/stream-deploy/free-text/free-text.component.ts
Free Text Component Provides a rich textarea with a semantic validation of the properties
changeDetection | ChangeDetectionStrategy.OnPush |
selector | app-stream-deploy-free-text |
styleUrls | styles.scss |
templateUrl | free-text.component.html |
Properties |
Methods |
Inputs |
Outputs |
constructor(notificationService: NotificationService)
|
||||||||
Constructor Initialize FormGroup
Parameters :
|
id
|
Stream ID
Type: |
isDeployed
|
Is Deployed
Default value: |
properties
|
Properties to load
Type: |
copyProperties
|
Emit for request copy $event type: EventEmitter
|
deploy
|
Emit for request deploy $event type: EventEmitter
|
exportProperties
|
Emit for request export $event type: EventEmitter
|
update
|
Emit on destroy component with the current value $event type: EventEmitter
|
copyToClipboard |
copyToClipboard()
|
Copye to clipboard
Returns :
void
|
deployStream |
deployStream()
|
Emit a request deploy
Returns :
void
|
exportProps |
exportProps()
|
Emit a request export
Returns :
void
|
fileChange | ||||||||
fileChange(contents: )
|
||||||||
Parse and load a file to the properties control Produce an exception when the user cancel the file dialog
Parameters :
Returns :
void
|
Private getCleanProperties |
getCleanProperties()
|
Returns :
any
|
ngOnDestroy |
ngOnDestroy()
|
On destroy, emit the update event
Returns :
void
|
ngOnInit |
ngOnInit()
|
On Init
Returns :
void
|
valueChanges | ||||||||
valueChanges(value: string)
|
||||||||
Textarea value Change
Parameters :
Returns :
void
|
formGroup |
formGroup:
|
Type : FormGroup
|
Form |
isExportable |
isExportable:
|
Default value : false
|
State of the form |
isSubmittable |
isSubmittable:
|
Default value : false
|
State of the form |
lines |
lines:
|
Type : Array<any>
|
Line of the textarea |
import { ChangeDetectionStrategy, Component, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { StreamDeployValidator } from '../stream-deploy.validator';
import { NotificationService } from '../../../shared/services/notification.service';
/**
* Free Text Component
* Provides a rich textarea with a semantic validation of the properties
*
* @author Damien Vitrac
*/
@Component({
selector: 'app-stream-deploy-free-text',
templateUrl: 'free-text.component.html',
styleUrls: ['styles.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class StreamDeployFreeTextComponent implements OnInit, OnDestroy {
/**
* Stream ID
*/
@Input() id: string;
/**
* Emit on destroy component with the current value
*/
@Output() update = new EventEmitter();
/**
* Emit for request export
*/
@Output() exportProperties = new EventEmitter();
/**
* Emit for request copy
*/
@Output() copyProperties = new EventEmitter();
/**
* Emit for request deploy
*/
@Output() deploy = new EventEmitter();
/**
* Properties to load
*/
@Input() properties: Array<string> = [];
/**
* Is Deployed
*/
@Input() isDeployed = false;
/**
* Form
*/
formGroup: FormGroup;
/**
* Line of the textarea
*/
lines: Array<any> = [{
label: 1,
valid: true,
message: ''
}];
/**
* State of the form
*/
isSubmittable = false;
/**
* State of the form
*/
isExportable = false;
/**
* Constructor
* Initialize FormGroup
*/
constructor(private notificationService: NotificationService) {
this.formGroup = new FormGroup({
'input': new FormControl(),
'file': new FormControl('')
});
}
/**
* On Init
*/
ngOnInit() {
this.formGroup.get('input').valueChanges
.subscribe((value) => {
this.valueChanges(value);
});
this.formGroup.get('input').setValue(this.properties.join('\n'));
}
private getCleanProperties() {
return this.formGroup.get('input').value.toString()
.split('\n')
.filter((line) => (line.replace(' ', '') !== ''));
}
/**
* On destroy, emit the update event
*/
ngOnDestroy() {
this.update.emit(this.getCleanProperties());
}
/**
* Textarea value Change
*/
valueChanges(value: string) {
let countInvalidProperties = 0;
let countValidProperties = 0;
this.lines = (value.toString() || ' ')
.split('\n')
.map((line: string, index: number) => {
const lineClean = line.replace(' ', '');
const message = StreamDeployValidator.property(lineClean);
if (lineClean !== '') {
if (message === true) {
countValidProperties++;
} else {
countInvalidProperties++;
}
}
return {
label: (index + 1),
valid: (message === true),
message: (message !== true) ? message : ''
};
});
this.isSubmittable = (countInvalidProperties === 0);
this.isExportable = (countInvalidProperties + countValidProperties) > 0;
}
/**
* Parse and load a file to the properties control
* Produce an exception when the user cancel the file dialog
*
* @param {Blob} contents File
*/
fileChange(contents) {
try {
const reader = new FileReader();
reader.onloadend = (e) => {
this.formGroup.get('input').setValue(reader.result);
this.formGroup.get('file').setValue('');
};
reader.readAsText(contents.target.files[0]);
} catch (e) {
}
}
/**
* Emit a request export
*/
exportProps() {
this.exportProperties.emit(this.getCleanProperties());
}
/**
* Copye to clipboard
*/
copyToClipboard() {
this.copyProperties.emit(this.getCleanProperties());
}
/**
* Emit a request deploy
*/
deployStream() {
if (!this.isSubmittable) {
this.notificationService.error('Some line(s) are invalid.');
} else {
this.deploy.emit(this.getCleanProperties());
}
}
}
<form [formGroup]="formGroup" (submit)="deployStream()" dataflowLayoutType type="large">
<p>Enter the list of properties into the text area field below. Alternatively, you can also select a file in your local file system, which is used to populate the text area field.</p>
<div class="form-textarea">
<div class="numbers">
<ng-template ngFor let-item [ngForOf]="lines">
<div class="number">
<span [class.invalid]="!item.valid">{{ item.label }}</span>
</div>
</ng-template>
</div>
<textarea dataflowAutoResize [dataflowFocus]="true" formControlName="input"></textarea>
</div>
<div class="input-file">
<label class="btn-file btn btn-default" placement="top" delay="500"
tooltip="Please provide a text file containing properties. This will be used to populate the text area above.">
<input formControlName="file" id="propertiesFile" name="propertiesFile" type="file"
(change)="fileChange($event)"/>
Import a local file
</label>
</div>
<app-page-actions>
<a id="btn-cancel" class="btn btn-default" routerLink="/streams">Cancel</a>
<button id="btn-export" type="button" class="btn btn-default" (click)="exportProps()">
Export
</button>
<button tabindex="200" id="btn-copy" type="button" class="btn btn-default" (click)="copyToClipboard()">
Copy to Clipboard
</button>
<button id="btn-deploy" type="submit" class="btn btn-primary">
<span *ngIf="!isDeployed">Deploy stream</span>
<span *ngIf="isDeployed">Update stream</span>
</button>
</app-page-actions>
</form>