-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainController.js
137 lines (122 loc) · 5.65 KB
/
mainController.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
'use strict';
var cs142App = angular.module('cs142App', ['ngRoute', 'ngMaterial', 'ngResource']);
cs142App.config(['$routeProvider',
function ($routeProvider) {
$routeProvider.
when('/users', {
templateUrl: 'components/user-list/user-listTemplate.html',
controller: 'UserListController'
}).
when('/users/:userId', {
templateUrl: 'components/user-detail/user-detailTemplate.html',
controller: 'UserDetailController'
}).
when('/photos/:userId', {
templateUrl: 'components/user-photos/user-photosTemplate.html',
controller: 'UserPhotosController'
}).
when('/login-register', {
templateUrl: 'components/login-register/login-registerTemplate.html',
controller: 'LoginRegisterController'
}).
when('/activity-feed', {
templateUrl: 'components/activity-feed/activity-feedTemplate.html',
controller: 'ActivityFeedController'
}).
when('/delete-user', {
templateUrl: 'components/delete-user/delete-userTemplate.html',
controller: 'DeleteUserController'
}).
otherwise({
redirectTo: '/login-register'
});
}]);
cs142App.run(function($rootScope) {
$rootScope.context = "Login Page";
$rootScope.currentUser = {};
$rootScope.logMessage = "";
$rootScope.firstname = "";
$rootScope.lastname = "";
$rootScope.user_id = "";
$rootScope.loggedIn = false;
});
cs142App.controller('MainController', ['$scope', '$routeParams', '$resource', '$rootScope', '$location', '$http',
function ($scope, $routeParams, $resource, $rootScope, $location, $http) {
$scope.main = {};
$scope.main.title = 'Users';
$scope.myName = "Lewin Cary";
$scope.curID = $routeParams.userId;
$scope.$on('loggedIn', function() {
//Do something here
//reload user list
});
function noOneIsLoggedIn() {
return $rootScope.loggedIn;
}
$rootScope.$on( "$routeChangeStart", function(event, next, current) {
if (!noOneIsLoggedIn()) {
// no logged user, redirect to /login-register unless already there
if (next.templateUrl !== "components/login-register/login-registerTemplate.html") {
$location.path("/login-register");
}
}
});
//--------------------------------------------------------//
//document.getElementById("uploadButton").onclick = function() {$scope.uploadPhoto()};
var selectedPhotoFile; // Holds the last file selected by the user
// Called on file selection - we simply save a reference to the file in selectedPhotoFile
$scope.inputFileNameChanged = function (element) {
selectedPhotoFile = element.files[0];
};
// Has the user selected a file?
$scope.inputFileNameSelected = function () {
return !!selectedPhotoFile;
};
// Upload the photo file selected by the user using a post request to the URL /photos/new
$scope.uploadPhoto = function () {
console.log("here");
if (!$scope.inputFileNameSelected()) {
console.error("uploadPhoto called will no selected file");
return;
}
console.log('fileSubmitted', selectedPhotoFile);
// Create a DOM form and add the file to it under the name uploadedphoto
var domForm = new FormData();
domForm.append('uploadedphoto', selectedPhotoFile);
// Using $http to POST the form
$http.post('/photos/new', domForm, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
}).success(function(newPhoto){
// The photo was successfully uploaded. XXX - Do whatever you want on success.
console.log("successfully uploaded");
//-------------------------------------------
var Activity = $resource('/activity-object');
var x = new Date();
var obj = {
type:"Photo Uploaded",
first_name:$rootScope.currentUser.first_name,
last_name:$rootScope.currentUser.last_name,
date_time:x.toString(),
file_name:selectedPhotoFile.name
};
console.log("about to send: ", obj)
Activity.save( {activityObj: obj}, function(result) {
console.log(result);
}, function errorHandling(err) {
console.log("Error creating activity-object.");
});
//-------------------------------------------
}).error(function(err){
// Couldn't upload the photo. XXX - Do whatever you want on failure.
console.error('ERROR uploading photo', err);
});
};
//--------------------------------------------------------//
$scope.deleteFunction = function() {
$location.path('/delete-user');
}
$scope.activityFunction = function() {
$location.path('/activity-feed');
}
}]);