From 270662478a47c9930bfcbd84f3a2c02eccb19eff Mon Sep 17 00:00:00 2001 From: "Mike P. Sinn" Date: Sun, 24 May 2020 18:34:51 -0500 Subject: [PATCH 1/2] getVariableCategoryName --- src/js/controllers/favoritesCtrl.js | 14 +++++---- .../controllers/historyAllMeasurementsCtrl.js | 12 ++------ src/js/controllers/measurementAddCtrl.js | 11 ++----- src/js/controllers/reminderAddCtrl.js | 21 +++++++------- src/js/controllers/remindersInboxCtrl.js | 7 +---- src/js/controllers/remindersManageCtrl.js | 15 +++++----- src/js/controllers/variableSearchCtrl.js | 10 +++---- src/js/qmHelpers.js | 29 ++++++++++++++++++- src/js/services/qmService.js | 15 ++-------- 9 files changed, 66 insertions(+), 68 deletions(-) diff --git a/src/js/controllers/favoritesCtrl.js b/src/js/controllers/favoritesCtrl.js index f2523c2f86..39de8def45 100644 --- a/src/js/controllers/favoritesCtrl.js +++ b/src/js/controllers/favoritesCtrl.js @@ -25,15 +25,16 @@ angular.module('starter').controller('FavoritesCtrl', ["$scope", "$state", "$ion diastolicValue: null, displayTotal: "Blood Pressure" }); - if($stateParams.variableCategoryName && $stateParams.variableCategoryName !== 'Anything'){ - $scope.variableCategoryName = $stateParams.variableCategoryName; - $scope.state.addButtonText = "Add favorite " + $stateParams.variableCategoryName.toLowerCase(); - $scope.state.title = 'Favorite ' + $stateParams.variableCategoryName; + var categoryName = qm.variableCategoryHelper.getVariableCategoryNameFromStateParamsOrUrl($stateParams); + if(categoryName){ + $scope.variableCategoryName = categoryName; + $scope.state.addButtonText = "Add favorite " + categoryName.toLowerCase(); + $scope.state.title = 'Favorite ' + categoryName; $scope.state.moreHelpText = null; }else{ $scope.variableCategoryName = null; } - if($stateParams.variableCategoryName === 'Treatments'){ + if(categoryName === 'Treatments'){ $scope.state.addButtonText = "Add an as-needed medication"; $scope.state.helpText = "Quickly record doses of medications taken as needed just by tapping. Tap twice for two doses, etc."; $scope.state.addButtonIcon = "ion-ios-medkit-outline"; @@ -49,7 +50,8 @@ angular.module('starter').controller('FavoritesCtrl', ["$scope", "$state", "$ion } }); var getFavoritesFromLocalStorage = function(){ - qmService.storage.getFavorites($stateParams.variableCategoryName).then(function(favorites){ + var categoryName = qm.variableCategoryHelper.getVariableCategoryNameFromStateParamsOrUrl($stateParams); + qmService.storage.getFavorites(categoryName).then(function(favorites){ $scope.state.favoritesArray = favorites; qmService.showInfoToast('Got ' + favorites.length + ' favorites!'); }); diff --git a/src/js/controllers/historyAllMeasurementsCtrl.js b/src/js/controllers/historyAllMeasurementsCtrl.js index 7708958a60..0349298d1e 100644 --- a/src/js/controllers/historyAllMeasurementsCtrl.js +++ b/src/js/controllers/historyAllMeasurementsCtrl.js @@ -33,8 +33,8 @@ angular.module('starter').controller('historyAllMeasurementsCtrl', ["$scope", "$ $scope.$on('$ionicView.enter', function(e){ qmLogService.debug($state.current.name + ': ' + 'Entering state ' + $state.current.name); qmService.navBar.showNavigationMenuIfHideUrlParamNotSet(); - var cat = $stateParams.variableCategoryName; - if(cat && cat!== 'Anything'){ + var cat = getVariableCategoryName(); + if(cat && cat !== 'Anything'){ document.title = $scope.state.title = cat + ' History'; $scope.state.showLocationToggle = cat === "Location"; } @@ -134,13 +134,7 @@ angular.module('starter').controller('historyAllMeasurementsCtrl', ["$scope", "$ qmLog.info("Could not get variableName") } function getVariableCategoryName(){ - if($stateParams.variableCategoryName){ - return $stateParams.variableCategoryName; - } - if(qm.urlHelper.getParam('variableCategoryName')){ - return qm.urlHelper.getParam('variableCategoryName'); - } - qmLog.info("Could not get variableCategoryName") + return qm.variableCategoryHelper.getVariableCategoryNameFromStateParamsOrUrl($stateParams); } function getConnectorName(){ if($stateParams.connectorName){ diff --git a/src/js/controllers/measurementAddCtrl.js b/src/js/controllers/measurementAddCtrl.js index 4e0bbde827..57f06faa8b 100644 --- a/src/js/controllers/measurementAddCtrl.js +++ b/src/js/controllers/measurementAddCtrl.js @@ -420,15 +420,8 @@ angular.module('starter').controller('MeasurementAddCtrl', ["$scope", "$q", "$ti }, 20000); }); function getVariableCategoryName(object){ - var name; - if(object && object.variableCategoryName){name = object.variableCategoryName;} - if(!name && $scope.state && $scope.state.measurement && $scope.state.measurement.variableCategoryName){ - name = $scope.state.measurement.variableCategoryName; - } - if(!name){name = $stateParams.variableCategoryName;} - if(!name){name = qm.urlHelper.getParam('variableCategoryName');} - if(!name && $stateParams.variableObject){name = $stateParams.variableObject.variableCategoryName;} - return name; + return qm.variableCategoryHelper.getVariableCategoryNameFromStateParamsOrUrl( + object, $scope.state.measurement, $stateParams, $stateParams.variableObject); } function getVariableCategory(){ return qmService.getVariableCategoryInfo(getVariableCategoryName()); diff --git a/src/js/controllers/reminderAddCtrl.js b/src/js/controllers/reminderAddCtrl.js index efaec401ad..a8e508d44d 100644 --- a/src/js/controllers/reminderAddCtrl.js +++ b/src/js/controllers/reminderAddCtrl.js @@ -68,12 +68,7 @@ angular.module('starter').controller('ReminderAddCtrl', ["$scope", "$state", "$s }); qmService.navBar.showNavigationMenuIfHideUrlParamNotSet(); qmService.login.sendToLoginIfNecessaryAndComeBack("beforeEnter in " + $state.current.name); - if($stateParams.variableObject){ - $stateParams.variableCategoryName = $stateParams.variableObject.variableCategoryName; - } - if($scope.state.trackingReminder && $scope.state.trackingReminder.variableCategoryName){ - $stateParams.variableCategoryName = $scope.state.trackingReminder.variableCategoryName; - } + $stateParams.variableCategoryName = getVariableCategoryName(); $scope.stateParams = $stateParams; setTitle(); var reminderIdUrlParameter = qm.urlHelper.getParam('reminderId'); @@ -87,9 +82,9 @@ angular.module('starter').controller('ReminderAddCtrl', ["$scope", "$state", "$s setupReminderEditingFromVariableId(variableIdUrlParameter); }else if($stateParams.variableName){ setupByVariableObject({variableName: $stateParams.variableName}); - }else if($stateParams.variableCategoryName){ - $scope.state.trackingReminder.variableCategoryName = $stateParams.variableCategoryName; - setupVariableCategory($scope.state.trackingReminder.variableCategoryName); + }else if(getVariableCategoryName()){ + $scope.state.trackingReminder.variableCategoryName = getVariableCategoryName(); + setupVariableCategory(getVariableCategoryName()); }else if(qm.getPrimaryOutcomeVariable()){ $scope.state.variableObject = qm.getPrimaryOutcomeVariable(); setupByVariableObject(qm.getPrimaryOutcomeVariable()); @@ -121,6 +116,10 @@ angular.module('starter').controller('ReminderAddCtrl', ["$scope", "$state", "$s }else{ qmLog.error($state.current.name + ': $rootScope.user is not defined!'); } + function getVariableCategoryName(){ + return qm.variableCategoryHelper.getVariableCategoryNameFromStateParamsOrUrl( + $scope.state.trackingReminder, $stateParams, $stateParams.variableObject); + } $scope.openReminderStartTimePicker = function(order){ var a = new Date(); if(order === 'first'){ @@ -499,13 +498,13 @@ angular.module('starter').controller('ReminderAddCtrl', ["$scope", "$state", "$s if($stateParams.favorite){ $scope.state.selectedFrequencyName = 'As-Needed'; if($stateParams.reminder){ - if($stateParams.variableCategoryName === 'Treatments'){ + if(getVariableCategoryName() === 'Treatments'){ $scope.state.title = "Modify As-Needed Med"; }else{ $scope.state.title = "Edit Favorite"; } }else{ - if($stateParams.variableCategoryName === 'Treatments'){ + if(getVariableCategoryName() === 'Treatments'){ $scope.state.title = "Add As-Needed Med"; }else{ $scope.state.title = "Add Favorite"; diff --git a/src/js/controllers/remindersInboxCtrl.js b/src/js/controllers/remindersInboxCtrl.js index 0b43205efe..652d724b80 100644 --- a/src/js/controllers/remindersInboxCtrl.js +++ b/src/js/controllers/remindersInboxCtrl.js @@ -568,11 +568,6 @@ angular.module('starter').controller('RemindersInboxCtrl', ["$scope", "$state", } } function getVariableCategoryName(){ - if($stateParams.variableCategoryName){ - return $stateParams.variableCategoryName; - } - if(qm.urlHelper.getParam('variableCategoryName')){ - return qm.urlHelper.getParam('variableCategoryName'); - } + return qm.variableCategoryHelper.getVariableCategoryNameFromStateParamsOrUrl($stateParams); } }]); diff --git a/src/js/controllers/remindersManageCtrl.js b/src/js/controllers/remindersManageCtrl.js index c5fca40d80..bde6c77ccc 100644 --- a/src/js/controllers/remindersManageCtrl.js +++ b/src/js/controllers/remindersManageCtrl.js @@ -36,9 +36,6 @@ angular.module('starter').controller('RemindersManageCtrl', ["$scope", "$state", $scope.$on('$ionicView.beforeEnter', function(e){ if (document.title !== $scope.state.title) {document.title = $scope.state.title;} qmLogService.info('beforeEnter RemindersManageCtrl', null); - if(qm.urlHelper.getParam('variableCategoryName')){ - $stateParams.variableCategoryName = qm.urlHelper.getParam('variableCategoryName'); - } qmService.showBasicLoader(); qmService.navBar.showNavigationMenuIfHideUrlParamNotSet(); $scope.stateParams = $stateParams; @@ -46,7 +43,7 @@ angular.module('starter').controller('RemindersManageCtrl', ["$scope", "$state", {text: 'Sort by Name'}, {text: 'Sort by Time'} ]; - if(!$stateParams.variableCategoryName || $stateParams.variableCategoryName === "Anything"){ + if(!getVariableCategoryName()){ if(!$scope.stateParams.title){ if($rootScope.platform.isMobile){ $scope.stateParams.title = "Reminders"; @@ -127,6 +124,11 @@ angular.module('starter').controller('RemindersManageCtrl', ["$scope", "$state", if(!$rootScope.reminderOrderParameter){ $rootScope.reminderOrderParameter = 'variableName'; } + function getVariableCategoryName(){ + var categoryName = qm.variableCategoryHelper.getVariableCategoryNameFromStateParamsOrUrl($stateParams); + if(categoryName){$stateParams.variableCategoryName = categoryName;} + return categoryName; + } function showAppropriateHelpInfoCards(){ $scope.state.showTreatmentInfoCard = (!$scope.state.trackingReminders || $scope.state.trackingReminders.length === 0) && (window.location.href.indexOf('Treatments') > -1 || $stateParams.variableCategoryName === 'Anything'); @@ -165,9 +167,8 @@ angular.module('starter').controller('RemindersManageCtrl', ["$scope", "$state", }); }; var getTrackingReminders = function(){ - if(qm.urlHelper.getParam('variableCategoryName')){ - $stateParams.variableCategoryName = qm.urlHelper.getParam('variableCategoryName'); - } + var cat = getVariableCategoryName(); + if(cat){$stateParams.variableCategoryName = getVariableCategoryName();} qmLogService.info('Getting ' + $stateParams.variableCategoryName + ' category reminders', null); qmService.getAllReminderTypes($stateParams.variableCategoryName).then(function(allTrackingReminderTypes){ addRemindersToScope(allTrackingReminderTypes); diff --git a/src/js/controllers/variableSearchCtrl.js b/src/js/controllers/variableSearchCtrl.js index 6a22833dab..fac9efaa5b 100644 --- a/src/js/controllers/variableSearchCtrl.js +++ b/src/js/controllers/variableSearchCtrl.js @@ -306,20 +306,18 @@ angular.module('starter').controller('VariableSearchCtrl', ["$scope", "$state", return null; } function getVariableCategory(){ - var variableCategoryName = getVariableCategoryName(); - if(variableCategoryName && $rootScope.variableCategories[variableCategoryName]){ - return $rootScope.variableCategories[variableCategoryName]; - } + var name = getVariableCategoryName(); + if(name){return qm.variableCategoryHelper.getByNameOrId(name);} return null; } function getVariableCategoryName(){ - var fromUrl = qm.urlHelper.getParam('variableCategoryName'); + var fromUrl = qm.variableCategoryHelper.getVariableCategoryNameFromStateParamsOrUrl(); if(fromUrl){return fromUrl;} var params = getVariableSearchParameters(); if(params.variableCategoryName){ return params.variableCategoryName; } - return qmService.getVariableCategoryNameFromStateParamsOrUrl($stateParams); + return qm.variableCategoryHelper.getVariableCategoryNameFromStateParamsOrUrl($stateParams); } function getPluralVariableCategoryName(){ return $filter('wordAliases')(pluralize(getVariableCategoryName(), 1)); diff --git a/src/js/qmHelpers.js b/src/js/qmHelpers.js index e8bacd8748..31c3bcdf6a 100644 --- a/src/js/qmHelpers.js +++ b/src/js/qmHelpers.js @@ -1357,6 +1357,9 @@ var qm = { return a; }, filterByRequestParams: function(provided, params){ + if(params && params.variableCategoryName){ + params.variableCategoryName = qm.variableCategoryHelper.replaceCategoryAliasWithActualNameIfNecessary(params.variableCategoryName); + } if(!provided){ qm.qmLog.error("Nothing provided to filterByRequestParams"); return provided; @@ -9440,6 +9443,14 @@ var qm = { }, }, variableCategoryHelper: { + replaceCategoryAliasWithActualNameIfNecessary: function(provided){ + if(provided === "Anything"){return null;} + var category = qm.variableCategoryHelper.getByNameOrId(provided); + if(!category){ + qmLog.errorAndExceptionTestingOrDevelopment("Category "+provided+" not found!"); + } + return category.name; + }, getVariableCategoriesFromApi: function(successHandler, errorHandler){ qm.qmLog.info("Getting variable categories from API..."); function globalSuccessHandler(variableCategories){ @@ -9498,12 +9509,28 @@ var qm = { successHandler(match); }); }, + getVariableCategoryNameFromStateParamsOrUrl: function(obj1, obj2, obj3){ + var name = qm.urlHelper.getParam('variableCategoryName'); + if(obj1 && obj1.variableCategoryName){name = obj1.variableCategoryName;} + if(obj2 && obj2.variableCategoryName){name = obj2.variableCategoryName;} + if(obj3 && obj3.variableCategoryName){name = obj3.variableCategoryName;} + if(name){name = qm.variableCategoryHelper.replaceCategoryAliasWithActualNameIfNecessary(name);} + return name; + }, getByNameOrId: function(nameOrId){ var cats = qm.variableCategoryHelper.getVariableCategoriesFromGlobals(); if(isNaN(nameOrId)){ + nameOrId = nameOrId.toLowerCase(); + nameOrId = nameOrId.replace("+", " ") return cats.find(function(c){ // noinspection EqualityComparisonWithCoercionJS - return c.id == nameOrId || c.name.toLowerCase() === nameOrId.toLowerCase(); + if(c.id == nameOrId){return true;} + if(c.name.toLowerCase() === nameOrId){return true;} + for (let i = 0; i < c.synonyms.length; i++) { + var syn = c.synonyms[i]; + if(nameOrId === syn.toLowerCase()){return true;} + } + return false; }); } else { return cats.find(function(c){return c.id === nameOrId;}); diff --git a/src/js/services/qmService.js b/src/js/services/qmService.js index a4e709bd93..84c2a19999 100644 --- a/src/js/services/qmService.js +++ b/src/js/services/qmService.js @@ -1793,7 +1793,8 @@ angular.module('starter').factory('qmService', ["$http", "$q", "$rootScope", "$i return false; } if(!measurement.variableCategoryName){ - measurement.variableCategoryName = qm.urlHelper.getParam('variableCategoryName'); + measurement.variableCategoryName = + qm.variableCategoryHelper.getVariableCategoryNameFromStateParamsOrUrl(); } if(!measurement.variableCategoryName){ message = 'Please select a variable category'; @@ -3876,18 +3877,6 @@ angular.module('starter').factory('qmService', ["$http", "$q", "$rootScope", "$i successHandler, errorHandler); }; - qmService.getVariableCategoryNameFromStateParamsOrUrl = function($stateParams){ - var variableCategoryName; - if($stateParams && $stateParams.variableCategoryName){ - variableCategoryName = $stateParams.variableCategoryName; - }else if(qm.urlHelper.getParam('variableCategoryName')){ - variableCategoryName = qm.urlHelper.getParam('variableCategoryName'); - } - if(variableCategoryName && variableCategoryName !== "Anything"){ - return variableCategoryName; - } - return null; - }; qmService.goToState = function(to, params, options){ //qmLog.info('Called goToState: ' + to, null, qmLog.getStackTrace()); qmLog.info('Going to state ' + to); From 36d670170a89600168d72579ac45d8eb3f651bf4 Mon Sep 17 00:00:00 2001 From: "Mike P. Sinn" Date: Mon, 25 May 2020 23:20:06 -0500 Subject: [PATCH 2/2] measurements = measurements || []; --- src/js/qmHelpers.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/js/qmHelpers.js b/src/js/qmHelpers.js index 662a2f2d9a..3ad7635806 100644 --- a/src/js/qmHelpers.js +++ b/src/js/qmHelpers.js @@ -4134,9 +4134,10 @@ var qm = { return startTime.toString()+":"+m.variableId; }, getLocalMeasurements: function(params, cb){ - var queue = qm.measurements.getMeasurementsFromQueue(params); - var recent = qm.measurements.getRecentlyPostedMeasurements(params); + var queue = qm.measurements.getMeasurementsFromQueue(params) || []; + var recent = qm.measurements.getRecentlyPostedMeasurements(params) || []; qm.measurements.getPrimaryOutcomeMeasurements(function (measurements) { + measurements = measurements || []; var indexed = {}; measurements.forEach(function(m){ indexed[qm.measurements.getUniqueKey(m)] = m;