CRM-13863 - Allow sorting to happen through ajax
authorColeman Watts <coleman@civicrm.org>
Thu, 12 Dec 2013 22:13:29 +0000 (14:13 -0800)
committerColeman Watts <coleman@civicrm.org>
Thu, 12 Dec 2013 22:13:29 +0000 (14:13 -0800)
CRM/Utils/Weight.php
js/Common.js

index a5b240216694300b6d394af0ee6a5315644d299d..f68a6bd1f88b03168a815f99f33550211d7619c2 100644 (file)
@@ -368,10 +368,10 @@ class CRM_Utils_Weight {
 
       if ($prevID != 0) {
         $alt = ts('Move to top');
-        $links[] = "<a href=\"{$url}&dst={$firstID}&dir=first\"><img src=\"{$imageURL}/first.gif\" title=\"$alt\" alt=\"$alt\" class=\"order-icon\"></a>";
+        $links[] = "<a class=\"crm-weight-arrow\" href=\"{$url}&dst={$firstID}&dir=first\"><img src=\"{$imageURL}/first.gif\" title=\"$alt\" alt=\"$alt\" class=\"order-icon\"></a>";
 
         $alt = ts('Move up one row');
-        $links[] = "<a href=\"{$url}&dst={$prevID}&dir=swap\"><img src=\"{$imageURL}/up.gif\" title=\"$alt\" alt=\"$alt\" class=\"order-icon\"></a>";
+        $links[] = "<a class=\"crm-weight-arrow\" href=\"{$url}&dst={$prevID}&dir=swap\"><img src=\"{$imageURL}/up.gif\" title=\"$alt\" alt=\"$alt\" class=\"order-icon\"></a>";
       }
       else {
         $links[] = "<img src=\"{$imageURL}/spacer.gif\" class=\"order-icon\">";
@@ -380,10 +380,10 @@ class CRM_Utils_Weight {
 
       if ($nextID != 0) {
         $alt = ts('Move down one row');
-        $links[] = "<a href=\"{$url}&dst={$nextID}&dir=swap\"><img src=\"{$imageURL}/down.gif\" title=\"$alt\" alt=\"$alt\" class=\"order-icon\"></a>";
+        $links[] = "<a class=\"crm-weight-arrow\" href=\"{$url}&dst={$nextID}&dir=swap\"><img src=\"{$imageURL}/down.gif\" title=\"$alt\" alt=\"$alt\" class=\"order-icon\"></a>";
 
         $alt = ts('Move to bottom');
-        $links[] = "<a href=\"{$url}&dst={$lastID}&dir=last\"><img src=\"{$imageURL}/last.gif\" title=\"$alt\" alt=\"$alt\" class=\"order-icon\"></a>";
+        $links[] = "<a class=\"crm-weight-arrow\" href=\"{$url}&dst={$lastID}&dir=last\"><img src=\"{$imageURL}/last.gif\" title=\"$alt\" alt=\"$alt\" class=\"order-icon\"></a>";
       }
       else {
         $links[] = "<img src=\"{$imageURL}/spacer.gif\" class=\"order-icon\">";
@@ -415,7 +415,7 @@ class CRM_Utils_Weight {
     $srcWeight = CRM_Core_DAO::getFieldValue($daoName, $src, 'weight', $idName);
     $dstWeight = CRM_Core_DAO::getFieldValue($daoName, $dst, 'weight', $idName);
     if ($srcWeight == $dstWeight) {
-      CRM_Utils_System::redirect($url);
+      self::fixOrderOutput($url);
     }
 
     $tableName = $object->tableName();
@@ -455,7 +455,17 @@ class CRM_Utils_Weight {
       CRM_Core_DAO::executeQuery($query, $params);
     }
 
-    CRM_Utils_System::redirect($url);
+    self::fixOrderOutput($url);
+  }
+  
+  static function fixOrderOutput($url) {
+    if (empty($_GET['snippet']) || $_GET['snippet'] !== 'json') {
+      CRM_Utils_System::redirect($url);
+    }
+
+    CRM_Core_Page_AJAX::returnJsonResponse(array(
+      'userContext' => $url,
+    ));
   }
 }
 
index 27aa8aab0f98082177203a117189eac2cbab2b42..68c8190af293271ca04a9f173d7e1eca6fcf4e20 100644 (file)
@@ -854,6 +854,7 @@ CRM.validate = CRM.validate || {
       if (!this.element.is('.crm-container *')) {
         this.element.addClass('crm-container');
       }
+      this._handleOrderLinks();
       this.options.url ? this.refresh() : this.options.url = document.location.href;
       this._originalUrl = this.options.url;
     },
@@ -869,17 +870,30 @@ CRM.validate = CRM.validate || {
       }
       return url;
     },
+    _handleOrderLinks: function() {
+      var that = this;
+      $('a.crm-weight-arrow', that.element).click(function(e) {
+        that.options.block && that.element.block();
+        $.getJSON(that._formatUrl(this.href)).done(function() {
+          that.refresh();
+        });
+        e.stopImmediatePropagation();
+        return false;
+      });
+    },
     refresh: function() {
       var that = this;
       var url = this._formatUrl(this.options.url);
-      this.options.block && this.element.block();
+      this.options.block && $('.blockOverlay', this.element).length < 1 && this.element.block();
       $.getJSON(url, function(data) {
         if (typeof(data) != 'object' || typeof(data.content) != 'string') {
           that._onFailure(data);
           return;
         }
         data.url = url;
-        that.element.html(data.content).trigger('crmLoad', data);
+        that.element.html(data.content);
+        that._handleOrderLinks();
+        that.element.trigger('crmLoad', data);
         that.options.crmForm && that.element.trigger('crmFormLoad', data);
       }).fail(function() {
           that._onFailure();