From 232232133c34a4645216153db03245ea71d8e107 Mon Sep 17 00:00:00 2001
From: Coleman Watts <coleman@civicrm.org>
Date: Wed, 1 Jan 2014 20:06:03 -0800
Subject: [PATCH] CRM-13863 - Ajax-enable contact actions/activity list

---
 CRM/Contact/BAO/Contact.php                   |  8 +++----
 CRM/Contact/Page/View/Summary.php             | 10 ++++----
 CRM/Tag/Form/Tag.php                          |  3 +++
 css/civicrm.css                               |  6 ++---
 js/Common.js                                  | 11 +++++++--
 js/crm.livePage.js                            |  4 ++--
 templates/CRM/Activity/Form/ActivityLinks.tpl | 20 ++++++++++++++--
 templates/CRM/Contact/Form/Relationship.tpl   |  4 ++--
 templates/CRM/Contact/Page/Inline/Actions.tpl | 17 ++-----------
 templates/CRM/Contact/Page/View/Summary.js    | 24 ++++++++++++++++++-
 templates/CRM/Member/Form/Task/Label.js       |  9 ++-----
 templates/CRM/Tag/Form/Tag.tpl                |  2 +-
 templates/CRM/common/TabHeader.js             |  6 ++---
 13 files changed, 77 insertions(+), 47 deletions(-)

diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php
index 83f791a099..18f240f80f 100644
--- a/CRM/Contact/BAO/Contact.php
+++ b/CRM/Contact/BAO/Contact.php
@@ -2725,10 +2725,10 @@ AND       civicrm_openid.is_primary = 1";
         'key' => 'delete',
         'permissions' => array('access deleted contacts', 'delete contacts'),
       ),
-      'contribution' => array('title' => ts('Add Contribution'),
+      'contribute' => array('title' => ts('Add Contribution'),
         'weight' => 5,
         'ref' => 'new-contribution',
-        'key' => 'contribution',
+        'key' => 'contribute',
         'component' => 'CiviContribute',
         'href' => CRM_Utils_System::url('civicrm/contact/view/contribution',
           'reset=1&action=add&context=contribution'
@@ -2768,10 +2768,10 @@ AND       civicrm_openid.is_primary = 1";
           'edit pledges',
         ),
       ),
-      'membership' => array('title' => ts('Add Membership'),
+      'member' => array('title' => ts('Add Membership'),
         'weight' => 20,
         'ref' => 'new-membership',
-        'key' => 'membership',
+        'key' => 'member',
         'component' => 'CiviMember',
         'href' => CRM_Utils_System::url('civicrm/contact/view/membership',
           'reset=1&action=add&context=membership'
diff --git a/CRM/Contact/Page/View/Summary.php b/CRM/Contact/Page/View/Summary.php
index f0a5f44ec2..ad287fdec1 100644
--- a/CRM/Contact/Page/View/Summary.php
+++ b/CRM/Contact/Page/View/Summary.php
@@ -132,7 +132,7 @@ class CRM_Contact_Page_View_Summary extends CRM_Contact_Page_View {
    * @access public
    */
   function view() {
-    // Add js for in-place editing and jstree for tags
+    // Add js for tabs, in-place editing, and jstree for tags
     CRM_Core_Resources::singleton()
       ->addScriptFile('civicrm', 'templates/CRM/Contact/Page/View/Summary.js')
       ->addScriptFile('civicrm', 'packages/jquery/plugins/jstree/jquery.jstree.js', 0, 'html-header', FALSE)
@@ -330,7 +330,8 @@ class CRM_Contact_Page_View_Summary extends CRM_Contact_Page_View {
           'title' => $elem['title'],
           'weight' => $elem['weight'],
           'count' => CRM_Contact_BAO_Contact::getCountComponent($u, $this->_contactId),
-          'class' => 'livePage',
+          // CiviCase UI not yet compatible with livePage popups
+          'class' => $u == 'case' ? '' : 'livePage',
         );
         // make sure to get maximum weight, rest of tabs go after
         // FIXME: not very elegant again
@@ -343,12 +344,11 @@ class CRM_Contact_Page_View_Summary extends CRM_Contact_Page_View {
     $rest = array(
       'activity' => array(
         'title' => ts('Activities'),
-      ),
-      'case' => array(
-        'title' => ts('Cases'),
+        'class' => 'livePage',
       ),
       'rel' => array(
         'title' => ts('Relationships'),
+        'class' => 'livePage',
       ),
       'group' => array(
         'title' => ts('Groups'),
diff --git a/CRM/Tag/Form/Tag.php b/CRM/Tag/Form/Tag.php
index 19ce7e0a22..ecf53180fa 100644
--- a/CRM/Tag/Form/Tag.php
+++ b/CRM/Tag/Form/Tag.php
@@ -72,6 +72,9 @@ class CRM_Tag_Form_Tag extends CRM_Core_Form {
    * @access public
    */
   public function buildQuickForm() {
+    CRM_Core_Resources::singleton()
+      ->addScriptFile('civicrm', 'packages/jquery/plugins/jstree/jquery.jstree.js', 0, 'html-header', FALSE)
+      ->addStyleFile('civicrm', 'packages/jquery/plugins/jstree/themes/default/style.css', 0, 'html-header');
     // get categories for the contact id
     $entityTag = CRM_Core_BAO_EntityTag::getTag($this->_entityID, $this->_entityTable);
     $this->assign('tagged', $entityTag);
diff --git a/css/civicrm.css b/css/civicrm.css
index d5ae236bf9..5408d0b449 100644
--- a/css/civicrm.css
+++ b/css/civicrm.css
@@ -2257,13 +2257,13 @@ div.grippie {
 
 /* crm button style */
 
-#crm-container .crm-submit-buttons,
-#crm-container .action-link {
+.crm-container .crm-submit-buttons,
+.crm-container .action-link {
   height: 27px;
   margin: 4px 0px 4px 2px;
 }
 
-#crm-container .register_link-top {
+.crm-container .register_link-top {
   float: right;
   margin-left: 8px;
 }
diff --git a/js/Common.js b/js/Common.js
index 274371ccda..52f9c92faf 100644
--- a/js/Common.js
+++ b/js/Common.js
@@ -809,7 +809,7 @@ CRM.validate = CRM.validate || {
     });
   }
 
-  $.widget('civicrm.crmSnippet', {
+  $.widget('civi.crmSnippet', {
     options: {
       url: null,
       block: true,
@@ -941,7 +941,7 @@ CRM.validate = CRM.validate || {
         validate: true,
         refreshAction: ['next_new', 'submit_savenext'],
         cancelButton: '.cancel.form-submit',
-        openInline: 'a.button:not([href="#"])',
+        openInline: 'a.button:not("[href=#], .no-popup")',
         onCancel: function(event) {},
         onError: function(data) {
           var $el = $(this);
@@ -1008,6 +1008,13 @@ CRM.validate = CRM.validate || {
             settings.onError.call($el, response);
           }
         },
+        beforeSerialize: function(form, options) {
+          if (window.CKEDITOR && window.CKEDITOR.instances) {
+            for (var instance in CKEDITOR.instances) {
+              CKEDITOR.instances[instance].updateElement();
+            }
+          }
+        },
         beforeSubmit: function(submission) {
           $el.crmSnippet('option', 'block') && $el.block();
           $el.trigger('crmFormSubmit', submission);
diff --git a/js/crm.livePage.js b/js/crm.livePage.js
index 2d01bef689..4d241ef88e 100644
--- a/js/crm.livePage.js
+++ b/js/crm.livePage.js
@@ -8,11 +8,11 @@ cj(function($) {
     .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')) {
+      if ($(this).attr('href') === '#' || $(this).attr('onclick') || $(this).hasClass('no-popup')) {
         return;
       }
       CRM.loadForm($(this).attr('href'), {
-        openInline: 'a:not([href="#"])'
+        openInline: 'a:not("[href=#], .no-popup")'
       }).on('crmFormSuccess', function(e, data) {
         // Refresh page when form completes
         $('#crm-main-content-wrapper').crmSnippet('refresh');
diff --git a/templates/CRM/Activity/Form/ActivityLinks.tpl b/templates/CRM/Activity/Form/ActivityLinks.tpl
index 879587ce13..d416981496 100644
--- a/templates/CRM/Activity/Form/ActivityLinks.tpl
+++ b/templates/CRM/Activity/Form/ActivityLinks.tpl
@@ -30,13 +30,29 @@
 {/if}
 
 {if $as_select} {* on 3.2, the activities can be either a drop down select (on the activity tab) or a list (on the action menu) *}
-<select onchange="if (this.value) window.location=''+ this.value; else return false" name="other_activity" id="other_activity" class="form-select">
+<select name="other_activity" id="other_activity" class="form-select">
   <option value="">{ts}- new activity -{/ts}</option>
 {foreach from=$activityTypes key=k item=link}
   <option value="{$urls.$k}">{$link}</option>
 {/foreach}
 </select>
-
+{literal}
+<script type="text/javascript">
+  cj(function($) {
+    $('#other_activity').change(function() {
+      var
+        $el = $(this),
+        url = $el.val();
+      if (url) {
+        $el.val('');
+        CRM.loadForm(url).on('crmFormSuccess', function() {
+          $el.closest('.crm-ajax-container, #crm-main-content-wrapper').crmSnippet().crmSnippet('refresh');
+        });
+      }
+    });
+  });
+</script>
+{/literal}
 {else}
 <ul>
 {foreach from=$activityTypes key=k item=link}
diff --git a/templates/CRM/Contact/Form/Relationship.tpl b/templates/CRM/Contact/Form/Relationship.tpl
index 5cc6f68d95..b712db17ad 100644
--- a/templates/CRM/Contact/Form/Relationship.tpl
+++ b/templates/CRM/Contact/Form/Relationship.tpl
@@ -34,7 +34,7 @@
       {foreach from=$viewRelationship item="row"}
             <tr>
                 <td class="label">{$row.relation}</td>
-                <td><a href="{crmURL p='civicrm/contact/view' q="reset=1&cid=`$row.cid`"}">{$row.name}</a></td>
+                <td><a class="no-popup" href="{crmURL p='civicrm/contact/view' q="reset=1&cid=`$row.cid`"}">{$row.name}</a></td>
             </tr>
             {if $isCurrentEmployer}
                 <tr><td class="label">{ts}Current Employee?{/ts}</td><td>{ts}Yes{/ts}</td></tr>
@@ -72,7 +72,7 @@
         {/foreach}
         </table>
         {include file="CRM/Custom/Page/CustomDataView.tpl"}
-        <div class="crm-submit-buttons"><input type="button" name='cancel' value="{ts}Done{/ts}" onclick="location.href='{crmURL p='civicrm/contact/view' q='action=browse&selectedChild=rel'}';"/></div>
+        <div class="crm-submit-buttons"><a class="button" name='form-submit cancel' href="{crmURL p='civicrm/contact/view' q='action=browse&selectedChild=rel'}">{ts}Done{/ts}</a></div>
         </div>
   {/if}
 
diff --git a/templates/CRM/Contact/Page/Inline/Actions.tpl b/templates/CRM/Contact/Page/Inline/Actions.tpl
index 28bcd24a21..981c0f5c84 100644
--- a/templates/CRM/Contact/Page/Inline/Actions.tpl
+++ b/templates/CRM/Contact/Page/Inline/Actions.tpl
@@ -26,7 +26,7 @@
 {* this template is used for the dropdown menu of the "Actions" button on contacts. *}
 
 <div id="crm-contact-actions-wrapper" data-edit-params='{ldelim}"cid": "{$contactId}", "class_name": "CRM_Contact_Page_Inline_Actions"{rdelim}'>
-  <a id="crm-contact-actions-link" class="button"><span><div class="icon dropdown-icon"></div>{ts}Actions{/ts}</span></a>
+  <a id="crm-contact-actions-link" href="#" class="button"><span><div class="icon dropdown-icon"></div>{ts}Actions{/ts}</span></a>
     <div class="ac_results" id="crm-contact-actions-list">
       <div class="crm-contact-actions-list-inner">
         <div class="crm-contact_activities-list">
@@ -72,7 +72,7 @@
           {foreach from=$actionsMenuList.moreActions item='row'}
           {if $row.href}
           <li class="crm-action-{$row.ref}">
-            <a href="{$row.href}&cid={$contactId}" title="{$row.title}">{$row.title}</a>
+            <a href="{$row.href}&cid={$contactId}" title="{$row.title}" data-tab="{$row.key}">{$row.title}</a>
           </li>
           {/if}
         {/foreach}
@@ -85,17 +85,4 @@
     </div>
   </div>
 {literal}
-<script type="text/javascript">
-
-cj('#crm-contact-actions-list').click(function(event){
-    event.stopPropagation();
-});
-
-cj('#crm-contact-actions-link').click(function(event) {
-  cj('#crm-contact-actions-list').toggle();
-  event.stopPropagation();
-  return false;
-});
-
-</script>
 {/literal}
diff --git a/templates/CRM/Contact/Page/View/Summary.js b/templates/CRM/Contact/Page/View/Summary.js
index 53e8535f5c..7516e68a3e 100644
--- a/templates/CRM/Contact/Page/View/Summary.js
+++ b/templates/CRM/Contact/Page/View/Summary.js
@@ -98,7 +98,7 @@
         CRM.reloadChangeLogTab();
       }
       // Refresh tab contents - Simple logging
-      else if ($('#changeLog').closest('.ui-tabs-panel').data('civicrmCrmSnippet')) {
+      else if ($('#changeLog').closest('.ui-tabs-panel').data('civiCrmSnippet')) {
         $('#changeLog').closest('.ui-tabs-panel').crmSnippet('destroy');
       }
     }
@@ -294,6 +294,28 @@
       $('#tab_log a').click();
       return false;
     });
+    // Handle action links in popup
+    $('#crm-container').on('click', '.crm-contact_actions-list a, .crm-contact_activities-list a', function() {
+      var tabName = $(this).data('tab') || 'activity';
+      var $tab = $('#tab_' + tabName);
+      var $panel = $('#' + $tab.attr('aria-controls'));
+      CRM.loadForm($(this).attr('href'))
+        .on('crmFormSuccess', function() {
+          if ($panel.data('civiCrmSnippet')) {
+            $panel.crmSnippet('refresh');
+          }
+          $('#mainTabContainer').tabs('option', 'active', $tab.prevAll().length);
+        });
+      return false;
+    });
+    // Actions menu
+    $(document).on('click', function(e) {
+      if ($(e.target).is('#crm-contact-actions-link, #crm-contact-actions-link *')) {
+        $('#crm-contact-actions-list').show();
+        return false;
+      }
+      $('#crm-contact-actions-list').hide();
+    });
     $().crmAccordions();
   });
 })(cj);
diff --git a/templates/CRM/Member/Form/Task/Label.js b/templates/CRM/Member/Form/Task/Label.js
index 193614a362..e5f6a3a0a8 100644
--- a/templates/CRM/Member/Form/Task/Label.js
+++ b/templates/CRM/Member/Form/Task/Label.js
@@ -2,16 +2,11 @@ cj(function ($) {
   'use strict';
   $('#per_membership').click(function() {
     if($(this).is(":checked")) {
-      $('#merge_same_address').prop('disabled', true);
-      $('#merge_same_household').prop('disabled', true);
-      $('#merge_same_address').prop('checked', false);
-      $('#merge_same_household').prop('checked', false);
+      $('#merge_same_address, #merge_same_household').prop({disabled: true, checked: false});
     }
     else {
-      $('#merge_same_address').prop('disabled', false);
-      $('#merge_same_household').prop('disabled', false);
+      $('#merge_same_address, #merge_same_household').prop('disabled', false);
     }
   });
 
-
 });
diff --git a/templates/CRM/Tag/Form/Tag.tpl b/templates/CRM/Tag/Form/Tag.tpl
index a34b70458c..4a59e37afb 100644
--- a/templates/CRM/Tag/Form/Tag.tpl
+++ b/templates/CRM/Tag/Form/Tag.tpl
@@ -72,7 +72,7 @@
         tags.push($(this).text());
       });
       // showing count of tags in summary tab
-      CRM.tabHeader.updateCount('#tab_tag', tags.length);
+      CRM.tabHeader && CRM.tabHeader.updateCount('#tab_tag', tags.length);
       // update summary tab
       $("#tags").html(tags.join(', '));
     };
diff --git a/templates/CRM/common/TabHeader.js b/templates/CRM/common/TabHeader.js
index a94d21f38f..0209f4e2f3 100644
--- a/templates/CRM/common/TabHeader.js
+++ b/templates/CRM/common/TabHeader.js
@@ -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') {
@@ -34,11 +34,11 @@ cj(function($) {
             .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')) {
+              if ($(this).attr('href') === '#' || $(this).attr('onclick') || $(this).hasClass('no-popup')) {
                 return;
               }
               CRM.loadForm($(this).attr('href'), {
-                openInline: 'a:not([href="#"])'
+                openInline: 'a:not("[href=#], .no-popup")'
               }).on('crmFormSuccess', function(e, data) {
                   // Refresh when form completes
                   ui.panel.crmSnippet('refresh');
-- 
GitLab