Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-04-30-12-58-23
[civicrm-core.git] / templates / CRM / common / TabHeader.js
index a94d21f38fb8e396052a4ab19d6d3ed97bd2d65e..8c044d8babaec8494b437db07b3b0330316b8213 100644 (file)
@@ -4,7 +4,7 @@
  * Tabs with class 'ajaxForm' will use CRM.loadForm instead, suitable for most forms
  * Tabs with class 'livePage' will get popup action links, suitable for crud tables
  */
-cj(function($) {
+CRM.$(function($) {
   var tabSettings = CRM.tabSettings || {};
   tabSettings.active = tabSettings.active ? $('#tab_' + tabSettings.active).prevAll().length : 0;
   $("#mainTabContainer")
@@ -17,7 +17,7 @@ cj(function($) {
     })
     .on('tabsbeforeload', function(e, ui) {
       // Use civicrm ajax wrappers rather than the default $.load
-      if (!ui.panel.data("civicrmCrmSnippet")) {
+      if (!ui.panel.data("civiCrmSnippet")) {
         var method = ui.tab.hasClass('ajaxForm') ? 'loadForm' : 'loadPage';
         var params = {target: ui.panel};
         if (method === 'loadForm') {
@@ -29,28 +29,23 @@ cj(function($) {
             })
           });
         }
-        if (ui.tab.hasClass('livePage')) {
+        if (ui.tab.hasClass('livePage') && CRM.config.ajaxPopupsEnabled) {
           ui.panel
             .off('click.crmLivePage')
-            .on('click.crmLivePage', 'a.button, a.action-item', function() {
-              // only follow real links not javascript buttons
-              if ($(this).attr('href') === '#' || $(this).attr('onclick')) {
-                return;
-              }
-              CRM.loadForm($(this).attr('href'), {
-                openInline: 'a:not([href="#"])'
-              }).on('crmFormSuccess', function(e, data) {
-                  // Refresh when form completes
-                  ui.panel.crmSnippet('refresh');
-                });
-              return false;
-            });
+            .on('click.crmLivePage', 'a.button, a.action-item', CRM.popup)
+            .on('crmPopupFormSuccess.crmLivePage', 'a.button, a.action-item', CRM.refreshParent);
         }
         ui.panel
-          .off('crmLoad.tabCount')
-          .on('crmLoad.tabCount', function(e, data) {
-            if (typeof(data.tabCount) !== 'undefined') {
-              CRM.tabHeader.updateCount(ui.tab, data.tabCount);
+          .off('.tabInfo')
+          .on('crmLoad.tabInfo crmFormSuccess.tabInfo', function(e, data) {
+            if (data) {
+              if (typeof(data.tabCount) !== 'undefined') {
+                CRM.tabHeader.updateCount(ui.tab, data.tabCount);
+              }
+              if (typeof(data.tabValid) !== 'undefined') {
+                var method = data.tabValid ? 'removeClass' : 'addClass';
+                ui.tab[method]('disabled');
+              }
             }
           });
         CRM[method]($('a', ui.tab).attr('href'), params);
@@ -58,14 +53,70 @@ cj(function($) {
       e.preventDefault();
     })
     .tabs(tabSettings);
+  // Any load/submit event could potentially call for tabs to refresh.
+  $(document).on('crmLoad.tabInfo crmFormSuccess.tabInfo', function(e, data) {
+    if (data && $.isPlainObject(data.updateTabs)) {
+      $.each(data.updateTabs, CRM.tabHeader.updateCount);
+      $.each(data.updateTabs, CRM.tabHeader.resetTab);
+    }
+  });
 });
 (function($) {
   // Utility functions
   CRM.tabHeader = CRM.tabHeader || {};
+
+  /**
+   * Return active tab
+   */
+  CRM.tabHeader.getActiveTab = function() {
+    return $('.ui-tabs-active', '#mainTabContainer');
+  }
+
+  /**
+   * Make a given tab the active one
+   * @param tab jQuery selector
+   */
+  CRM.tabHeader.focus = function(tab) {
+    $('#mainTabContainer').tabs('option', 'active', $(tab).prevAll().length);
+  };
+
+  /**
+   * @param tab jQuery selector
+   * @returns panel jQuery object
+   */
+  CRM.tabHeader.getTabPanel = function(tab) {
+    return $('#' + $(tab).attr('aria-controls'));
+  };
+
+  CRM.tabHeader.getCount = function(tab) {
+    return parseInt($(tab).find('a em').text(), 10);
+  }
+
+  /**
+   * Update the counter in a tab
+   * @param tab jQuery selector
+   * @param count number
+   */
   CRM.tabHeader.updateCount = function(tab, count) {
+    var oldClass = $(tab).attr('class').match(/(crm-count-\d+)/);
+    if (oldClass) {
+      $(tab).removeClass(oldClass[0]);
+    }
     $(tab)
-      .removeClass($(tab).attr('class').match(/(crm-count-\d+)/)[0] || 'null')
       .addClass('crm-count-' + count)
       .find('a em').html('' + count);
-  }
-})(cj);
+  };
+
+  /**
+   * Clears tab content so that it will be refreshed next time the user clicks on it
+   * @param tab
+   */
+  CRM.tabHeader.resetTab = function(tab) {
+    var $panel = CRM.tabHeader.getTabPanel(tab);
+    if ($(tab).hasClass('ui-tabs-active')) {
+      $panel.crmSnippet('refresh');
+    } else {
+      $panel.data("civiCrmSnippet") && $panel.crmSnippet('destroy');
+    }
+  };
+})(CRM.$);