Js cleanup
[civicrm-core.git] / templates / CRM / common / enableDisableApi.tpl
CommitLineData
12798ddc
CW
1{*
2 +--------------------------------------------------------------------+
3 | CiviCRM version 4.4 |
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC (c) 2004-2013 |
6 +--------------------------------------------------------------------+
7 | This file is a part of CiviCRM. |
8 | |
9 | CiviCRM is free software; you can copy, modify, and distribute it |
10 | under the terms of the GNU Affero General Public License |
11 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
12 | |
13 | CiviCRM is distributed in the hope that it will be useful, but |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
16 | See the GNU Affero General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU Affero General Public |
19 | License and the CiviCRM Licensing Exception along |
20 | with this program; if not, contact CiviCRM LLC |
21 | at info[AT]civicrm[DOT]org. If you have questions about the |
22 | GNU Affero General Public License or the licensing of CiviCRM, |
23 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
24 +--------------------------------------------------------------------+
25*}
26{* handle common enable/disable actions *}
27{literal}
28<script type="text/javascript">
29 cj(function($) {
d4838efc 30 var $row, $table, info, enabled, fieldLabel;
12798ddc
CW
31
32 function refresh() {
1fe97a01
CW
33 if (false && $.fn.DataTable.fnIsDataTable($table[0])) { // fixme why doesn't this work?
34 $table.dataTable().fnDraw();
35 } else {
36 // Refresh an existing ajax container or create a new one
37 $row.closest('.crm-ajax-container, #crm-main-content-wrapper').crmSnippet().crmSnippet('refresh');
38 }
d4838efc
CW
39 {/literal} {* client-side variable substitutions in smarty are AWKWARD! *}
40 var msg = enabled ? '{ts escape="js" 1="<em>%1</em>"}%1 Disabled{/ts}' : '{ts escape="js" 1="<em>%1</em>"}%1 Enabled{/ts}'{literal};
41 CRM.alert('', ts(msg, {1: fieldLabel}), 'success');
12798ddc
CW
42 }
43
44 function save() {
1fe97a01
CW
45 $table = $row.closest('table');
46 $table.block();
d4838efc 47 CRM.api(info.entity, 'setvalue', {id: info.id, field: 'is_active', value: enabled ? 0 : 1}, {success: refresh});
4d17a233
CW
48 if (enabled) {
49 $(this).dialog('close');
50 }
12798ddc
CW
51 }
52
53 function confirmation() {
4d17a233 54 var conf = $(this);
d4838efc 55 $.getJSON(CRM.url('civicrm/ajax/statusmsg', {entity: info.entity, id: info.id}), function(response) {
4d17a233
CW
56 conf.html(response.content);
57 if (!response.illegal) {
58 conf.dialog('option', 'buttons', [
59 {text: {/literal}'{ts escape="js"}Disable{/ts}'{literal}, click: save},
60 {text: {/literal}'{ts escape="js"}Cancel{/ts}'{literal}, click: function() {$(this).dialog('close');}}
61 ]);
62 }
12798ddc
CW
63 });
64 }
65
66 function enableDisable() {
d4838efc
CW
67 $row = $(this).closest('.crm-entity');
68 info = $(this).crmEditableEntity();
69 fieldLabel = info.label || info.title || info.name || {/literal}'{ts escape="js"}Record{/ts}'{literal};
12798ddc
CW
70 enabled = !$row.hasClass('disabled');
71 if (enabled) {
4d17a233 72 CRM.confirm({}, {{/literal}
12798ddc 73 message: '<div class="crm-loading-element">{ts escape="js"}Loading{/ts}...</div>',
d4838efc
CW
74 {* client-side variable substitutions in smarty are AWKWARD! *}
75 title: ts('{ts escape="js" 1='%1'}Disable %1{/ts}{literal}', {1: fieldLabel}),
12798ddc
CW
76 width: 300,
77 open: confirmation
d4838efc 78 });
12798ddc
CW
79 } else {
80 save();
81 }
82 return false;
83 }
84
85 // Because this is an inline script it may get added to the document more than once, so remove handler before adding
86 $('body')
87 .off('click', '.action-item.crm-enable-disable')
88 .on('click', '.action-item.crm-enable-disable', enableDisable);
89 });
90</script>
91{/literal}