Commit | Line | Data |
---|---|---|
4165b7e5 | 1 | // https://civicrm.org/licensing |
4b628e67 CW |
2 | /** |
3 | * By default this simply loads tabs via ajax CRM.loadPage method | |
4 | * Tabs with class 'ajaxForm' will use CRM.loadForm instead, suitable for most forms | |
5 | * Tabs with class 'livePage' will get popup action links, suitable for crud tables | |
6 | */ | |
3cc60a06 | 7 | CRM.$(function($) { |
4165b7e5 CW |
8 | var tabSettings = CRM.tabSettings || {}; |
9 | tabSettings.active = tabSettings.active ? $('#tab_' + tabSettings.active).prevAll().length : 0; | |
10 | $("#mainTabContainer") | |
11 | .on('tabsbeforeactivate', function(e, ui) { | |
e9a3e054 CW |
12 | // CRM-14353 - Warn of unsaved changes for all forms except those which have opted out |
13 | if (CRM.utils.initialValueChanged($('form:not([data-warn-changes=false])', ui.oldPanel))) { | |
4165b7e5 | 14 | CRM.alert(ts('Your changes in the <em>%1</em> tab have not been saved.', {1: ui.oldTab.text()}), ts('Unsaved Changes'), 'warning'); |
4165b7e5 CW |
15 | } |
16 | }) | |
17 | .on('tabsbeforeload', function(e, ui) { | |
18 | // Use civicrm ajax wrappers rather than the default $.load | |
23223213 | 19 | if (!ui.panel.data("civiCrmSnippet")) { |
d6539f93 CW |
20 | var method = ui.tab.hasClass('ajaxForm') ? 'loadForm' : 'loadPage'; |
21 | var params = {target: ui.panel}; | |
22 | if (method === 'loadForm') { | |
23 | params.autoClose = params.openInline = params.cancelButton = params.refreshAction = false; | |
24 | ui.panel.on('crmFormLoad', function() { | |
25 | // Hack: "Save and done" and "Cancel" buttons submit without ajax | |
97e557d7 | 26 | $('.cancel.crm-form-submit, input[name$=upload_done]', this).on('click', function(e) { |
d6539f93 | 27 | $(this).closest('form').ajaxFormUnbind(); |
d681c9f3 | 28 | }); |
d6539f93 CW |
29 | }); |
30 | } | |
53f2643c | 31 | if (ui.tab.hasClass('livePage') && CRM.config.ajaxPopupsEnabled) { |
4b628e67 CW |
32 | ui.panel |
33 | .off('click.crmLivePage') | |
1a62cf90 | 34 | .on('click.crmLivePage', 'a.button, a.action-item', CRM.popup) |
369f685e | 35 | .on('crmPopupFormSuccess.crmLivePage', 'a.button, a.action-item:not(.crm-enable-disable)', CRM.refreshParent); |
4b628e67 | 36 | } |
4e8065a9 | 37 | ui.panel |
5d92a7e7 CW |
38 | .off('.tabInfo') |
39 | .on('crmLoad.tabInfo crmFormSuccess.tabInfo', function(e, data) { | |
23590e77 CW |
40 | if (data) { |
41 | if (typeof(data.tabCount) !== 'undefined') { | |
42 | CRM.tabHeader.updateCount(ui.tab, data.tabCount); | |
43 | } | |
44 | if (typeof(data.tabValid) !== 'undefined') { | |
45 | var method = data.tabValid ? 'removeClass' : 'addClass'; | |
46 | ui.tab[method]('disabled'); | |
47 | } | |
5d92a7e7 | 48 | } |
4e8065a9 | 49 | }); |
d6539f93 | 50 | CRM[method]($('a', ui.tab).attr('href'), params); |
4165b7e5 CW |
51 | } |
52 | e.preventDefault(); | |
53 | }) | |
54 | .tabs(tabSettings); | |
fa9fbb61 CW |
55 | // Any load/submit event could potentially call for tabs to refresh. |
56 | $(document).on('crmLoad.tabInfo crmFormSuccess.tabInfo', function(e, data) { | |
57 | if (data && $.isPlainObject(data.updateTabs)) { | |
58 | $.each(data.updateTabs, CRM.tabHeader.updateCount); | |
59 | $.each(data.updateTabs, CRM.tabHeader.resetTab); | |
60 | } | |
61 | }); | |
4165b7e5 | 62 | }); |
58b65bf6 | 63 | (function($) { |
4e8065a9 CW |
64 | // Utility functions |
65 | CRM.tabHeader = CRM.tabHeader || {}; | |
2840a035 | 66 | |
1724f32f CW |
67 | /** |
68 | * Return active tab | |
69 | */ | |
70 | CRM.tabHeader.getActiveTab = function() { | |
71 | return $('.ui-tabs-active', '#mainTabContainer'); | |
c312052e | 72 | }; |
1724f32f | 73 | |
f1cf499b CW |
74 | /** |
75 | * Make a given tab the active one | |
76 | * @param tab jQuery selector | |
77 | */ | |
78 | CRM.tabHeader.focus = function(tab) { | |
79 | $('#mainTabContainer').tabs('option', 'active', $(tab).prevAll().length); | |
80 | }; | |
81 | ||
2840a035 CW |
82 | /** |
83 | * @param tab jQuery selector | |
84 | * @returns panel jQuery object | |
85 | */ | |
86 | CRM.tabHeader.getTabPanel = function(tab) { | |
c312052e CW |
87 | var selector = $(tab).attr('aria-controls'); |
88 | return selector ? $('#' + selector) : $(); | |
2840a035 CW |
89 | }; |
90 | ||
c312052e CW |
91 | /** |
92 | * @param tab jQuery selector | |
93 | * @returns {string|null} | |
94 | */ | |
95 | function getCountClass(tab) { | |
96 | var $tab = $(tab), | |
97 | css = $tab.attr('class') || '', | |
98 | val = css.match(/(crm-count-\d+)/); | |
99 | return val && val.length ? val[0] : null; | |
1724f32f CW |
100 | } |
101 | ||
c312052e CW |
102 | /** |
103 | * @param tab jQuery selector | |
104 | * @returns {Number|null} | |
105 | */ | |
106 | CRM.tabHeader.getCount = function(tab) { | |
107 | var cssClass = getCountClass(tab); | |
108 | return cssClass ? parseInt(cssClass.slice(10), 10) : null; | |
109 | }; | |
110 | ||
2840a035 CW |
111 | /** |
112 | * Update the counter in a tab | |
113 | * @param tab jQuery selector | |
c312052e | 114 | * @param count {Number} |
2840a035 | 115 | */ |
4e8065a9 | 116 | CRM.tabHeader.updateCount = function(tab, count) { |
c312052e | 117 | var oldClass = getCountClass(tab); |
ecc20f0e | 118 | if (oldClass) { |
c312052e | 119 | $(tab).removeClass(oldClass); |
ecc20f0e | 120 | } |
58b65bf6 | 121 | $(tab) |
58b65bf6 CW |
122 | .addClass('crm-count-' + count) |
123 | .find('a em').html('' + count); | |
2840a035 CW |
124 | }; |
125 | ||
126 | /** | |
b4efde7a CW |
127 | * Refresh tab immediately if it is active (or force=true) |
128 | * otherwise ensure it will be refreshed next time the user clicks on it | |
129 | * | |
2840a035 | 130 | * @param tab |
b4efde7a | 131 | * @param force |
2840a035 | 132 | */ |
b4efde7a | 133 | CRM.tabHeader.resetTab = function(tab, force) { |
2840a035 CW |
134 | var $panel = CRM.tabHeader.getTabPanel(tab); |
135 | if ($(tab).hasClass('ui-tabs-active')) { | |
136 | $panel.crmSnippet('refresh'); | |
c312052e | 137 | } |
b4efde7a CW |
138 | else if (force) { |
139 | if ($panel.data("civiCrmSnippet")) { | |
140 | $panel.crmSnippet('refresh'); | |
141 | } else { | |
142 | $("#mainTabContainer").trigger('tabsbeforeload', [{panel: $panel, tab: $(tab)}]); | |
143 | } | |
144 | } | |
c312052e CW |
145 | else if ($panel.data("civiCrmSnippet")) { |
146 | $panel.crmSnippet('destroy'); | |
2840a035 CW |
147 | } |
148 | }; | |
3cc60a06 | 149 | })(CRM.$); |