This repository has been archived on 2024-01-30. You can view files and clone it, but cannot push or open issues or pull requests.
trev/nodejs/public/javascripts/angularApp.js

269 lines
No EOL
6.4 KiB
JavaScript

var app = angular.module('autome', ['ui.router']);
app.config(['$stateProvider', '$urlRouterProvider',function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('home', {
url: '/home',
templateUrl: '/home.html',
controller: 'MainCtrl',
onEnter: ['$state', 'auth', function ($state, auth) {
if (!auth.isLoggedIn()) {
$state.go('login');
}
}],
resolve: {
postPromise: ['arduino', function(arduino) {
return arduino.getRCs();
}]
}
})
.state('login', {
url: '/login',
templateUrl: '/login.html',
controller: 'AuthCtrl',
onEnter: ['$state', 'auth', function ($state, auth) {
if (auth.isLoggedIn()) {
$state.go('home');
}
}]
})
.state('register', {
url: '/register',
templateUrl: '/register.html',
controller: 'AuthCtrl',
onEnter: ['$state', 'auth', function ($state, auth) {
if (!auth.isLoggedIn()) {
$state.go('home');
}
}]
});
$urlRouterProvider.otherwise('home');
}]);
app.factory('auth', ['$http', '$window', function ($http, $window) {
var auth = {};
auth.saveToken = function (token) {
$window.localStorage['autohome-token'] = token;
};
auth.getToken = function () {
return $window.localStorage['autohome-token'];
};
auth.isLoggedIn = function () {
var token = auth.getToken();
if (token) {
var payload = JSON.parse($window.atob(token.split('.')[1]));
return payload.exp > Date.now() / 1000;
}
return false;
};
auth.currentUser = function () {
if (auth.isLoggedIn()) {
var token = auth.getToken();
var payload = JSON.parse($window.atob(token.split('.')[1]));
return payload.username;
}
};
auth.register = function (user) {
return $http.post('/register', user, {
headers: {Authorization: 'Bearer '+auth.getToken()}
}).success(function (data) {
auth.saveToken(data.token);
});
};
auth.logIn = function (user) {
return $http.post('/login', user).success(function (data) {
auth.saveToken(data.token);
});
};
auth.logOut = function () {
$window.localStorage.removeItem('autohome-token');
};
return auth;
}]);
app.factory('arduino', ['$http', 'auth', function($http, auth){
var o = {
RCs: []
};
o.emptyrc = {
name: '',
on_value: '',
off_value:'',
tristate: true
};
o.getRCs = function () {
return $http.get('/getRCs',{
headers: {Authorization: 'Bearer '+auth.getToken()}
}).success(function(data) {
angular.copy(data, o.RCs);
});
}
o.sendRC = function (RC) {
var code = (RC.state) ? RC.off_value : RC.on_value;
return $http.post('/sendRC', {code}, {
headers: {Authorization: 'Bearer '+auth.getToken()}
}).success(function (data) {
RC.state = !RC.state;
});
}
o.addRC = function (RC) {
return $http.post('/addRC', RC, {
headers: {Authorization: 'Bearer '+auth.getToken()}
}).success(function (data) {
console.log(data);
RC.switch_id = data.insertId;
o.RCs.push(RC);
});
}
o.deleteRC = function (RC) {
var id = RC.switch_id;
return $http.post('/deleteRC', {id}, {
headers: {Authorization: 'Bearer '+auth.getToken()}
}).success(function (data) {
o.RCs.splice(o.RCs.indexOf(o.RCs.filter(function (obj) {
return obj.switch_id == id;
})), 1);
});
}
o.changeRC = function (RC) {
return $http.post('/changeRC', RC, {
headers: {Authorization: 'Bearer '+auth.getToken()}
}).success(function (data) {
o.RCs[o.RCs.indexOf(o.RCs.filter(function (obj) {
return obj.switch_id == RC.switch_id;
}))] = RC;
});
}
o.irTest = function () {
return $http.get('/irTest',{
headers: {Authorization: 'Bearer '+auth.getToken()}
});
}
return o;
}]);
app.controller('MainCtrl', [
'$scope',
'arduino',
function ($scope, arduino){
$scope.RCs = arduino.RCs;
$scope.sendRC = arduino.sendRC;
$scope.irTest = arduino.irTest;
$scope.newrc = angular.copy(arduino.emptyrc);
$scope.modal = {
shown: false,
delete: false
};
$scope.toggleModal = function(RC) {
if(RC) {
$scope.newrc = RC;
$scope.newrc.tristate = RC.tristate > 0;
$scope.modal.delete = true;
} else {
$scope.modal.delete = false;
$scope.newrc = angular.copy(arduino.emptyrc);
}
$scope.modal.shown = !$scope.modal.shown;
};
$scope.addRC = function(){
arduino.addRC($scope.newrc);
$scope.modal.shown = !$scope.modal.shown;
$scope.newrc = angular.copy(arduino.emptyrc);
}
$scope.deleteRC = function() {
arduino.deleteRC($scope.newrc);
$scope.modal.shown = !$scope.modal.shown;
$scope.newrc = angular.copy(arduino.emptyrc);
}
$scope.changeRC = function() {
arduino.changeRC($scope.newrc);
$scope.modal.shown = !$scope.modal.shown;
$scope.newrc = angular.copy(arduino.emptyrc);
}
}]);
app.controller('AuthCtrl', [
'$scope',
'$state',
'auth',
function($scope, $state, auth){
$scope.user = {};
$scope.register = function(){
auth.register($scope.user).error(function(error){
$scope.error = error;
}).then(function(){
$state.go('home');
});
};
$scope.logIn = function(){
auth.logIn($scope.user).error(function(error){
$scope.error = error;
}).then(function(){
$state.go('home');
});
};
}]);
app.controller('NavCtrl', [
'$scope',
'auth',
function($scope, auth){
$scope.isLoggedIn = auth.isLoggedIn;
$scope.currentUser = auth.currentUser;
$scope.logOut = auth.logOut;
}]);
app.directive('modal', function() {
return {
restrict: 'E',
scope: {
show: '='
},
replace: true,
transclude: true,
link: function($scope, element, attrs) {
$scope.dialogStyle = {};
if (attrs.width)
$scope.dialogStyle.width = attrs.width;
if (attrs.height)
$scope.dialogStyle.height = attrs.height;
$scope.hideModal = function() {
$scope.show = false;
};
},
templateUrl: '/modal.html'
};
});