Merge pull request #3601 from atif-shaikh/CRM-14942
[civicrm-core.git] / templates / CRM / common / TabHeader.js
index 46e4b0870345e6f0954d22c3c0d7ba2f16b3c236..82c19815e039c202bb7e671862d2df45570e7fa1 100644 (file)
@@ -4,15 +4,14 @@
  * 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")
     .on('tabsbeforeactivate', function(e, ui) {
-      // Warn of unsaved changes - requires formNavigate.tpl to be included in each tab
-      if (!global_formNavigate) {
+      // CRM-14353 - Warn of unsaved changes for all forms except those which have opted out
+      if (CRM.utils.initialValueChanged($('form:not([data-warn-changes=false])', ui.oldPanel))) {
         CRM.alert(ts('Your changes in the <em>%1</em> tab have not been saved.', {1: ui.oldTab.text()}), ts('Unsaved Changes'), 'warning');
-        global_formNavigate = true;
       }
     })
     .on('tabsbeforeload', function(e, ui) {
@@ -29,23 +28,11 @@ 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() {
-              var url = $(this).attr('href');
-              // only follow real links not javascript buttons
-              if (url === '#' || $(this).attr('onclick') || $(this).hasClass('no-popup')) {
-                return;
-              }
-              CRM.loadForm(url, {
-                openInline: 'a:not("[href=#], .no-popup")'
-              }).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('.tabInfo')
@@ -54,10 +41,6 @@ cj(function($) {
               if (typeof(data.tabCount) !== 'undefined') {
                 CRM.tabHeader.updateCount(ui.tab, data.tabCount);
               }
-              if ($.isPlainObject(data.updateTabs)) {
-                $.each(data.updateTabs, CRM.tabHeader.updateCount);
-                $.each(data.updateTabs, CRM.tabHeader.resetTab);
-              }
               if (typeof(data.tabValid) !== 'undefined') {
                 var method = data.tabValid ? 'removeClass' : 'addClass';
                 ui.tab[method]('disabled');
@@ -69,11 +52,33 @@ 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
@@ -82,6 +87,10 @@ cj(function($) {
     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
@@ -109,4 +118,4 @@ cj(function($) {
       $panel.data("civiCrmSnippet") && $panel.crmSnippet('destroy');
     }
   };
-})(cj);
+})(CRM.$);