Merge pull request #3271 from jitendrapurohit/CoreWebtestFix4.5alpha1
[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 {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
30 crmCaseType.config(['$routeProvider',
31 function($routeProvider) {
32 $routeProvider.when('/caseType/:id', {
33 templateUrl: partialUrl('edit.html'),
34 controller: 'CaseTypeCtrl',
35 resolve: {
36 selectedCaseType: function($route, crmApi) {
37 return crmApi('CaseType', 'getsingle', {id: $route.current.params.id});
38 }
39 }
40 });
41 }
42 ]);
43
44 // Add a new record by name.
45 // Ex: <crmAddName crm-options="['Alpha','Beta','Gamma']" crm-var="newItem" crm-on-add="callMyCreateFunction(newItem)" />
46 crmCaseType.directive('crmAddName', function() {
47 return {
48 restrict: 'AE',
49 scope: {
50 crmOptions: '=',
51 crmVar: '=',
52 crmOnAdd: '&'
53 },
54 templateUrl: partialUrl('addName.html')
55 };
56 });
57
58 crmCaseType.controller('CaseTypeCtrl', function($scope, crmApi, selectedCaseType) {
59 $scope.partialUrl = partialUrl;
60
61 $scope.activityStatuses = CRM.crmCaseType.actStatuses;
62 $scope.activityTypes = CRM.crmCaseType.actTypes;
63 $scope.activityTypeNames = _.pluck(CRM.crmCaseType.actTypes, 'name');
64
65 $scope.workflows = {
66 'timeline': 'Timeline',
67 'pipeline': 'Sequence'
68 };
69
70 $scope.caseType = selectedCaseType;
71 $scope.caseType.definition = $scope.caseType.definition || _.extend({}, newCaseTypeDefinitionTemplate);
72 $scope.caseType.definition.activityTypes = $scope.caseType.definition.activityTypes || [];
73 $scope.caseType.definition.activitySets = $scope.caseType.definition.activitySets || [];
74 $scope.caseType.definition.caseRoles = $scope.caseType.definition.caseRoles || [];
75 window.ct = $scope.caseType;
76
77 $scope.addActivitySet = function(workflow) {
78 var activitySet = {};
79 activitySet[workflow] = '1';
80 activitySet.activityTypes = [];
81
82 var offset = 1;
83 var names = _.pluck($scope.caseType.definition.activitySets, 'name');
84 while (_.contains(names, workflow + '_' + offset)) offset++;
85 activitySet.name = workflow + '_' + offset;
86 activitySet.label = (offset == 1 ) ? $scope.workflows[workflow] : ($scope.workflows[workflow] + ' #' + offset);
87
88 $scope.caseType.definition.activitySets.push(activitySet);
89 _.defer(function() {
90 $('.crmCaseType-acttab').tabs('refresh').tabs({active: -1});
91 });
92 };
93
94 /// Add a new activity entry to an activity-set
95 $scope.addActivity = function(activitySet, activityType) {
96 activitySet.activityTypes.push({
97 name: activityType
98 });
99 };
100
101 /// Add a new top-level activity-type entry
102 $scope.addActivityType = function(activityType) {
103 var names = _.pluck($scope.caseType.definition.activityTypes, 'name');
104 if (!_.contains(names, activityType)) {
105 $scope.caseType.definition.activityTypes.push({
106 name: activityType
107 });
108 }
109 };
110
111 $scope.onManagerChange = function(managerRole) {
112 angular.forEach($scope.caseType.definition.caseRoles, function(caseRole) {
113 if (caseRole != managerRole) {
114 caseRole.manager = '0';
115 }
116 });
117 };
118
119 $scope.removeItem = function(array, item) {
120 var idx = _.indexOf(array, item);
121 if (idx != -1) {
122 array.splice(idx, 1);
123 }
124 };
125
126 $scope.isNewActivitySetAllowed = function(workflow) {
127 switch (workflow) {
128 case 'timeline':
129 return true;
130 case 'pipeline':
131 return 0 == _.where($scope.caseType.definition.activitySets, {pipeline: '1'}).length;
132 default:
133 if (console && console.log) console.log('Denied access to unrecognized workflow: (' + workflow + ')');
134 return false;
135 }
136 };
137
138 $scope.getWorkflowName = function(activitySet) {
139 var result = 'Unknown';
140 _.each($scope.workflows, function(value, key) {
141 if (activitySet[key]) result = value;
142 });
143 return result;
144 };
145
146 /**
147 * Determine which HTML partial to use for a particular
148 *
149 * @return string URL of the HTML partial
150 */
151 $scope.activityTableTemplate = function(activitySet) {
152 if (activitySet.timeline) {
153 return partialUrl('timelineTable.html');
154 } else if (activitySet.pipeline) {
155 return partialUrl('pipelineTable.html');
156 } else {
157 return '';
158 }
159 };
160
161 $scope.dump = function() {
162 console.log($scope.caseType);
163 };
164
165 $scope.save = function() {
166 crmApi('CaseType', 'create', $scope.caseType, true);
167 };
168
169 $scope.$watchCollection('caseType.definition.activitySets', function() {
170 _.defer(function() {
171 $('.crmCaseType-acttab').tabs('refresh');
172 });
173 });
174 });
175
176 })(angular, CRM.$, CRM._);