Commit | Line | Data |
---|---|---|
5ffd5a35 | 1 | // https://civicrm.org/licensing |
6ce08914 | 2 | (function($, CRM) { |
5ffd5a35 | 3 | |
c91df8b4 CW |
4 | function refresh(table) { |
5 | if (table) { | |
6 | $(table).dataTable().fnDraw(); | |
7 | } else { | |
8 | $('#crm-main-content-wrapper').crmSnippet('refresh'); | |
9 | } | |
5ffd5a35 CW |
10 | } |
11 | ||
c91df8b4 | 12 | function open(url, options, table) { |
5ffd5a35 | 13 | if (CRM.config.ajaxPopupsEnabled) { |
c91df8b4 CW |
14 | CRM.loadForm(url, options).on('crmFormSuccess', function() { |
15 | refresh(table); | |
16 | }); | |
5ffd5a35 CW |
17 | } |
18 | else { | |
f4af7a1e | 19 | window.location.href = url; |
5ffd5a35 CW |
20 | } |
21 | } | |
22 | ||
6ce08914 CW |
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 | |
c91df8b4 | 35 | * @type {pre, post} |
6ce08914 | 36 | */ |
c91df8b4 CW |
37 | var miniForms = { |
38 | '#manageTagsDialog': { | |
39 | post: function(data) { | |
95ef220a | 40 | var tagsChecked = $("#tags", this) ? $("#tags", this).select2('val').join(',') : '', |
6ce08914 CW |
41 | tagList = {}, |
42 | url = CRM.url('civicrm/case/ajax/processtags'); | |
c91df8b4 | 43 | $("input[name^=case_taglist]", this).each(function() { |
6ce08914 CW |
44 | var tsId = $(this).attr('id').split('_'); |
45 | tagList[tsId[2]] = $(this).val(); | |
46 | }); | |
c91df8b4 | 47 | $.extend(data, { |
6ce08914 CW |
48 | case_id: caseId(), |
49 | tag: tagsChecked, | |
c91df8b4 CW |
50 | taglist: tagList |
51 | }); | |
6ce08914 | 52 | return $.post(url, data); |
c91df8b4 CW |
53 | } |
54 | }, | |
55 | '#mergeCasesDialog': { | |
56 | post: function(data) { | |
2a06342c CW |
57 | if ($('select#merge_case_id').val()) { |
58 | $('select#merge_case_id').appendTo('form#CaseView'); | |
59 | $('[name="_qf_CaseView_next_merge_case"]').click(); | |
c91df8b4 CW |
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); | |
2a06342c | 103 | } |
c91df8b4 CW |
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}); | |
2a06342c | 123 | }, |
c91df8b4 CW |
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; | |
6ce08914 | 130 | } |
c91df8b4 CW |
131 | } |
132 | }, | |
133 | detached = {}; | |
6ce08914 CW |
134 | |
135 | function detachMiniForms() { | |
136 | detached = {}; | |
137 | $.each(miniForms, function(selector) { | |
2a06342c | 138 | detached[selector] = $(selector).detach().removeClass('hiddenElement'); |
5ffd5a35 | 139 | }); |
6ce08914 CW |
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 | }) | |
2a06342c CW |
151 | .on('change', 'select[name=timeline_id]', function() { |
152 | if ($(this).val()) { | |
5fb83680 | 153 | CRM.confirm({ |
2a06342c CW |
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 | }) | |
5fb83680 | 157 | .on('crmConfirm:yes', function() { |
2a06342c CW |
158 | $('[name=_qf_CaseView_next]').click(); |
159 | }) | |
5fb83680 | 160 | .on('crmConfirm:no', function() { |
2a06342c CW |
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 | }) | |
5fb83680 | 177 | .on('click', 'a.case-miniform', function(e) { |
6ce08914 CW |
178 | var dialog, |
179 | $el = $(this), | |
180 | target = $el.attr('href'); | |
181 | function submit() { | |
c91df8b4 CW |
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 | |
2a06342c CW |
185 | if (submission) { |
186 | dialog.parent().block(); | |
c91df8b4 | 187 | submission.done(function(data) { |
2a06342c | 188 | dialog.dialog('close'); |
c91df8b4 CW |
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 | } | |
2a06342c CW |
194 | }); |
195 | return false; | |
196 | } | |
c91df8b4 CW |
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; | |
6ce08914 | 206 | } |
5fb83680 | 207 | dialog = CRM.confirm({ |
2a06342c | 208 | title: $(this).attr('title') || $(this).text(), |
6ce08914 | 209 | message: detached[target], |
a243158e | 210 | resizable: true, |
c91df8b4 | 211 | open: miniForms[target].pre |
5fb83680 CW |
212 | }) |
213 | .on('dialogclose', function() { | |
214 | detached[target] = $(target, dialog).detach(); | |
215 | }) | |
216 | .on('crmConfirm:yes', submit); | |
217 | e.preventDefault(); | |
6ce08914 | 218 | }); |
ca0de7b5 | 219 | |
ca0de7b5 CW |
220 | // Keep the state of accordions when refreshing |
221 | var accordionStates = []; | |
222 | $('#crm-main-content-wrapper') | |
223 | .on('crmBeforeLoad', function(e) { | |
224 | if ($(e.target).is(this)) { | |
225 | accordionStates = []; | |
226 | $('.crm-accordion-wrapper', this).each(function() { | |
227 | accordionStates.push($(this).hasClass('collapsed')); | |
228 | }); | |
229 | } | |
230 | }) | |
231 | .on('crmLoad', function(e) { | |
232 | if ($(e.target).is(this)) { | |
233 | var $targets = $('.crm-accordion-wrapper', this); | |
234 | $.each(accordionStates, function(i, isCollapsed) { | |
235 | $targets.eq(i).toggleClass('collapsed', isCollapsed); | |
236 | }); | |
237 | } | |
238 | }); | |
6ce08914 CW |
239 | }); |
240 | }(cj, CRM)) |