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