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 | */ | |
4165b7e5 CW |
7 | cj(function($) { |
8 | var tabSettings = CRM.tabSettings || {}; | |
9 | tabSettings.active = tabSettings.active ? $('#tab_' + tabSettings.active).prevAll().length : 0; | |
10 | $("#mainTabContainer") | |
11 | .on('tabsbeforeactivate', function(e, ui) { | |
12 | // Warn of unsaved changes - requires formNavigate.tpl to be included in each tab | |
13 | if (!global_formNavigate) { | |
14 | CRM.alert(ts('Your changes in the <em>%1</em> tab have not been saved.', {1: ui.oldTab.text()}), ts('Unsaved Changes'), 'warning'); | |
15 | global_formNavigate = true; | |
16 | } | |
17 | }) | |
18 | .on('tabsbeforeload', function(e, ui) { | |
19 | // Use civicrm ajax wrappers rather than the default $.load | |
23223213 | 20 | if (!ui.panel.data("civiCrmSnippet")) { |
d6539f93 CW |
21 | var method = ui.tab.hasClass('ajaxForm') ? 'loadForm' : 'loadPage'; |
22 | var params = {target: ui.panel}; | |
23 | if (method === 'loadForm') { | |
24 | params.autoClose = params.openInline = params.cancelButton = params.refreshAction = false; | |
25 | ui.panel.on('crmFormLoad', function() { | |
26 | // Hack: "Save and done" and "Cancel" buttons submit without ajax | |
27 | $('.cancel.form-submit, input[name$=upload_done]', this).on('click', function(e) { | |
28 | $(this).closest('form').ajaxFormUnbind(); | |
29 | }) | |
30 | }); | |
31 | } | |
53f2643c | 32 | if (ui.tab.hasClass('livePage') && CRM.config.ajaxPopupsEnabled) { |
4b628e67 CW |
33 | ui.panel |
34 | .off('click.crmLivePage') | |
1a62cf90 CW |
35 | .on('click.crmLivePage', 'a.button, a.action-item', CRM.popup) |
36 | .on('crmPopupFormSuccess.crmLivePage', 'a.button, a.action-item', function() { | |
37 | // Refresh panel when form completes | |
38 | ui.panel.crmSnippet('refresh'); | |
4b628e67 CW |
39 | }); |
40 | } | |
4e8065a9 | 41 | ui.panel |
5d92a7e7 CW |
42 | .off('.tabInfo') |
43 | .on('crmLoad.tabInfo crmFormSuccess.tabInfo', function(e, data) { | |
23590e77 CW |
44 | if (data) { |
45 | if (typeof(data.tabCount) !== 'undefined') { | |
46 | CRM.tabHeader.updateCount(ui.tab, data.tabCount); | |
47 | } | |
48 | if (typeof(data.tabValid) !== 'undefined') { | |
49 | var method = data.tabValid ? 'removeClass' : 'addClass'; | |
50 | ui.tab[method]('disabled'); | |
51 | } | |
5d92a7e7 | 52 | } |
4e8065a9 | 53 | }); |
d6539f93 | 54 | CRM[method]($('a', ui.tab).attr('href'), params); |
4165b7e5 CW |
55 | } |
56 | e.preventDefault(); | |
57 | }) | |
58 | .tabs(tabSettings); | |
fa9fbb61 CW |
59 | // Any load/submit event could potentially call for tabs to refresh. |
60 | $(document).on('crmLoad.tabInfo crmFormSuccess.tabInfo', function(e, data) { | |
61 | if (data && $.isPlainObject(data.updateTabs)) { | |
62 | $.each(data.updateTabs, CRM.tabHeader.updateCount); | |
63 | $.each(data.updateTabs, CRM.tabHeader.resetTab); | |
64 | } | |
65 | }); | |
4165b7e5 | 66 | }); |
58b65bf6 | 67 | (function($) { |
4e8065a9 CW |
68 | // Utility functions |
69 | CRM.tabHeader = CRM.tabHeader || {}; | |
2840a035 | 70 | |
f1cf499b CW |
71 | /** |
72 | * Make a given tab the active one | |
73 | * @param tab jQuery selector | |
74 | */ | |
75 | CRM.tabHeader.focus = function(tab) { | |
76 | $('#mainTabContainer').tabs('option', 'active', $(tab).prevAll().length); | |
77 | }; | |
78 | ||
2840a035 CW |
79 | /** |
80 | * @param tab jQuery selector | |
81 | * @returns panel jQuery object | |
82 | */ | |
83 | CRM.tabHeader.getTabPanel = function(tab) { | |
84 | return $('#' + $(tab).attr('aria-controls')); | |
85 | }; | |
86 | ||
87 | /** | |
88 | * Update the counter in a tab | |
89 | * @param tab jQuery selector | |
90 | * @param count number | |
91 | */ | |
4e8065a9 | 92 | CRM.tabHeader.updateCount = function(tab, count) { |
ecc20f0e CW |
93 | var oldClass = $(tab).attr('class').match(/(crm-count-\d+)/); |
94 | if (oldClass) { | |
95 | $(tab).removeClass(oldClass[0]); | |
96 | } | |
58b65bf6 | 97 | $(tab) |
58b65bf6 CW |
98 | .addClass('crm-count-' + count) |
99 | .find('a em').html('' + count); | |
2840a035 CW |
100 | }; |
101 | ||
102 | /** | |
103 | * Clears tab content so that it will be refreshed next time the user clicks on it | |
104 | * @param tab | |
105 | */ | |
106 | CRM.tabHeader.resetTab = function(tab) { | |
107 | var $panel = CRM.tabHeader.getTabPanel(tab); | |
108 | if ($(tab).hasClass('ui-tabs-active')) { | |
109 | $panel.crmSnippet('refresh'); | |
110 | } else { | |
111 | $panel.data("civiCrmSnippet") && $panel.crmSnippet('destroy'); | |
112 | } | |
113 | }; | |
58b65bf6 | 114 | })(cj); |