← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 4300: save dataValues in localStorage (then upload with ajax if possible), work in progress..

 

------------------------------------------------------------
revno: 4300
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2011-08-15 14:44:50 +0200
message:
  save dataValues in localStorage (then upload with ajax if possible), work in progress..
modified:
  dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/entry.js
  dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/form.js


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/entry.js'
--- dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/entry.js	2011-08-15 10:35:59 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/entry.js	2011-08-15 12:44:50 +0000
@@ -204,6 +204,31 @@
     return false;
 }
 
+$(document).ready(function() {
+    $("#orgUnitTree").one("ouwtLoaded", function() {
+        saveDataValuesInLocalStorage();
+    });
+})
+
+function saveDataValuesInLocalStorage() {
+    var dataValues = storageManager.getAllDataValues();
+
+    for(var dataValueKey in dataValues) {
+        var dataValue = dataValues[dataValueKey];
+        console.log(dataValue);
+
+        $.ajax( {
+            url : "saveValue.action",
+            data : dataValue,
+            dataType : 'json',
+            success : function( json ) {
+                storageManager.clearDataValueJSON( dataValue );
+                console.log("Successfully saved dataValue with value " + dataValue.value);
+            }
+        } );
+    }
+}
+
 // -----------------------------------------------------------------------------
 // Saver objects
 // -----------------------------------------------------------------------------
@@ -222,13 +247,16 @@
 
     this.save = function()
     {
-        storageManager.saveDataValueJSON( dataValue );
+        storageManager.saveDataValue( dataValue );
 
         $.ajax( {
             url : "saveValue.action",
             data : dataValue,
             dataType : 'json',
-            success : handleResponse,
+            success : function( json ) {
+                storageManager.clearDataValueJSON( dataValue );
+                handleResponse( json );
+            },
             error : handleError
         } );
     };

=== modified file 'dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/form.js'
--- dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/form.js	2011-08-15 10:48:36 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/form.js	2011-08-15 12:44:50 +0000
@@ -1005,25 +1005,11 @@
 	/**
 	 * Saves a data value.
 	 * 
-	 * @param dataValue The datavalue and identifiers in json format.
-	 */
-	this.saveDataValueJSON = function( dataValue )
-	{
-	    this.saveDataValue( dataValue.dataElementId, dataValue.optionComboId, dataValue.periodId, dataValue.organisationUnitId, dataValue.value );
-	}
-
-	/**
-	 * Saves a data value.
-	 * 
-	 * @param dataElementId the data element identifier.
-	 * @param categoryOptionComboId the category option combo identifier.
-	 * @param periodId the period identifier.
-	 * @param organisationUnitId the organisation unit identifier.
-	 * @param value the value.
-	 */
-	this.saveDataValue = function( dataElementId, categoryOptionComboId, periodId, organisationUnitId, value )
-	{
-		var id = this.getDataValueIdentifier( dataElementId, categoryOptionComboId, periodId, organisationUnitId );
+     * @param dataValue The datavalue and identifiers in json format.
+	 */
+	this.saveDataValue = function( dataValue )
+	{
+		var id = this.getDataValueIdentifier( dataValue.dataElementId, dataValue.optionComboId, dataValue.periodId, dataValue.organisationUnitId );
 
 		var dataValues = {};
 
@@ -1032,7 +1018,7 @@
 			dataValues = JSON.parse( localStorage[KEY_DATAVALUES] );
 		}
 
-		dataValues[id] = value;
+		dataValues[id] = dataValue;
 
 		try
 		{
@@ -1059,16 +1045,45 @@
 	this.getDataValue = function( dataElementId, categoryOptionComboId, periodId, organisationUnitId )
 	{
 		var id = this.getDataValueIdentifier( dataElementId, categoryOptionComboId, periodId, organisationUnitId );
-		
+
 		if ( localStorage[KEY_DATAVALUES] != null )
 		{
 			var dataValues = JSON.parse( localStorage[KEY_DATAVALUES] );
 			
 			return dataValues[id];
 		}
-		
+
 		return null;
 	}
+
+	/**
+	 * Removes the wanted dataValue from localStorage.
+	 * 
+	 * @param dataValue The datavalue and identifiers in json format.
+	 */
+	this.clearDataValueJSON = function( dataValue )
+	{
+	    this.clearDataValue( dataValue.dataElementId, dataValue.optionComboId, dataValue.periodId, dataValue.organisationUnitId );
+	}
+
+	/**
+     * Removes the wanted dataValue from localStorage.
+     * 
+     * @param dataElementId the data element identifier.
+     * @param categoryOptionComboId the category option combo identifier.
+     * @param periodId the period identifier.
+     * @param organisationUnitId the organisation unit identifier.
+     */
+	this.clearDataValue = function( dataElementId, categoryOptionComboId, periodId, organisationUnitId )
+	{
+	    var id = this.getDataValueIdentifier( dataElementId, categoryOptionComboId, periodId, organisationUnitId );
+	    var dataValues = this.getAllDataValues();
+
+	    console.log("deleting with id " + id);
+	    
+	    delete dataValues[id];
+	    localStorage[KEY_DATAVALUES] = JSON.stringify( dataValues );
+	}
 	
 	/**
 	 * Returns a JSON associative array where the keys are on the form