slavailchenko35 6 jaren geleden
bovenliggende
commit
1fb92be2e9

+ 19 - 0
app/app.config.js

@@ -4,6 +4,25 @@ app.run(function($rootScope, $location) {
 	});
 
 });
+app.config(['$translateProvider', function($translateProvider) {
+	// var translations = {
+	// 	en: {
+	// 		'Authors': 'Authors'
+	// 	},
+	// 	ru: {
+	// 		'Authors': 'Автор'
+	// 	}
+	// }
+	// $translateProvider
+	// 	.translations('en', translations.en)
+	// 	.translations('ru', translations.ru)
+	// 	.preferredLanguage(localStorage.getItem('preferredLanguage') || 'en');
+	$translateProvider.useStaticFilesLoader({
+		prefix: 'i18n/',
+		suffix: '.json'
+	});
+	$translateProvider.preferredLanguage(localStorage.getItem('preferredLanguage') || 'en');
+}]);
 
 // check authorization
 app.config(['$httpProvider', function($httpProvider) {

+ 3 - 1
app/app.module.js

@@ -1,4 +1,6 @@
 var app = angular.module('fea5', [
 	'ngRoute',
-	'ui.bootstrap'
+	'ui.bootstrap',
+	'cgNotify',
+	'pascalprecht.translate'
 	]);

+ 12 - 2
app/controllers/authors.controller.js

@@ -1,11 +1,21 @@
 (function(){
-    app.controller('Authors', ['$scope', 'books.repository', '$routeParams', function ($scope, booksRepository, $routeParams){
+    app.controller('Authors', ['$scope', 'books.repository', '$routeParams','utils', function ($scope, booksRepository, $routeParams, utils){
         booksRepository.getAuthors()
         .then(function(respons){
             $scope.authors = respons.data
+        },function(error){
+            utils.notify({
+                message: error.statusText,
+                type: 'danger'
+            })
         })
         $scope.deleteAuthor = function(id){
             $scope.authors.splice($scope.authors.indexOf(id), 1)
-        }
+        };
+        // utils.notify({
+        //     message: 'her na lob',
+        //     type: 'danger'
+        // })
+        
     }])
 })();

+ 6 - 2
app/controllers/bookdetails.controller.js

@@ -2,7 +2,7 @@
 {
     'use strict';
 
-    app.controller('BookDetails', ['$scope', 'books.repository', '$routeParams', function ($scope, booksRepository, $routeParams)
+    app.controller('BookDetails', ['$scope', 'books.repository', '$routeParams','utils', function ($scope, booksRepository, $routeParams, utils)
             {
                 var details = {};
                 booksRepository.getBookById($routeParams.id)
@@ -11,7 +11,11 @@
                     $scope.details.date = new Date($scope.details.date)
                     console.log($scope.details)
                 }, function(error) {
-                    alert(error)}
+                    utils.notify({
+                        message: error.statusText,
+                        type: 'danger'
+                    })
+                }
                     )
 
                 $scope.isEditeMode = false;

+ 8 - 3
app/controllers/header.controller.js

@@ -2,13 +2,18 @@
 {
     'use strict';
 
-    app.controller('Header', function ($scope)
-    {
+    app.controller('Header', [ '$scope','$translate',function ($scope, $translate){
         $scope.isLogged = function() {
         	return localStorage.getItem('authToken') ? true : false;
         };
+        $scope.langSwitch = function(){
+            var lang = localStorage.getItem('preferredLanguage');
+            var curentleng = lang === 'en'? 'ru' : 'en';
+            $translate.use(curentleng);
+            localStorage.setItem('preferredLanguage', curentleng);
+        }
     }
-    );
+    ]);
 
 }
 )();

+ 7 - 3
app/controllers/login.controller.js

@@ -2,7 +2,7 @@
 {
     'use strict';
 
-    app.controller('Login', ['$scope', 'account.repository', '$location', function ($scope, accountRepository, $location)
+    app.controller('Login', ['$scope', 'account.repository', '$location','utils',function ($scope, accountRepository, $location, utils)
             {
                 $scope.user =
                 {
@@ -14,12 +14,16 @@
                 {
                     accountRepository.login($scope.user)
                     .then(function (response)  {
+                        console.log(response)
                         $location.path('/');
                         localStorage.setItem('authToken', response.data.authToken);
-                        //console.log(localStorage.authToken);
+                        utils.notify({
+                            message: 'hello, dude ' + response.data.login,
+                            type: 'succes'
+                        })
                     }, function (error)
                     {
-                        alert(error)
+                        
                     }
                     )
                 };

+ 18 - 0
app/services/utils.services.js

@@ -0,0 +1,18 @@
+(function(){
+    'use strict';
+    app.service('utils', ['notify', function(notify){
+        this.notify = function(data){
+            notify.closeAll();
+            var defaults = {
+                message: '',
+                type: 'alert',
+                duration: 3000
+            }
+            data = angular.extend(defaults, data);
+            notify({
+                message: data.message,
+                classes: 'alert alert-' + data.type
+            })
+        }
+    }]);
+})();

+ 4 - 1
app/views/header.template.html

@@ -7,12 +7,15 @@
             <ul class="nav navbar-nav">
                <li ng-class="{active: currentMenuItem === '/'}"><a href="#/">Home</a></li>
                <li ng-class="{active: currentMenuItem === '/books'}"><a href="#/books">Books</a></li>
-               <li ng-class="{active: currentMenuItem === '/authors'}"><a href="#/authors">Authors</a></li>
+               <li ng-class="{active: currentMenuItem === '/authors'}"><a href="#/authors">{{'Authors' | translate }}</a></li>
             </ul>
             <ul class="nav navbar-nav navbar-right">
                <li ng-if="!isLogged()" ng-class="{active: currentMenuItem == '/login'}">
                  <a href="#/login">Authorization</a>
                </li>
+               <li class="p-t-5">
+                   <button class="btn" ng-click="langSwitch()">Language</button>
+               </li>
             </ul>
         </div>
     </div>

+ 3 - 0
i18n/en.json

@@ -0,0 +1,3 @@
+{
+    "Authors": "Authors"
+}

+ 3 - 0
i18n/ru.json

@@ -0,0 +1,3 @@
+{
+    "Authors": "Автор"
+}

+ 7 - 2
index.html

@@ -2,7 +2,8 @@
 <html lang="en" ng-app="fea5" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
 <head>
   <meta charset="UTF-8" />
-  <title>Angular JS</title>
+	<title>Angular JS</title>
+	<link rel="stylesheet" href="node_modules/@cgross/angular-notify/dist/angular-notify.css"></link>
   <link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css" />
   <link rel="stylesheet" href="css/style.css">
 </head>
@@ -15,11 +16,15 @@
 
 	  <script src="node_modules/angular/angular.js"></script> 
 		<script src="node_modules/angular-route/angular-route.min.js"></script>
+		<script src="node_modules/@cgross/angular-notify/dist/angular-notify.js"></script>
 		<script src="node_modules/angular-ui-bootstrap/dist/ui-bootstrap-tpls.js"></script>
+		<script src="node_modules/angular-translate/dist/angular-translate.min.js"></script>
+		<script src="node_modules/angular-translate-loader-static-files/angular-translate-loader-static-files.min.js"></script>
 	  <script src="app/app.module.js"></script> 
 	  <script src="app/constants/webApi.com.js"></script>
 	  <script src="app/app.config.js"></script> 
-	  <script src="app/app.routes.js"></script> 
+		<script src="app/app.routes.js"></script> 
+		<script src="app/services/utils.services.js"></script> 
 	  <script src="app/controllers/main.controller.js"></script>
 	  <script src="app/controllers/bookslist.controller.js"></script>
 		<script src="app/controllers/header.controller.js"></script>

+ 16 - 0
package-lock.json

@@ -112,6 +112,22 @@
       "resolved": "https://registry.npmjs.org/angular-route/-/angular-route-1.7.1.tgz",
       "integrity": "sha512-UK4Ky6llpNMMMp4y+CJlg4rQFWz+mvt/0iGc6HHX6MbilkAp13vgwJ2kuAjb0qgRcn121FU9y4z8pt6fzjS/NQ=="
     },
+    "angular-translate": {
+      "version": "2.18.1",
+      "resolved": "https://registry.npmjs.org/angular-translate/-/angular-translate-2.18.1.tgz",
+      "integrity": "sha512-Mw0kFBqsv5j8ItL9IhRZunIlVmIRW6iFsiTmRs9wGr2QTt8z4rehYlWyHos8qnXc/kyOYJiW50iH50CSNHGB9A==",
+      "requires": {
+        "angular": "1.6.6"
+      }
+    },
+    "angular-translate-loader-static-files": {
+      "version": "2.18.1",
+      "resolved": "https://registry.npmjs.org/angular-translate-loader-static-files/-/angular-translate-loader-static-files-2.18.1.tgz",
+      "integrity": "sha512-5MuyzAROfc493kjLjKlLGLBzXiRmZIFbcWZGutDRxW5SRXSpwrH0u0hh0ENNnUyUQbe2vUspHNPIuZqlq8qIhw==",
+      "requires": {
+        "angular-translate": "2.18.1"
+      }
+    },
     "angular-ui-bootstrap": {
       "version": "2.5.6",
       "resolved": "https://registry.npmjs.org/angular-ui-bootstrap/-/angular-ui-bootstrap-2.5.6.tgz",

+ 2 - 0
package.json

@@ -19,6 +19,8 @@
     "angular": "^1.6.6",
     "angular-bootstrap": "^0.12.2",
     "angular-route": "^1.7.1",
+    "angular-translate": "^2.18.1",
+    "angular-translate-loader-static-files": "^2.18.1",
     "angular-ui-bootstrap": "^2.5.6",
     "bootstrap": "^3.3.7"
   }