Merge pull request #3334 from totten/master-casexmlrepo
[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;
5 } ;
6
bbb29627 7 var crmCaseType = angular.module('crmCaseType', ['ngRoute', 'ui.utils']);
4c58e251
TO
8
9 crmCaseType.config(['$routeProvider',
10 function($routeProvider) {
11 $routeProvider.when('/caseType/:id', {
64790edd 12 templateUrl: partialUrl('edit.html'),
4c58e251
TO
13 controller: 'CaseTypeCtrl'
14 });
15 }
16 ]);
17
95fd24c0
TO
18 // Add a new record by name.
19 // Ex: <crmAddName crm-options="['Alpha','Beta','Gamma']" crm-var="newItem" crm-on-add="callMyCreateFunction(newItem)" />
20 crmCaseType.directive('crmAddName', function(){
21 return {
22 restrict: 'AE',
23 scope: {
24 crmOptions: '=',
25 crmVar: '=',
26 crmOnAdd: '&'
27 },
28 templateUrl: partialUrl('addName.html')
29 };
30 });
31
4c58e251 32 crmCaseType.controller('CaseTypeCtrl', function($scope) {
64790edd 33 $scope.partialUrl = partialUrl;
76e4acb8 34
dc7a657e
TO
35 $scope.activityStatuses = CRM.crmCaseType.actStatuses;
36 $scope.activityTypes = CRM.crmCaseType.actTypes;
d7c25f6c 37 $scope.activityTypeNames = _.pluck(CRM.crmCaseType.actTypes, 'name');
dc7a657e 38
76e4acb8
TO
39 $scope.workflows = {
40 'timeline': 'Timeline',
41 'pipeline': 'Sequence'
42 };
43
4c58e251
TO
44 $scope.caseType = {
45 id: 123,
46 label: 'Adult Day Care Referral',
47 description: 'Superkalafragalisticexpialitotious',
48 is_active: '1', // Angular won't bind checkbox correctly with numeric 1
49 definition: { // This is the serialized field
50 name: 'Adult Day Care Referral',
51 activityTypes: [
52 {name: 'Open Case', max_instances: 1 },
53 {name: 'Medical evaluation'}
54 ],
55 activitySets: [
56 {
57 name: 'standard_timeline',
58 label: 'Standard Timeline',
59 timeline: '1', // Angular won't bind checkbox correctly with numeric 1
60 activityTypes: [
61 {name: 'Open Case', status: 'Completed' },
62 {name: 'Medical evaluation', reference_activity: 'Open Case', reference_offset: 3, reference_select: 'newest'}
63 ]
64 },
65 {
66 name: 'my_sequence',
76e4acb8 67 label: 'My Sequence',
4c58e251
TO
68 pipeline: '1', // Angular won't bind checkbox correctly with numeric 1
69 activityTypes: [
70 {name: 'Medical evaluation'},
71 {name: 'Meeting'},
72 {name: 'Phone Call'}
73 ]
74 }
75
76 ],
77 caseRoles: [
78 { name: 'Senior Services Coordinator', creator: '1', manager: '1' },
79 { name: 'Health Services Coordinator' },
80 { name: 'Benefits Specialist' }
81 ]
82 }
83 };
dc7a657e 84 window.ct = $scope.caseType;
4c58e251 85
76e4acb8
TO
86 $scope.addActivitySet = function(workflow) {
87 var activitySet = {};
88 activitySet[workflow] = '1';
89 activitySet.activityTypes = [];
90
91 var offset = 1;
92 var names = _.pluck($scope.caseType.definition.activitySets, 'name');
93 while (_.contains(names, workflow + '_' + offset)) offset++;
94 activitySet.name = workflow + '_' + offset;
95 activitySet.label = (offset == 1 ) ? $scope.workflows[workflow] : ($scope.workflows[workflow] + ' #' + offset);
96
97 $scope.caseType.definition.activitySets.push(activitySet);
98 _.defer(function() {
99 $('.crmCaseType-acttab').tabs('refresh').tabs({active: -1});
100 });
101 };
102
d7c25f6c
TO
103 /// Add a new activity entry to an activity-set
104 $scope.addActivity = function(activitySet, activityType) {
105 activitySet.activityTypes.push({
106 name: activityType
107 });
108 };
109
110 /// Add a new top-level activity-type entry
111 $scope.addActivityType = function(activityType) {
112 var names = _.pluck($scope.caseType.definition.activityTypes, 'name');
113 if (!_.contains(names, activityType)) {
114 $scope.caseType.definition.activityTypes.push({
115 name: activityType
116 });
117 }
118 };
119
4c58e251
TO
120 $scope.onManagerChange = function(managerRole) {
121 angular.forEach($scope.caseType.definition.caseRoles, function(caseRole) {
122 if (caseRole != managerRole) {
123 caseRole.manager = '0';
124 }
125 });
126 };
127
128 $scope.removeItem = function(array, item) {
129 var idx = _.indexOf(array, item);
130 if (idx != -1) {
131 array.splice(idx, 1);
132 }
133 };
134
5d973e24
TO
135 $scope.isNewActivitySetAllowed = function(workflow) {
136 switch (workflow) {
137 case 'timeline':
138 return true;
139 case 'pipeline':
140 return 0 == _.where($scope.caseType.definition.activitySets, {pipeline: '1'}).length;
141 default:
142 if (console && console.log) console.log('Denied access to unrecognized workflow: (' + workflow + ')');
143 return false;
144 }
145 };
146
4c58e251 147 $scope.getWorkflowName = function(activitySet) {
76e4acb8
TO
148 var result = 'Unknown';
149 _.each($scope.workflows, function(value, key) {
150 if (activitySet[key]) result = value;
151 });
152 return result;
4c58e251
TO
153 };
154
155 /**
156 * Determine which HTML partial to use for a particular
157 *
158 * @return string URL of the HTML partial
159 */
160 $scope.activityTableTemplate = function(activitySet) {
161 if (activitySet.timeline) {
64790edd 162 return partialUrl('timelineTable.html');
4c58e251 163 } else if (activitySet.pipeline) {
64790edd 164 return partialUrl('pipelineTable.html');
4c58e251
TO
165 } else {
166 return '';
167 }
168 };
169
170 $scope.dump = function() {
171 console.log($scope.caseType);
76e4acb8
TO
172 };
173
174 $scope.$watchCollection('caseType.definition.activitySets', function() {
175 _.defer(function() {
176 $('.crmCaseType-acttab').tabs('refresh');
177 });
178 });
4c58e251
TO
179 });
180
181})(angular, CRM.$, CRM._);