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