strongly typed events, removed deprecations (#2525)

This commit is contained in:
Denys Vuika 2017-10-21 11:09:55 +01:00 committed by Eugenio Romano
parent 4d9591c76c
commit 9ac0c06acd
9 changed files with 85 additions and 39 deletions

View File

@ -45,7 +45,7 @@
<mat-icon>settings</mat-icon> <mat-icon>settings</mat-icon>
</a> </a>
<alfresco-login <adf-login
#alfrescologin #alfrescologin
[providers]="providers" [providers]="providers"
[fieldsValidation]="customValidation" [fieldsValidation]="customValidation"
@ -93,4 +93,4 @@
</mat-slide-toggle> </mat-slide-toggle>
</p> </p>
</div> </div>
</alfresco-login> </adf-login>

View File

@ -28,18 +28,17 @@ export { LoginHeaderDirective } from './src/directives/login-header.directive';
export { LoginFooterDirective } from './src/directives/login-footer.directive'; export { LoginFooterDirective } from './src/directives/login-footer.directive';
export { LoginComponent } from './src/components/login.component'; export { LoginComponent } from './src/components/login.component';
// Old Deprecated export export { LoginErrorEvent } from './src/models/login-error.event';
import { LoginComponent as AlfrescoLoginComponent } from './src/components/login.component'; export { LoginSubmitEvent } from './src/models/login-submit.event';
export { LoginComponent as AlfrescoLoginComponent } from './src/components/login.component'; export { LoginSuccessEvent } from './src/models/login-success.event';
export const ALFRESCO_LOGIN_DIRECTIVES: any[] = [ export function declarations() {
return [
LoginComponent, LoginComponent,
LoginFooterDirective, LoginFooterDirective,
LoginHeaderDirective, LoginHeaderDirective
// Old Deprecated export
AlfrescoLoginComponent
]; ];
}
@NgModule({ @NgModule({
imports: [ imports: [
@ -47,9 +46,7 @@ export const ALFRESCO_LOGIN_DIRECTIVES: any[] = [
CoreModule, CoreModule,
MaterialModule MaterialModule
], ],
declarations: [ declarations: declarations(),
...ALFRESCO_LOGIN_DIRECTIVES
],
providers: [ providers: [
{ {
provide: TRANSLATION_PROVIDER, provide: TRANSLATION_PROVIDER,
@ -61,7 +58,7 @@ export const ALFRESCO_LOGIN_DIRECTIVES: any[] = [
} }
], ],
exports: [ exports: [
...ALFRESCO_LOGIN_DIRECTIVES, ...declarations(),
MaterialModule MaterialModule
] ]
}) })

View File

@ -21,9 +21,9 @@ import { Observable } from 'rxjs/Rx';
export class AuthenticationMock /*extends AlfrescoAuthenticationService*/ { export class AuthenticationMock /*extends AlfrescoAuthenticationService*/ {
// TODO: real auth service returns Observable<string> // TODO: real auth service returns Observable<string>
login(username: string, password: string): Observable<boolean> { login(username: string, password: string): Observable<{ type: string, ticket: any }> {
if (username === 'fake-username' && password === 'fake-password') { if (username === 'fake-username' && password === 'fake-password') {
return Observable.of(true); return Observable.of({ type: 'type', ticket: 'ticket'});
} }
if (username === 'fake-username-CORS-error' && password === 'fake-password') { if (username === 'fake-username-CORS-error' && password === 'fake-password') {

View File

@ -48,7 +48,7 @@
</div> </div>
<!--PASSWORD FIELD--> <!--PASSWORD FIELD-->
<div class="adf-login__field alfresco-login__password"> <div class="adf-login__field">
<mat-form-field class="adf-full-width" floatPlaceholder="never" color="primary"> <mat-form-field class="adf-full-width" floatPlaceholder="never" color="primary">
<input matInput placeholder="{{'LOGIN.LABEL.PASSWORD' | translate }}" <input matInput placeholder="{{'LOGIN.LABEL.PASSWORD' | translate }}"
type="password" type="password"

View File

@ -25,11 +25,13 @@ import { AlfrescoAuthenticationService, CoreModule } from 'ng2-alfresco-core';
import { AlfrescoTranslationService } from 'ng2-alfresco-core'; import { AlfrescoTranslationService } from 'ng2-alfresco-core';
import { MaterialModule } from '../material.module'; import { MaterialModule } from '../material.module';
import { LoginErrorEvent } from '../models/login-error.event';
import { LoginSuccessEvent } from '../models/login-success.event';
import { AuthenticationMock } from './../assets/authentication.service.mock'; import { AuthenticationMock } from './../assets/authentication.service.mock';
import { TranslationMock } from './../assets/translation.service.mock'; import { TranslationMock } from './../assets/translation.service.mock';
import { LoginComponent } from './login.component'; import { LoginComponent } from './login.component';
describe('AlfrescoLogin', () => { describe('LoginComponent', () => {
let component: LoginComponent; let component: LoginComponent;
let fixture: ComponentFixture<LoginComponent>; let fixture: ComponentFixture<LoginComponent>;
let debug: DebugElement; let debug: DebugElement;
@ -523,11 +525,9 @@ describe('AlfrescoLogin', () => {
expect(component.error).toBe(false); expect(component.error).toBe(false);
expect(component.success).toBe(true); expect(component.success).toBe(true);
expect(component.onSuccess.emit).toHaveBeenCalledWith({ expect(component.onSuccess.emit).toHaveBeenCalledWith(
token: true, new LoginSuccessEvent({ type: 'type', ticket: 'ticket' }, 'fake-username', 'fake-password')
username: 'fake-username', );
password: 'fake-password'
});
}); });
it('should emit onError event after the login has failed', () => { it('should emit onError event after the login has failed', () => {
@ -553,7 +553,9 @@ describe('AlfrescoLogin', () => {
expect(component.success).toBe(false); expect(component.success).toBe(false);
expect(getLoginErrorElement()).toBeDefined(); expect(getLoginErrorElement()).toBeDefined();
expect(getLoginErrorMessage()).toEqual('LOGIN.MESSAGES.LOGIN-ERROR-CREDENTIALS'); expect(getLoginErrorMessage()).toEqual('LOGIN.MESSAGES.LOGIN-ERROR-CREDENTIALS');
expect(component.onError.emit).toHaveBeenCalledWith('Fake server error'); expect(component.onError.emit).toHaveBeenCalledWith(
new LoginErrorEvent('Fake server error')
);
}); });
it('should render the password in clear when the toggleShowPassword is call', () => { it('should render the password in clear when the toggleShowPassword is call', () => {
@ -598,6 +600,6 @@ describe('AlfrescoLogin', () => {
expect(component.success).toBe(false); expect(component.success).toBe(false);
expect(getLoginErrorElement()).toBeDefined(); expect(getLoginErrorElement()).toBeDefined();
expect(getLoginErrorMessage()).toEqual('LOGIN.MESSAGES.LOGIN-ERROR-PROVIDERS'); expect(getLoginErrorMessage()).toEqual('LOGIN.MESSAGES.LOGIN-ERROR-PROVIDERS');
expect(component.onError.emit).toHaveBeenCalledWith('LOGIN.MESSAGES.LOGIN-ERROR-PROVIDERS'); expect(component.onError.emit).toHaveBeenCalledWith(new LoginErrorEvent('LOGIN.MESSAGES.LOGIN-ERROR-PROVIDERS'));
}); });
}); });

View File

@ -19,7 +19,10 @@ import { Component, ElementRef, EventEmitter, Input, OnInit, Output, TemplateRef
import { AbstractControl, FormBuilder, FormGroup, Validators } from '@angular/forms'; import { AbstractControl, FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { AlfrescoAuthenticationService, AlfrescoSettingsService, AlfrescoTranslationService, LogService } from 'ng2-alfresco-core'; import { AlfrescoAuthenticationService, AlfrescoSettingsService, AlfrescoTranslationService, LogService } from 'ng2-alfresco-core';
import { FormSubmitEvent } from '../models/form-submit-event.model';
import { LoginErrorEvent } from '../models/login-error.event';
import { LoginSubmitEvent } from '../models/login-submit.event';
import { LoginSuccessEvent } from '../models/login-success.event';
declare var require: any; declare var require: any;
@ -30,7 +33,7 @@ enum LoginSteps {
} }
@Component({ @Component({
selector: 'adf-login, alfresco-login', selector: 'adf-login',
templateUrl: './login.component.html', templateUrl: './login.component.html',
styleUrls: ['./login.component.scss'], styleUrls: ['./login.component.scss'],
host: {'(blur)': 'onBlur($event)'}, host: {'(blur)': 'onBlur($event)'},
@ -74,13 +77,13 @@ export class LoginComponent implements OnInit {
successRoute: string = null; successRoute: string = null;
@Output() @Output()
onSuccess = new EventEmitter(); onSuccess = new EventEmitter<LoginSuccessEvent>();
@Output() @Output()
onError = new EventEmitter(); onError = new EventEmitter<LoginErrorEvent>();
@Output() @Output()
executeSubmit: EventEmitter<FormSubmitEvent> = new EventEmitter<FormSubmitEvent>(); executeSubmit = new EventEmitter<LoginSubmitEvent>();
form: FormGroup; form: FormGroup;
error: boolean = false; error: boolean = false;
@ -139,7 +142,7 @@ export class LoginComponent implements OnInit {
this.disableError(); this.disableError();
let args = new FormSubmitEvent(this.form); const args = new LoginSubmitEvent(this.form);
this.executeSubmit.emit(args); this.executeSubmit.emit(args);
if (args.defaultPrevented) { if (args.defaultPrevented) {
@ -183,7 +186,7 @@ export class LoginComponent implements OnInit {
(token: any) => { (token: any) => {
this.actualLoginStep = LoginSteps.Welcome; this.actualLoginStep = LoginSteps.Welcome;
this.success = true; this.success = true;
this.onSuccess.emit({token: token, username: values.username, password: values.password}); this.onSuccess.emit(new LoginSuccessEvent(token, values.username, values.password));
if (this.successRoute) { if (this.successRoute) {
this.router.navigate([this.successRoute]); this.router.navigate([this.successRoute]);
} }
@ -192,7 +195,7 @@ export class LoginComponent implements OnInit {
this.actualLoginStep = LoginSteps.Landing; this.actualLoginStep = LoginSteps.Landing;
this.displayErrorMessage(err); this.displayErrorMessage(err);
this.enableError(); this.enableError();
this.onError.emit(err); this.onError.emit(new LoginErrorEvent(err));
}, },
() => this.logService.info('Login done') () => this.logService.info('Login done')
); );
@ -230,7 +233,7 @@ export class LoginComponent implements OnInit {
this.enableError(); this.enableError();
let messageProviders: any; let messageProviders: any;
messageProviders = this.translateService.get(this.errorMsg); messageProviders = this.translateService.get(this.errorMsg);
this.onError.emit(messageProviders.value); this.onError.emit(new LoginErrorEvent(messageProviders.value));
return false; return false;
} }
return true; return true;
@ -251,7 +254,7 @@ export class LoginComponent implements OnInit {
* @param ruleId - i.e. required | minlength | maxlength * @param ruleId - i.e. required | minlength | maxlength
* @param msg * @param msg
*/ */
public addCustomValidationError(field: string, ruleId: string, msg: string, params?: any) { addCustomValidationError(field: string, ruleId: string, msg: string, params?: any) {
if (params) { if (params) {
this.translateService.get(msg, params).subscribe((res: string) => { this.translateService.get(msg, params).subscribe((res: string) => {
this._message[field][ruleId] = res; this._message[field][ruleId] = res;

View File

@ -0,0 +1,20 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* 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.
*/
export class LoginErrorEvent {
constructor(public err: any) {}
}

View File

@ -15,7 +15,7 @@
* limitations under the License. * limitations under the License.
*/ */
export class FormSubmitEvent { export class LoginSubmitEvent {
private _values: any; private _values: any;
private _defaultPrevented: boolean = false; private _defaultPrevented: boolean = false;

View File

@ -0,0 +1,24 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* 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.
*/
export class LoginSuccessEvent {
constructor(
public token: any,
public username: string,
public password: string) {
}
}