Browse Source

Remove empty fields from view when loaded via ajax

Abdullah Almsaeed 6 years ago
parent
commit
b50aac6961
1 changed files with 51 additions and 15 deletions
  1. 51 15
      tripal/theme/js/tripal.js

+ 51 - 15
tripal/theme/js/tripal.js

@@ -1,27 +1,63 @@
-// Using the closure to map jQuery to $. 
+// Using the closure to map jQuery to $.
 (function ($) {
 (function ($) {
   // Store our function as a property of Drupal.behaviors.
   // Store our function as a property of Drupal.behaviors.
   Drupal.behaviors.tripal = {
   Drupal.behaviors.tripal = {
     attach: function (context, settings) {
     attach: function (context, settings) {
 
 
-      $(".tripal-entity-unattached .field-items").replaceWith('<div class="field-items">Loading... <img src="' + tripal_path + '/theme/images/ajax-loader.gif"></div>');
-      $(".tripal-entity-unattached").each(function() {
-        id = $(this).attr('id');
+      $('.tripal-entity-unattached .field-items').replaceWith('<div class="field-items">Loading... <img src="' + tripal_path + '/theme/images/ajax-loader.gif"></div>');
+      $('.tripal-entity-unattached').each(function () {
+        var id = $(this).attr('id');
         if (id) {
         if (id) {
           $.ajax({
           $.ajax({
-            url: baseurl + '/bio_data/ajax/field_attach/' + id,
+            url     : baseurl + '/bio_data/ajax/field_attach/' + id,
             dataType: 'json',
             dataType: 'json',
-            type: 'GET',
-            success: function(data){
+            type    : 'GET',
+            success : function (data) {
               var content = data['content'];
               var content = data['content'];
-              var id = data['id'];
-              $("#" + id + ' .field-items').replaceWith(content);
+              var id      = data['id'];
+              $('#' + id + ' .field-items').replaceWith(content);
+
+              // If the field has no content, check to verify the pane is empty
+              // then remove it.
+              if (content.trim().length === 0) {
+                var field   = $('#' + id);
+                var classes = field.parents('.tripal_pane').first().attr('class').split(' ');
+                var pane_id = null;
+
+                // Remove the field since it's empty
+                field.remove();
+
+                // Get the tripal pane id to remove the pane if it is empty
+                var sub_length = 'tripal_pane-fieldset-'.length;
+                classes.map(function (cls) {
+                  if (cls.indexOf('tripal_pane-fieldset-') > -1) {
+                    pane_id = cls.substring(sub_length, cls.length);
+                  }
+                });
+
+                if (pane_id) {
+                  var pane = $('#' + pane_id);
+
+                  // If the pane has only the title and close button, we can
+                  // remove it
+                  var has_children = $('.tripal_pane-fieldset-' + pane_id)
+                    .first()
+                    .children()
+                    .not('.tripal_pane-fieldset-buttons')
+                    .not('.field-group-format-title')
+                    .not('#' + id).length > 0;
+
+                  if (!has_children) {
+                    pane.remove();
+                  }
+                }
+              }
             }
             }
           });
           });
         }
         }
       });
       });
     }
     }
-  }
+  };
 
 
 })(jQuery);
 })(jQuery);
 
 
@@ -34,11 +70,11 @@ function tripal_navigate_field_pager(id, page) {
   });
   });
 
 
   jQuery.ajax({
   jQuery.ajax({
-    type: "GET",
-    url: Drupal.settings["basePath"] + "bio_data/ajax/field_attach/" + id,
-    data: { 'page' : page },
-    success: function(response) {
-      jQuery("#" + id + ' .field-items').replaceWith(response['content']);
+    type   : 'GET',
+    url    : Drupal.settings['basePath'] + 'bio_data/ajax/field_attach/' + id,
+    data   : {'page': page},
+    success: function (response) {
+      jQuery('#' + id + ' .field-items').replaceWith(response['content']);
     }
     }
   });
   });
 }
 }