Merge pull request #13427 from chamilwijesooriya/issue-652
[civicrm-core.git] / templates / CRM / Case / Form / CaseView.js
CommitLineData
5ffd5a35 1// https://civicrm.org/licensing
6ce08914 2(function($, CRM) {
5ffd5a35 3
c91df8b4 4 function refresh(table) {
ad280fb6 5 $('#crm-main-content-wrapper').crmSnippet('refresh');
5ffd5a35
CW
6 }
7
c91df8b4 8 function open(url, options, table) {
5ffd5a35 9 if (CRM.config.ajaxPopupsEnabled) {
c91df8b4
CW
10 CRM.loadForm(url, options).on('crmFormSuccess', function() {
11 refresh(table);
12 });
5ffd5a35
CW
13 }
14 else {
f4af7a1e 15 window.location.href = url;
5ffd5a35
CW
16 }
17 }
18
6ce08914
CW
19 function caseId() {
20 return $('.crm-entity[data-entity=case]').data('id');
21 }
22
23 function contactId() {
24 return $('.crm-entity[data-entity=case]').data('cid');
25 }
26
27 /**
28 * The CaseView form includes some extra fields which are meant to open in a dialog.
29 * We stash them initially and pop them up as needed
30 * TODO: Creating a separate form class for each of these and opening them as standard popup links would be simpler and more reusable
c91df8b4 31 * @type {pre, post}
6ce08914 32 */
c91df8b4
CW
33 var miniForms = {
34 '#manageTagsDialog': {
35 post: function(data) {
b733747a 36 var tagsChecked = $("#tags", this) ? $("#tags", this).val() : '',
6ce08914
CW
37 tagList = {},
38 url = CRM.url('civicrm/case/ajax/processtags');
c91df8b4 39 $("input[name^=case_taglist]", this).each(function() {
6ce08914
CW
40 var tsId = $(this).attr('id').split('_');
41 tagList[tsId[2]] = $(this).val();
42 });
c91df8b4 43 $.extend(data, {
6ce08914
CW
44 case_id: caseId(),
45 tag: tagsChecked,
c91df8b4
CW
46 taglist: tagList
47 });
6ce08914 48 return $.post(url, data);
c91df8b4
CW
49 }
50 },
51 '#mergeCasesDialog': {
52 post: function(data) {
2a06342c
CW
53 if ($('select#merge_case_id').val()) {
54 $('select#merge_case_id').appendTo('form#CaseView');
55 $('[name="_qf_CaseView_next_merge_case"]').click();
c91df8b4
CW
56 } else {
57 return false;
58 }
59 }
60 },
61 '#deleteCaseRoleDialog': {
62 post: function(data) {
63 data.case_id = caseId();
64 return $.post(CRM.url('civicrm/ajax/delcaserole'), data);
65 }
66 },
67 '#addCaseRoleDialog': {
68 pre: function() {
0004ae05
CW
69 var $contactField = $('[name=add_role_contact_id]', this);
70 $('[name=role_type]', this)
71 .off('.miniform')
72 .on('change.miniform', function() {
73 var val = $(this).val();
74 $contactField.val('').change().prop('disabled', !val);
75 if (val) {
76 var
77 pieces = val.split('_'),
78 rType = pieces[0],
79 target = pieces[2], // b or a
80 contact_type = CRM.vars.relationshipTypes[rType]['contact_type_' + target],
81 contact_sub_type = CRM.vars.relationshipTypes[rType]['contact_sub_type_' + target],
82 api = {params: {}};
83 if (contact_type) {
84 api.params.contact_type = contact_type;
85 }
86 if (contact_sub_type) {
87 api.params.contact_sub_type = contact_sub_type;
88 }
89 $contactField
90 .data('api-params', api)
91 .data('user-filter', {})
92 .attr('placeholder', CRM.vars.relationshipTypes[rType]['placeholder_' + target])
93 .change();
94 }
95 })
96 .val('')
97 .change();
98 $contactField.val('').crmEntityRef({create: true, api: {params: {contact_type: 'Individual'}}});
c91df8b4
CW
99 },
100 post: function(data) {
4d46efc4
CW
101 var contactID = $('[name=add_role_contact_id]', this).val(),
102 relType = $('[name=role_type]', this).val();
c91df8b4
CW
103 if (contactID && relType) {
104 $.extend(data, {
105 case_id: caseId(),
106 rel_contact: contactID,
107 rel_type: relType,
108 contact_id: contactId()
109 });
110 return $.post(CRM.url('civicrm/ajax/relation'), data);
111 }
112 return false;
113 }
114 },
115 '#editCaseRoleDialog': {
ba9be71a
CW
116 pre: function(data) {
117 var params = {create: true};
118 if (data.contact_type) {
119 params.api = {params: {contact_type: data.contact_type}};
120 }
121 $('[name=edit_role_contact_id]', this).val('').crmEntityRef(params);
c91df8b4
CW
122 },
123 post: function(data) {
ba9be71a 124 data.rel_contact = $('[name=edit_role_contact_id]', this).val();
c91df8b4
CW
125 if (data.rel_contact) {
126 $.extend(data, {
127 case_id: caseId(),
128 contact_id: contactId()
129 });
130 return $.post(CRM.url('civicrm/ajax/relation'), data);
2a06342c 131 }
c91df8b4
CW
132 return false;
133 }
134 },
135 '#addClientDialog': {
136 pre: function() {
137 $('[name=add_client_id]', this).val('').crmEntityRef({create: true});
138 },
139 post: function(data) {
ba9be71a 140 data.contactID = $('[name=add_client_id]', this).val();
c91df8b4
CW
141 if (data.contactID) {
142 data.caseID = caseId();
143 return $.post(CRM.url('civicrm/case/ajax/addclient'), data);
144 }
145 return false;
146 }
147 },
148 '#addMembersToGroupDialog': {
149 pre: function() {
ba9be71a 150 $('[name=add_member_to_group_contact_id]', this).val('').crmEntityRef({create: true, select: {multiple: true}});
2a06342c 151 },
c91df8b4 152 post: function(data) {
ba9be71a
CW
153 var requests = [],
154 cids = $('[name=add_member_to_group_contact_id]', this).val();
155 if (cids) {
156 $.each(cids.split(','), function (k, cid) {
157 requests.push(['group_contact', 'create', $.extend({contact_id: cid}, data)]);
158 });
159 return CRM.api3(requests);
c91df8b4
CW
160 }
161 return false;
6ce08914 162 }
c91df8b4
CW
163 }
164 },
165 detached = {};
6ce08914
CW
166
167 function detachMiniForms() {
168 detached = {};
169 $.each(miniForms, function(selector) {
2a06342c 170 detached[selector] = $(selector).detach().removeClass('hiddenElement');
5ffd5a35 171 });
6ce08914
CW
172 }
173
174 $('#crm-container').on('crmLoad', '#crm-main-content-wrapper', detachMiniForms);
175
176 $(document).ready(function() {
177 detachMiniForms();
178 $('#crm-container')
179 .on('change', 'select[name=add_activity_type_id]', function() {
180 open($(this).val());
181 $(this).select2('val', '');
182 })
2a06342c
CW
183 .on('change', 'select[name=timeline_id]', function() {
184 if ($(this).val()) {
5fb83680 185 CRM.confirm({
2a06342c
CW
186 title: $('option:first', this).text(),
187 message: ts('Add the %1 set of scheduled activities to this case?', {1: '<em>' + $('option:selected', this).text() + '</em>'})
188 })
5fb83680 189 .on('crmConfirm:yes', function() {
2a06342c
CW
190 $('[name=_qf_CaseView_next]').click();
191 })
5fb83680 192 .on('crmConfirm:no', function() {
2a06342c
CW
193 $('select[name=timeline_id]').select2('val', '');
194 });
195 }
196 })
197 .on('change', 'select[name=report_id]', function() {
198 if ($(this).val()) {
199 var url = CRM.url('civicrm/case/report', {
200 reset: 1,
201 cid: contactId(),
202 caseid: caseId(),
203 asn: $(this).val()
204 });
205 open(url, {dialog: {width: '50%', height: 'auto'}});
206 $(this).select2('val', '');
207 }
208 })
ccfd6962
CW
209 // When changing case subject, record an activity
210 .on('crmFormSuccess', '[data-field=subject]', function(e, value) {
211 var id = caseId();
212 CRM.api3('Activity', 'create', {
213 case_id: id,
214 activity_type_id: 'Change Case Subject',
215 subject: value,
216 status_id: 'Completed'
217 }).done(function() {
218 $('#case_id_' + id).dataTable().api().draw();
219 });
220 })
5fb83680 221 .on('click', 'a.case-miniform', function(e) {
6ce08914
CW
222 var dialog,
223 $el = $(this),
224 target = $el.attr('href');
225 function submit() {
c91df8b4
CW
226 // Call post function with dialog as context and link data as param
227 var submission = miniForms[target].post.call(dialog[0], $.extend({}, $el.data()));
228 // Function should return a deferred object
2a06342c 229 if (submission) {
ba9be71a 230 dialog.block();
c91df8b4 231 submission.done(function(data) {
2a06342c 232 dialog.dialog('close');
c91df8b4
CW
233 var table = $el.closest('table.dataTable');
234 refresh(table.length ? table : $el.attr('rel'));
235 if ($.isPlainObject(data) && data.is_error && data.error_message) {
236 CRM.alert(data.error_message, ts('Error'), 'error');
237 }
2a06342c
CW
238 });
239 return false;
240 }
c91df8b4
CW
241 // Validation failed - show an error msg on empty fields
242 else if (submission === false) {
243 $(':input', dialog).not('.select2-container *').each(function() {
244 if (!$(this).val()) {
245 $(this).crmError(ts('Please select a value'));
246 }
aab589e2 247 });
c91df8b4
CW
248 }
249 return submission;
6ce08914 250 }
5fb83680 251 dialog = CRM.confirm({
2a06342c 252 title: $(this).attr('title') || $(this).text(),
6ce08914 253 message: detached[target],
a243158e 254 resizable: true,
cc1bf3ed 255 options: {yes: ts('Save'), no: ts('Cancel')},
ba9be71a 256 open: function() {
9c632128 257 if (miniForms[target].pre) miniForms[target].pre.call(this, $el.data());
ba9be71a 258 }
5fb83680
CW
259 })
260 .on('dialogclose', function() {
261 detached[target] = $(target, dialog).detach();
262 })
263 .on('crmConfirm:yes', submit);
264 e.preventDefault();
6ce08914 265 });
ca0de7b5 266
ca0de7b5
CW
267 // Keep the state of accordions when refreshing
268 var accordionStates = [];
269 $('#crm-main-content-wrapper')
270 .on('crmBeforeLoad', function(e) {
271 if ($(e.target).is(this)) {
272 accordionStates = [];
273 $('.crm-accordion-wrapper', this).each(function() {
274 accordionStates.push($(this).hasClass('collapsed'));
275 });
276 }
277 })
278 .on('crmLoad', function(e) {
279 if ($(e.target).is(this)) {
280 var $targets = $('.crm-accordion-wrapper', this);
281 $.each(accordionStates, function(i, isCollapsed) {
282 $targets.eq(i).toggleClass('collapsed', isCollapsed);
283 });
284 }
285 });
6ce08914 286 });
aab589e2 287}(cj, CRM));