CRM-14486 - ts and link for empowered by image
[civicrm-core.git] / templates / CRM / common / TabHeader.js
CommitLineData
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 7CRM.$(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) {
12 // Warn of unsaved changes - requires formNavigate.tpl to be included in each tab
329758bb 13 if (CRM.utils.initialValueChanged(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
26 $('.cancel.form-submit, input[name$=upload_done]', this).on('click', function(e) {
27 $(this).closest('form').ajaxFormUnbind();
28 })
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)
554b1768 35 .on('crmPopupFormSuccess.crmLivePage', 'a.button, a.action-item', 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');
72 }
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) {
87 return $('#' + $(tab).attr('aria-controls'));
88 };
89
1724f32f
CW
90 CRM.tabHeader.getCount = function(tab) {
91 return parseInt($(tab).find('a em').text(), 10);
92 }
93
2840a035
CW
94 /**
95 * Update the counter in a tab
96 * @param tab jQuery selector
97 * @param count number
98 */
4e8065a9 99 CRM.tabHeader.updateCount = function(tab, count) {
ecc20f0e
CW
100 var oldClass = $(tab).attr('class').match(/(crm-count-\d+)/);
101 if (oldClass) {
102 $(tab).removeClass(oldClass[0]);
103 }
58b65bf6 104 $(tab)
58b65bf6
CW
105 .addClass('crm-count-' + count)
106 .find('a em').html('' + count);
2840a035
CW
107 };
108
109 /**
110 * Clears tab content so that it will be refreshed next time the user clicks on it
111 * @param tab
112 */
113 CRM.tabHeader.resetTab = function(tab) {
114 var $panel = CRM.tabHeader.getTabPanel(tab);
115 if ($(tab).hasClass('ui-tabs-active')) {
116 $panel.crmSnippet('refresh');
117 } else {
118 $panel.data("civiCrmSnippet") && $panel.crmSnippet('destroy');
119 }
120 };
3cc60a06 121})(CRM.$);