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