Browse Source

Revert "Revert "Remove empty fields from view when loaded via ajax""

Bradford Condon 6 years ago
parent
commit
d59e2980c9

+ 10 - 1
tripal/api/tripal.entities.api.inc

@@ -967,7 +967,6 @@ function tripal_update_bundle_field($field_name, $field_info, $entity_type_name,
  * @ingroup tripal_entities_api
  */
 function tripal_get_bundle_variable($variable_name, $bundle_id, $default = FALSE) {
-
   $variable = tripal_get_variable($variable_name);
 
   // Warn if we can't find the tripal_variable.
@@ -1017,6 +1016,16 @@ function tripal_set_bundle_variable($variable_name, $bundle_id, $value) {
         return FALSE;
       }
     }
+    else if ($variable_name === 'ajax_field') {
+      tripal_insert_variable(
+        'ajax_field',
+        'Structure->Tripal Content Type->edit checkbox to allow for ajax fields for that bundle.'
+      );
+      $variable = tripal_get_variable($variable_name);
+      if (!$variable) {
+        return false;
+      }
+    }
     else {
       return FALSE;
     }

+ 31 - 11
tripal/includes/TripalBundleUIController.inc

@@ -219,19 +219,35 @@ function tripal_tripal_bundle_form($form, &$form_state, $entityDataType) {
   else {
     $form['description']['#default_value'] = tripal_get_bundle_variable('description', $bundle->id, '');
   }
-
-  $empty_fields = tripal_get_bundle_variable('hide_empty_field', $bundle->id, '');
+  $hide_empty_field_var = tripal_get_bundle_variable('hide_empty_field', $bundle->id);
+  if ($hide_empty_field_var != 0) {
+    $hide_empty_field_var = TRUE;
+  }
+  $ajax_field_var = tripal_get_bundle_variable('ajax_field', $bundle->id);
+  if ($ajax_field_var != 0) {
+    $ajax_field_var = TRUE;
+  }
   $form['hide_empty_field'] = array(
-    '#type' => 'select',
-    '#title' => t('Field Display'),
-    '#options' => array(
-      'hide' => t('Hide empty fields'),
-      'show' => t('Show empty fields'),
-    ),
-    '#description' => t('Choose either to show or hide all empty fields.  If "Show empty fields" is selected then fields will be loaded via AJAX to help speed page loads.'),
-    '#default_value' => !empty($empty_fields) ? array($empty_fields,) : array('hide',),
+    '#type' => 'checkbox',
+    '#title' => t('Hide empty fields'),
+    '#description' => t('Uncheck this box if you would like to show all empty fields.'),
+    '#default_value' => $hide_empty_field_var,
   );
 
+  $form['ajax_field'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Load field using AJAX'),
+    '#description' => t('Uncheck this box if you do not want field data to load by ajax, this may signifiantly increase page load times.'),
+    '#default_value' => $ajax_field_var,
+  );
+  $form['ajax_field disclaimer'] = array(
+    '#type' => 'item',
+    '#markup' => t(
+      '<p>Please note, if both "Hide empty fields" and "Load field using AJAX" 
+      are checked empty fields will be hidden using javascript which may result
+       in some jumpiness of content as the page finishes loading.</p>'
+    ),
+  );
   $form['additional_settings'] = array(
     '#type' => 'vertical_tabs',
     '#weight' => 99,
@@ -393,7 +409,10 @@ function tripal_tripal_bundle_form_validate($form, $form_state) {
   // VALIDATE: That there is a value passed for the hide_empty_field option. If
   // no value passed default to hide field.
   if(empty($form_state['values']['hide_empty_field'])){
-    $form_state['values']['hide_empty_field'] = 'hide';
+    $form_state['values']['hide_empty_field'] = TRUE;
+  }
+  if (empty($form_state['values']['ajax_field'])) {
+    $form_state['values']['ajax_field'] = TRUE;
   }
   // VALIDATE: The only tokens used should be those we mentioned under "Available Tokens".
   // PART 1: Set Titles.
@@ -464,6 +483,7 @@ function tripal_tripal_bundle_form_submit($form, &$form_state) {
 
     // Save the hide_empty_field setting.
     tripal_set_bundle_variable('hide_empty_field', $bundle->id, $form_state['values']['hide_empty_field']);
+    tripal_set_bundle_variable('ajax_field', $bundle->id, $form_state['values']['ajax_field']);
 
     // Save the page title format.
     tripal_save_title_format(

+ 5 - 4
tripal/includes/TripalEntityController.inc

@@ -636,6 +636,7 @@ class TripalEntityController extends EntityAPIController {
         $info = entity_get_info($queried_entities[$id]->type);
         $field_cache = array_key_exists('field cache', $info) ? $info['field cache'] : FALSE;
         $bundle_name = $queried_entities[$id]->bundle;
+        $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
 
         // Iterate through the field instances and find those that are set to
         // 'auto_attach' and which are attached to this bundle. Add all
@@ -687,13 +688,13 @@ class TripalEntityController extends EntityAPIController {
           // attach then we will ignore it. It can only be set by providing
           // the id in the $field_id array handled previously.
           else {
-            // We only load via AJAX if empty fields are not hidden.
-            $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
-            $hide_variable = tripal_get_bundle_variable('hide_empty_field', $bundle->id, 'hide');
+            // We only load via AJAX if instructed to do so and only for fields
+            // that are not auto attached.
+            $ajax_field = tripal_get_bundle_variable('ajax_field', $bundle->id);
             if (array_key_exists('settings', $instance) and
                 array_key_exists('auto_attach', $instance['settings']) and
                 $instance['settings']['auto_attach'] == FALSE and
-                $hide_variable == 'show') {
+                $ajax_field == TRUE) {
 
                // Add an empty value. This will allow the tripal_entity_view()
                // hook to add the necessary prefixes to the field for ajax

+ 143 - 19
tripal/theme/js/tripal.js

@@ -1,28 +1,152 @@
-// Using the closure to map jQuery to $. 
+// Using the closure to map jQuery to $.
 (function ($) {
   // Store our function as a property of Drupal.behaviors.
   Drupal.behaviors.tripal = {
     attach: function (context, settings) {
+      // If we don't have any settings, this is not a entity page so exit
+      if (!settings.tripal_ds) {
+        return;
+      }
 
-      $(".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');
+      var use_ajax    = parseInt(settings.tripal_ds.tripal_field_settings_ajax) === 1;
+      var hide_fields = settings.tripal_ds.tripal_field_settings_hide !== false;
+
+      if (!use_ajax) {
+        return;
+      }
+
+      $('.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) {
-          $.ajax({
-            url: baseurl + '/bio_data/ajax/field_attach/' + id,
-            dataType: 'json',
-            type: 'GET',
-            success: function(data){
-              var content = data['content'];
-              var id = data['id'];
-              $("#" + id + ' .field-items').replaceWith(content);
-            }
-          });
+          var field = new AjaxField(id, hide_fields);
+          if (hide_fields) {
+            field.hidePaneTitle();
+          }
+          field.load();
         }
       });
     }
+  };
+
+  /**
+   * AjaxField Constructor.
+   *
+   * @param {Number} id
+   * @param {Boolean} hide_fields
+   * @constructor
+   */
+  function AjaxField(id, hide_fields) {
+    this.id          = id;
+    this.hide_fields = hide_fields;
   }
 
+  /**
+   * Hide pane title if the content of the pane has only ajax fields.
+   */
+  AjaxField.prototype.hidePaneTitle = function () {
+    var id      = this.id;
+    var field   = $('#' + id);
+    var classes = field.parents('.tripal_pane').first().attr('class').split(' ');
+    var pane_id = this.extractPaneID(classes);
+
+    if (pane_id) {
+      // Check if the fieldset has children that are not AJAX fields
+      var has_children = $('.tripal_pane-fieldset-' + pane_id)
+        .first()
+        .children()
+        .not('.tripal_pane-fieldset-buttons')
+        .not('.field-group-format-title')
+        .not('.tripal-entity-unattached')
+        .not('#' + id).length > 0;
+
+      // If there are no children, hide the pane title
+      if (!has_children) {
+        $('#' + pane_id).hide(0);
+      }
+    }
+  };
+
+  /**
+   * Load the field's content from the server.
+   */
+  AjaxField.prototype.load = function () {
+    $.ajax({
+      url     : baseurl + '/bio_data/ajax/field_attach/' + this.id,
+      dataType: 'json',
+      type    : 'GET',
+      success : this.handleSuccess.bind(this)
+    });
+  };
+
+  /**
+   * Add the content of the field to its pane.
+   *
+   * @param data
+   */
+  AjaxField.prototype.handleSuccess = function (data) {
+    var content = data['content'];
+    var id      = data['id'];
+    var field   = $('#' + id);
+    var classes = field.parents('.tripal_pane').first().attr('class').split(' ');
+    var pane_id = this.extractPaneID(classes);
+    $('#' + id + ' .field-items').replaceWith(content);
+
+    // Hiding of content is not set
+    if (!this.hide_fields) {
+      return;
+    }
+
+    // If the field has no content, check to verify the pane is empty
+    // then remove it.
+    if (content.trim().length === 0) {
+
+      // Remove the field since it's empty
+      field.remove();
+
+      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();
+        }
+      }
+    }
+    else {
+      if (pane_id) {
+        $('#' + pane_id).show(0);
+      }
+    }
+  };
+
+  /**
+   * Extract the pane id from parent classes.
+   *
+   * @param classes
+   * @return {String|null}
+   */
+  AjaxField.prototype.extractPaneID = function (classes) {
+    var sub_length = 'tripal_pane-fieldset-'.length;
+    var pane_id    = null;
+
+    classes.map(function (cls) {
+      if (cls.indexOf('tripal_pane-fieldset-') > -1) {
+        pane_id = cls.substring(sub_length, cls.length);
+      }
+    });
+
+    return pane_id;
+  };
+
 })(jQuery);
 
 // Used for ajax update of fields by links in a pager.
@@ -34,11 +158,11 @@ function tripal_navigate_field_pager(id, page) {
   });
 
   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']);
     }
   });
 }

+ 22 - 0
tripal/tripal.install

@@ -50,6 +50,10 @@ function tripal_add_variables() {
     'hide_empty_field',
     'Structure->Tripal Content Type->edit checkbox to hide empty fields for that bundle.'
   );
+  tripal_insert_variable(
+    'ajax_field',
+    'Structure->Tripal Content Type->edit checkbox for ajax fields for that bundle.'
+  );
 }
 
 /**
@@ -1209,3 +1213,21 @@ function tripal_update_7312() {
   }
 }
 
+
+/**
+ * Adds a tripal_storage_api setting to all field storage details
+ */
+function tripal_update_7313() {
+  $transaction = db_transaction();
+  try {
+    tripal_insert_variable(
+      'ajax_field',
+      'Structure->Tripal Content Type->edit checkbox ajax fields for that bundle.'
+    );
+  } catch (\PDOException $e) {
+    $transaction->rollback();
+    $error = $e->getMessage();
+    throw new DrupalUpdateException('Could not perform update: ' . $error);
+  }
+}
+

+ 9 - 7
tripal/tripal.module

@@ -1523,9 +1523,10 @@ function tripal_field_display_TripalEntity_alter(&$display, $context){
   $bundle_info = tripal_load_bundle_entity(array('name' => $bundle));
 
   // Hide fields that are empty, but only if the hide_empty_field variable
-  // is set to 'hide' for this bundel.
-  $hide_variable = tripal_get_bundle_variable('hide_empty_field', $bundle_info->id, 'hide');
-  if($hide_variable == 'hide'){
+  // is set to TRUE for this bundle and ajax load is turned off.
+  $hide_variable = tripal_get_bundle_variable('hide_empty_field', $bundle_info->id);
+  $ajax_field = tripal_get_bundle_variable('ajax_field', $bundle_info->id);
+  if($hide_variable === TRUE && $ajax_field === FALSE){
     $item = field_get_items('TripalEntity', $context['entity'], $field_name);
     if($item) {
       $field = field_info_field($field_name);
@@ -1555,10 +1556,11 @@ function tripal_field_group_table_rows_alter(&$element, &$children) {
       $bundle = $element[$child]['#bundle'];
       $bundle_info = tripal_load_bundle_entity(array('name' => $bundle));
 
-      // If the hide empty variable is turned on then remove fields from
-      // the field group.
-      $hide_variable = tripal_get_bundle_variable('hide_empty_field', $bundle_info->id, 'hide');
-      if($hide_variable == 'hide'){
+      // If the hide empty variable is turned on and ajax load is turned off
+      // then remove fields from the field group.
+      $hide_variable = tripal_get_bundle_variable('hide_empty_field', $bundle_info->id);
+      $ajax_field = tripal_get_bundle_variable('ajax_field', $bundle_info->id);
+      if($hide_variable == TRUE && $ajax_field == FALSE){
         $items = $element[$child]['#items'];
         // Case #1: there are no items.
         if (count($items) == 0) {

+ 4 - 2
tripal_ds/tripal_ds.module

@@ -467,9 +467,10 @@ function tripal_ds_field_display_alter(&$display, $context){
     $field_name = $context['field']['field_name'];
     $bundle = $context['entity']->bundle;
     $bundle_info = tripal_load_bundle_entity(array('name' => $bundle));
-    $hide_variable = tripal_get_bundle_variable('hide_empty_field', $bundle_info->id, 'hide');
+    $hide_variable = tripal_get_bundle_variable('hide_empty_field', $bundle_info->id);
+    $ajax_variable = tripal_get_bundle_variable('ajax_field', $bundle_info->id);
 
-    if ($field_name && ($hide_variable == 'hide')) {
+    if ($field_name && ($hide_variable == TRUE) && ($ajax_variable == FALSE)) {
       $item = field_get_items('TripalEntity', $context['entity'], $field_name);
       $field = field_info_field($field_name);
       if ($item) {
@@ -486,6 +487,7 @@ function tripal_ds_field_display_alter(&$display, $context){
         }
       }
     }
+    drupal_add_js(array('tripal_ds' => array('tripal_field_settings_hide' => $hide_variable, 'tripal_field_settings_ajax' => $ajax_variable)), 'setting');
   }
 }
 

+ 24 - 8
tripal_ws/includes/TripalWebService/TripalContentService_v0_1.inc

@@ -248,8 +248,13 @@ class TripalContentService_v0_1 extends TripalWebService {
 
     // If the entity is set to hide fields that have no values then we
     // want to honor that in the web services too.
-    $hide_fields = tripal_get_bundle_variable('hide_empty_field', $bundle->id, 'hide');
-
+    $hide_fields = tripal_get_bundle_variable('hide_empty_field', $bundle->id);
+    $ajax_field = tripal_get_bundle_variable('ajax_field', $bundle_info->id);
+    // If ajax is turned off the hide fields functionality will be handled by 
+    // javascript so change the $hide_fields variable to false to avoid any actions.
+    if ($ajax_field == TRUE) {
+      $hide_fields = FALSE;
+    }
     // Get information about the fields attached to this bundle and sort them
     // in the order they were set for the display.
     $instances = field_info_instances('TripalEntity', $bundle->name);
@@ -320,7 +325,7 @@ class TripalContentService_v0_1 extends TripalWebService {
           $this->addResourceProperty($resource, $term, $service_path . '/' . $entity->id . '/' . urlencode($term['name']), array('lowercase', 'spacing'));
         }
         else {
-          if ($hide_fields == 'show') {
+          if ($hide_fields == FALSE) {
             $this->addResourceProperty($resource, $term, NULL, array('lowercase', 'spacing'));
           }
         }
@@ -340,7 +345,13 @@ class TripalContentService_v0_1 extends TripalWebService {
 
     // If the entity is set to hide fields that have no values then we
     // want to honor that in the web services too.
-    $hide_fields = tripal_get_bundle_variable('hide_empty_field', $bundle->id, 'hide');
+    $hide_fields = tripal_get_bundle_variable('hide_empty_field', $bundle->id);
+    $ajax_field = tripal_get_bundle_variable('ajax_field', $bundle_info->id);
+    // If ajax is turned off the hide fields functionality will be handled by 
+    // javascript so change the $hide_fields variable to false to avoid any actions.
+    if ($ajax_field == TRUE) {
+      $hide_fields = FALSE;
+    }
 
     // Get the field  settings.
     $field_name = $field['field_name'];
@@ -371,7 +382,7 @@ class TripalContentService_v0_1 extends TripalWebService {
       }
     }
 
-    if ($hide_fields == 'hide' and empty($values[0])) {
+    if ($hide_fields == TRUE and empty($values[0])) {
       return;
     }
 
@@ -436,8 +447,13 @@ class TripalContentService_v0_1 extends TripalWebService {
 
     // If the entity is set to hide fields that have no values then we
     // want to honor that in the web services too.
-    $hide_fields = tripal_get_bundle_variable('hide_empty_field', $bundle->id, 'hide');
-
+    $hide_fields = tripal_get_bundle_variable('hide_empty_field', $bundle->id);
+    $ajax_field = tripal_get_bundle_variable('ajax_field', $bundle_info->id);
+    // If ajax is turned off the hide fields functionality will be handled by 
+    // javascript so change the $hide_fields variable to false to avoid any actions.
+    if ($ajax_field == TRUE) {
+      $hide_fields = FALSE;
+    }
     $new_value = '';
     // If the value is an array rather than a scalar then map the sub elements
     // to controlled vocabulary terms.
@@ -446,7 +462,7 @@ class TripalContentService_v0_1 extends TripalWebService {
       foreach ($value as $k => $v) {
 
         // exclude fields that have no values so we can hide them
-        if (!isset($v) and $hide_fields == 'hide') {
+        if (!isset($v) and $hide_fields == TRUE) {
           continue;
         }