This repository was archived by the owner on Apr 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathallFollowupsService.js
More file actions
118 lines (94 loc) · 4.11 KB
/
allFollowupsService.js
File metadata and controls
118 lines (94 loc) · 4.11 KB
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
angular.module('codebrag.dashboard')
.factory('allFollowupsService', function($http, $rootScope, events) {
var followupsListLocal = new codebrag.followups.LocalFollowupsList();
var listFetched = false;
function allFollowups() {
return _httpRequest('GET').then(function(response) {
followupsListLocal.addAll(response.data.followupsByCommit);
listFetched = true;
return followupsListLocal.collection;
});
}
function removeAndGetNext(followupId, commitId) {
return _httpRequest('DELETE', followupId, {unique: true, requestId: 'removeFollowup_' + followupId}).then(function() {
triggerCounterDecrease();
var nextFollowup = followupsListLocal.removeOneAndGetNext(followupId, commitId);
return nextFollowup;
});
}
function loadFollowupDetails(followupId) {
return _httpRequest('GET', followupId).then(function(response) {
return response.data;
});
}
function hasFollowups() {
return followupsListLocal.hasFollowups();
}
function mightHaveFollowups() {
return !listFetched || followupsListLocal.hasFollowups()
}
function _httpRequest(method, id, config) {
var followupsUrl = 'rest/allfollowups/' + (id || '');
var reqConfig = angular.extend(config || {}, {method: method, url: followupsUrl});
return $http(reqConfig);
}
function triggerCounterDecrease() {
$rootScope.$broadcast(events.followupDone);
}
return {
allFollowups: allFollowups,
removeAndGetNext: removeAndGetNext,
loadFollowupDetails: loadFollowupDetails,
hasFollowups: hasFollowups,
mightHaveFollowups: mightHaveFollowups
};
});
var codebrag = codebrag || {};
codebrag.followups = codebrag.followups || {};
codebrag.followups.LocalFollowupsList = function(collection) {
var self = this;
this.collection = collection || [];
this.addAll = function(newCollection) {
this.collection.length = 0;
Array.prototype.push.apply(this.collection, newCollection);
};
function nextFollowup(commit, removeAtIndex) {
var followupToReturn = null;
var currentCommitIndex = self.collection.indexOf(commit);
if (commit.followups[removeAtIndex]) {
followupToReturn = commit.followups[removeAtIndex];
} else if (self.collection[currentCommitIndex + 1]) {
followupToReturn = self.collection[currentCommitIndex + 1].followups[0];
} else if (removeAtIndex > 0) {
followupToReturn = commit.followups[removeAtIndex - 1];
} else if (removeAtIndex === 0 && currentCommitIndex > 0) {
var previousCommitFollowupsLength = self.collection[currentCommitIndex - 1].followups.length;
followupToReturn = self.collection[currentCommitIndex - 1].followups[previousCommitFollowupsLength - 1];
}
if (!commit.followups.length) {
self.collection.splice(currentCommitIndex, 1);
}
return followupToReturn;
}
this.removeOneAndGetNext = function(followupId) {
var currentCommit = _.find(this.collection, function(group) {
return _.some(group.followups, function(followup) {
return followup.followupId === followupId;
});
});
var followupToRemove = _.find(currentCommit.followups, function(followup) {
return followup.followupId === followupId;
});
var indexToRemove = currentCommit.followups.indexOf(followupToRemove);
currentCommit.followups.splice(indexToRemove, 1);
return nextFollowup(currentCommit, indexToRemove);
};
this.hasFollowups = function() {
return this.collection.length > 0;
};
this.followupsCount = function() {
return _.reduce(this.collection, function(sum, followupsGroup) {
return sum + followupsGroup.followups.length;
}, 0);
};
};