Merge pull request #72 from dpradeep/upgrade-handling
[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) ? $("#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(data) {
93 var params = {create: true};
94 if (data.contact_type) {
95 params.api = {params: {contact_type: data.contact_type}};
96 }
97 $('[name=edit_role_contact_id]', this).val('').crmEntityRef(params);
98 },
99 post: function(data) {
100 data.rel_contact = $('[name=edit_role_contact_id]', this).val();
101 if (data.rel_contact) {
102 $.extend(data, {
103 case_id: caseId(),
104 contact_id: contactId()
105 });
106 return $.post(CRM.url('civicrm/ajax/relation'), data);
107 }
108 return false;
109 }
110 },
111 '#addClientDialog': {
112 pre: function() {
113 $('[name=add_client_id]', this).val('').crmEntityRef({create: true});
114 },
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);
120 }
121 return false;
122 }
123 },
124 '#addMembersToGroupDialog': {
125 pre: function() {
126 $('[name=add_member_to_group_contact_id]', this).val('').crmEntityRef({create: true, select: {multiple: true}});
127 },
128 post: function(data) {
129 var requests = [],
130 cids = $('[name=add_member_to_group_contact_id]', this).val();
131 if (cids) {
132 $.each(cids.split(','), function (k, cid) {
133 requests.push(['group_contact', 'create', $.extend({contact_id: cid}, data)]);
134 });
135 return CRM.api3(requests);
136 }
137 return false;
138 }
139 }
140 },
141 detached = {};
142
143 function detachMiniForms() {
144 detached = {};
145 $.each(miniForms, function(selector) {
146 detached[selector] = $(selector).detach().removeClass('hiddenElement');
147 });
148 }
149
150 $('#crm-container').on('crmLoad', '#crm-main-content-wrapper', detachMiniForms);
151
152 $(document).ready(function() {
153 detachMiniForms();
154 $('#crm-container')
155 .on('change', 'select[name=add_activity_type_id]', function() {
156 open($(this).val());
157 $(this).select2('val', '');
158 })
159 .on('change', 'select[name=timeline_id]', function() {
160 if ($(this).val()) {
161 CRM.confirm({
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>'})
164 })
165 .on('crmConfirm:yes', function() {
166 $('[name=_qf_CaseView_next]').click();
167 })
168 .on('crmConfirm:no', function() {
169 $('select[name=timeline_id]').select2('val', '');
170 });
171 }
172 })
173 .on('change', 'select[name=report_id]', function() {
174 if ($(this).val()) {
175 var url = CRM.url('civicrm/case/report', {
176 reset: 1,
177 cid: contactId(),
178 caseid: caseId(),
179 asn: $(this).val()
180 });
181 open(url, {dialog: {width: '50%', height: 'auto'}});
182 $(this).select2('val', '');
183 }
184 })
185 .on('click', 'a.case-miniform', function(e) {
186 var dialog,
187 $el = $(this),
188 target = $el.attr('href');
189 function submit() {
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
193 if (submission) {
194 dialog.block();
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');
201 }
202 });
203 return false;
204 }
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'));
210 }
211 })
212 }
213 return submission;
214 }
215 dialog = CRM.confirm({
216 title: $(this).attr('title') || $(this).text(),
217 message: detached[target],
218 resizable: true,
219 open: function() {
220 miniForms[target].pre && miniForms[target].pre.call(this, $el.data());
221 }
222 })
223 .on('dialogclose', function() {
224 detached[target] = $(target, dialog).detach();
225 })
226 .on('crmConfirm:yes', submit);
227 e.preventDefault();
228 });
229
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'));
238 });
239 }
240 })
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);
246 });
247 }
248 });
249 });
250 }(cj, CRM))