thoughts/web/controllers.js
web/controllers.jsRaw
1
2
3var url="https://thoughtsserver.herokuapp.com/api/";
4angular.module('thoughtsApp', [])
5 .controller('ThoughtsController', function(
6 $scope,
7 $http
8 ) {
9
10 var thoughtsList = this;
11
12 if(window.sessionStorage.getItem('thoughtsToken'))
13 {
14 $scope.userLogged=true;
15 }else{
16 $scope.userLogged=false;
17
18 }
19 $http({
20 method : "GET",
21 url : url + "thoughts"
22 }).then(function mySucces(response) {
23 thoughtsList.thoughts = response.data;
24 }, function myError(response) {
25 $scope.myWelcome = response.statusText;
26 });
27
28 $scope.availableAvatars=[
29 "img/icons/animals/cat.png",
30 "img/icons/animals/crab.png",
31 "img/icons/animals/toucan.png"
32 ];
33
34 $scope.getAllThoughts = function(){
35 $http({
36 method : "GET",
37 url : url + "thoughts"
38 }).then(function mySucces(response) {
39 thoughtsList.thoughts = response.data;
40 }, function myError(response) {
41 $scope.myWelcome = response.statusText;
42 });
43 };
44
45
46
47 thoughtsList.addTodo = function() {
48 todoList.todos.push({text:todoList.todoText, done:false});
49 todoList.todoText = '';
50 };
51
52 $scope.signin = function(){
53 ActivateLoadBar();
54
55 var obj = {
56 username: $scope.username,
57 password: $scope.password,
58 description: $scope.description,
59 mail: $scope.mail,
60 avatar: $scope.avatar
61 };
62 console.log(obj);
63 $http({
64 method : "POST",
65 url : url + "users",
66 data: obj
67 }).then(function mySucces(response) {
68 toastr.success("Signed in, now login");
69 setTimeout(function(){
70 window.location="login.html";
71 }, 1000);
72 }, function myError(response) {
73 toastr.error(response.statusText);
74 });
75 };
76
77 $scope.login = function(){
78 ActivateLoadBar();
79
80 var obj = {
81 username: $scope.username,
82 password: $scope.password
83 };
84 $http({
85 method : "POST",
86 url : url + "auth",
87 data: obj
88 }).then(function mySucces(response) {
89 if(response.data.success==true)
90 {
91 window.sessionStorage.setItem('thoughtsUsername', $scope.username);
92 window.sessionStorage.setItem('thoughtsToken', response.data.token);
93 window.sessionStorage.setItem('thoughtsUserAvatar', response.data.avatar);
94 toastr.success("Logged in");
95 setTimeout(function(){
96 window.location="index.html";
97 }, 1000);
98 }else{
99 toastr.error(response.data.message);
100 setTimeout(function(){
101 window.location="login.html";
102 }, 1000);
103 }
104 }, function myError(response) {
105 toastr.error(response.statusText);
106 });
107 };
108 $scope.logout = function(){
109 window.sessionStorage.removeItem('thoughtsUsername')
110 window.sessionStorage.removeItem('thoughtsToken');
111 window.sessionStorage.removeItem('thoughtsUserAvatar');
112 toastr.info("logging out");
113 setTimeout(function(){
114 window.location="index.html";
115 }, 1000);
116 }
117
118 $scope.postThought = function(){
119 ActivateLoadBar();
120
121 var obj = {
122 time: new Date(),
123 content: $scope.newthought,
124 username: window.sessionStorage.getItem('thoughtsUsername'),
125 avatar: window.sessionStorage.getItem('thoughtsUserAvatar'),
126 token: window.sessionStorage.getItem('thoughtsToken')
127 };
128 $http({
129 method : "POST",
130 url : url + "thoughts",
131 data: obj
132 }).then(function mySucces(response) {
133 $scope.myWelcome = response.data;
134 toastr.success("Thought published");
135 setTimeout(function(){
136 window.location="index.html";
137 }, 1000);
138 }, function myError(response) {
139 toastr.error(response.statusText);
140 });
141 };
142
143 });
144
145
146/* LOADBAR */
147 function ActivateLoadBar(){
148 var html="";
149 html+="<br>";
150 html+="<div id='loadbar' class='progress'>";
151 html+=" <div class='indeterminate'></div>";
152 html+="</div>";
153 document.body.innerHTML+=html;
154 }
155 function DesactivateLoadBar(){
156 document.getElementById('loadbar').innerHTML="";
157 }
158 /* </LOADBAR */