Skip to content

Commit bfe5256

Browse files
committed
validation tooltip directive
1 parent 50a2f5d commit bfe5256

18 files changed

+359
-120
lines changed

angular.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": 1,
44
"newProjectRoot": "projects",
55
"projects": {
6-
"project8": {
6+
"validation-tooltip": {
77
"root": "",
88
"sourceRoot": "src",
99
"projectType": "application",
@@ -13,7 +13,7 @@
1313
"build": {
1414
"builder": "@angular-devkit/build-angular:browser",
1515
"options": {
16-
"outputPath": "dist/project8",
16+
"outputPath": "dist/validation-tooltip",
1717
"index": "src/index.html",
1818
"main": "src/main.ts",
1919
"polyfills": "src/polyfills.ts",
@@ -50,18 +50,18 @@
5050
"serve": {
5151
"builder": "@angular-devkit/build-angular:dev-server",
5252
"options": {
53-
"browserTarget": "project8:build"
53+
"browserTarget": "validation-tooltip:build"
5454
},
5555
"configurations": {
5656
"production": {
57-
"browserTarget": "project8:build:production"
57+
"browserTarget": "validation-tooltip:build:production"
5858
}
5959
}
6060
},
6161
"extract-i18n": {
6262
"builder": "@angular-devkit/build-angular:extract-i18n",
6363
"options": {
64-
"browserTarget": "project8:build"
64+
"browserTarget": "validation-tooltip:build"
6565
}
6666
},
6767
"test": {
@@ -95,19 +95,19 @@
9595
}
9696
}
9797
},
98-
"project8-e2e": {
98+
"validation-tooltip-e2e": {
9999
"root": "e2e/",
100100
"projectType": "application",
101101
"architect": {
102102
"e2e": {
103103
"builder": "@angular-devkit/build-angular:protractor",
104104
"options": {
105105
"protractorConfig": "e2e/protractor.conf.js",
106-
"devServerTarget": "project8:serve"
106+
"devServerTarget": "validation-tooltip:serve"
107107
},
108108
"configurations": {
109109
"production": {
110-
"devServerTarget": "project8:serve:production"
110+
"devServerTarget": "validation-tooltip:serve:production"
111111
}
112112
}
113113
},
@@ -123,5 +123,5 @@
123123
}
124124
}
125125
},
126-
"defaultProject": "project8"
126+
"defaultProject": "validation-tooltip"
127127
}

e2e/src/app.e2e-spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ describe('workspace-project App', () => {
99

1010
it('should display welcome message', () => {
1111
page.navigateTo();
12-
expect(page.getParagraphText()).toEqual('Welcome to project8!');
12+
expect(page.getParagraphText()).toEqual('Welcome to validation-tooltip!');
1313
});
1414
});

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "project8",
2+
"name": "validation-tooltip",
33
"version": "0.0.0",
44
"scripts": {
55
"ng": "ng",

src/app/api.service.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,32 @@ export class ApiService {
1010
private http: HttpClient
1111
) { }
1212

13+
/**
14+
* Get Form content
15+
*/
16+
getFormContent() {
17+
// tslint:disable-next-line:no-shadowed-variable
18+
const promise = new Promise((resolve, reject) => {
19+
const apiURL = 'http://localhost:4200/assets/formcontent.json';
20+
return this.http.get<{frcontent: any}>(apiURL).toPromise().then(
21+
res => {
22+
resolve(res);
23+
},
24+
msg => {
25+
reject(msg);
26+
}
27+
);
28+
});
29+
return promise;
30+
}
31+
1332
/**
14-
* Get Form validation error message
33+
* Get Field Info message
1534
*/
16-
getValidationErrorMessage() {
35+
getFieldInfoMessage() {
1736
// tslint:disable-next-line:no-shadowed-variable
1837
const promise = new Promise((resolve, reject) => {
19-
const apiURL = 'http://localhost:4201/assets/validationerrors.json';
38+
const apiURL = 'http://localhost:4200/assets/fieldInfoContent.json';
2039
return this.http.get<{vlderrors: any}>(apiURL).toPromise().then(
2140
res => {
2241
resolve(res);

src/app/app.component.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.globalError{
2+
position: fixed;
3+
width: 100%;
4+
top: 0;
5+
background: #ffffff;
6+
text-align: center;
7+
}
8+
.globalError p{
9+
margin: 10px 0;
10+
}
11+
.globalError p a{
12+
font-weight: bold;
13+
}

src/app/app.component.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
<!--The content below is only a placeholder and can be replaced.-->
2-
<app-register></app-register>
2+
<div class="globalError" *ngIf="isGlobalError">
3+
<p>To register, please fill up <a href="javascript:void(0);" (click)="goToField()">{{errorsObj.length}}</a> fields.</p>
4+
</div>
5+
<app-register (formErrorsCount)='formErrorsEvent($event)'></app-register>
36

src/app/app.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ describe('AppComponent', () => {
2222
const fixture = TestBed.createComponent(AppComponent);
2323
fixture.detectChanges();
2424
const compiled = fixture.debugElement.nativeElement;
25-
expect(compiled.querySelector('h1').textContent).toContain('Welcome to project8!');
25+
expect(compiled.querySelector('h1').textContent).toContain('Welcome to validation-tooltip!');
2626
}));
2727
});

src/app/app.component.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component } from '@angular/core';
1+
import { Component, ElementRef } from '@angular/core';
22

33
@Component({
44
selector: 'app-root',
@@ -7,4 +7,29 @@ import { Component } from '@angular/core';
77
})
88
export class AppComponent {
99
title = 'app';
10+
errorsObj: any;
11+
isGlobalError = false;
12+
globalErrorTimer: any;
13+
14+
constructor(private el: ElementRef) {
15+
}
16+
17+
formErrorsEvent(evt) {
18+
this.errorsObj = evt;
19+
console.log('formErrorsArr => ', this.errorsObj);
20+
if (this.errorsObj.length > 0) {
21+
this.isGlobalError = true;
22+
window.scrollTo(0, 0);
23+
this.globalErrorTimer = setTimeout( () => {
24+
console.log(this.el.nativeElement.querySelector('.globalError'));
25+
this.el.nativeElement.querySelector('.globalError').focus();
26+
}, 0);
27+
}
28+
}
29+
30+
goToField() {
31+
console.log(this.el.nativeElement);
32+
this.el.nativeElement.querySelector('#' + this.errorsObj[0].fieldName).focus();
33+
}
34+
1035
}

src/app/app.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BrowserModule } from '@angular/platform-browser';
2-
import { NgModule } from '@angular/core';
2+
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
33

44
import { AppComponent } from './app.component';
55
import { RegisterComponent } from './register/register.component';
@@ -22,6 +22,7 @@ import { ValidationLabelDirective } from './validation-label.directive';
2222
HttpClientModule
2323
],
2424
providers: [],
25+
schemas: [ CUSTOM_ELEMENTS_SCHEMA ],
2526
bootstrap: [AppComponent]
2627
})
2728
export class AppModule { }

0 commit comments

Comments
 (0)