thoughts/web/controllers.js
web/controllers.jsRaw
1
2var url="http://localhost:3000/api/";
3angular.module('thoughtsApp', [])
4 .controller('ThoughtsController', function(
5 $scope,
6 $http
7 ) {
8 var thoughtsList = this;
9 if(window.sessionStorage.getItem('thoughtsToken'))
10 {
11 $scope.userLogged=true;
12 }else{
13 $scope.userLogged=false;
14 }
15
16 $http({
17 method : "GET",
18 url : url + "thoughts"
19 }).then(function mySucces(response) {
20 thoughtsList.thoughts = response.data;
21 }, function myError(response) {
22 $scope.myWelcome = response.statusText;
23 });
24
25 thoughtsList.addTodo = function() {
26 todoList.todos.push({text:todoList.todoText, done:false});
27 todoList.todoText = '';
28 };
29
30 $scope.login = function(){
31 ActivateLoadBar();
32
33 var obj = {
34 username: $scope.username,
35 password: $scope.password
36 };
37 $http({
38 method : "POST",
39 url : url + "auth",
40 data: obj
41 }).then(function mySucces(response) {
42 if(response.data.success==true)
43 {
44 window.sessionStorage.setItem('thoughtsUsername', $scope.username);
45 window.sessionStorage.setItem('thoughtsToken', response.data.token);
46 window.sessionStorage.setItem('thoughtsUserAvatar', response.data.avatar);
47 toastr.success("Logged in");
48 setTimeout(function(){
49 window.location="index.html";
50 }, 1000);
51 }else{
52 toastr.error(response.data.message);
53 setTimeout(function(){
54 window.location="login.html";
55 }, 1000);
56 }
57 }, function myError(response) {
58 toastr.error(response.statusText);
59 });
60 };
61 $scope.logout = function(){
62 window.sessionStorage.removeItem('thoughtsUsername')
63 window.sessionStorage.removeItem('thoughtsToken');
64 window.sessionStorage.removeItem('thoughtsUserAvatar');
65 toastr.info("logging out");
66 setTimeout(function(){
67 window.location="index.html";
68 }, 1000);
69 }
70
71 $scope.postThought = function(){
72 ActivateLoadBar();
73
74 var obj = {
75 time: new Date(),
76 content: $scope.newthought,
77 username: window.sessionStorage.getItem('thoughtsUsername'),
78 avatar: window.sessionStorage.getItem('thoughtsUserAvatar'),
79 token: window.sessionStorage.getItem('thoughtsToken')
80 };
81 $http({
82 method : "POST",
83 url : url + "thoughts",
84 data: obj
85 }).then(function mySucces(response) {
86 $scope.myWelcome = response.data;
87 toastr.success("Thought published");
88 setTimeout(function(){
89 window.location="index.html";
90 }, 1000);
91 }, function myError(response) {
92 toastr.error(response.statusText);
93 });
94 };
95
96 });
97
98
99/* LOADBAR */
100 function ActivateLoadBar(){
101 var html="";
102 html+="<br>";
103 html+="<div id='loadbar' class='progress'>";
104 html+=" <div class='indeterminate'></div>";
105 html+="</div>";
106 document.body.innerHTML+=html;
107 }
108 function DesactivateLoadBar(){
109 document.getElementById('loadbar').innerHTML="";
110 }
111 /* </LOADBAR */