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