Commit | Line | Data |
---|---|---|
4c58e251 TO |
1 | (function(angular, $, _) { |
2 | ||
0b199194 | 3 | var crmCaseType = angular.module('crmCaseType', CRM.angRequires('crmCaseType')); |
4c58e251 | 4 | |
506cd414 | 5 | // Note: This template will be passed to cloneDeep(), so don't put any funny stuff in here! |
9be5fc34 TO |
6 | var newCaseTypeTemplate = { |
7 | title: "", | |
8 | name: "", | |
9 | is_active: "1", | |
10 | weight: "1", | |
11 | definition: { | |
12 | activityTypes: [ | |
4d8bbcf6 TO |
13 | {name: 'Open Case', max_instances: 1}, |
14 | {name: 'Email'}, | |
15 | {name: 'Follow up'}, | |
16 | {name: 'Meeting'}, | |
17 | {name: 'Phone Call'} | |
9be5fc34 TO |
18 | ], |
19 | activitySets: [ | |
20 | { | |
21 | name: 'standard_timeline', | |
22 | label: 'Standard Timeline', | |
23 | timeline: '1', // Angular won't bind checkbox correctly with numeric 1 | |
24 | activityTypes: [ | |
25 | {name: 'Open Case', status: 'Completed' } | |
26 | ] | |
27 | } | |
28 | ], | |
29 | caseRoles: [ | |
30 | { name: 'Case Coordinator', creator: '1', manager: '1'} | |
31 | ] | |
32 | } | |
4d74de55 TO |
33 | }; |
34 | ||
4c58e251 TO |
35 | crmCaseType.config(['$routeProvider', |
36 | function($routeProvider) { | |
b75c2546 | 37 | $routeProvider.when('/caseType', { |
ef5d18a1 | 38 | templateUrl: '~/crmCaseType/list.html', |
b75c2546 TO |
39 | controller: 'CaseTypeListCtrl', |
40 | resolve: { | |
41 | caseTypes: function($route, crmApi) { | |
a214ce43 | 42 | return crmApi('CaseType', 'get', {options: {limit: 0}}); |
b75c2546 TO |
43 | } |
44 | } | |
45 | }); | |
4c58e251 | 46 | $routeProvider.when('/caseType/:id', { |
ef5d18a1 | 47 | templateUrl: '~/crmCaseType/edit.html', |
4d74de55 TO |
48 | controller: 'CaseTypeCtrl', |
49 | resolve: { | |
9625aad1 TO |
50 | apiCalls: function($route, crmApi) { |
51 | var reqs = {}; | |
52 | reqs.actStatuses = ['OptionValue', 'get', { | |
7c2b40d1 CW |
53 | option_group_id: 'activity_status', |
54 | sequential: 1, | |
55 | options: {limit: 0} | |
56 | }]; | |
57 | reqs.caseStatuses = ['OptionValue', 'get', { | |
58 | option_group_id: 'case_status', | |
59 | sequential: 1, | |
60 | options: {limit: 0} | |
9625aad1 TO |
61 | }]; |
62 | reqs.actTypes = ['OptionValue', 'get', { | |
63 | option_group_id: 'activity_type', | |
4324b8d7 | 64 | sequential: 1, |
9625aad1 TO |
65 | options: { |
66 | sort: 'name', | |
67 | limit: 0 | |
68 | } | |
69 | }]; | |
70 | reqs.relTypes = ['RelationshipType', 'get', { | |
4324b8d7 | 71 | sequential: 1, |
9625aad1 TO |
72 | options: { |
73 | sort: CRM.crmCaseType.REL_TYPE_CNAME, | |
74 | limit: 0 | |
75 | } | |
76 | }]; | |
77 | if ($route.current.params.id !== 'new') { | |
78 | reqs.caseType = ['CaseType', 'getsingle', { | |
79 | id: $route.current.params.id | |
80 | }]; | |
87dcd909 | 81 | } |
9625aad1 | 82 | return crmApi(reqs); |
4d74de55 TO |
83 | } |
84 | } | |
4c58e251 TO |
85 | }); |
86 | } | |
87 | ]); | |
88 | ||
95fd24c0 TO |
89 | // Add a new record by name. |
90 | // Ex: <crmAddName crm-options="['Alpha','Beta','Gamma']" crm-var="newItem" crm-on-add="callMyCreateFunction(newItem)" /> | |
8fc6fba7 | 91 | crmCaseType.directive('crmAddName', function() { |
95fd24c0 TO |
92 | return { |
93 | restrict: 'AE', | |
9597c394 | 94 | template: '<input class="add-activity crm-action-menu fa-plus" type="hidden" />', |
bafce1db | 95 | link: function(scope, element, attrs) { |
bafce1db TO |
96 | |
97 | var input = $('input', element); | |
98 | ||
99 | scope._resetSelection = function() { | |
100 | $(input).select2('close'); | |
101 | $(input).select2('val', ''); | |
102 | scope[attrs.crmVar] = ''; | |
103 | }; | |
104 | ||
4324b8d7 | 105 | $(input).crmSelect2({ |
c0bb8bd4 DB |
106 | data: function () { |
107 | return { results: scope[attrs.crmOptions] }; | |
108 | }, | |
bafce1db | 109 | createSearchChoice: function(term) { |
4324b8d7 | 110 | return {id: term, text: term + ' (' + ts('new') + ')'}; |
00eee619 | 111 | }, |
4324b8d7 | 112 | createSearchChoicePosition: 'bottom', |
00eee619 | 113 | placeholder: attrs.placeholder |
bafce1db TO |
114 | }); |
115 | $(input).on('select2-selecting', function(e) { | |
116 | scope[attrs.crmVar] = e.val; | |
117 | scope.$evalAsync(attrs.crmOnAdd); | |
118 | scope.$evalAsync('_resetSelection()'); | |
119 | e.preventDefault(); | |
120 | }); | |
121 | } | |
95fd24c0 TO |
122 | }; |
123 | }); | |
124 | ||
d7a470db DB |
125 | crmCaseType.directive('crmEditableTabTitle', function($timeout) { |
126 | return { | |
127 | restrict: 'AE', | |
128 | link: function(scope, element, attrs) { | |
129 | element.addClass('crm-editable crm-editable-enabled'); | |
130 | var titleLabel = $(element).find('span'); | |
131 | var penIcon = $('<i class="crm-i fa-pencil crm-editable-placeholder"></i>').prependTo(element); | |
132 | var saveButton = $('<button type="button"><i class="crm-i fa-check"></i></button>').appendTo(element); | |
133 | var cancelButton = $('<button type="cancel"><i class="crm-i fa-times"></i></button>').appendTo(element); | |
134 | $('button', element).wrapAll('<div class="crm-editable-form" style="display:none" />'); | |
135 | var buttons = $('.crm-editable-form', element); | |
136 | titleLabel.on('click', startEditMode); | |
137 | penIcon.on('click', startEditMode); | |
138 | ||
139 | function detectEscapeKeyPress (event) { | |
140 | var isEscape = false; | |
141 | ||
142 | if ("key" in event) { | |
143 | isEscape = (event.key == "Escape" || event.key == "Esc"); | |
144 | } else { | |
145 | isEscape = (event.keyCode == 27); | |
146 | } | |
147 | ||
148 | return isEscape; | |
149 | } | |
150 | ||
151 | function detectEnterKeyPress (event) { | |
152 | var isEnter = false; | |
153 | ||
154 | if ("key" in event) { | |
155 | isEnter = (event.key == "Enter"); | |
156 | } else { | |
157 | isEnter = (event.keyCode == 13); | |
158 | } | |
159 | ||
160 | return isEnter; | |
161 | } | |
162 | ||
163 | function startEditMode () { | |
164 | if (titleLabel.is(":focus")) { | |
165 | return; | |
166 | } | |
167 | ||
168 | penIcon.hide(); | |
169 | buttons.show(); | |
170 | ||
171 | saveButton.click(function () { | |
172 | updateTextValue(); | |
173 | stopEditMode(); | |
174 | }); | |
175 | ||
176 | cancelButton.click(function () { | |
177 | revertTextValue(); | |
178 | stopEditMode(); | |
179 | }); | |
180 | ||
181 | $(element).addClass('crm-editable-editing'); | |
182 | ||
183 | titleLabel | |
184 | .attr("contenteditable", "true") | |
185 | .focus() | |
186 | .focusout(function (event) { | |
187 | $timeout(function () { | |
188 | revertTextValue(); | |
189 | stopEditMode(); | |
190 | }, 500); | |
191 | }) | |
192 | .keydown(function(event) { | |
193 | event.stopImmediatePropagation(); | |
194 | ||
195 | if(detectEscapeKeyPress(event)) { | |
196 | revertTextValue(); | |
197 | stopEditMode(); | |
198 | } else if(detectEnterKeyPress(event)) { | |
199 | event.preventDefault(); | |
200 | updateTextValue(); | |
201 | stopEditMode(); | |
202 | } | |
203 | }); | |
204 | } | |
205 | ||
206 | function stopEditMode () { | |
207 | titleLabel.removeAttr("contenteditable").off("focusout"); | |
208 | titleLabel.off("keydown"); | |
209 | saveButton.off("click"); | |
210 | cancelButton.off("click"); | |
211 | $(element).removeClass('crm-editable-editing'); | |
212 | ||
213 | penIcon.show(); | |
214 | buttons.hide(); | |
215 | } | |
216 | ||
217 | function revertTextValue () { | |
218 | titleLabel.text(scope.activitySet.label); | |
219 | } | |
220 | ||
221 | function updateTextValue () { | |
222 | var updatedTitle = titleLabel.text(); | |
223 | ||
224 | scope.$evalAsync(function () { | |
225 | scope.activitySet.label = updatedTitle; | |
226 | }); | |
227 | } | |
228 | } | |
229 | }; | |
230 | }); | |
231 | ||
9625aad1 | 232 | crmCaseType.controller('CaseTypeCtrl', function($scope, crmApi, apiCalls) { |
4324b8d7 CW |
233 | // CRM_Case_XMLProcessor::REL_TYPE_CNAME |
234 | var REL_TYPE_CNAME = CRM.crmCaseType.REL_TYPE_CNAME, | |
235 | ||
236 | ts = $scope.ts = CRM.ts(null); | |
76e4acb8 | 237 | |
7c2b40d1 CW |
238 | $scope.activityStatuses = apiCalls.actStatuses.values; |
239 | $scope.caseStatuses = _.indexBy(apiCalls.caseStatuses.values, 'name'); | |
4324b8d7 CW |
240 | $scope.activityTypes = _.indexBy(apiCalls.actTypes.values, 'name'); |
241 | $scope.activityTypeOptions = _.map(apiCalls.actTypes.values, formatActivityTypeOption); | |
242 | $scope.relationshipTypeOptions = _.map(apiCalls.relTypes.values, function(type) { | |
243 | return {id: type[REL_TYPE_CNAME], text: type.label_b_a}; | |
244 | }); | |
1317b266 | 245 | $scope.locks = {caseTypeName: true, activitySetName: true}; |
dc7a657e | 246 | |
76e4acb8 TO |
247 | $scope.workflows = { |
248 | 'timeline': 'Timeline', | |
b387506c | 249 | 'sequence': 'Sequence' |
76e4acb8 TO |
250 | }; |
251 | ||
9625aad1 | 252 | $scope.caseType = apiCalls.caseType ? apiCalls.caseType : _.cloneDeep(newCaseTypeTemplate); |
87dcd909 | 253 | $scope.caseType.definition = $scope.caseType.definition || []; |
4d74de55 TO |
254 | $scope.caseType.definition.activityTypes = $scope.caseType.definition.activityTypes || []; |
255 | $scope.caseType.definition.activitySets = $scope.caseType.definition.activitySets || []; | |
be5aae33 AH |
256 | _.each($scope.caseType.definition.activitySets, function (set) { |
257 | _.each(set.activityTypes, function (type, name) { | |
258 | type.label = $scope.activityTypes[type.name].label; | |
259 | }); | |
260 | }); | |
4d74de55 | 261 | $scope.caseType.definition.caseRoles = $scope.caseType.definition.caseRoles || []; |
7c2b40d1 CW |
262 | $scope.caseType.definition.statuses = $scope.caseType.definition.statuses || []; |
263 | ||
264 | $scope.selectedStatuses = {}; | |
265 | _.each(apiCalls.caseStatuses.values, function (status) { | |
266 | $scope.selectedStatuses[status.name] = !$scope.caseType.definition.statuses.length || $scope.caseType.definition.statuses.indexOf(status.name) > -1; | |
267 | }); | |
4c58e251 | 268 | |
76e4acb8 TO |
269 | $scope.addActivitySet = function(workflow) { |
270 | var activitySet = {}; | |
271 | activitySet[workflow] = '1'; | |
272 | activitySet.activityTypes = []; | |
273 | ||
274 | var offset = 1; | |
275 | var names = _.pluck($scope.caseType.definition.activitySets, 'name'); | |
276 | while (_.contains(names, workflow + '_' + offset)) offset++; | |
277 | activitySet.name = workflow + '_' + offset; | |
278 | activitySet.label = (offset == 1 ) ? $scope.workflows[workflow] : ($scope.workflows[workflow] + ' #' + offset); | |
279 | ||
280 | $scope.caseType.definition.activitySets.push(activitySet); | |
281 | _.defer(function() { | |
282 | $('.crmCaseType-acttab').tabs('refresh').tabs({active: -1}); | |
283 | }); | |
284 | }; | |
285 | ||
4324b8d7 CW |
286 | function formatActivityTypeOption(type) { |
287 | return {id: type.name, text: type.label, icon: type.icon}; | |
288 | } | |
289 | ||
290 | function addActivityToSet(activitySet, activityTypeName) { | |
d7c25f6c | 291 | activitySet.activityTypes.push({ |
4324b8d7 | 292 | name: activityTypeName, |
be5aae33 | 293 | label: $scope.activityTypes[activityTypeName].label, |
183241d8 | 294 | status: 'Scheduled', |
295 | reference_activity: 'Open Case', | |
296 | reference_offset: '1', | |
297 | reference_select: 'newest' | |
d7c25f6c | 298 | }); |
4324b8d7 CW |
299 | } |
300 | ||
301 | function createActivity(name, callback) { | |
302 | CRM.loadForm(CRM.url('civicrm/admin/options/activity_type', {action: 'add', reset: 1, label: name, component_id: 7})) | |
303 | .on('crmFormSuccess', function(e, data) { | |
304 | $scope.activityTypes[data.optionValue.name] = data.optionValue; | |
305 | $scope.activityTypeOptions.push(formatActivityTypeOption(data.optionValue)); | |
306 | callback(data.optionValue); | |
307 | $scope.$digest(); | |
308 | }); | |
309 | } | |
310 | ||
311 | // Add a new activity entry to an activity-set | |
312 | $scope.addActivity = function(activitySet, activityType) { | |
313 | if ($scope.activityTypes[activityType]) { | |
314 | addActivityToSet(activitySet, activityType); | |
315 | } else { | |
316 | createActivity(activityType, function(newActivity) { | |
317 | addActivityToSet(activitySet, newActivity.name); | |
318 | }); | |
60dd172b | 319 | } |
d7c25f6c TO |
320 | }; |
321 | ||
322 | /// Add a new top-level activity-type entry | |
323 | $scope.addActivityType = function(activityType) { | |
324 | var names = _.pluck($scope.caseType.definition.activityTypes, 'name'); | |
325 | if (!_.contains(names, activityType)) { | |
4324b8d7 CW |
326 | // Add an activity type that exists |
327 | if ($scope.activityTypes[activityType]) { | |
328 | $scope.caseType.definition.activityTypes.push({name: activityType}); | |
329 | } else { | |
330 | createActivity(activityType, function(newActivity) { | |
331 | $scope.caseType.definition.activityTypes.push({name: newActivity.name}); | |
332 | }); | |
333 | } | |
d7c25f6c TO |
334 | } |
335 | }; | |
336 | ||
8c7e0ae8 TO |
337 | /// Add a new role |
338 | $scope.addRole = function(roles, roleName) { | |
bafce1db TO |
339 | var names = _.pluck($scope.caseType.definition.caseRoles, 'name'); |
340 | if (!_.contains(names, roleName)) { | |
4324b8d7 CW |
341 | if (_.where($scope.relationshipTypeOptions, {id: roleName}).length) { |
342 | roles.push({name: roleName}); | |
343 | } else { | |
344 | CRM.loadForm(CRM.url('civicrm/admin/reltype', {action: 'add', reset: 1, label_a_b: roleName, label_b_a: roleName})) | |
345 | .on('crmFormSuccess', function(e, data) { | |
346 | roles.push({name: data.relationshipType[REL_TYPE_CNAME]}); | |
347 | $scope.relationshipTypeOptions.push({id: data.relationshipType[REL_TYPE_CNAME], text: data.relationshipType.label_b_a}); | |
348 | $scope.$digest(); | |
349 | }); | |
350 | } | |
60dd172b | 351 | } |
8c7e0ae8 TO |
352 | }; |
353 | ||
4c58e251 TO |
354 | $scope.onManagerChange = function(managerRole) { |
355 | angular.forEach($scope.caseType.definition.caseRoles, function(caseRole) { | |
356 | if (caseRole != managerRole) { | |
357 | caseRole.manager = '0'; | |
358 | } | |
359 | }); | |
360 | }; | |
361 | ||
362 | $scope.removeItem = function(array, item) { | |
363 | var idx = _.indexOf(array, item); | |
364 | if (idx != -1) { | |
365 | array.splice(idx, 1); | |
366 | } | |
367 | }; | |
368 | ||
b40b4114 | 369 | $scope.isForkable = function() { |
f2bad133 | 370 | return !$scope.caseType.id || $scope.caseType.is_forkable; |
b40b4114 TO |
371 | }; |
372 | ||
7c2b40d1 CW |
373 | $scope.newStatus = function() { |
374 | CRM.loadForm(CRM.url('civicrm/admin/options/case_status', {action: 'add', reset: 1})) | |
375 | .on('crmFormSuccess', function(e, data) { | |
376 | $scope.caseStatuses[data.optionValue.name] = data.optionValue; | |
377 | $scope.selectedStatuses[data.optionValue.name] = true; | |
378 | $scope.$digest(); | |
379 | }); | |
380 | }; | |
381 | ||
5d973e24 TO |
382 | $scope.isNewActivitySetAllowed = function(workflow) { |
383 | switch (workflow) { | |
384 | case 'timeline': | |
385 | return true; | |
b387506c | 386 | case 'sequence': |
b04e5ffb | 387 | return 0 === _.where($scope.caseType.definition.activitySets, {sequence: '1'}).length; |
5d973e24 | 388 | default: |
bba9b4f0 | 389 | CRM.console('warn', 'Denied access to unrecognized workflow: (' + workflow + ')'); |
5d973e24 TO |
390 | return false; |
391 | } | |
392 | }; | |
393 | ||
259a7652 | 394 | $scope.isActivityRemovable = function(activitySet, activity) { |
12b84ade | 395 | return true; |
259a7652 TO |
396 | }; |
397 | ||
f42b448f TO |
398 | $scope.isValidName = function(name) { |
399 | return !name || name.match(/^[a-zA-Z0-9_]+$/); | |
400 | }; | |
401 | ||
4c58e251 | 402 | $scope.getWorkflowName = function(activitySet) { |
76e4acb8 TO |
403 | var result = 'Unknown'; |
404 | _.each($scope.workflows, function(value, key) { | |
405 | if (activitySet[key]) result = value; | |
406 | }); | |
407 | return result; | |
4c58e251 TO |
408 | }; |
409 | ||
410 | /** | |
411 | * Determine which HTML partial to use for a particular | |
412 | * | |
413 | * @return string URL of the HTML partial | |
414 | */ | |
415 | $scope.activityTableTemplate = function(activitySet) { | |
416 | if (activitySet.timeline) { | |
ef5d18a1 | 417 | return '~/crmCaseType/timelineTable.html'; |
b387506c | 418 | } else if (activitySet.sequence) { |
ef5d18a1 | 419 | return '~/crmCaseType/sequenceTable.html'; |
4c58e251 TO |
420 | } else { |
421 | return ''; | |
422 | } | |
423 | }; | |
424 | ||
425 | $scope.dump = function() { | |
426 | console.log($scope.caseType); | |
76e4acb8 TO |
427 | }; |
428 | ||
aa1a7c2e | 429 | $scope.save = function() { |
7c2b40d1 CW |
430 | // Add selected statuses |
431 | var selectedStatuses = []; | |
432 | _.each($scope.selectedStatuses, function(v, k) { | |
433 | if (v) selectedStatuses.push(k); | |
434 | }); | |
435 | // Ignore if ALL or NONE selected | |
436 | $scope.caseType.definition.statuses = selectedStatuses.length == _.size($scope.selectedStatuses) ? [] : selectedStatuses; | |
c7bccb5f | 437 | var result = crmApi('CaseType', 'create', $scope.caseType, true); |
3140a415 | 438 | result.then(function(data) { |
b04e5ffb | 439 | if (data.is_error === 0 || data.is_error == '0') { |
c7bccb5f | 440 | $scope.caseType.id = data.id; |
1ab5b88e | 441 | window.location.href = '#/caseType'; |
c7bccb5f | 442 | } |
443 | }); | |
aa1a7c2e TO |
444 | }; |
445 | ||
76e4acb8 TO |
446 | $scope.$watchCollection('caseType.definition.activitySets', function() { |
447 | _.defer(function() { | |
8fc6fba7 | 448 | $('.crmCaseType-acttab').tabs('refresh'); |
76e4acb8 TO |
449 | }); |
450 | }); | |
685acae4 | 451 | |
452 | var updateCaseTypeName = function () { | |
453 | if (!$scope.caseType.id && $scope.locks.caseTypeName) { | |
454 | // Should we do some filtering? Lowercase? Strip whitespace? | |
a5ca1f48 | 455 | var t = $scope.caseType.title ? $scope.caseType.title : ''; |
456 | $scope.caseType.name = t.replace(/ /g, '_').replace(/[^a-zA-Z0-9_]/g, '').toLowerCase(); | |
685acae4 | 457 | } |
458 | }; | |
459 | $scope.$watch('locks.caseTypeName', updateCaseTypeName); | |
460 | $scope.$watch('caseType.title', updateCaseTypeName); | |
b40b4114 TO |
461 | |
462 | if (!$scope.isForkable()) { | |
463 | CRM.alert(ts('The CiviCase XML file for this case-type prohibits editing the definition.')); | |
464 | } | |
4c58e251 TO |
465 | }); |
466 | ||
b75c2546 | 467 | crmCaseType.controller('CaseTypeListCtrl', function($scope, crmApi, caseTypes) { |
7abbf317 CW |
468 | var ts = $scope.ts = CRM.ts(null); |
469 | ||
b75c2546 | 470 | $scope.caseTypes = caseTypes.values; |
4b8c8b42 TO |
471 | $scope.toggleCaseType = function (caseType) { |
472 | caseType.is_active = (caseType.is_active == '1') ? '0' : '1'; | |
473 | crmApi('CaseType', 'create', caseType, true) | |
c99f1a0a TO |
474 | .catch(function (data) { |
475 | caseType.is_active = (caseType.is_active == '1') ? '0' : '1'; // revert | |
476 | $scope.$digest(); | |
4b8c8b42 TO |
477 | }); |
478 | }; | |
479 | $scope.deleteCaseType = function (caseType) { | |
eb8e4c2d TO |
480 | crmApi('CaseType', 'delete', {id: caseType.id}, { |
481 | error: function (data) { | |
7abbf317 | 482 | CRM.alert(data.error_message, ts('Error'), 'error'); |
eb8e4c2d TO |
483 | } |
484 | }) | |
4b8c8b42 | 485 | .then(function (data) { |
c99f1a0a | 486 | delete caseTypes.values[caseType.id]; |
4b8c8b42 TO |
487 | }); |
488 | }; | |
470a458e TO |
489 | $scope.revertCaseType = function (caseType) { |
490 | caseType.definition = 'null'; | |
491 | caseType.is_forked = '0'; | |
492 | crmApi('CaseType', 'create', caseType, true) | |
c99f1a0a TO |
493 | .catch(function (data) { |
494 | caseType.is_forked = '1'; // restore | |
495 | $scope.$digest(); | |
470a458e TO |
496 | }); |
497 | }; | |
b75c2546 TO |
498 | }); |
499 | ||
bba9b4f0 | 500 | })(angular, CRM.$, CRM._); |