-
Notifications
You must be signed in to change notification settings - Fork 114
/
taskDetailController.js
76 lines (65 loc) · 3.03 KB
/
taskDetailController.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
/*
Copyright © 2016 ServiceNow, Inc.
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*
* Task details controller. This controller responsible for load task and comments. To load task it finds the
* task from the existing root scope object and load comments directly from the back end.
*/
myTasksControllers.controller('taskDetailCtrl', ['$scope', '$rootScope',
'$routeParams', '$http', '$location', '$window', '$route', '$routeParams', 'LoginSvc',
function($scope, $rootScope, $routeParams, $http, $location, $window, $route, $routeParams, LoginSvc) {
$scope.loginSvc = LoginSvc;
$scope.infoMessage = '';
$scope.errorMessage = '';
angular.element(document).ready(function() {
// Not in session storage, seems like user has logged out.
// Redirect to login page.
if (!$window.sessionStorage.getItem('tasks')) {
$location.url("/login");
$scope.$apply();
} else {
//case: page reload, recover from session storage.
if (!$rootScope.tasks)
$rootScope.tasks = JSON.parse($window.sessionStorage.getItem('tasks'));
//collect task type and index to identify the specific task.
if ($routeParams.type && $routeParams.id) {
$scope.task = $rootScope.tasks[$routeParams.type][$routeParams.id];
}
if ($scope.task) {
$scope.getComments();
}
}
});
$scope.addComment = function() {
var body = {};
body['comment'] = $scope.comment;
$scope.infoMessage = 'Adding comment ...';
$http.post('/task/' + $scope.task.sys_id + '/comments', body).success(
function(data, status, headers, config) {
$scope.infoMessage = '';
$scope.comment = '';
// Reload the comments to display new one.
$scope.getComments();
}).error(function(data, status, headers, config) {
$scope.infoMessage = '';
$scope.errorMessage = status + " : " + data;
});
}
$scope.getComments = function() {
$scope.infoMessage = 'Loading comments ...';
$http.get('/task/' + $scope.task.sys_id + '/comments', {}).success(
function(data, status, headers, config) {
$scope.status = status;
$scope.infoMessage = '';
$scope.comments = data.result;
}).error(function(data, status, headers, config) {
$scope.status = status;
$scope.infoMessage = '';
$scope.errorMessage = status + " : " + data;
});
}
}
]);