add QuotaExceededError exception handling to saveConfig function
[samples/web/ExercisePlanner.git] / js / app.js
index 071a3c6..1836776 100644 (file)
--- a/js/app.js
+++ b/js/app.js
@@ -54,6 +54,11 @@ var ExercisePlanner = function () {
                                enabled: false
                        }],
 
+                       /**
+                        * List of generate available exercises;
+                        */
+                       availableExercises: [],
+
                        // deprecated for this version;
                        increasingStrength: true,
 
@@ -139,7 +144,12 @@ var ExercisePlanner = function () {
         * (use localStorage)
         */
        ExercisePlanner.prototype.saveConfig = function () {
-               localStorage.setItem('config', JSON.stringify(this.config));
+               try {
+                       localStorage.setItem('config', JSON.stringify(this.config));
+               } catch (e) {
+                       if (e.code === 22) //QuotaExceededError
+                               this.ui.showErrors([{name: 'Not enough memory. Please remove unnecessary files'}]);
+               }
        };
 
        ExercisePlanner.prototype.stopTraining = function () {
@@ -249,14 +259,12 @@ var ExercisePlanner = function () {
                        return a.date - b.date;
                });
 
-               if (this.config.nearestExercise > -1) {
+               if (this.config.availableExercises.length > 0) {
                        this.ui.showAlarmInMonitor({
                                alarm: alarms[0],
-                               exerciseName: this.config.exercises[this.config.nearestExercise].name,
+                               exerciseName: this.config.availableExercises[this.config.nearestExercise].name,
                                numberOfTimes: this.getStrength(this.config.strength.workday, this.config.count)
                        });
-
-                       this.config.count += 1;
                }
                this.saveConfig();
        };
@@ -266,10 +274,8 @@ var ExercisePlanner = function () {
         * @param type
         */
        ExercisePlanner.prototype.changeTypeOfPeriods = function changeTypeOfPeriods(type) {
-               if (this.config.currentTypeOfPeriods !== type) {
                        this.config.currentTypeOfPeriods = type;
                        this.updateGraph(this.config.currentTypeOfPeriods);
-               }
        };
 
        /**
@@ -341,10 +347,10 @@ var ExercisePlanner = function () {
         * Generate name of exercise for nearest workout
         */
        ExercisePlanner.prototype.generateNearestExercise = function () {
-               var tmp = this.config.exercises.filter(function (item) {
+               this.config.availableExercises = this.config.exercises.filter(function (item) {
                        return item.enabled;
                });
-               this.config.nearestExercise = parseInt(Math.random() * tmp.length, 10);
+               this.config.nearestExercise = parseInt(Math.random() * this.config.availableExercises.length, 10);
        };
 
 
@@ -444,7 +450,6 @@ var ExercisePlanner = function () {
                this.config.currentTypeOfPeriods = (this.todayIsWorkday()) ? 'workday' : 'weekend';
 
                this.generateAlarms();
-
                this.ui.initialize(onUiInitialize);
        };
 }());