Preserve page padding on Joomla upgrade screen
[civicrm-core.git] / ang / crmCaseType.js
CommitLineData
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 }];
ad8d1ce3
RO
70 reqs.defaultAssigneeTypes = ['OptionValue', 'get', {
71 option_group_id: 'activity_default_assignee',
72 sequential: 1,
73 options: {
74 limit: 0
75 }
76 }];
9625aad1 77 reqs.relTypes = ['RelationshipType', 'get', {
4324b8d7 78 sequential: 1,
1e921db0 79 is_active: 1,
9625aad1
TO
80 options: {
81 sort: CRM.crmCaseType.REL_TYPE_CNAME,
82 limit: 0
83 }
84 }];
85 if ($route.current.params.id !== 'new') {
86 reqs.caseType = ['CaseType', 'getsingle', {
87 id: $route.current.params.id
88 }];
87dcd909 89 }
9625aad1 90 return crmApi(reqs);
4d74de55
TO
91 }
92 }
4c58e251
TO
93 });
94 }
95 ]);
96
95fd24c0
TO
97 // Add a new record by name.
98 // Ex: <crmAddName crm-options="['Alpha','Beta','Gamma']" crm-var="newItem" crm-on-add="callMyCreateFunction(newItem)" />
8fc6fba7 99 crmCaseType.directive('crmAddName', function() {
95fd24c0
TO
100 return {
101 restrict: 'AE',
9597c394 102 template: '<input class="add-activity crm-action-menu fa-plus" type="hidden" />',
bafce1db 103 link: function(scope, element, attrs) {
bafce1db
TO
104
105 var input = $('input', element);
106
107 scope._resetSelection = function() {
108 $(input).select2('close');
109 $(input).select2('val', '');
110 scope[attrs.crmVar] = '';
111 };
112
4324b8d7 113 $(input).crmSelect2({
c0bb8bd4
DB
114 data: function () {
115 return { results: scope[attrs.crmOptions] };
116 },
bafce1db 117 createSearchChoice: function(term) {
4324b8d7 118 return {id: term, text: term + ' (' + ts('new') + ')'};
00eee619 119 },
4324b8d7 120 createSearchChoicePosition: 'bottom',
00eee619 121 placeholder: attrs.placeholder
bafce1db
TO
122 });
123 $(input).on('select2-selecting', function(e) {
124 scope[attrs.crmVar] = e.val;
125 scope.$evalAsync(attrs.crmOnAdd);
126 scope.$evalAsync('_resetSelection()');
127 e.preventDefault();
128 });
129 }
95fd24c0
TO
130 };
131 });
132
d7a470db
DB
133 crmCaseType.directive('crmEditableTabTitle', function($timeout) {
134 return {
135 restrict: 'AE',
136 link: function(scope, element, attrs) {
137 element.addClass('crm-editable crm-editable-enabled');
138 var titleLabel = $(element).find('span');
139 var penIcon = $('<i class="crm-i fa-pencil crm-editable-placeholder"></i>').prependTo(element);
140 var saveButton = $('<button type="button"><i class="crm-i fa-check"></i></button>').appendTo(element);
141 var cancelButton = $('<button type="cancel"><i class="crm-i fa-times"></i></button>').appendTo(element);
142 $('button', element).wrapAll('<div class="crm-editable-form" style="display:none" />');
143 var buttons = $('.crm-editable-form', element);
144 titleLabel.on('click', startEditMode);
145 penIcon.on('click', startEditMode);
146
147 function detectEscapeKeyPress (event) {
148 var isEscape = false;
149
150 if ("key" in event) {
151 isEscape = (event.key == "Escape" || event.key == "Esc");
152 } else {
153 isEscape = (event.keyCode == 27);
154 }
155
156 return isEscape;
157 }
158
159 function detectEnterKeyPress (event) {
160 var isEnter = false;
161
162 if ("key" in event) {
163 isEnter = (event.key == "Enter");
164 } else {
165 isEnter = (event.keyCode == 13);
166 }
167
168 return isEnter;
169 }
170
171 function startEditMode () {
172 if (titleLabel.is(":focus")) {
173 return;
174 }
175
176 penIcon.hide();
177 buttons.show();
178
179 saveButton.click(function () {
180 updateTextValue();
181 stopEditMode();
182 });
183
184 cancelButton.click(function () {
185 revertTextValue();
186 stopEditMode();
187 });
188
189 $(element).addClass('crm-editable-editing');
190
191 titleLabel
192 .attr("contenteditable", "true")
193 .focus()
194 .focusout(function (event) {
195 $timeout(function () {
196 revertTextValue();
197 stopEditMode();
198 }, 500);
199 })
200 .keydown(function(event) {
201 event.stopImmediatePropagation();
202
203 if(detectEscapeKeyPress(event)) {
204 revertTextValue();
205 stopEditMode();
206 } else if(detectEnterKeyPress(event)) {
207 event.preventDefault();
208 updateTextValue();
209 stopEditMode();
210 }
211 });
212 }
213
214 function stopEditMode () {
215 titleLabel.removeAttr("contenteditable").off("focusout");
216 titleLabel.off("keydown");
217 saveButton.off("click");
218 cancelButton.off("click");
219 $(element).removeClass('crm-editable-editing');
220
221 penIcon.show();
222 buttons.hide();
223 }
224
225 function revertTextValue () {
226 titleLabel.text(scope.activitySet.label);
227 }
228
229 function updateTextValue () {
230 var updatedTitle = titleLabel.text();
231
232 scope.$evalAsync(function () {
233 scope.activitySet.label = updatedTitle;
234 });
235 }
236 }
237 };
238 });
239
9625aad1 240 crmCaseType.controller('CaseTypeCtrl', function($scope, crmApi, apiCalls) {
ad8d1ce3
RO
241 var REL_TYPE_CNAME, defaultAssigneeDefaultValue, ts;
242
243 (function init () {
244 // CRM_Case_XMLProcessor::REL_TYPE_CNAME
245 REL_TYPE_CNAME = CRM.crmCaseType.REL_TYPE_CNAME;
246
247 ts = $scope.ts = CRM.ts(null);
248 $scope.locks = { caseTypeName: true, activitySetName: true };
249 $scope.workflows = { timeline: 'Timeline', sequence: 'Sequence' };
250 defaultAssigneeDefaultValue = _.find(apiCalls.defaultAssigneeTypes.values, { is_default: '1' }) || {};
251
252 storeApiCallsResults();
253 initCaseType();
254 initCaseTypeDefinition();
255 initSelectedStatuses();
256 })();
257
258 /// Stores the api calls results in the $scope object
259 function storeApiCallsResults() {
260 $scope.activityStatuses = apiCalls.actStatuses.values;
261 $scope.caseStatuses = _.indexBy(apiCalls.caseStatuses.values, 'name');
262 $scope.activityTypes = _.indexBy(apiCalls.actTypes.values, 'name');
263 $scope.activityTypeOptions = _.map(apiCalls.actTypes.values, formatActivityTypeOption);
264 $scope.defaultAssigneeTypes = apiCalls.defaultAssigneeTypes.values;
265 $scope.relationshipTypeOptions = _.map(apiCalls.relTypes.values, function(type) {
266 return {id: type[REL_TYPE_CNAME], text: type.label_b_a};
267 });
68098e7b 268 $scope.defaultRelationshipTypeOptions = getDefaultRelationshipTypeOptions();
ad8d1ce3
RO
269 // stores the default assignee values indexed by their option name:
270 $scope.defaultAssigneeTypeValues = _.chain($scope.defaultAssigneeTypes)
271 .indexBy('name').mapValues('value').value();
272 }
273
68098e7b
RO
274 /// Returns the default relationship type options. If the relationship is
275 /// bidirectional (Ex: Spouse of) it adds a single option otherwise it adds
276 /// two options representing the relationship type directions
277 /// (Ex: Employee of, Employer is)
278 function getDefaultRelationshipTypeOptions() {
279 return _.transform(apiCalls.relTypes.values, function(result, relType) {
280 var isBidirectionalRelationship = relType.label_a_b === relType.label_b_a;
281
282 result.push({
283 label: relType.label_b_a,
284 value: relType.id + '_b_a'
285 });
286
287 if (!isBidirectionalRelationship) {
288 result.push({
289 label: relType.label_a_b,
290 value: relType.id + '_a_b'
291 });
292 }
293 }, []);
294 }
295
ad8d1ce3
RO
296 /// initializes the case type object
297 function initCaseType() {
298 var isNewCaseType = !apiCalls.caseType;
299
300 if (isNewCaseType) {
301 $scope.caseType = _.cloneDeep(newCaseTypeTemplate);
302 } else {
303 $scope.caseType = apiCalls.caseType;
304 }
305 }
76e4acb8 306
ad8d1ce3
RO
307 /// initializes the case type definition object
308 function initCaseTypeDefinition() {
309 $scope.caseType.definition = $scope.caseType.definition || [];
310 $scope.caseType.definition.activityTypes = $scope.caseType.definition.activityTypes || [];
311 $scope.caseType.definition.activitySets = $scope.caseType.definition.activitySets || [];
312 $scope.caseType.definition.caseRoles = $scope.caseType.definition.caseRoles || [];
313 $scope.caseType.definition.statuses = $scope.caseType.definition.statuses || [];
314 $scope.caseType.definition.timelineActivityTypes = $scope.caseType.definition.timelineActivityTypes || [];
315
316 _.each($scope.caseType.definition.activitySets, function (set) {
317 _.each(set.activityTypes, function (type, name) {
318 var isDefaultAssigneeTypeUndefined = _.isUndefined(type.default_assignee_type);
1e921db0
RO
319 var typeDefinition = $scope.activityTypes[type.name];
320 type.label = (typeDefinition && typeDefinition.label) || type.name;
ad8d1ce3
RO
321
322 if (isDefaultAssigneeTypeUndefined) {
323 type.default_assignee_type = defaultAssigneeDefaultValue.value;
324 }
325 });
be5aae33 326 });
ad8d1ce3 327 }
7c2b40d1 328
ad8d1ce3
RO
329 /// initializes the selected statuses
330 function initSelectedStatuses() {
331 $scope.selectedStatuses = {};
093f1cfd 332
ad8d1ce3
RO
333 _.each(apiCalls.caseStatuses.values, function (status) {
334 $scope.selectedStatuses[status.name] = !$scope.caseType.definition.statuses.length || $scope.caseType.definition.statuses.indexOf(status.name) > -1;
335 });
336 }
4c58e251 337
76e4acb8
TO
338 $scope.addActivitySet = function(workflow) {
339 var activitySet = {};
340 activitySet[workflow] = '1';
341 activitySet.activityTypes = [];
342
343 var offset = 1;
344 var names = _.pluck($scope.caseType.definition.activitySets, 'name');
345 while (_.contains(names, workflow + '_' + offset)) offset++;
346 activitySet.name = workflow + '_' + offset;
347 activitySet.label = (offset == 1 ) ? $scope.workflows[workflow] : ($scope.workflows[workflow] + ' #' + offset);
348
349 $scope.caseType.definition.activitySets.push(activitySet);
350 _.defer(function() {
351 $('.crmCaseType-acttab').tabs('refresh').tabs({active: -1});
352 });
353 };
354
4324b8d7
CW
355 function formatActivityTypeOption(type) {
356 return {id: type.name, text: type.label, icon: type.icon};
357 }
358
359 function addActivityToSet(activitySet, activityTypeName) {
0f25eb9c
SA
360 activitySet.activityTypes = activitySet.activityTypes || [];
361 var activity = {
093f1cfd
AP
362 name: activityTypeName,
363 label: $scope.activityTypes[activityTypeName].label,
364 status: 'Scheduled',
365 reference_activity: 'Open Case',
366 reference_offset: '1',
ad8d1ce3
RO
367 reference_select: 'newest',
368 default_assignee_type: $scope.defaultAssigneeTypeValues.NONE
093f1cfd
AP
369 };
370 activitySet.activityTypes.push(activity);
371 if(typeof activitySet.timeline !== "undefined" && activitySet.timeline == "1") {
372 $scope.caseType.definition.timelineActivityTypes.push(activity);
373 }
374 }
375
376 function resetTimelineActivityTypes() {
377 $scope.caseType.definition.timelineActivityTypes = [];
378 angular.forEach($scope.caseType.definition.activitySets, function(activitySet) {
379 angular.forEach(activitySet.activityTypes, function(activityType) {
380 $scope.caseType.definition.timelineActivityTypes.push(activityType);
381 });
382 });
4324b8d7
CW
383 }
384
385 function createActivity(name, callback) {
386 CRM.loadForm(CRM.url('civicrm/admin/options/activity_type', {action: 'add', reset: 1, label: name, component_id: 7}))
387 .on('crmFormSuccess', function(e, data) {
388 $scope.activityTypes[data.optionValue.name] = data.optionValue;
389 $scope.activityTypeOptions.push(formatActivityTypeOption(data.optionValue));
390 callback(data.optionValue);
391 $scope.$digest();
392 });
393 }
394
395 // Add a new activity entry to an activity-set
396 $scope.addActivity = function(activitySet, activityType) {
397 if ($scope.activityTypes[activityType]) {
398 addActivityToSet(activitySet, activityType);
399 } else {
400 createActivity(activityType, function(newActivity) {
401 addActivityToSet(activitySet, newActivity.name);
402 });
60dd172b 403 }
d7c25f6c
TO
404 };
405
406 /// Add a new top-level activity-type entry
407 $scope.addActivityType = function(activityType) {
408 var names = _.pluck($scope.caseType.definition.activityTypes, 'name');
409 if (!_.contains(names, activityType)) {
4324b8d7
CW
410 // Add an activity type that exists
411 if ($scope.activityTypes[activityType]) {
412 $scope.caseType.definition.activityTypes.push({name: activityType});
413 } else {
414 createActivity(activityType, function(newActivity) {
415 $scope.caseType.definition.activityTypes.push({name: newActivity.name});
416 });
417 }
d7c25f6c
TO
418 }
419 };
420
ad8d1ce3
RO
421 /// Clears the activity's default assignee values for relationship and contact
422 $scope.clearActivityDefaultAssigneeValues = function(activity) {
423 activity.default_assignee_relationship = null;
424 activity.default_assignee_contact = null;
425 };
426
8c7e0ae8
TO
427 /// Add a new role
428 $scope.addRole = function(roles, roleName) {
bafce1db
TO
429 var names = _.pluck($scope.caseType.definition.caseRoles, 'name');
430 if (!_.contains(names, roleName)) {
4324b8d7
CW
431 if (_.where($scope.relationshipTypeOptions, {id: roleName}).length) {
432 roles.push({name: roleName});
433 } else {
434 CRM.loadForm(CRM.url('civicrm/admin/reltype', {action: 'add', reset: 1, label_a_b: roleName, label_b_a: roleName}))
435 .on('crmFormSuccess', function(e, data) {
436 roles.push({name: data.relationshipType[REL_TYPE_CNAME]});
437 $scope.relationshipTypeOptions.push({id: data.relationshipType[REL_TYPE_CNAME], text: data.relationshipType.label_b_a});
438 $scope.$digest();
439 });
440 }
60dd172b 441 }
8c7e0ae8
TO
442 };
443
4c58e251
TO
444 $scope.onManagerChange = function(managerRole) {
445 angular.forEach($scope.caseType.definition.caseRoles, function(caseRole) {
446 if (caseRole != managerRole) {
447 caseRole.manager = '0';
448 }
449 });
450 };
451
452 $scope.removeItem = function(array, item) {
453 var idx = _.indexOf(array, item);
454 if (idx != -1) {
455 array.splice(idx, 1);
093f1cfd 456 resetTimelineActivityTypes();
4c58e251
TO
457 }
458 };
459
b40b4114 460 $scope.isForkable = function() {
f2bad133 461 return !$scope.caseType.id || $scope.caseType.is_forkable;
b40b4114
TO
462 };
463
7c2b40d1
CW
464 $scope.newStatus = function() {
465 CRM.loadForm(CRM.url('civicrm/admin/options/case_status', {action: 'add', reset: 1}))
466 .on('crmFormSuccess', function(e, data) {
467 $scope.caseStatuses[data.optionValue.name] = data.optionValue;
468 $scope.selectedStatuses[data.optionValue.name] = true;
469 $scope.$digest();
470 });
471 };
472
5d973e24
TO
473 $scope.isNewActivitySetAllowed = function(workflow) {
474 switch (workflow) {
475 case 'timeline':
476 return true;
b387506c 477 case 'sequence':
b04e5ffb 478 return 0 === _.where($scope.caseType.definition.activitySets, {sequence: '1'}).length;
5d973e24 479 default:
bba9b4f0 480 CRM.console('warn', 'Denied access to unrecognized workflow: (' + workflow + ')');
5d973e24
TO
481 return false;
482 }
483 };
484
259a7652 485 $scope.isActivityRemovable = function(activitySet, activity) {
12b84ade 486 return true;
259a7652
TO
487 };
488
f42b448f
TO
489 $scope.isValidName = function(name) {
490 return !name || name.match(/^[a-zA-Z0-9_]+$/);
491 };
492
4c58e251 493 $scope.getWorkflowName = function(activitySet) {
76e4acb8
TO
494 var result = 'Unknown';
495 _.each($scope.workflows, function(value, key) {
496 if (activitySet[key]) result = value;
497 });
498 return result;
4c58e251
TO
499 };
500
501 /**
502 * Determine which HTML partial to use for a particular
503 *
504 * @return string URL of the HTML partial
505 */
506 $scope.activityTableTemplate = function(activitySet) {
507 if (activitySet.timeline) {
ef5d18a1 508 return '~/crmCaseType/timelineTable.html';
b387506c 509 } else if (activitySet.sequence) {
ef5d18a1 510 return '~/crmCaseType/sequenceTable.html';
4c58e251
TO
511 } else {
512 return '';
513 }
514 };
515
516 $scope.dump = function() {
517 console.log($scope.caseType);
76e4acb8
TO
518 };
519
aa1a7c2e 520 $scope.save = function() {
7c2b40d1
CW
521 // Add selected statuses
522 var selectedStatuses = [];
523 _.each($scope.selectedStatuses, function(v, k) {
524 if (v) selectedStatuses.push(k);
525 });
526 // Ignore if ALL or NONE selected
527 $scope.caseType.definition.statuses = selectedStatuses.length == _.size($scope.selectedStatuses) ? [] : selectedStatuses;
c7bccb5f 528 var result = crmApi('CaseType', 'create', $scope.caseType, true);
3140a415 529 result.then(function(data) {
b04e5ffb 530 if (data.is_error === 0 || data.is_error == '0') {
c7bccb5f 531 $scope.caseType.id = data.id;
1ab5b88e 532 window.location.href = '#/caseType';
c7bccb5f 533 }
534 });
aa1a7c2e
TO
535 };
536
76e4acb8
TO
537 $scope.$watchCollection('caseType.definition.activitySets', function() {
538 _.defer(function() {
8fc6fba7 539 $('.crmCaseType-acttab').tabs('refresh');
76e4acb8
TO
540 });
541 });
685acae4 542
543 var updateCaseTypeName = function () {
544 if (!$scope.caseType.id && $scope.locks.caseTypeName) {
545 // Should we do some filtering? Lowercase? Strip whitespace?
a5ca1f48 546 var t = $scope.caseType.title ? $scope.caseType.title : '';
547 $scope.caseType.name = t.replace(/ /g, '_').replace(/[^a-zA-Z0-9_]/g, '').toLowerCase();
685acae4 548 }
549 };
550 $scope.$watch('locks.caseTypeName', updateCaseTypeName);
551 $scope.$watch('caseType.title', updateCaseTypeName);
b40b4114
TO
552
553 if (!$scope.isForkable()) {
554 CRM.alert(ts('The CiviCase XML file for this case-type prohibits editing the definition.'));
555 }
093f1cfd 556
4c58e251
TO
557 });
558
b75c2546 559 crmCaseType.controller('CaseTypeListCtrl', function($scope, crmApi, caseTypes) {
7abbf317
CW
560 var ts = $scope.ts = CRM.ts(null);
561
b75c2546 562 $scope.caseTypes = caseTypes.values;
4b8c8b42
TO
563 $scope.toggleCaseType = function (caseType) {
564 caseType.is_active = (caseType.is_active == '1') ? '0' : '1';
565 crmApi('CaseType', 'create', caseType, true)
c99f1a0a
TO
566 .catch(function (data) {
567 caseType.is_active = (caseType.is_active == '1') ? '0' : '1'; // revert
568 $scope.$digest();
4b8c8b42
TO
569 });
570 };
571 $scope.deleteCaseType = function (caseType) {
eb8e4c2d
TO
572 crmApi('CaseType', 'delete', {id: caseType.id}, {
573 error: function (data) {
7abbf317 574 CRM.alert(data.error_message, ts('Error'), 'error');
eb8e4c2d
TO
575 }
576 })
4b8c8b42 577 .then(function (data) {
c99f1a0a 578 delete caseTypes.values[caseType.id];
4b8c8b42
TO
579 });
580 };
470a458e
TO
581 $scope.revertCaseType = function (caseType) {
582 caseType.definition = 'null';
583 caseType.is_forked = '0';
584 crmApi('CaseType', 'create', caseType, true)
c99f1a0a
TO
585 .catch(function (data) {
586 caseType.is_forked = '1'; // restore
587 $scope.$digest();
470a458e
TO
588 });
589 };
b75c2546
TO
590 });
591
bba9b4f0 592})(angular, CRM.$, CRM._);