Merge pull request #3386 from monishdeb/CRM-14107-fix
[civicrm-core.git] / js / angular-crmCaseType.js
CommitLineData
4c58e251
TO
1(function(angular, $, _) {
2
64790edd
TO
3 var partialUrl = function(relPath) {
4 return CRM.resourceUrls['civicrm'] + '/partials/crmCaseType/' + relPath;
8fc6fba7 5 };
64790edd 6
bbb29627 7 var crmCaseType = angular.module('crmCaseType', ['ngRoute', 'ui.utils']);
4c58e251 8
4d74de55
TO
9 var newCaseTypeDefinitionTemplate = {
10 activityTypes: [
11 {name: 'Open Case', max_instances: 1 },
12 {name: 'Example activity'}
13 ],
14 activitySets: [
15 {
16 name: 'standard_timeline',
17 label: 'Standard Timeline',
18 timeline: '1', // Angular won't bind checkbox correctly with numeric 1
19 activityTypes: [
20 {name: 'Open Case', status: 'Completed' },
21 {name: 'Example activity', reference_activity: 'Open Case', reference_offset: 3, reference_select: 'newest'}
22 ]
23 }
24 ],
25 caseRoles: [
26 { name: 'Case Coordinator', creator: '1', manager: '1'}
27 ]
28 };
29
4c58e251
TO
30 crmCaseType.config(['$routeProvider',
31 function($routeProvider) {
b75c2546
TO
32 $routeProvider.when('/caseType', {
33 templateUrl: partialUrl('list.html'),
34 controller: 'CaseTypeListCtrl',
35 resolve: {
36 caseTypes: function($route, crmApi) {
37 return crmApi('CaseType', 'get', {});
38 }
39 }
40 });
4c58e251 41 $routeProvider.when('/caseType/:id', {
64790edd 42 templateUrl: partialUrl('edit.html'),
4d74de55
TO
43 controller: 'CaseTypeCtrl',
44 resolve: {
45 selectedCaseType: function($route, crmApi) {
46 return crmApi('CaseType', 'getsingle', {id: $route.current.params.id});
47 }
48 }
4c58e251
TO
49 });
50 }
51 ]);
52
95fd24c0
TO
53 // Add a new record by name.
54 // Ex: <crmAddName crm-options="['Alpha','Beta','Gamma']" crm-var="newItem" crm-on-add="callMyCreateFunction(newItem)" />
8fc6fba7 55 crmCaseType.directive('crmAddName', function() {
95fd24c0
TO
56 return {
57 restrict: 'AE',
58 scope: {
59 crmOptions: '=',
60 crmVar: '=',
61 crmOnAdd: '&'
62 },
63 templateUrl: partialUrl('addName.html')
64 };
65 });
66
4d74de55 67 crmCaseType.controller('CaseTypeCtrl', function($scope, crmApi, selectedCaseType) {
64790edd 68 $scope.partialUrl = partialUrl;
76e4acb8 69
dc7a657e
TO
70 $scope.activityStatuses = CRM.crmCaseType.actStatuses;
71 $scope.activityTypes = CRM.crmCaseType.actTypes;
d7c25f6c 72 $scope.activityTypeNames = _.pluck(CRM.crmCaseType.actTypes, 'name');
8c7e0ae8 73 $scope.relationshipTypeNames = _.pluck(CRM.crmCaseType.relTypes, 'label_b_a'); // label_b_a is CRM_Case_XMLProcessor::REL_TYPE_CNAME
dc7a657e 74
76e4acb8
TO
75 $scope.workflows = {
76 'timeline': 'Timeline',
b387506c 77 'sequence': 'Sequence'
76e4acb8
TO
78 };
79
4d74de55
TO
80 $scope.caseType = selectedCaseType;
81 $scope.caseType.definition = $scope.caseType.definition || _.extend({}, newCaseTypeDefinitionTemplate);
82 $scope.caseType.definition.activityTypes = $scope.caseType.definition.activityTypes || [];
83 $scope.caseType.definition.activitySets = $scope.caseType.definition.activitySets || [];
84 $scope.caseType.definition.caseRoles = $scope.caseType.definition.caseRoles || [];
dc7a657e 85 window.ct = $scope.caseType;
4c58e251 86
76e4acb8
TO
87 $scope.addActivitySet = function(workflow) {
88 var activitySet = {};
89 activitySet[workflow] = '1';
90 activitySet.activityTypes = [];
91
92 var offset = 1;
93 var names = _.pluck($scope.caseType.definition.activitySets, 'name');
94 while (_.contains(names, workflow + '_' + offset)) offset++;
95 activitySet.name = workflow + '_' + offset;
96 activitySet.label = (offset == 1 ) ? $scope.workflows[workflow] : ($scope.workflows[workflow] + ' #' + offset);
97
98 $scope.caseType.definition.activitySets.push(activitySet);
99 _.defer(function() {
100 $('.crmCaseType-acttab').tabs('refresh').tabs({active: -1});
101 });
102 };
103
d7c25f6c
TO
104 /// Add a new activity entry to an activity-set
105 $scope.addActivity = function(activitySet, activityType) {
106 activitySet.activityTypes.push({
107 name: activityType
108 });
109 };
110
111 /// Add a new top-level activity-type entry
112 $scope.addActivityType = function(activityType) {
113 var names = _.pluck($scope.caseType.definition.activityTypes, 'name');
114 if (!_.contains(names, activityType)) {
115 $scope.caseType.definition.activityTypes.push({
116 name: activityType
117 });
118 }
119 };
120
8c7e0ae8
TO
121 /// Add a new role
122 $scope.addRole = function(roles, roleName) {
123 roles.push({
124 name: roleName
125 });
126 };
127
4c58e251
TO
128 $scope.onManagerChange = function(managerRole) {
129 angular.forEach($scope.caseType.definition.caseRoles, function(caseRole) {
130 if (caseRole != managerRole) {
131 caseRole.manager = '0';
132 }
133 });
134 };
135
136 $scope.removeItem = function(array, item) {
137 var idx = _.indexOf(array, item);
138 if (idx != -1) {
139 array.splice(idx, 1);
140 }
141 };
142
5d973e24
TO
143 $scope.isNewActivitySetAllowed = function(workflow) {
144 switch (workflow) {
145 case 'timeline':
146 return true;
b387506c
TO
147 case 'sequence':
148 return 0 == _.where($scope.caseType.definition.activitySets, {sequence: '1'}).length;
5d973e24
TO
149 default:
150 if (console && console.log) console.log('Denied access to unrecognized workflow: (' + workflow + ')');
151 return false;
152 }
153 };
154
4c58e251 155 $scope.getWorkflowName = function(activitySet) {
76e4acb8
TO
156 var result = 'Unknown';
157 _.each($scope.workflows, function(value, key) {
158 if (activitySet[key]) result = value;
159 });
160 return result;
4c58e251
TO
161 };
162
163 /**
164 * Determine which HTML partial to use for a particular
165 *
166 * @return string URL of the HTML partial
167 */
168 $scope.activityTableTemplate = function(activitySet) {
169 if (activitySet.timeline) {
64790edd 170 return partialUrl('timelineTable.html');
b387506c
TO
171 } else if (activitySet.sequence) {
172 return partialUrl('sequenceTable.html');
4c58e251
TO
173 } else {
174 return '';
175 }
176 };
177
178 $scope.dump = function() {
179 console.log($scope.caseType);
76e4acb8
TO
180 };
181
aa1a7c2e
TO
182 $scope.save = function() {
183 crmApi('CaseType', 'create', $scope.caseType, true);
184 };
185
76e4acb8
TO
186 $scope.$watchCollection('caseType.definition.activitySets', function() {
187 _.defer(function() {
8fc6fba7 188 $('.crmCaseType-acttab').tabs('refresh');
76e4acb8
TO
189 });
190 });
4c58e251
TO
191 });
192
b75c2546
TO
193 crmCaseType.controller('CaseTypeListCtrl', function($scope, crmApi, caseTypes) {
194 $scope.caseTypes = caseTypes.values;
195 });
196
4c58e251 197})(angular, CRM.$, CRM._);