1 // https://civicrm.org/licensing
4 function refresh(table
) {
6 $(table
).dataTable().fnDraw();
8 $('#crm-main-content-wrapper').crmSnippet('refresh');
12 function open(url
, options
, table
) {
13 if (CRM
.config
.ajaxPopupsEnabled
) {
14 CRM
.loadForm(url
, options
).on('crmFormSuccess', function() {
19 window
.location
.href
= url
;
24 return $('.crm-entity[data-entity=case]').data('id');
27 function contactId() {
28 return $('.crm-entity[data-entity=case]').data('cid');
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
38 '#manageTagsDialog': {
39 post: function(data
) {
40 var tagsChecked
= $("#tags", this) ? $("#tags", this).select2('val').join(',') : '',
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();
52 return $.post(url
, data
);
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();
65 '#deleteCaseRoleDialog': {
66 post: function(data
) {
67 data
.case_id
= caseId();
68 return $.post(CRM
.url('civicrm/ajax/delcaserole'), data
);
71 '#addCaseRoleDialog': {
73 $('[name=role_type]', this).val('').change();
74 $('[name=add_role_contact_id]', this).val('').crmEntityRef({create
: true, api
: {params
: {contact_type
: 'Individual'}}});
76 post: function(data
) {
77 var contactID
= $('[name=add_role_contact_id]').val(),
78 relType
= $('[name=role_type]').val();
79 if (contactID
&& relType
) {
82 rel_contact
: contactID
,
84 contact_id
: contactId()
86 return $.post(CRM
.url('civicrm/ajax/relation'), data
);
91 '#editCaseRoleDialog': {
93 var params
= {create
: true};
94 if (data
.contact_type
) {
95 params
.api
= {params
: {contact_type
: data
.contact_type
}};
97 $('[name=edit_role_contact_id]', this).val('').crmEntityRef(params
);
99 post: function(data
) {
100 data
.rel_contact
= $('[name=edit_role_contact_id]', this).val();
101 if (data
.rel_contact
) {
104 contact_id
: contactId()
106 return $.post(CRM
.url('civicrm/ajax/relation'), data
);
111 '#addClientDialog': {
113 $('[name=add_client_id]', this).val('').crmEntityRef({create
: true});
115 post: function(data
) {
116 data
.contactID
= $('[name=add_client_id]', this).val();
117 if (data
.contactID
) {
118 data
.caseID
= caseId();
119 return $.post(CRM
.url('civicrm/case/ajax/addclient'), data
);
124 '#addMembersToGroupDialog': {
126 $('[name=add_member_to_group_contact_id]', this).val('').crmEntityRef({create
: true, select
: {multiple
: true}});
128 post: function(data
) {
130 cids
= $('[name=add_member_to_group_contact_id]', this).val();
132 $.each(cids
.split(','), function (k
, cid
) {
133 requests
.push(['group_contact', 'create', $.extend({contact_id
: cid
}, data
)]);
135 return CRM
.api3(requests
);
143 function detachMiniForms() {
145 $.each(miniForms
, function(selector
) {
146 detached
[selector
] = $(selector
).detach().removeClass('hiddenElement');
150 $('#crm-container').on('crmLoad', '#crm-main-content-wrapper', detachMiniForms
);
152 $(document
).ready(function() {
155 .on('change', 'select[name=add_activity_type_id]', function() {
157 $(this).select2('val', '');
159 .on('change', 'select[name=timeline_id]', function() {
162 title
: $('option:first', this).text(),
163 message
: ts('Add the %1 set of scheduled activities to this case?', {1: '<em>' + $('option:selected', this).text() + '</em>'})
165 .on('crmConfirm:yes', function() {
166 $('[name=_qf_CaseView_next]').click();
168 .on('crmConfirm:no', function() {
169 $('select[name=timeline_id]').select2('val', '');
173 .on('change', 'select[name=report_id]', function() {
175 var url
= CRM
.url('civicrm/case/report', {
181 open(url
, {dialog
: {width
: '50%', height
: 'auto'}});
182 $(this).select2('val', '');
185 .on('click', 'a.case-miniform', function(e
) {
188 target
= $el
.attr('href');
190 // Call post function with dialog as context and link data as param
191 var submission
= miniForms
[target
].post
.call(dialog
[0], $.extend({}, $el
.data()));
192 // Function should return a deferred object
195 submission
.done(function(data
) {
196 dialog
.dialog('close');
197 var table
= $el
.closest('table.dataTable');
198 refresh(table
.length
? table
: $el
.attr('rel'));
199 if ($.isPlainObject(data
) && data
.is_error
&& data
.error_message
) {
200 CRM
.alert(data
.error_message
, ts('Error'), 'error');
205 // Validation failed - show an error msg on empty fields
206 else if (submission
=== false) {
207 $(':input', dialog
).not('.select2-container *').each(function() {
208 if (!$(this).val()) {
209 $(this).crmError(ts('Please select a value'));
215 dialog
= CRM
.confirm({
216 title
: $(this).attr('title') || $(this).text(),
217 message
: detached
[target
],
220 miniForms
[target
].pre
&& miniForms
[target
].pre
.call(this, $el
.data());
223 .on('dialogclose', function() {
224 detached
[target
] = $(target
, dialog
).detach();
226 .on('crmConfirm:yes', submit
);
230 // Keep the state of accordions when refreshing
231 var accordionStates
= [];
232 $('#crm-main-content-wrapper')
233 .on('crmBeforeLoad', function(e
) {
234 if ($(e
.target
).is(this)) {
235 accordionStates
= [];
236 $('.crm-accordion-wrapper', this).each(function() {
237 accordionStates
.push($(this).hasClass('collapsed'));
241 .on('crmLoad', function(e
) {
242 if ($(e
.target
).is(this)) {
243 var $targets
= $('.crm-accordion-wrapper', this);
244 $.each(accordionStates
, function(i
, isCollapsed
) {
245 $targets
.eq(i
).toggleClass('collapsed', isCollapsed
);