← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20437: Moved most file entry logic from form.js to separate file entry.fileresource.js

 

------------------------------------------------------------
revno: 20437
committer: Halvdan Hoem Grelland <halvdanhg@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2015-10-01 15:22:12 +0200
message:
  Moved most file entry logic from form.js to separate file entry.fileresource.js
added:
  dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/entry.fileresource.js
modified:
  dhis-2/dhis-web/dhis-web-dataentry/src/main/resources/struts.xml
  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/style/dhis-web-dataentry.css


--
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/resources/struts.xml'
--- dhis-2/dhis-web/dhis-web-dataentry/src/main/resources/struts.xml	2015-09-17 05:52:20 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/resources/struts.xml	2015-10-01 13:22:12 +0000
@@ -13,7 +13,7 @@
       <result name="success" type="velocity">/main.vm</result>
       <param name="page">/dhis-web-dataentry/select.vm</param>
       <param name="menu">/dhis-web-dataentry/menu.vm</param>
-      <param name="javascripts">../dhis-web-commons/ouwt/ouwt.js,javascript/form.js,javascript/entry.js,javascript/history.js</param>
+      <param name="javascripts">../dhis-web-commons/ouwt/ouwt.js,javascript/form.js,javascript/entry.js,javascript/history.js,javascript/entry.fileresource.js</param>
       <param name="stylesheets">style/dhis-web-dataentry.css</param>
       <param name="manifest">../dhis-web-commons/cacheManifest.action</param>
     </action>

=== added file 'dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/entry.fileresource.js'
--- dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/entry.fileresource.js	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/entry.fileresource.js	2015-10-01 13:22:12 +0000
@@ -0,0 +1,175 @@
+( function ( $ ) {
+    $.fn.fileResourceEntryField = function() {
+
+        // TODO Use i18n
+        // TODO Disable field when offline
+        // TODO Re-init on period change
+
+        var $container = $( this );
+
+        var $field = $container.find( '.entryfileresource' );
+        var $button = $container.find( '.upload-button' );
+
+        var $fileInput = $container.find( 'input[type=file]' );
+
+        var $fileinfo = $container.find( '.upload-fileinfo' );
+        var $fileinfoName = $fileinfo.find( '.upload-fileinfo-name' );
+        var $fileinfoSize = $fileinfo.find( '.upload-fileinfo-size' );
+
+        var $progress = $container.find( '.upload-progress' );
+        var $progressBar = $progress.find( '.upload-progress-bar' );
+        var $progressInfo = $progress.find( '.upload-progress-info' );
+
+        var id = $field.attr( 'id' );
+
+        var split = dhis2.de.splitFieldId( id );
+
+        var dataElementId = split.dataElementId;
+        var optionComboId = split.optionComboId;
+        var orgUnitid = dhis2.de.currentOrganisationUnitId;
+        var periodId = $( '#selectedPeriodId' ).val();
+
+        var formData = {
+            'de': dataElementId,
+            'co': optionComboId,
+            'ou': orgUnitid,
+            'pe': periodId
+        };
+
+        // Logic
+        var deleteFileDataValue = function() {
+            var postData = formData;
+            postData.value = '';
+
+            $.ajax( {
+                url: '../api/dataValues',
+                type: 'POST',
+                dataType: 'json',
+                data: postData,
+                success: function()
+                {
+                    $fileinfoName.text( '' );
+                    $fileinfoSize.text( '' );
+                    $fileinfo.hide();
+                    $field.css( 'background-color', '' );
+                    setButtonUpload();
+                },
+                error: function( data )
+                {
+                    console.log( data.errorThrown );
+                }
+            } );
+        };
+
+        var setButtonDelete = function() {
+            $button.button( {
+                text: false,
+                icons: {
+                    primary: 'fa fa-trash-o'
+                }
+            } );
+            $button.unbind( 'click' );
+            $button.on( 'click', function() {
+                $( '#fileDeleteConfirmationDialog' ).dialog( {
+                    title: 'Confirm deletion',
+                    resizable: false,
+                    height: 140,
+                    modal: true,
+                    buttons: {
+                        'Delete': function() {
+                            deleteFileDataValue();
+                            $( this ).dialog( 'close' );
+                        },
+                        Cancel: function() {
+                            $( this ).dialog( 'close' );
+                        }
+                    }
+                } );
+            } );
+            $button.button( 'enable' );
+        };
+
+        var setButtonUpload = function() {
+            $button.button( {
+                text: false,
+                icons: {
+                    primary: 'fa fa-upload'
+                }
+            } );
+            $button.unbind( 'click' );
+            $button.on( 'click', function()
+            {
+                $fileInput.click();
+            } );
+            $button.button( 'enable' );
+        };
+
+        var resetAndHideProgress = function() {
+            $progressBar.toggleClass( 'upload-progress-bar-complete', true );
+            $progressBar.css( 'width', 0 );
+            $progress.hide();
+        };
+
+        var onFileDataValueSavedSuccess = function( fileResource ) {
+            var name = fileResource.name, size = '(' + filesize( fileResource.contentLength ) + ')';
+
+            $fileinfoName.text( name );
+            $fileinfoSize.text( size );
+            $progressBar.toggleClass( 'upload-progress-bar-complete' );
+            $fileinfo.show();
+            resetAndHideProgress();
+            setButtonDelete();
+            $button.button( 'enable' );
+        };
+
+        // Button setup
+        $button.button( {
+            text: false,
+            icons: {
+                primary: 'fa fa-ban'
+            }
+        } );
+        $button.button( 'disable' );
+
+        $( document ).on( dhis2.de.event.dataValuesLoaded, function() {
+            ( typeof( $field.data( 'value' ) ) == 'undefined' ) ? setButtonUpload() : setButtonDelete();
+        } );
+
+        // Initialize file uploader
+        $fileInput.fileupload( {
+            url: '../api/dataValues/files',
+            paramName: 'file',
+            multipart: true,
+            replaceFileInput: false,
+            progressInterval: 250, /* ms */
+            formData: formData,
+            start: function( e )
+            {
+                $button.button( 'disable' );
+                $progressBar.toggleClass( 'upload-progress-bar-complete', false );
+                $fileinfo.hide();
+                $progress.show();
+            },
+            progress: function( e, data )
+            {
+                var percent = parseInt( data.loaded / data.total * 100, 10 );
+                $progressBar.css( 'width', percent + '%' );
+                $progressInfo.text( percent + '%' );
+            },
+            fail: function( e, data )
+            {
+                setHeaderDelayMessage( "File upload failed!" );
+                console.log( data.errorThrown );
+                setButtonUpload();
+            },
+            done: function( e, data )
+            {
+                var fileResource = data.result.response.fileResource;
+
+                saveFileResource( dataElementId, optionComboId, id, fileResource, function() {
+                    onFileDataValueSavedSuccess( fileResource );
+                } );
+            }
+        } );
+    };
+} )( jQuery );

=== 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	2015-10-01 12:35:14 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/form.js	2015-10-01 13:22:12 +0000
@@ -641,176 +641,7 @@
 
     $( '.entryfileresource-container' ).each( function()
     {
-        // TODO Use i18n
-        // TODO Disable when offline
-
-        // Object refs
-        var $container = $( this );
-        var $field = $container.find( '.entryfileresource' );
-        var $button = $container.find( '.upload-button' );
-
-        var $fileInput = $container.find( 'input[type=file]' );
-
-        var $fileinfo = $container.find( '.upload-fileinfo' );
-        var $fileinfoName = $fileinfo.find( '.upload-fileinfo-name' );
-        var $fileinfoSize = $fileinfo.find( '.upload-fileinfo-size' );
-
-        var $progress = $container.find( '.upload-progress' );
-        var $progressBar = $progress.find( '.upload-progress-bar' );
-        var $progressInfo = $progress.find( '.upload-progress-info' );
-
-        // IDs
-        var id = $field.attr( 'id' );
-
-        var split = dhis2.de.splitFieldId( id );
-
-        var dataElementId = split.dataElementId;
-        var optionComboId = split.optionComboId;
-        var orgUnitid = dhis2.de.currentOrganisationUnitId;
-        var periodId = $( '#selectedPeriodId' ).val();
-
-        var formData = {
-            'de': dataElementId,
-            'co': optionComboId,
-            'ou': orgUnitid,
-            'pe': periodId
-        };
-
-        // Functions
-        var deleteFileDataValue = function() {
-            var postData = formData;
-            postData.value = '';
-
-            $.ajax( {
-                url: '../api/dataValues',
-                type: 'POST',
-                dataType: 'json',
-                data: postData,
-                success: function()
-                {
-                    $fileinfoName.text( '' );
-                    $fileinfoSize.text( '' );
-                    $fileinfo.hide();
-                    $field.css( 'background-color', '' );
-                    setButtonUpload();
-                },
-                error: function( data )
-                {
-                    console.log( data.errorThrown );
-                }
-            } );
-        };
-
-        var setButtonDelete = function() {
-            $button.button( {
-                text: false,
-                icons: {
-                    primary: 'fa fa-trash-o'
-                }
-            } );
-            $button.unbind( 'click' );
-            $button.on( 'click', function() {
-                $( '#fileDeleteConfirmationDialog' ).dialog( {
-                    title: 'Confirm deletion',
-                    resizable: false,
-                    height: 140,
-                    modal: true,
-                    buttons: {
-                        'Delete': function() {
-                            deleteFileDataValue();
-                            $( this ).dialog( 'close' );
-                        },
-                        Cancel: function() {
-                            $( this ).dialog( 'close' );
-                        }
-                    }
-                } );
-            } );
-            $button.button( 'enable' );
-        };
-
-        var setButtonUpload = function() {
-            $button.button( {
-                text: false,
-                icons: {
-                    primary: 'fa fa-upload'
-                }
-            } );
-            $button.unbind( 'click' );
-            $button.on( 'click', function()
-            {
-                $fileInput.click();
-            } );
-            $button.button( 'enable' );
-        };
-
-        var resetAndHideProgress = function() {
-            $progressBar.toggleClass( 'upload-progress-bar-complete', true );
-            $progressBar.css( 'width', 0 );
-            $progress.hide();
-        };
-
-        var onFileDataValueSavedSuccess = function( fileResource ) {
-            var name = fileResource.name, size = '(' + filesize( fileResource.contentLength ) + ')';
-
-            $fileinfoName.text( name );
-            $fileinfoSize.text( size );
-            $progressBar.toggleClass( 'upload-progress-bar-complete' );
-            $fileinfo.show();
-            resetAndHideProgress();
-            setButtonDelete();
-            $button.button( 'enable' );
-        };
-
-        // Initialize button
-        $button.button( {
-            text: false,
-            icons: {
-                primary: 'fa fa-ban'
-            }
-        } );
-        $button.button( 'disable' );
-
-        $( document ).on( dhis2.de.event.dataValuesLoaded, function() {
-            ( typeof( $field.data( 'value' ) ) == 'undefined' ) ? setButtonUpload() : setButtonDelete();
-        } );
-
-        // Initialize file uploader
-        $fileInput.fileupload( {
-            url: '../api/dataValues/files',
-            paramName: 'file',
-            multipart: true,
-            replaceFileInput: false,
-            progressInterval: 250, /* ms */
-            formData: formData,
-            start: function( e )
-            {
-                $button.button( 'disable' );
-                $progressBar.toggleClass( 'upload-progress-bar-complete', false );
-                $fileinfo.hide();
-                $progress.show();
-            },
-            progress: function( e, data )
-            {
-                var percent = parseInt( data.loaded / data.total * 100, 10 );
-                $progressBar.css( 'width', percent + '%' );
-                $progressInfo.text( percent + '%' );
-            },
-            fail: function( e, data )
-            {
-                setHeaderDelayMessage( "File upload failed!" );
-                console.log( data.errorThrown );
-                setButtonUpload();
-            },
-            done: function( e, data )
-            {
-                var fileResource = data.result.response.fileResource;
-
-                saveFileResource( dataElementId, optionComboId, id, fileResource, function() {
-                    onFileDataValueSavedSuccess( fileResource );
-                } );
-            }
-        } );
+        $( this ).fileResourceEntryField();
     } );
 }
 

=== modified file 'dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/style/dhis-web-dataentry.css'
--- dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/style/dhis-web-dataentry.css	2015-10-01 12:35:14 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/style/dhis-web-dataentry.css	2015-10-01 13:22:12 +0000
@@ -316,7 +316,6 @@
 
 .upload-fileinfo-size
 {
-    /*width: 80px;*/
     float: right;
     text-align: right;
     padding-left: 3px;
@@ -366,14 +365,14 @@
     vertical-align: middle;
 }
 
+#fileDeleteConfirmationDialog .fa
+{
+    margin-right: 5px;
+}
+
 .confirm-dialog-content
 {
     display: inline-block;
     position: absolute;
     top: 16px;
-}
-
-#fileDeleteConfirmationDialog .fa
-{
-    margin-right: 5px;
 }
\ No newline at end of file