Browse Source

Chado Node API: Added messages to indicate when changes have been made reminding the user to save

Lacey Sanderson 10 years ago
parent
commit
060340d180

+ 17 - 2
tripal_core/api/tripal_core.chado_nodes.dbxrefs.api.inc

@@ -205,6 +205,20 @@ function chado_add_node_form_dbxrefs(&$form, &$form_state, $details) {
     '#theme' => 'chado_node_additional_dbxrefs_form_table'
   );
 
+  // We need to provide feedback to the user that changes made
+  // are not saved until the node is saved.
+  $form['addtl_dbxrefs']['dbxref_table']['save_warning'] = array(
+    '#type' => 'markup',
+    '#prefix' => '<div id="dbxref-save-warning" class="messages warning" style="display:none;">',
+    '#suffix' => '</div>',
+    '#markup' => '* The changes to these references will not be saved until the "Save" button at the bottom of this form is clicked. <span class="specific-changes"></span>',
+    '#attached' => array(
+      'js' => array(
+        'chado-node-api-unsaved' => drupal_get_path('module', 'tripal_core') . '/theme/js/chadoNodeApi_unsavedNotify.js',
+      ),
+    ),
+  );
+
   /* DBxrefs can come to us in two ways:
    *
    * 1) In the form state in the $form_state['chado_additional_dbxrefs']. Data is in this field
@@ -311,7 +325,8 @@ function chado_add_node_form_dbxrefs(&$form, &$form_state, $details) {
 
       $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$dbxref->dbxref_id]['db'] = array(
         '#type' => 'markup',
-        '#markup' => $dbxref->db_name
+        '#markup' => $dbxref->db_name,
+        '#prefix' => '<span class="row-unsaved-warning"></span>'
       );
 
       $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$dbxref->dbxref_id]['dbxref_version'] = array(
@@ -594,7 +609,7 @@ function theme_chado_add_node_form_dbxrefs_table($variables) {
     }
   }
 
-  return theme('table', array(
+  return render($element['save_warning']) . theme('table', array(
     'header' => $header,
     'rows' => $rows
   ));

+ 18 - 2
tripal_core/api/tripal_core.chado_nodes.properties.api.inc

@@ -357,6 +357,21 @@ function chado_add_node_form_properties(&$form, &$form_state, $details) {
     '#theme' => 'chado_node_properties_form_table'
   );
 
+  // We need to provide feedback to the user that changes made
+  // are not saved until the node is saved.
+  $form['properties']['property_table']['save_warning'] = array(
+    '#type' => 'markup',
+    '#prefix' => '<div id="property-save-warning" class="messages warning" style="display:none;">',
+    '#suffix' => '</div>',
+    '#markup' => '* The changes to these properties will not be saved until the 
+      "Save" button at the bottom of this form is clicked. <span class="specific-changes"></span>',
+    '#attached' => array(
+      'js' => array(
+        'chado-node-api-unsaved' => drupal_get_path('module', 'tripal_core') . '/theme/js/chadoNodeApi_unsavedNotify.js',
+      ),
+    ),
+  );
+
   // Add defaults into form_state to be used elsewhere
   $form['properties']['property_table']['details'] = array(
     '#type' => 'hidden',
@@ -500,7 +515,8 @@ function chado_add_node_form_properties(&$form, &$form_state, $details) {
 
       $form['properties']['property_table'][$property->type_id][$property->property_id]['type'] = array(
         '#type' => 'markup',
-        '#markup' => $property->type_name
+        '#markup' => $property->type_name,
+        '#prefix' => '<span class="row-unsaved-warning"></span>'
       );
       // If a definition is available we want to add that to the type column
       // to make it easier for users to determine what an added property means.
@@ -808,7 +824,7 @@ function theme_chado_add_node_form_properties($variables) {
     }
   }
 
-  return theme('table', array(
+  return render($element['save_warning']) . theme('table', array(
     'header' => $header,
     'rows' => $rows
   ));

+ 17 - 2
tripal_core/api/tripal_core.chado_nodes.relationships.api.inc

@@ -330,6 +330,20 @@ function chado_add_node_form_relationships(&$form, &$form_state, $details) {
     '#value' => serialize($details)
   );
 
+  // We need to provide feedback to the user that changes made
+  // are not saved until the node is saved.
+  $form['relationships']['relationship_table']['save_warning'] = array(
+    '#type' => 'markup',
+    '#prefix' => '<div id="relationship-save-warning" class="messages warning" style="display:none;">',
+    '#suffix' => '</div>',
+    '#markup' => '* The changes to these relationships will not be saved until the "Save" button at the bottom of this form is clicked. <span class="specific-changes"></span>',
+    '#attached' => array(
+      'js' => array(
+        'chado-node-api-unsaved' => drupal_get_path('module', 'tripal_core') . '/theme/js/chadoNodeApi_unsavedNotify.js',
+      ),
+    ),
+  );
+
   // Add relationships already attached to the node
   //---------------------------------------------
   /* Relationships can come to us in two ways:
@@ -456,7 +470,8 @@ function chado_add_node_form_relationships(&$form, &$form_state, $details) {
 
       $form['relationships']['relationship_table'][$relationship->type_id][$relationship->relationship_id]['subject_name'] = array(
         '#type' => 'markup',
-        '#markup' => $relationship->subject_name
+        '#markup' => $relationship->subject_name,
+        '#prefix' => '<span class="row-unsaved-warning"></span>'
       );
 
       $form['relationships']['relationship_table'][$relationship->type_id][$relationship->relationship_id]['rank'] = array(
@@ -876,7 +891,7 @@ function theme_chado_add_node_form_relationships_table($variables) {
     }
   }
 
-  return theme('table', array(
+  return render($element['save_warning']) . theme('table', array(
     'header' => $header,
     'rows' => $rows,
     'sticky' => FALSE

+ 5 - 1
tripal_core/theme/css/chado_node_api.css

@@ -8,4 +8,8 @@
   color: #680000;
   font-style: italic;
   font-weight: bold;
-}
+}
+tr.unsaved td {
+  background-color: #FFB;
+  color: #840;
+}

+ 82 - 0
tripal_core/theme/js/chadoNodeApi_unsavedNotify.js

@@ -0,0 +1,82 @@
+
+(function ($) {
+
+  Drupal.behaviors.chadoNodeApiChangeNotify = {
+    attach: function (context) {
+
+    ChadoNodeApi_notifyChanges({
+      machineName: {
+        plural: 'properties',
+        singular:'property'
+      },
+      readableName: {
+        plural: 'properties',
+        singular:'property'
+      }
+    });
+
+    ChadoNodeApi_notifyChanges({
+        machineName: {
+          plural: 'dbxrefs',
+          singular:'dbxref'
+        },
+        readableName: {
+          plural: 'references',
+          singular:'reference'
+        }
+      });
+
+    ChadoNodeApi_notifyChanges({
+        machineName: {
+          plural: 'relationships',
+          singular:'relationship'
+        },
+        readableName: {
+          plural: 'relationships',
+          singular:'relationship'
+        }
+      });
+    
+    function ChadoNodeApi_notifyChanges(api) {
+
+      var numCurrent = $('tr.' + api.machineName.singular).length;
+      var numOriginal = $('input.num-' + api.machineName.plural, context).val();
+      var numSaved = $('tr.saved.' + api.machineName.singular).length;
+      var numUnsaved = $('tr.unsaved.' + api.machineName.singular).length;
+      var numRemoved = numOriginal - numSaved;
+
+      // If changes have been made then notify the user.
+      if (numUnsaved > 0 || numRemoved > 0) {
+    	// Make the warning visible.
+    	$('#' + api.machineName.singular + '-save-warning').css("display","inherit");
+
+    	// Determine singular versus plural.
+    	var unsavedReadable = api.readableName.plural;
+        if (numUnsaved == 1) {
+          unsavedReadable = api.readableName.singular;
+        }
+        var removedReadable = api.readableName.plural;
+        if (numRemoved == 1) {
+          removedReadable = api.readableName.singular;
+        }
+        
+    	// Specify the changes made in the warning.
+    	var note = '';
+    	if (numUnsaved > 0 && numRemoved > 0) {
+    		note = 'NOTE: Changes include the addition of ' + numUnsaved + ' ' + unsavedReadable + ' and the removal of ' + numRemoved + ' saved ' + removedReadable + '.';
+    	}
+    	else if (numUnsaved > 0) {
+    		note = 'NOTE: Changes include the addition of ' + numUnsaved + ' ' + unsavedReadable + '.';
+    	}
+    	else if (numRemoved > 0) {
+    		note = 'NOTE: Changes include the removal of ' + numRemoved + ' saved ' + removedReadable + '.';
+    	}
+    	$('#' + api.machineName.singular + '-save-warning span.specific-changes').html(note);
+    	
+    	// Add a * to any new records to make the warning more accessible.
+    	$('tr.unsaved.' + api.machineName.singular + ' span.row-unsaved-warning').html('*');
+
+      }
+    }
+  }};
+})(jQuery);