Merge pull request #2687 from giant-rabbit/event_info_custom_validation
[civicrm-core.git] / templates / CRM / common / TabHeader.js
1 // https://civicrm.org/licensing
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 */
7 CRM.$(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
20 if (!ui.panel.data("civiCrmSnippet")) {
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 }
32 if (ui.tab.hasClass('livePage') && CRM.config.ajaxPopupsEnabled) {
33 ui.panel
34 .off('click.crmLivePage')
35 .on('click.crmLivePage', 'a.button, a.action-item', CRM.popup)
36 .on('crmPopupFormSuccess.crmLivePage', 'a.button, a.action-item', CRM.refreshParent);
37 }
38 ui.panel
39 .off('.tabInfo')
40 .on('crmLoad.tabInfo crmFormSuccess.tabInfo', function(e, data) {
41 if (data) {
42 if (typeof(data.tabCount) !== 'undefined') {
43 CRM.tabHeader.updateCount(ui.tab, data.tabCount);
44 }
45 if (typeof(data.tabValid) !== 'undefined') {
46 var method = data.tabValid ? 'removeClass' : 'addClass';
47 ui.tab[method]('disabled');
48 }
49 }
50 });
51 CRM[method]($('a', ui.tab).attr('href'), params);
52 }
53 e.preventDefault();
54 })
55 .tabs(tabSettings);
56 // Any load/submit event could potentially call for tabs to refresh.
57 $(document).on('crmLoad.tabInfo crmFormSuccess.tabInfo', function(e, data) {
58 if (data && $.isPlainObject(data.updateTabs)) {
59 $.each(data.updateTabs, CRM.tabHeader.updateCount);
60 $.each(data.updateTabs, CRM.tabHeader.resetTab);
61 }
62 });
63 });
64 (function($) {
65 // Utility functions
66 CRM.tabHeader = CRM.tabHeader || {};
67
68 /**
69 * Make a given tab the active one
70 * @param tab jQuery selector
71 */
72 CRM.tabHeader.focus = function(tab) {
73 $('#mainTabContainer').tabs('option', 'active', $(tab).prevAll().length);
74 };
75
76 /**
77 * @param tab jQuery selector
78 * @returns panel jQuery object
79 */
80 CRM.tabHeader.getTabPanel = function(tab) {
81 return $('#' + $(tab).attr('aria-controls'));
82 };
83
84 /**
85 * Update the counter in a tab
86 * @param tab jQuery selector
87 * @param count number
88 */
89 CRM.tabHeader.updateCount = function(tab, count) {
90 var oldClass = $(tab).attr('class').match(/(crm-count-\d+)/);
91 if (oldClass) {
92 $(tab).removeClass(oldClass[0]);
93 }
94 $(tab)
95 .addClass('crm-count-' + count)
96 .find('a em').html('' + count);
97 };
98
99 /**
100 * Clears tab content so that it will be refreshed next time the user clicks on it
101 * @param tab
102 */
103 CRM.tabHeader.resetTab = function(tab) {
104 var $panel = CRM.tabHeader.getTabPanel(tab);
105 if ($(tab).hasClass('ui-tabs-active')) {
106 $panel.crmSnippet('refresh');
107 } else {
108 $panel.data("civiCrmSnippet") && $panel.crmSnippet('destroy');
109 }
110 };
111 })(CRM.$);