Merge pull request #2308 from civicrm/4.4
[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 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
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')) {
33 ui.panel
34 .off('click.crmLivePage')
35 .on('click.crmLivePage', 'a.button, a.action-item', function() {
36 // only follow real links not javascript buttons
37 if ($(this).attr('href') === '#' || $(this).attr('onclick') || $(this).hasClass('no-popup')) {
38 return;
39 }
40 CRM.loadForm($(this).attr('href'), {
41 openInline: 'a:not("[href=#], .no-popup")'
42 }).on('crmFormSuccess', function(e, data) {
43 // Refresh when form completes
44 ui.panel.crmSnippet('refresh');
45 });
46 return false;
47 });
48 }
49 ui.panel
50 .off('.tabInfo')
51 .on('crmLoad.tabInfo crmFormSuccess.tabInfo', function(e, data) {
52 if (typeof(data.tabCount) !== 'undefined') {
53 CRM.tabHeader.updateCount(ui.tab, data.tabCount);
54 }
55 if (typeof(data.tabValid) !== 'undefined') {
56 var method = data.tabValid ? 'removeClass' : 'addClass';
57 ui.tab[method]('disabled');
58 }
59 });
60 CRM[method]($('a', ui.tab).attr('href'), params);
61 }
62 e.preventDefault();
63 })
64 .tabs(tabSettings);
65 });
66 (function($) {
67 // Utility functions
68 CRM.tabHeader = CRM.tabHeader || {};
69 CRM.tabHeader.updateCount = function(tab, count) {
70 var oldClass = $(tab).attr('class').match(/(crm-count-\d+)/);
71 if (oldClass) {
72 $(tab).removeClass(oldClass[0]);
73 }
74 $(tab)
75 .addClass('crm-count-' + count)
76 .find('a em').html('' + count);
77 }
78 })(cj);