@@ -9,9 +9,6 @@ angular.module('starter').controller('MeasurementAddCtrl', ["$scope", "$q", "$ti
99 showVariableCategorySelector : false ,
1010 showUnits : false ,
1111 unitCategories : [ ] ,
12- variableCategoryName : getVariableCategoryName ( ) ,
13- variableCategoryObject : getVariableCategory ( ) ,
14- helpText : getVariableCategory ( ) . helpText ,
1512 unitAbbreviatedName : '' ,
1613 measurement : { } ,
1714 searchedUnits : [ ] ,
@@ -30,6 +27,16 @@ angular.module('starter').controller('MeasurementAddCtrl', ["$scope", "$q", "$ti
3027 qmService . navBar . showNavigationMenuIfHideUrlParamNotSet ( ) ;
3128 qmService . rootScope . setProperty ( 'bloodPressure' , { systolicValue : null , diastolicValue : null , show : false } ) ;
3229 $scope . state . title = 'Record a Measurement' ;
30+ setupMeasurement ( ) ;
31+ var cat = getVariableCategory ( ) ;
32+ if ( cat ) { setupVariableCategory ( cat ) ; }
33+ } ) ;
34+ $scope . $on ( '$ionicView.enter' , function ( e ) {
35+ qmLogService . debug ( '$ionicView.enter ' + $state . current . name ) ;
36+ qmService . hideLoader ( ) ;
37+ qmLog . info ( "$ionicView.enter $scope.state.measurement is " , $scope . state . measurement ) ;
38+ } ) ;
39+ function setupMeasurement ( ) {
3340 $scope . state . selectedDate = moment ( ) ;
3441 $scope . state . units = qm . unitHelper . getNonAdvancedUnits ( ) ;
3542 var reminderFromUrl = qm . urlHelper . getParam ( 'trackingReminderObject' , window . location . href , true ) ;
@@ -39,33 +46,34 @@ angular.module('starter').controller('MeasurementAddCtrl', ["$scope", "$q", "$ti
3946 var v = $stateParams . variableObject ;
4047 var n = $stateParams . reminderNotification ;
4148 var id = qm . urlHelper . getParam ( 'measurementId' , location . href , true ) ;
42- if ( tr ) {
49+ if ( tr ) {
4350 setupTrackingByReminder ( tr ) ;
44- } else if ( m ) {
51+ } else if ( m ) {
4552 setupTrackingByMeasurement ( m ) ;
46- } else if ( measurementFromUrl ) {
53+ } else if ( measurementFromUrl ) {
4754 setupTrackingByMeasurement ( JSON . parse ( measurementFromUrl ) ) ;
48- } else if ( v ) {
55+ } else if ( v ) {
4956 setupFromVariable ( v ) ;
50- } else if ( reminderFromUrl ) {
57+ } else if ( reminderFromUrl ) {
5158 setupTrackingByReminder ( JSON . parse ( reminderFromUrl ) ) ;
52- } else if ( n ) {
59+ } else if ( n ) {
5360 setupTrackingByReminder ( n ) ;
54- } else if ( id ) {
55- setMeasurementVariablesByMeasurementId ( id ) . then ( function ( ) {
56- if ( ! $scope . state . measurementIsSetup ) { $scope . goBack ( ) ; }
61+ } else if ( id ) {
62+ setMeasurementVariablesByMeasurementId ( id ) . then ( function ( ) {
63+ if ( ! $scope . state . measurementIsSetup ) {
64+ $scope . goBack ( ) ;
65+ }
5766 } ) ;
58- } else if ( $stateParams . variableName ) {
67+ } else if ( $stateParams . variableName ) {
5968 setupFromVariableName ( $stateParams . variableName ) ;
6069 }
61- if ( ! $scope . state . measurementIsSetup ) { setupFromUrlParameters ( ) ; }
62- if ( ! $scope . state . measurementIsSetup ) { setupFromVariable ( qm . getPrimaryOutcomeVariable ( ) ) ; }
63- } ) ;
64- $scope . $on ( '$ionicView.enter' , function ( e ) {
65- qmLogService . debug ( '$ionicView.enter ' + $state . current . name ) ;
66- qmService . hideLoader ( ) ;
67- qmLog . info ( "$ionicView.enter $scope.state.measurement is " , $scope . state . measurement ) ;
68- } ) ;
70+ if ( ! $scope . state . measurementIsSetup ) {
71+ setupFromUrlParameters ( ) ;
72+ }
73+ if ( ! $scope . state . measurementIsSetup ) {
74+ setupFromVariable ( qm . getPrimaryOutcomeVariable ( ) ) ;
75+ }
76+ }
6977 var trackBloodPressure = function ( ) {
7078 if ( ! $rootScope . bloodPressure . diastolicValue || ! $rootScope . bloodPressure . systolicValue ) {
7179 qmService . validationFailure ( 'Please enter both values for blood pressure.' , $scope . state . measurement ) ;
@@ -142,16 +150,20 @@ angular.module('starter').controller('MeasurementAddCtrl', ["$scope", "$q", "$ti
142150 }
143151 } ;
144152 $scope . variableCategorySelectorChange = function ( variableCategoryName ) {
145- var cat = qmService . getVariableCategoryInfo ( variableCategoryName ) ;
153+ var cat = qm . variableCategoryHelper . findVariableCategory ( variableCategoryName ) ;
146154 setupUnit ( cat . defaultUnitAbbreviatedName ) ;
147155 $scope . state . defaultValuePlaceholderText = 'Enter a value' ;
148156 $scope . state . defaultValueLabel = 'Value' ;
149157 setupVariableCategory ( variableCategoryName ) ;
150158 } ;
151159 var setupVariableCategory = function ( variableCategoryName ) {
160+ var cat = getVariableCategory ( variableCategoryName ) ;
152161 qmLogService . debug ( $state . current . name + ': ' + 'variableCategoryName is ' + variableCategoryName ) ;
153- $scope . state . measurement . variableCategoryName = variableCategoryName ;
154- var cat = getVariableCategory ( ) ;
162+ if ( $scope . state . measurement ) {
163+ $scope . state . measurement . variableCategoryName = cat . name ;
164+ }
165+ $scope . state . variableCategoryObject = cat ;
166+ $scope . state . helpText = cat . helpText ;
155167 $scope . state . title = "Add Measurement" ;
156168 $scope . state . measurementSynonymSingularLowercase = cat . measurementSynonymSingularLowercase ;
157169 if ( cat . defaultValueLabel ) { $scope . state . defaultValueLabel = cat . defaultValueLabel ; }
@@ -183,8 +195,9 @@ angular.module('starter').controller('MeasurementAddCtrl', ["$scope", "$q", "$ti
183195 $scope . state . measurement . unitAbbreviatedName = unitAbbreviatedName ;
184196 $scope . state . measurement = qm . unitHelper . updateAllUnitPropertiesOnObject ( unitAbbreviatedName , $scope . state . measurement ) ;
185197 qmLog . info ( "Setting $scope.state.measurement to " , $scope . state . measurement ) ;
198+ qm . unitHelper . setInputType ( $scope . state . measurement ) ;
199+ $scope . state . units = qm . unitHelper . getUnitArrayContaining ( unitAbbreviatedName ) ;
186200 }
187- setupValueFieldType ( unitAbbreviatedName , valence ) ;
188201 }
189202 $scope . selectPrimaryOutcomeVariableValue = function ( $event , newValue ) {
190203 // remove any previous primary outcome variables if present
@@ -245,7 +258,7 @@ angular.module('starter').controller('MeasurementAddCtrl', ["$scope", "$q", "$ti
245258 if ( v . unitAbbreviatedName ) {
246259 setupUnit ( v . unitAbbreviatedName , v . valence ) ;
247260 } else if ( v . variableCategoryName ) {
248- var category = qmService . getVariableCategoryInfo ( v . variableCategoryName ) ;
261+ var category = qm . variableCategoryHelper . findVariableCategory ( v ) ;
249262 setupUnit ( category . defaultUnitAbbreviatedName , v . valence ) ;
250263 }
251264 var m = qm . measurements . newMeasurement ( v ) ;
@@ -297,15 +310,6 @@ angular.module('starter').controller('MeasurementAddCtrl', ["$scope", "$q", "$ti
297310 measurement : $stateParams . measurement
298311 } ) ;
299312 } ;
300- function setupValueFieldType ( unitAbbreviatedName , valence , variableName ) {
301- $scope . state . units = qm . unitHelper . getUnitArrayContaining ( unitAbbreviatedName ) ;
302- //if($scope.state.measurement.inputType){return;} Why is this here? It prevents updating when we change a unit! :(
303- if ( ! unitAbbreviatedName ) {
304- qmLogService . error ( 'No unitAbbreviatedName provided to setupValueFieldType' ) ;
305- return false ;
306- }
307- $scope . state . measurement . inputType = qm . unitHelper . getInputType ( unitAbbreviatedName , valence , variableName ) ;
308- }
309313 function setVariableObjectFromMeasurement ( ) {
310314 $scope . state . variableObject = {
311315 unitAbbreviatedName : $scope . state . measurement . unitAbbreviatedName ,
@@ -398,11 +402,20 @@ angular.module('starter').controller('MeasurementAddCtrl', ["$scope", "$q", "$ti
398402 hideSheet ( ) ;
399403 } , 20000 ) ;
400404 } ) ;
401- function getVariableCategoryName ( object ) {
402- return qm . variableCategoryHelper . getVariableCategoryNameFromStateParamsOrUrl (
403- object , $scope . state . measurement , $stateParams , $stateParams . variableObject ) ;
405+ function getVariableCategoryName ( obj ) {
406+ var cat = getVariableCategory ( obj ) ;
407+ if ( ! cat ) { return null ; }
408+ return cat . name ;
404409 }
405- function getVariableCategory ( ) {
406- return qmService . getVariableCategoryInfo ( getVariableCategoryName ( ) ) ;
410+ function getVariableCategory ( obj ) {
411+ var cat ;
412+ if ( obj ) { cat = qm . variableCategoryHelper . findVariableCategory ( obj ) ; }
413+ if ( ! cat && $scope . state ) { cat = qm . variableCategoryHelper . findVariableCategory ( $scope . state ) ; }
414+ if ( ! cat ) { cat = qm . variableCategoryHelper . findVariableCategory ( $stateParams ) ; }
415+ if ( ! cat ) {
416+ qmLog . debug ( "No variable category name from getVariableCategory" )
417+ return null ;
418+ }
419+ return cat ;
407420 }
408421 } ] ) ;
0 commit comments