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