From e48813b6551ef33897a19ce9064d4936bb76738b Mon Sep 17 00:00:00 2001 From: Deepu k Sasidharan Date: Mon, 10 Oct 2016 17:44:19 +0530 Subject: [PATCH 1/8] ng2: update ng-bootstrap to alpha 7 --- generators/client-2/templates/_package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generators/client-2/templates/_package.json b/generators/client-2/templates/_package.json index 5a3232fa3f4e..353e5c11bb0f 100644 --- a/generators/client-2/templates/_package.json +++ b/generators/client-2/templates/_package.json @@ -16,7 +16,7 @@ "@angular/platform-browser": "2.0.1", "@angular/platform-browser-dynamic": "2.0.1", "@angular/upgrade": "2.0.1", - "@ng-bootstrap/ng-bootstrap": "1.0.0-alpha.6", + "@ng-bootstrap/ng-bootstrap": "1.0.0-alpha.7", "angular-ui-router": "1.0.0-beta.3", "core-js": "2.4.1", "jquery": "3.1.0", From 26ae8af85bdc9b674d4e165725c45b3c98f468b3 Mon Sep 17 00:00:00 2001 From: Deepu k Sasidharan Date: Mon, 10 Oct 2016 22:21:55 +0530 Subject: [PATCH 2/8] ng2: update login modal to use component #4305 --- generators/client-2/index.js | 2 + .../account/activate/_activate.component.ts | 15 ++-- .../webapp/app/account/activate/activate.html | 6 +- .../_password-reset-finish.component.ts | 8 +- .../finish/password-reset-finish.html | 6 +- .../account/register/_register.component.ts | 8 +- .../webapp/app/account/register/register.html | 6 +- .../main/webapp/app/home/_home.component.ts | 8 +- .../src/main/webapp/app/home/home.html | 6 +- .../app/layouts/navbar/_navbar.component.ts | 10 +-- .../webapp/app/layouts/navbar/navbar.html | 6 +- .../src/main/webapp/app/shared/_index.ts | 2 + .../app/shared/_shared-libs.ng2module.ts | 4 +- .../webapp/app/shared/_shared.ng2module.ts | 5 ++ .../webapp/app/shared/auth/_auth.service.ts | 83 ++----------------- .../app/shared/auth/_state-storage.service.ts | 22 +++++ .../app/shared/login/_login-modal.service.ts | 22 +++++ .../app/shared/login/_login.component.ts | 30 +++---- .../webapp/app/shared/login/_login.service.ts | 71 ++++++++++++---- .../main/webapp/app/shared/login/login.html | 2 +- 20 files changed, 167 insertions(+), 155 deletions(-) create mode 100644 generators/client-2/templates/src/main/webapp/app/shared/auth/_state-storage.service.ts create mode 100644 generators/client-2/templates/src/main/webapp/app/shared/login/_login-modal.service.ts diff --git a/generators/client-2/index.js b/generators/client-2/index.js index f1679cfd53c6..c9b322f51e32 100644 --- a/generators/client-2/index.js +++ b/generators/client-2/index.js @@ -520,6 +520,7 @@ module.exports = JhipsterClientGenerator.extend({ } this.copyHtml(ANGULAR_DIR + 'shared/login/login.html', ANGULAR_DIR + 'shared/login/login.html'); this.template(ANGULAR_DIR + 'shared/login/_login.service.ts', ANGULAR_DIR + 'shared/login/login.service.ts', this, {}); + this.template(ANGULAR_DIR + 'shared/login/_login-modal.service.ts', ANGULAR_DIR + 'shared/login/login-modal.service.ts', this, {}); this.template(ANGULAR_DIR + 'shared/login/_login.component.ts', ANGULAR_DIR + 'shared/login/login.component.ts', this, {}); //alert service code @@ -533,6 +534,7 @@ module.exports = JhipsterClientGenerator.extend({ // services this.template(ANGULAR_DIR + 'shared/auth/_auth.service.ts', ANGULAR_DIR + 'shared/auth/auth.service.ts', this, {}); this.template(ANGULAR_DIR + 'shared/auth/_csrf.service.ts', ANGULAR_DIR + 'shared/auth/csrf.service.ts', this, {}); + this.template(ANGULAR_DIR + 'shared/auth/_state-storage.service.ts', ANGULAR_DIR + 'shared/auth/state-storage.service.ts', this, {}); this.template(ANGULAR_DIR + 'shared/auth/_principal.service.ts', ANGULAR_DIR + 'shared/auth/principal.service.ts', this, {}); this.template(ANGULAR_DIR + 'shared/auth/_has-authority.directive.ts', ANGULAR_DIR + 'shared/auth/has-authority.directive.ts', this, {}); this.template(ANGULAR_DIR + 'shared/auth/_has-any-authority.directive.ts', ANGULAR_DIR + 'shared/auth/has-any-authority.directive.ts', this, {}); diff --git a/generators/client-2/templates/src/main/webapp/app/account/activate/_activate.component.ts b/generators/client-2/templates/src/main/webapp/app/account/activate/_activate.component.ts index bc16a916c89d..8635624638d7 100644 --- a/generators/client-2/templates/src/main/webapp/app/account/activate/_activate.component.ts +++ b/generators/client-2/templates/src/main/webapp/app/account/activate/_activate.component.ts @@ -2,7 +2,7 @@ import { Component, Inject, OnInit } from '@angular/core'; import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap'; import { Activate } from './activate.service'; -import { LoginService } from "../../shared"; +import { LoginModalService } from "../../shared"; @Component({ selector: 'activate', @@ -13,10 +13,11 @@ export class ActivateComponent implements OnInit { success: string; modalRef: NgbModalRef; - constructor(private activate: Activate, - private loginService : LoginService, - @Inject('$stateParams') private $stateParams - ) {} + constructor( + private activate: Activate, + private loginModalService : LoginModalService, + @Inject('$stateParams') private $stateParams + ) {} ngOnInit () { this.activate.get(this.$stateParams.key).subscribe(() => { @@ -28,7 +29,7 @@ export class ActivateComponent implements OnInit { }); } - login(template) { - this.modalRef = this.loginService.open(template); + login() { + this.modalRef = this.loginModalService.open(); } } diff --git a/generators/client-2/templates/src/main/webapp/app/account/activate/activate.html b/generators/client-2/templates/src/main/webapp/app/account/activate/activate.html index d8cfe28c685d..27f84f0e4a4b 100644 --- a/generators/client-2/templates/src/main/webapp/app/account/activate/activate.html +++ b/generators/client-2/templates/src/main/webapp/app/account/activate/activate.html @@ -4,7 +4,7 @@

Activation

- Your user has been activated. Please sign in. + Your user has been activated. Please sign in.
@@ -14,7 +14,3 @@

Activation

- - diff --git a/generators/client-2/templates/src/main/webapp/app/account/password-reset/finish/_password-reset-finish.component.ts b/generators/client-2/templates/src/main/webapp/app/account/password-reset/finish/_password-reset-finish.component.ts index 6279af34ac43..154547c0998a 100644 --- a/generators/client-2/templates/src/main/webapp/app/account/password-reset/finish/_password-reset-finish.component.ts +++ b/generators/client-2/templates/src/main/webapp/app/account/password-reset/finish/_password-reset-finish.component.ts @@ -2,7 +2,7 @@ import { Component, OnInit, Inject, Renderer, ElementRef } from '@angular/core'; import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap'; import { PasswordResetFinish } from './password-reset-finish.service'; -import { LoginService } from "../../../shared"; +import { LoginModalService } from "../../../shared"; @Component({ selector: 'password-reset-finish', @@ -18,7 +18,7 @@ export class PasswordResetFinishComponent implements OnInit { modalRef: NgbModalRef; constructor(private passwordResetFinish: PasswordResetFinish, - private loginService : LoginService, + private loginModalService : LoginModalService, @Inject('$stateParams') private $stateParams, private elementRef: ElementRef, private renderer: Renderer ) {} @@ -49,7 +49,7 @@ export class PasswordResetFinishComponent implements OnInit { } } - login(template) { - this.modalRef = this.loginService.open(template); + login() { + this.modalRef = this.loginModalService.open(); } } diff --git a/generators/client-2/templates/src/main/webapp/app/account/password-reset/finish/password-reset-finish.html b/generators/client-2/templates/src/main/webapp/app/account/password-reset/finish/password-reset-finish.html index 873acd56083c..3433faea34ec 100644 --- a/generators/client-2/templates/src/main/webapp/app/account/password-reset/finish/password-reset-finish.html +++ b/generators/client-2/templates/src/main/webapp/app/account/password-reset/finish/password-reset-finish.html @@ -16,7 +16,7 @@

Reset password

-

Your password has been reset. Please sign in.

+

Your password has been reset. Please sign in.

@@ -74,7 +74,3 @@

Reset password

- - diff --git a/generators/client-2/templates/src/main/webapp/app/account/register/_register.component.ts b/generators/client-2/templates/src/main/webapp/app/account/register/_register.component.ts index 9504ae5b19c4..15c00e38997a 100644 --- a/generators/client-2/templates/src/main/webapp/app/account/register/_register.component.ts +++ b/generators/client-2/templates/src/main/webapp/app/account/register/_register.component.ts @@ -2,7 +2,7 @@ import { Component, OnInit, Inject, Renderer, ElementRef } from '@angular/core'; import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap'; import { Register } from './register.service'; -import { LoginService } from "../../shared"; +import { LoginModalService } from "../../shared"; @Component({ selector: '<%=jhiPrefix%>-register', @@ -21,7 +21,7 @@ export class RegisterComponent implements OnInit { constructor( @Inject('$translate') private $translate, - private loginService : LoginService, + private loginModalService : LoginModalService, private registerService: Register, private elementRef: ElementRef, private renderer: Renderer) { @@ -62,7 +62,7 @@ export class RegisterComponent implements OnInit { } } - openLogin(template) { - this.modalRef = this.loginService.open(template); + openLogin() { + this.modalRef = this.loginModalService.open(); } } diff --git a/generators/client-2/templates/src/main/webapp/app/account/register/register.html b/generators/client-2/templates/src/main/webapp/app/account/register/register.html index 841175f74b29..cc8a460d1caf 100644 --- a/generators/client-2/templates/src/main/webapp/app/account/register/register.html +++ b/generators/client-2/templates/src/main/webapp/app/account/register/register.html @@ -118,7 +118,7 @@

Registration

- If you want to sign in, you can try the default accounts:
- Administrator (login="admin" and password="admin")
- User (login="user" and password="user"). + If you want to sign in, you can try the default accounts:
- Administrator (login="admin" and password="admin")
- User (login="user" and password="user").
<%_ if (enableSocialSignIn) { _%> @@ -132,7 +132,3 @@

Registration

<%_ } _%> - - diff --git a/generators/client-2/templates/src/main/webapp/app/home/_home.component.ts b/generators/client-2/templates/src/main/webapp/app/home/_home.component.ts index b4af92e1b4ef..728b7e2e9ec3 100644 --- a/generators/client-2/templates/src/main/webapp/app/home/_home.component.ts +++ b/generators/client-2/templates/src/main/webapp/app/home/_home.component.ts @@ -2,7 +2,7 @@ import { Component, OnInit, Inject } from '@angular/core'; import { StateService } from "ui-router-ng2"; import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap'; -import { Account, LoginService, Principal } from "../shared"; +import { Account, LoginModalService, Principal } from "../shared"; @Component({ selector: 'home', @@ -14,7 +14,7 @@ export class HomeComponent implements OnInit { constructor(private principal: Principal, private $state: StateService, - private loginService : LoginService) { + private loginModalService : LoginModalService) { } ngOnInit() { @@ -31,7 +31,7 @@ export class HomeComponent implements OnInit { this.$state.go('register'); } - login(template) { - this.modalRef = this.loginService.open(template); + login() { + this.modalRef = this.loginModalService.open(); } } diff --git a/generators/client-2/templates/src/main/webapp/app/home/home.html b/generators/client-2/templates/src/main/webapp/app/home/home.html index 0c29c25026b3..aa3a1206988f 100644 --- a/generators/client-2/templates/src/main/webapp/app/home/home.html +++ b/generators/client-2/templates/src/main/webapp/app/home/home.html @@ -13,7 +13,7 @@

Welcome, Java Hipster!

- If you want to sign in, you can try the default accounts:
- Administrator (login="admin" and password="admin")
- User (login="user" and password="user"). + If you want to sign in, you can try the default accounts:
- Administrator (login="admin" and password="admin")
- User (login="user" and password="user").
@@ -40,7 +40,3 @@

Welcome, Java Hipster!

- - diff --git a/generators/client-2/templates/src/main/webapp/app/layouts/navbar/_navbar.component.ts b/generators/client-2/templates/src/main/webapp/app/layouts/navbar/_navbar.component.ts index 11a79d82d8c8..e48b0b441f13 100644 --- a/generators/client-2/templates/src/main/webapp/app/layouts/navbar/_navbar.component.ts +++ b/generators/client-2/templates/src/main/webapp/app/layouts/navbar/_navbar.component.ts @@ -3,7 +3,7 @@ import { StateService } from "ui-router-ng2"; import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap'; import { ProfileService } from '../profiles/profile.service'; //barrel doesnt work here -import { <% if (enableTranslation){ %><%=jhiPrefixCapitalized%>LanguageService, <% } %>Principal, AuthService, LoginService } from '../../shared'; +import { <% if (enableTranslation){ %><%=jhiPrefixCapitalized%>LanguageService, <% } %>Principal, LoginModalService, LoginService } from '../../shared'; @Component({ selector: 'navbar', @@ -25,7 +25,7 @@ export class NavbarComponent implements OnInit { private languageService: <%=jhiPrefixCapitalized%>LanguageService, <%_ } _%> private principal: Principal, - private authService: AuthService, + private loginModalService: LoginModalService, private profileService: ProfileService ) { } @@ -52,13 +52,13 @@ export class NavbarComponent implements OnInit { return this.principal.isAuthenticated(); } - login(template) { - this.modalRef = this.loginService.open(template); + login() { + this.modalRef = this.loginModalService.open(); } logout() { this.collapseNavbar(); - this.authService.logout(); + this.loginService.logout(); this.$state.go('home'); } diff --git a/generators/client-2/templates/src/main/webapp/app/layouts/navbar/navbar.html b/generators/client-2/templates/src/main/webapp/app/layouts/navbar/navbar.html index 404fde09d399..81dc2ad5291b 100644 --- a/generators/client-2/templates/src/main/webapp/app/layouts/navbar/navbar.html +++ b/generators/client-2/templates/src/main/webapp/app/layouts/navbar/navbar.html @@ -73,7 +73,7 @@
  • - +   Sign in @@ -184,7 +184,3 @@ - - diff --git a/generators/client-2/templates/src/main/webapp/app/shared/_index.ts b/generators/client-2/templates/src/main/webapp/app/shared/_index.ts index cac291d96935..e36182cdf8a0 100644 --- a/generators/client-2/templates/src/main/webapp/app/shared/_index.ts +++ b/generators/client-2/templates/src/main/webapp/app/shared/_index.ts @@ -3,6 +3,7 @@ export * from './alert/alert-error.component'; export * from './alert/alert.service'; export * from './alert/alert.provider'; export * from './auth/csrf.service'; +export * from './auth/state-storage.service'; export * from './auth/account.service'; <%_ if (authenticationType === 'oauth2') { _%> export * from './auth/auth-oauth2.service'; @@ -27,6 +28,7 @@ export * from './tracker/tracker.service'; <%_ } _%> export * from './login/login.component'; export * from './login/login.service'; +export * from './login/login-modal.service'; export * from './component/jhi-item-count.component'; export * from './constants/pagination.constants'; export * from './directive/maxbytes.directive'; diff --git a/generators/client-2/templates/src/main/webapp/app/shared/_shared-libs.ng2module.ts b/generators/client-2/templates/src/main/webapp/app/shared/_shared-libs.ng2module.ts index af31ec1dc2d8..f72365f071a9 100644 --- a/generators/client-2/templates/src/main/webapp/app/shared/_shared-libs.ng2module.ts +++ b/generators/client-2/templates/src/main/webapp/app/shared/_shared-libs.ng2module.ts @@ -5,7 +5,9 @@ import { CommonModule } from '@angular/common'; import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; @NgModule({ - imports: [], + imports: [ + NgbModule.forRoot() + ], exports: [ FormsModule, HttpModule, diff --git a/generators/client-2/templates/src/main/webapp/app/shared/_shared.ng2module.ts b/generators/client-2/templates/src/main/webapp/app/shared/_shared.ng2module.ts index d6e1b66bc158..a31f76970403 100644 --- a/generators/client-2/templates/src/main/webapp/app/shared/_shared.ng2module.ts +++ b/generators/client-2/templates/src/main/webapp/app/shared/_shared.ng2module.ts @@ -11,7 +11,9 @@ import { Base64, <%_ } _%> AccountService, + StateStorageService, LoginService, + LoginModalService, Principal, <%_ if (websocket === 'spring-websocket') { _%> <%=jhiPrefixCapitalized%>TrackerService, @@ -33,7 +35,9 @@ import { ], providers: [ LoginService, + LoginModalService, AccountService, + StateStorageService, Principal, CSRFService, AuthService, @@ -45,6 +49,7 @@ import { <%_ } _%> AuthServerProvider ], + entryComponents: [<%=jhiPrefixCapitalized%>LoginModalComponent], exports: [ <%=angular2AppName%>SharedCommonModule, <%=jhiPrefixCapitalized%>LoginModalComponent, diff --git a/generators/client-2/templates/src/main/webapp/app/shared/auth/_auth.service.ts b/generators/client-2/templates/src/main/webapp/app/shared/auth/_auth.service.ts index 3647a1e39528..b30928319919 100644 --- a/generators/client-2/templates/src/main/webapp/app/shared/auth/_auth.service.ts +++ b/generators/client-2/templates/src/main/webapp/app/shared/auth/_auth.service.ts @@ -2,18 +2,9 @@ import { Injectable, Inject } from '@angular/core'; import { StateService } from 'ui-router-ng2'; import { SessionStorageService } from 'ng2-webstorage'; -import { LoginService } from "../login/login.service"; +import { LoginModalService } from "../login/login.modal.service"; import { Principal } from './principal.service'; -<%_ if (authenticationType === 'oauth2') { _%> -import { AuthServerProvider } from './auth-oauth2.service'; -<%_ } else if (authenticationType === 'jwt' || authenticationType === 'uaa') { _%> -import { AuthServerProvider } from './auth-jwt.service'; -<%_ } else { _%> -import { AuthServerProvider } from './auth-session.service'; -<%_ } _%> -<%_ if (websocket === 'spring-websocket') { _%> -import { <%=jhiPrefixCapitalized%>TrackerService } from '../tracker/tracker.service'; -<%_ } _%> +import { StateStorageService } from './state-storage.service'; @Injectable() export class AuthService { @@ -21,12 +12,9 @@ export class AuthService { constructor( private principal: Principal, private $state: StateService, - private authServerProvider: AuthServerProvider, - private loginService : LoginService, + private stateStorageService: StateStorageService, + private loginModalService : LoginModalService, private $sessionStorage: SessionStorageService, - <%_ if (websocket === 'spring-websocket') { _%> - private trackerService: <%=jhiPrefixCapitalized%>TrackerService, - <%_ } _%> <%_ if (enableTranslation){ _%> @Inject('$translate') private $translate, <%_ } _%> @@ -47,9 +35,9 @@ export class AuthService { } // recover and clear previousState after external login redirect (e.g. oauth2) - if (isAuthenticated && !this.$rootScope.fromState.name && this.getPreviousState()) { - var previousState = this.getPreviousState(); - this.resetPreviousState(); + var previousState = this.stateStorageService.getPreviousState(); + if (isAuthenticated && !this.$rootScope.fromState.name && previousState) { + this.stateStorageService.resetPreviousState(); this.$state.go(previousState.name, previousState.params); } @@ -61,67 +49,14 @@ export class AuthService { else { // user is not authenticated. stow the state they wanted before you // send them to the login service, so you can return them when you're done - this.storePreviousState(this.$rootScope.toState.name, this.$rootScope.toStateParams); + this.stateStorageService.storePreviousState(this.$rootScope.toState.name, this.$rootScope.toStateParams); // now, send them to the signin state so they can log in this.$state.go('accessdenied').then(() => { - //this.loginService.open(); //TODO needs to fixed once modal supports components + this.loginModalService.open(); //TODO needs to fixed once modal supports components }); } } } } - - login (credentials, callback?) { - var cb = callback || function(){}; - - return new Promise((resolve, reject) => { - this.authServerProvider.login(credentials).subscribe(data => { - this.principal.identity(true).then(account => { - <%_ if (enableTranslation){ _%> - // After the login the language will be changed to - // the language selected by the user during his registration - if (account!== null) { //TODO migrate - this.$translate.use(account.langKey).then(() => { - this.$translate.refresh(); - }); - } - <%_ } _%> - <%_ if (websocket === 'spring-websocket') { _%> - this.trackerService.sendActivity(); - <%_ } _%> - resolve(data); - }); - return cb(); - }, err => { - this.logout(); - reject(err); - return cb(err); - }); - }); - } - - <%_ if (authenticationType == 'jwt') { _%> - loginWithToken(jwt, rememberMe) { - return this.authServerProvider.loginWithToken(jwt, rememberMe); - } - <%_ } _%> - - logout () { - this.authServerProvider.logout().subscribe(); - this.principal.authenticate(null); - } - - getPreviousState() { - return this.$sessionStorage.retrieve('previousState'); - } - - resetPreviousState() { - this.$sessionStorage.clear('previousState'); - } - - storePreviousState(previousStateName, previousStateParams) { - var previousState = { "name": previousStateName, "params": previousStateParams }; - this.$sessionStorage.store('previousState', previousState); - } } diff --git a/generators/client-2/templates/src/main/webapp/app/shared/auth/_state-storage.service.ts b/generators/client-2/templates/src/main/webapp/app/shared/auth/_state-storage.service.ts new file mode 100644 index 000000000000..256f2bb59e31 --- /dev/null +++ b/generators/client-2/templates/src/main/webapp/app/shared/auth/_state-storage.service.ts @@ -0,0 +1,22 @@ +import { Injectable, Inject } from '@angular/core'; +import { SessionStorageService } from 'ng2-webstorage'; + +@Injectable() +export class StateStorageService { + constructor( + private $sessionStorage: SessionStorageService + ){} + + getPreviousState() { + return this.$sessionStorage.retrieve('previousState'); + } + + resetPreviousState() { + this.$sessionStorage.clear('previousState'); + } + + storePreviousState(previousStateName, previousStateParams) { + var previousState = { "name": previousStateName, "params": previousStateParams }; + this.$sessionStorage.store('previousState', previousState); + } +} diff --git a/generators/client-2/templates/src/main/webapp/app/shared/login/_login-modal.service.ts b/generators/client-2/templates/src/main/webapp/app/shared/login/_login-modal.service.ts new file mode 100644 index 000000000000..16e7a4209eac --- /dev/null +++ b/generators/client-2/templates/src/main/webapp/app/shared/login/_login-modal.service.ts @@ -0,0 +1,22 @@ +import { Injectable, Inject } from '@angular/core'; +import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'; + +import { <%=jhiPrefixCapitalized%>LoginModalComponent } from './login.component'; + +@Injectable() +export class LoginModalService { + + constructor ( + private modalService: NgbModal, + ) {} + + open (): NgbModalRef { + let modalRef = this.modalService.open(<%=jhiPrefixCapitalized%>LoginModalComponent); + modalRef.result.then(result => { + console.log(`Closed with: ${result}`); + }, (reason) => { + console.log(`Dismissed ${reason}`); + }); + return modalRef; + } +} diff --git a/generators/client-2/templates/src/main/webapp/app/shared/login/_login.component.ts b/generators/client-2/templates/src/main/webapp/app/shared/login/_login.component.ts index e756d8c6e22b..196bb5e2bdd4 100644 --- a/generators/client-2/templates/src/main/webapp/app/shared/login/_login.component.ts +++ b/generators/client-2/templates/src/main/webapp/app/shared/login/_login.component.ts @@ -1,13 +1,13 @@ import { Component, OnInit, Inject, Renderer, ElementRef } from '@angular/core'; -import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap'; +import { NgbModalRef, NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'; import { StateService } from "ui-router-ng2"; -import { AuthService, Principal } from '../'; +import { LoginService } from '../login/login.service'; +import { StateStorageService } from '../auth/state-storage.service'; @Component({ selector: '<%=jhiPrefix%>-login-modal', - templateUrl: 'app/shared/login/login.html', - inputs: ['modalRef', 'dismiss'] + templateUrl: 'app/shared/login/login.html' }) export class <%=jhiPrefixCapitalized%>LoginModalComponent implements OnInit { authenticationError: boolean; @@ -15,15 +15,15 @@ export class <%=jhiPrefixCapitalized%>LoginModalComponent implements OnInit { rememberMe: boolean; username: string; credentials: any; - modalRef: NgbModalRef; constructor( @Inject('$rootScope') private $rootScope, private $state: StateService, - private principal: Principal, - private auth: AuthService, + private loginService: LoginService, + private stateStorageService: StateStorageService, private elementRef: ElementRef, - private renderer: Renderer + private renderer: Renderer, + private activeModal: NgbActiveModal ) { this.credentials = {}; } @@ -42,17 +42,17 @@ export class <%=jhiPrefixCapitalized%>LoginModalComponent implements OnInit { rememberMe: true }; this.authenticationError = false; - this.modalRef.dismiss('cancel'); + this.activeModal.dismiss('cancel'); } login () { - this.auth.login({ + this.loginService.login({ username: this.username, password: this.password, rememberMe: this.rememberMe }).then(() => { this.authenticationError = false; - this.modalRef.dismiss('cancel'); + this.activeModal.dismiss('login success'); if (this.$state.current.name === 'register' || this.$state.current.name === 'activate' || this.$state.current.name === 'finishReset' || this.$state.current.name === 'requestReset') { this.$state.go('home'); @@ -62,9 +62,9 @@ export class <%=jhiPrefixCapitalized%>LoginModalComponent implements OnInit { // previousState was set in the authExpiredInterceptor before being redirected to login modal. // since login is succesful, go to stored previousState and clear previousState - var previousState = this.auth.getPreviousState(); + var previousState = this.stateStorageService.getPreviousState(); if (previousState) { - this.auth.resetPreviousState(); + this.stateStorageService.resetPreviousState(); this.$state.go(previousState.name, previousState.params); } }).catch(() => { @@ -73,12 +73,12 @@ export class <%=jhiPrefixCapitalized%>LoginModalComponent implements OnInit { } register () { - this.modalRef.dismiss('cancel'); + this.activeModal.dismiss('to state register'); this.$state.go('register'); } requestResetPassword () { - this.modalRef.dismiss('cancel'); + this.activeModal.dismiss('to state requestReset'); this.$state.go('requestReset'); } } diff --git a/generators/client-2/templates/src/main/webapp/app/shared/login/_login.service.ts b/generators/client-2/templates/src/main/webapp/app/shared/login/_login.service.ts index 0cf68e559352..f8e912b557af 100644 --- a/generators/client-2/templates/src/main/webapp/app/shared/login/_login.service.ts +++ b/generators/client-2/templates/src/main/webapp/app/shared/login/_login.service.ts @@ -1,25 +1,66 @@ -import { Injectable } from '@angular/core'; -import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'; +import { Injectable, Inject } from '@angular/core'; + +import { Principal } from '../auth/principal.service'; +<%_ if (authenticationType === 'oauth2') { _%> +import { AuthServerProvider } from '../auth/auth-oauth2.service'; +<%_ } else if (authenticationType === 'jwt' || authenticationType === 'uaa') { _%> +import { AuthServerProvider } from '../auth/auth-jwt.service'; +<%_ } else { _%> +import { AuthServerProvider } from '../auth/auth-session.service'; +<%_ } _%> +<%_ if (websocket === 'spring-websocket') { _%> +import { <%=jhiPrefixCapitalized%>TrackerService } from '../tracker/tracker.service'; +<%_ } _%> @Injectable() export class LoginService { - modalInstance: any; - constructor (private modalService: NgbModal) { - this.modalInstance = null; + constructor ( + @Inject('$translate') private $translate, + private principal: Principal, + <%_ if (websocket === 'spring-websocket') { _%> + private trackerService: <%=jhiPrefixCapitalized%>TrackerService, + <%_ } _%> + private authServerProvider: AuthServerProvider + ) {} + + login (credentials, callback?) { + var cb = callback || function(){}; + + return new Promise((resolve, reject) => { + this.authServerProvider.login(credentials).subscribe(data => { + this.principal.identity(true).then(account => { + <%_ if (enableTranslation){ _%> + // After the login the language will be changed to + // the language selected by the user during his registration + if (account!== null) { //TODO migrate + this.$translate.use(account.langKey).then(() => { + this.$translate.refresh(); + }); + } + <%_ } _%> + <%_ if (websocket === 'spring-websocket') { _%> + this.trackerService.sendActivity(); + <%_ } _%> + resolve(data); + }); + return cb(); + }, err => { + this.logout(); + reject(err); + return cb(err); + }); + }); } - resetModal () : any { - this.modalInstance = null; + <%_ if (authenticationType == 'jwt') { _%> + loginWithToken(jwt, rememberMe) { + return this.authServerProvider.loginWithToken(jwt, rememberMe); } + <%_ } _%> - open (template): NgbModalRef { - let modalRef = this.modalService.open(template); - modalRef.result.then(result => { - console.log(`Closed with: ${result}`); - }, (reason) => { - console.log(`Dismissed ${reason}`); - }); - return modalRef; + logout () { + this.authServerProvider.logout().subscribe(); + this.principal.authenticate(null); } } diff --git a/generators/client-2/templates/src/main/webapp/app/shared/login/login.html b/generators/client-2/templates/src/main/webapp/app/shared/login/login.html index d7928a4bd3c9..f23d1b3040f3 100644 --- a/generators/client-2/templates/src/main/webapp/app/shared/login/login.html +++ b/generators/client-2/templates/src/main/webapp/app/shared/login/login.html @@ -1,5 +1,5 @@ From 14024da68eabd3e74b7ab8d25828689715a26e16 Mon Sep 17 00:00:00 2001 From: Deepu k Sasidharan Date: Mon, 10 Oct 2016 22:36:17 +0530 Subject: [PATCH 3/8] ng2: polish --- .../src/main/webapp/app/shared/auth/_auth.service.ts | 4 ++-- .../src/main/webapp/app/shared/login/_login-modal.service.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/generators/client-2/templates/src/main/webapp/app/shared/auth/_auth.service.ts b/generators/client-2/templates/src/main/webapp/app/shared/auth/_auth.service.ts index b30928319919..93d89b3f25ea 100644 --- a/generators/client-2/templates/src/main/webapp/app/shared/auth/_auth.service.ts +++ b/generators/client-2/templates/src/main/webapp/app/shared/auth/_auth.service.ts @@ -2,7 +2,7 @@ import { Injectable, Inject } from '@angular/core'; import { StateService } from 'ui-router-ng2'; import { SessionStorageService } from 'ng2-webstorage'; -import { LoginModalService } from "../login/login.modal.service"; +import { LoginModalService } from "../login/login-modal.service"; import { Principal } from './principal.service'; import { StateStorageService } from './state-storage.service'; @@ -53,7 +53,7 @@ export class AuthService { // now, send them to the signin state so they can log in this.$state.go('accessdenied').then(() => { - this.loginModalService.open(); //TODO needs to fixed once modal supports components + this.loginModalService.open(); }); } } diff --git a/generators/client-2/templates/src/main/webapp/app/shared/login/_login-modal.service.ts b/generators/client-2/templates/src/main/webapp/app/shared/login/_login-modal.service.ts index 16e7a4209eac..b82a537c8e2d 100644 --- a/generators/client-2/templates/src/main/webapp/app/shared/login/_login-modal.service.ts +++ b/generators/client-2/templates/src/main/webapp/app/shared/login/_login-modal.service.ts @@ -1,4 +1,4 @@ -import { Injectable, Inject } from '@angular/core'; +import { Injectable } from '@angular/core'; import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'; import { <%=jhiPrefixCapitalized%>LoginModalComponent } from './login.component'; From 3ec08de697e4c276b94eb221806e8b7473d0dd4a Mon Sep 17 00:00:00 2001 From: Deepu k Sasidharan Date: Tue, 11 Oct 2016 13:41:38 +0530 Subject: [PATCH 4/8] polish: ng2 --- .../src/main/webapp/app/account/_account.ng2module.ts | 3 +-- .../templates/src/main/webapp/app/admin/_admin.ng2module.ts | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/generators/client-2/templates/src/main/webapp/app/account/_account.ng2module.ts b/generators/client-2/templates/src/main/webapp/app/account/_account.ng2module.ts index 5a3eba1df79e..11df16b92182 100644 --- a/generators/client-2/templates/src/main/webapp/app/account/_account.ng2module.ts +++ b/generators/client-2/templates/src/main/webapp/app/account/_account.ng2module.ts @@ -1,5 +1,4 @@ -import { NgModule } from '@angular/core'; -import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; +import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; import { UIRouterModule } from 'ui-router-ng2'; import { <%=angular2AppName%>SharedModule } from '../shared'; diff --git a/generators/client-2/templates/src/main/webapp/app/admin/_admin.ng2module.ts b/generators/client-2/templates/src/main/webapp/app/admin/_admin.ng2module.ts index 8978a1d9ff50..5ab23f532e11 100644 --- a/generators/client-2/templates/src/main/webapp/app/admin/_admin.ng2module.ts +++ b/generators/client-2/templates/src/main/webapp/app/admin/_admin.ng2module.ts @@ -1,5 +1,4 @@ -import { NgModule } from '@angular/core'; -import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; +import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; import { UIRouterModule } from 'ui-router-ng2'; import { <%=angular2AppName%>SharedModule, ParseLinks } from '../shared'; From 712640f80923965593024bfeb5a15594cde5c581 Mon Sep 17 00:00:00 2001 From: Deepu k Sasidharan Date: Tue, 11 Oct 2016 14:50:56 +0530 Subject: [PATCH 5/8] feat: i18n remove dummy translate pipe --- generators/client-2/index.js | 1 - .../src/main/webapp/app/shared/pipe/translate.pipe.ts | 8 -------- 2 files changed, 9 deletions(-) delete mode 100644 generators/client-2/templates/src/main/webapp/app/shared/pipe/translate.pipe.ts diff --git a/generators/client-2/index.js b/generators/client-2/index.js index cf14a8ba4b90..9258cebc11a8 100644 --- a/generators/client-2/index.js +++ b/generators/client-2/index.js @@ -487,7 +487,6 @@ module.exports = JhipsterClientGenerator.extend({ this.template(ANGULAR_DIR + 'shared/model/account.model.ts', ANGULAR_DIR + 'shared/model/account.model.ts', this, {}); - this.template(ANGULAR_DIR + 'shared/pipe/translate.pipe.ts', ANGULAR_DIR + 'shared/pipe/translate.pipe.ts', this, {}); this.template(ANGULAR_DIR + 'shared/pipe/keys.pipe.ts', ANGULAR_DIR + 'shared/pipe/keys.pipe.ts', this, {}); this.template(ANGULAR_DIR + 'shared/pipe/filter.pipe.ts', ANGULAR_DIR + 'shared/pipe/filter.pipe.ts', this, {}); this.template(ANGULAR_DIR + 'shared/pipe/order-by.pipe.ts', ANGULAR_DIR + 'shared/pipe/order-by.pipe.ts', this, {}); diff --git a/generators/client-2/templates/src/main/webapp/app/shared/pipe/translate.pipe.ts b/generators/client-2/templates/src/main/webapp/app/shared/pipe/translate.pipe.ts deleted file mode 100644 index 21a10014a772..000000000000 --- a/generators/client-2/templates/src/main/webapp/app/shared/pipe/translate.pipe.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { Pipe, PipeTransform } from '@angular/core'; -//TODO dummy translate pipe -@Pipe({name: 'translate'}) -export class TranslatePipe implements PipeTransform { - transform(input: any, filterValue: string, field: string): any { - return input; - } -} From 973b257062fe15a86bfb7f507dfc429115a841c7 Mon Sep 17 00:00:00 2001 From: Deepu k Sasidharan Date: Tue, 11 Oct 2016 21:08:13 +0530 Subject: [PATCH 6/8] feat: i18n add translate module to libs --- .../src/main/webapp/app/home/_home.state.ts | 2 +- .../src/main/webapp/app/home/home.html | 27 ++++++------ .../src/main/webapp/app/shared/_index.ts | 3 +- .../app/shared/_shared-common.ng2module.ts | 8 ++-- .../app/shared/_shared-libs.ng2module.ts | 24 ++++++++++- .../webapp/app/shared/_shared.ng2module.ts | 43 +++---------------- 6 files changed, 50 insertions(+), 57 deletions(-) diff --git a/generators/client-2/templates/src/main/webapp/app/home/_home.state.ts b/generators/client-2/templates/src/main/webapp/app/home/_home.state.ts index ab3e61fc6955..a139d97fce4b 100644 --- a/generators/client-2/templates/src/main/webapp/app/home/_home.state.ts +++ b/generators/client-2/templates/src/main/webapp/app/home/_home.state.ts @@ -15,6 +15,6 @@ export const homeState = { resolve: [{ token: 'translate', deps: [JhiLanguageService], - resolveFn: (jhiLanguageService) => jhiLanguageService.setLocation('home') + resolveFn: (jhiLanguageService) => jhiLanguageService.setLocations(['home']) }] } diff --git a/generators/client-2/templates/src/main/webapp/app/home/home.html b/generators/client-2/templates/src/main/webapp/app/home/home.html index d148f2b934ce..3bef8cf424aa 100644 --- a/generators/client-2/templates/src/main/webapp/app/home/home.html +++ b/generators/client-2/templates/src/main/webapp/app/home/home.html @@ -4,38 +4,39 @@
    -

    Welcome, Java Hipster!

    -

    This is your homepage

    +

    Welcome, Java Hipster!

    +

    This is your homepage

    -
    - You are logged in as user "{{account.login}}". +
    + You are logged in as user "{{account.login}}".
    -
    +
    If you want to sign in, you can try the default accounts:
    - Administrator (login="admin" and password="admin")
    - User (login="user" and password="user").
    -
    +
    You don't have an account yet? Register a new account
    -

    +

    If you have any question on JHipster:

    - If you like JHipster, don't forget to give us a star on  Github! + If you like JHipster, don't forget to give us a star on  Github!

    diff --git a/generators/client-2/templates/src/main/webapp/app/shared/_index.ts b/generators/client-2/templates/src/main/webapp/app/shared/_index.ts index e36182cdf8a0..086063cf7933 100644 --- a/generators/client-2/templates/src/main/webapp/app/shared/_index.ts +++ b/generators/client-2/templates/src/main/webapp/app/shared/_index.ts @@ -21,7 +21,8 @@ export * from './auth/has-authority.directive'; export * from './language/language.constants'; export * from './language/language.service'; export * from './language/language.pipe'; -export * from './pipe/translate.pipe'; +export * from './language/jhi-translate.directive'; +export * from './language/jhi-missing-translation.config'; <%_ } _%> <%_ if (websocket === 'spring-websocket') { _%> export * from './tracker/tracker.service'; diff --git a/generators/client-2/templates/src/main/webapp/app/shared/_shared-common.ng2module.ts b/generators/client-2/templates/src/main/webapp/app/shared/_shared-common.ng2module.ts index 6bfaded8cce4..6d98d92faf6b 100644 --- a/generators/client-2/templates/src/main/webapp/app/shared/_shared-common.ng2module.ts +++ b/generators/client-2/templates/src/main/webapp/app/shared/_shared-common.ng2module.ts @@ -8,7 +8,8 @@ import { FilterPipe, OrderByPipe, <%_ if (enableTranslation){ _%> - TranslatePipe, + <%=jhiPrefixCapitalized%>Translate, + <%=jhiPrefixCapitalized%>MissingTranslationHandler, <%=jhiPrefixCapitalized%>LanguageService, FindLanguageFromKeyPipe, <%_ }_%> @@ -34,7 +35,7 @@ import { CapitalizePipe, KeysPipe, <%_ if (enableTranslation){ _%> - TranslatePipe, + <%=jhiPrefixCapitalized%>Translate, FindLanguageFromKeyPipe, <%_ } _%> JhiAlertComponent, @@ -47,6 +48,7 @@ import { providers: [ <%_ if (enableTranslation){ _%> <%=jhiPrefixCapitalized%>LanguageService, + { provide: MissingTranslationHandler, useClass: <%=jhiPrefixCapitalized%>MissingTranslationHandler } <%_ } _%> alertServiceProvider() ], @@ -59,7 +61,7 @@ import { CapitalizePipe, KeysPipe, <%_ if (enableTranslation){ _%> - TranslatePipe, + <%=jhiPrefixCapitalized%>Translate, FindLanguageFromKeyPipe, <%_ } _%> JhiAlertComponent, diff --git a/generators/client-2/templates/src/main/webapp/app/shared/_shared-libs.ng2module.ts b/generators/client-2/templates/src/main/webapp/app/shared/_shared-libs.ng2module.ts index f72365f071a9..e054971389b2 100644 --- a/generators/client-2/templates/src/main/webapp/app/shared/_shared-libs.ng2module.ts +++ b/generators/client-2/templates/src/main/webapp/app/shared/_shared-libs.ng2module.ts @@ -1,18 +1,38 @@ -import { NgModule } from '@angular/core'; +import { NgModule, ModuleWithProviders } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { HttpModule } from '@angular/http'; import { CommonModule } from '@angular/common'; import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; +<%_ if (enableTranslation){ _%> +import { TranslateModule } from 'ng2-translate/ng2-translate'; +import { createTranslatePartialLoader } from './language/translate-partial-loader.provider'; +<%_ } _%> @NgModule({ imports: [ + <%_ if (enableTranslation){ _%> + TranslateModule.forRoot(createTranslatePartialLoader()), + <%_ } _%> NgbModule.forRoot() + ], exports: [ FormsModule, HttpModule, CommonModule, + <%_ if (enableTranslation){ _%> + TranslateModule, + <%_ } _%> NgbModule ] }) -export class <%=angular2AppName%>SharedLibsModule {} +export class <%=angular2AppName%>SharedLibsModule { + <%_ if (enableTranslation){ _%> + /*static forRoot(): ModuleWithProviders { + return { + ngModule: Angular2TestSharedLibsModule, + providers: [TranslateService], + }; + }*/ + <%_ } _%> +} diff --git a/generators/client-2/templates/src/main/webapp/app/shared/_shared.ng2module.ts b/generators/client-2/templates/src/main/webapp/app/shared/_shared.ng2module.ts index 51e5aa8704db..fbdd4d88702a 100644 --- a/generators/client-2/templates/src/main/webapp/app/shared/_shared.ng2module.ts +++ b/generators/client-2/templates/src/main/webapp/app/shared/_shared.ng2module.ts @@ -1,15 +1,4 @@ -import { NgModule } from '@angular/core'; -import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; - -<%_ if (enableTranslation){ _%> -import { ModuleWithProviders } from '@angular/core'; -import { Http } from '@angular/http'; - -import { TranslateModule, TranslateLoader, TranslateStaticLoader, TranslateService,MissingTranslationHandler } from 'ng2-translate/ng2-translate'; -import { <%=jhiPrefixCapitalized%>MissingTranslationHandler} from './language/<%=jhiPrefix%>-missing-translation.config'; -<%_ } _%> - -import {<%=jhiPrefixCapitalized%>Translate} from './directive/<%=jhiPrefix%>-translate'; +import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; import { <%=angular2AppName%>SharedLibsModule, @@ -36,19 +25,12 @@ import { @NgModule({ imports: [ <%=angular2AppName%>SharedLibsModule, - <%=angular2AppName%>SharedCommonModule<%_ if (enableTranslation){ _%>, - TranslateModule.forRoot({ - provide: TranslateLoader, - useFactory: (http: Http) => new TranslateStaticLoader(http, '/i18n', '.json'), - deps: [Http] - }) - <%_ } _%> + <%=angular2AppName%>SharedCommonModule ], declarations: [ <%=jhiPrefixCapitalized%>LoginModalComponent, HasAuthorityDirective, - HasAnyAuthorityDirective, - <%=jhiPrefixCapitalized%>Translate + HasAnyAuthorityDirective ], providers: [ LoginService, @@ -64,29 +46,16 @@ import { <%_ if (websocket === 'spring-websocket') { _%> <%=jhiPrefixCapitalized%>TrackerService, <%_ } _%> - AuthServerProvider<%_ if (enableTranslation){ _%>, - { provide: MissingTranslationHandler, useClass: <%=jhiPrefixCapitalized%>MissingTranslationHandler } - <%_ } _%> + AuthServerProvider ], entryComponents: [<%=jhiPrefixCapitalized%>LoginModalComponent], exports: [ <%=angular2AppName%>SharedCommonModule, <%=jhiPrefixCapitalized%>LoginModalComponent, HasAuthorityDirective, - HasAnyAuthorityDirective, - TranslateModule, - <%=jhiPrefixCapitalized%>Translate + HasAnyAuthorityDirective ], schemas: [CUSTOM_ELEMENTS_SCHEMA] }) -export class <%=angular2AppName%>SharedModule { - <%_ if (enableTranslation){ _%> - static forRoot(): ModuleWithProviders { - return { - ngModule: <%=angular2AppName%>SharedModule, - providers: [TranslateService], - }; - } - <%_ } _%> -} +export class <%=angular2AppName%>SharedModule {} From 0d70bec783754642f15c2dd9c10f49251b48ae3e Mon Sep 17 00:00:00 2001 From: Deepu k Sasidharan Date: Tue, 11 Oct 2016 21:18:18 +0530 Subject: [PATCH 7/8] feat: i18n add custom partial loader --- generators/client-2/index.js | 3 +- .../app/shared/directive/jhi-translate.ts | 28 ---------- .../shared/language/_language.constants.ts | 3 +- .../app/shared/language/_language.service.ts | 36 ++++++++----- .../language/jhi-translate.directive.ts | 13 +++++ .../translate-partial-loader.provider.ts | 52 +++++++++++++++++++ 6 files changed, 93 insertions(+), 42 deletions(-) delete mode 100644 generators/client-2/templates/src/main/webapp/app/shared/directive/jhi-translate.ts create mode 100644 generators/client-2/templates/src/main/webapp/app/shared/language/jhi-translate.directive.ts create mode 100644 generators/client-2/templates/src/main/webapp/app/shared/language/translate-partial-loader.provider.ts diff --git a/generators/client-2/index.js b/generators/client-2/index.js index 9258cebc11a8..a390f0640f30 100644 --- a/generators/client-2/index.js +++ b/generators/client-2/index.js @@ -500,7 +500,6 @@ module.exports = JhipsterClientGenerator.extend({ this.template(ANGULAR_DIR + 'shared/directive/maxbytes.directive.ts', ANGULAR_DIR + 'shared/directive/maxbytes.directive.ts', this, {}); this.template(ANGULAR_DIR + 'shared/directive/minbytes.directive.ts', ANGULAR_DIR + 'shared/directive/minbytes.directive.ts', this, {}); this.template(ANGULAR_DIR + 'shared/directive/number-of-bytes.ts', ANGULAR_DIR + 'shared/directive/number-of-bytes.ts', this, {}); - this.template(ANGULAR_DIR + 'shared/directive/jhi-translate.ts', ANGULAR_DIR + 'shared/directive/jhi-translate.ts', this, {}); this.template(ANGULAR_DIR + 'shared/service/date-util.service.ts', ANGULAR_DIR + 'shared/service/date-util.service.ts', this, {}); this.template(ANGULAR_DIR + 'shared/service/data-util.service.ts', ANGULAR_DIR + 'shared/service/data-util.service.ts', this, {}); @@ -514,6 +513,8 @@ module.exports = JhipsterClientGenerator.extend({ this.template(ANGULAR_DIR + 'shared/_shared-common.ng2module.ts', ANGULAR_DIR + 'shared/shared-common.ng2module.ts', this, {}); //components if (this.enableTranslation) { + this.template(ANGULAR_DIR + 'shared/language/jhi-translate.directive.ts', ANGULAR_DIR + 'shared/language/jhi-translate.directive.ts', this, {}); + this.template(ANGULAR_DIR + 'shared/language/translate-partial-loader.provider.ts', ANGULAR_DIR + 'shared/language/translate-partial-loader.provider.ts', this, {}); this.template(ANGULAR_DIR + 'shared/language/_language.pipe.ts', ANGULAR_DIR + 'shared/language/language.pipe.ts', this, {}); this.template(ANGULAR_DIR + 'shared/language/_language.constants.ts', ANGULAR_DIR + 'shared/language/language.constants.ts', this, {}); this.template(ANGULAR_DIR + 'shared/language/_language.service.ts', ANGULAR_DIR + 'shared/language/language.service.ts', this, {}); diff --git a/generators/client-2/templates/src/main/webapp/app/shared/directive/jhi-translate.ts b/generators/client-2/templates/src/main/webapp/app/shared/directive/jhi-translate.ts deleted file mode 100644 index 39897be0a944..000000000000 --- a/generators/client-2/templates/src/main/webapp/app/shared/directive/jhi-translate.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { Directive, ElementRef, Input, Renderer, TemplateRef } from '@angular/core'; - - -@Directive({ - selector: '[jhiTranslate]', - exportAs: 'jhiTranslate' -}) -export class JhiTranslate { - - private _jhiTranslate: string | TemplateRef; - - constructor(private el: ElementRef, private renderer: Renderer) { } - - @Input() - set jhiTranslate(value: string | TemplateRef) { - this._jhiTranslate = value; - this.setTranslatedValue(); - } - - setTranslatedValue() { - if (this._jhiTranslate ) { - if ( this._jhiTranslate != 'err-no-translation-found') { - this.el.nativeElement.innerHTML = this._jhiTranslate; - } - } - } - -} diff --git a/generators/client-2/templates/src/main/webapp/app/shared/language/_language.constants.ts b/generators/client-2/templates/src/main/webapp/app/shared/language/_language.constants.ts index 868d099e2ac4..d1992527d6d6 100644 --- a/generators/client-2/templates/src/main/webapp/app/shared/language/_language.constants.ts +++ b/generators/client-2/templates/src/main/webapp/app/shared/language/_language.constants.ts @@ -3,5 +3,6 @@ They are written in English to avoid character encoding issues (not a perfect solution) */ export const LANGUAGES: string[] = [ + '<%= nativeLanguage %>' // jhipster-needle-i18n-language-constant - JHipster will add/remove languages in this array -]; \ No newline at end of file +]; diff --git a/generators/client-2/templates/src/main/webapp/app/shared/language/_language.service.ts b/generators/client-2/templates/src/main/webapp/app/shared/language/_language.service.ts index 2257a5e031e7..77ea557cfe61 100644 --- a/generators/client-2/templates/src/main/webapp/app/shared/language/_language.service.ts +++ b/generators/client-2/templates/src/main/webapp/app/shared/language/_language.service.ts @@ -1,30 +1,42 @@ import { Injectable, Inject } from '@angular/core'; - import { TranslateService } from 'ng2-translate/ng2-translate'; import { LANGUAGES } from './language.constants'; +import { TranslatePartialLoader } from './translate-partial-loader.provider'; + @Injectable() export class <%=jhiPrefixCapitalized%>LanguageService { - // TODO: Replace this with nativeLanguage - currentLang = 'en'; - currentLocation = 'home'; + defaultLang = '<%= nativeLanguage %>'; + defaultLocation = 'global'; + currentLang = '<%= nativeLanguage %>'; + locations: string[] = ['home']; constructor (public translateService: TranslateService){ - this.translateService = translateService; + translateService.setDefaultLang(this.defaultLang); this.translateService.currentLang = this.currentLang; } - changeLanguage(languageKey: string) { - this.translateService.use(languageKey+'/'+ this.currentLocation); - } + changeLanguage(languageKey: string) { + this.currentLang = languageKey; + this.reload(); + } - setLocation(locationKey: string){ - this.currentLocation = locationKey; - this.translateService.use(this.currentLang+'/'+this.currentLocation); - } + setLocations(locations: string[]) { + this.locations = locations; + this.locations.push(this.defaultLocation); + this.reload(); + } + + reload() { + this.translateService.setDefaultLang(this.currentLang); + let translatePartialLoader: TranslatePartialLoader = this.translateService.currentLoader; + translatePartialLoader.setLocations(this.locations); + + this.translateService.use(this.currentLang); + } getAll(): Promise { return Promise.resolve(LANGUAGES); diff --git a/generators/client-2/templates/src/main/webapp/app/shared/language/jhi-translate.directive.ts b/generators/client-2/templates/src/main/webapp/app/shared/language/jhi-translate.directive.ts new file mode 100644 index 000000000000..71f994deaf55 --- /dev/null +++ b/generators/client-2/templates/src/main/webapp/app/shared/language/jhi-translate.directive.ts @@ -0,0 +1,13 @@ +import { Component } from '@angular/core'; + +@Component({ + selector: '[jhi-translate],[jhiTranslate]', + template: '', + inputs: ['key:jhi-translate', 'args:translate-values'] +}) +export class <%=jhiPrefixCapitalized%>Translate { + + private key: string; + private args: any; + //FIXME add support to pass translate-compile/ directives in translated content doesnt work +} diff --git a/generators/client-2/templates/src/main/webapp/app/shared/language/translate-partial-loader.provider.ts b/generators/client-2/templates/src/main/webapp/app/shared/language/translate-partial-loader.provider.ts new file mode 100644 index 000000000000..c745384386ce --- /dev/null +++ b/generators/client-2/templates/src/main/webapp/app/shared/language/translate-partial-loader.provider.ts @@ -0,0 +1,52 @@ +import { TranslateLoader } from 'ng2-translate/ng2-translate'; +import { Http, Response } from "@angular/http"; +import { Observable } from "rxjs/Observable"; + +export class TranslatePartialLoader implements TranslateLoader { + private locations: string[]; + + constructor(private http: Http, private prefix: string = 'i18n', private suffix: string = ".json") { + } + + public setLocations(locations: string[]) { + this.locations = locations; + } + + public getTranslation(lang: string): Observable { + var combinedObject = new Object(); + var oldObsevers; + var newObserver; + this.locations.forEach((value) => { + newObserver = this.getPartFile(value, combinedObject, lang); + if (oldObsevers == null) { + oldObsevers = newObserver; + } + else { + oldObsevers = oldObsevers.merge(newObserver); + } + }); + return oldObsevers; + } + + private getPartFile(part, combinedObject, lang: string) { + return Observable.create(observer => { + this.http.get(`${this.prefix}/${lang}/${part}${this.suffix}`).subscribe((res) => { + let responseObj = res.json(); + Object.keys(responseObj).forEach(key=>{ + combinedObject[key] = responseObj[key]; + }); + observer.next(combinedObject); + //call complete to close this stream (like a promise) + observer.complete(); + }); + }); + } +} + +export function createTranslatePartialLoader() { + return { + provide: TranslateLoader, + useFactory: (http: Http) => new TranslatePartialLoader(http, 'i18n', '.json'), + deps: [Http] + } +} From e6412f25d5f15c3471c11b873768b849c55aa74b Mon Sep 17 00:00:00 2001 From: Deepu k Sasidharan Date: Tue, 11 Oct 2016 21:27:04 +0530 Subject: [PATCH 8/8] feat: i18n polish --- .../src/main/webapp/app/shared/_shared-common.ng2module.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/generators/client-2/templates/src/main/webapp/app/shared/_shared-common.ng2module.ts b/generators/client-2/templates/src/main/webapp/app/shared/_shared-common.ng2module.ts index 6d98d92faf6b..ca0df2c1589c 100644 --- a/generators/client-2/templates/src/main/webapp/app/shared/_shared-common.ng2module.ts +++ b/generators/client-2/templates/src/main/webapp/app/shared/_shared-common.ng2module.ts @@ -1,4 +1,5 @@ import { NgModule } from '@angular/core'; +import { MissingTranslationHandler } from 'ng2-translate/ng2-translate'; import { <%=angular2AppName%>SharedLibsModule, @@ -48,7 +49,7 @@ import { providers: [ <%_ if (enableTranslation){ _%> <%=jhiPrefixCapitalized%>LanguageService, - { provide: MissingTranslationHandler, useClass: <%=jhiPrefixCapitalized%>MissingTranslationHandler } + { provide: MissingTranslationHandler, useClass: <%=jhiPrefixCapitalized%>MissingTranslationHandler }, <%_ } _%> alertServiceProvider() ],