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).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 $('[name=edit_role_contact_id]', this).val('').crmEntityRef({create
: true, api
: {params
: {contact_type
: 'Individual'}}});
95 post: function(data
) {
96 data
.rel_contact
= $('[name=edit_role_contact_id]').val();
97 if (data
.rel_contact
) {
100 contact_id
: contactId()
102 return $.post(CRM
.url('civicrm/ajax/relation'), data
);
107 '#addClientDialog': {
109 $('[name=add_client_id]', this).val('').crmEntityRef({create
: true});
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
);
120 '#addMembersToGroupDialog': {
122 $('[name=add_member_to_group_contact_id]', this).val('').crmEntityRef({create
: true});
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
);
135 function detachMiniForms() {
137 $.each(miniForms
, function(selector
) {
138 detached
[selector
] = $(selector
).detach().removeClass('hiddenElement');
142 $('#crm-container').on('crmLoad', '#crm-main-content-wrapper', detachMiniForms
);
144 $(document
).ready(function() {
147 .on('change', 'select[name=add_activity_type_id]', function() {
149 $(this).select2('val', '');
151 .on('change', 'select[name=timeline_id]', function() {
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>'})
157 .on('crmConfirm:yes', function() {
158 $('[name=_qf_CaseView_next]').click();
160 .on('crmConfirm:no', function() {
161 $('select[name=timeline_id]').select2('val', '');
165 .on('change', 'select[name=report_id]', function() {
167 var url
= CRM
.url('civicrm/case/report', {
173 open(url
, {dialog
: {width
: '50%', height
: 'auto'}});
174 $(this).select2('val', '');
177 .on('click', 'a.case-miniform', function(e
) {
180 target
= $el
.attr('href');
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
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');
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'));
207 dialog
= CRM
.confirm({
208 title
: $(this).attr('title') || $(this).text(),
209 message
: detached
[target
],
210 open
: miniForms
[target
].pre
212 .on('dialogclose', function() {
213 detached
[target
] = $(target
, dialog
).detach();
215 .on('crmConfirm:yes', submit
);
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'));
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
);