Updated application sources
[apps/web/sample/ExercisePlanner.git] / project / js / app.model.js
1 /*
2  *      Copyright 2013  Samsung Electronics Co., Ltd
3  *
4  *      Licensed under the Flora License, Version 1.1 (the "License");
5  *      you may not use this file except in compliance with the License.
6  *      You may obtain a copy of the License at
7  *
8  *              http://floralicense.org/license/
9  *
10  *      Unless required by applicable law or agreed to in writing, software
11  *      distributed under the License is distributed on an "AS IS" BASIS,
12  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *      See the License for the specific language governing permissions and
14  *      limitations under the License.
15  */
16
17 /*jslint devel:true*/
18 /*global tizen, window, Alarm, app */
19
20 /**
21  * @class Model
22  */
23 var Model = function Model() {
24     'use strict';
25 };
26
27 (function () { // strict mode wrapper
28     'use strict';
29     Model.prototype = {
30
31         /**
32          * Initialization function
33          * @param {Object} app object
34          */
35         init: function Model_init(app) {
36             var exercises = window.localStorage.getItem('exercises');
37
38             this.app = app;
39             this.exercises = exercises ? JSON.parse(exercises) : [];
40             this.alarmHelper = new Alarm();
41         },
42
43         /**
44          * Adds new exercise and save it in local storage and set new alarm
45          * @param {Object} exercise new object
46          * @return {Object|undefined} exercise object
47          */
48         add: function Model_saveAlarm(exercise) {
49             var alarmId = this.alarmHelper.add(exercise);
50
51             if (alarmId) {
52                 exercise.id = alarmId;
53
54                 // add to session storage
55                 this.exercises.push(exercise);
56
57                 // add to localStorage
58                 this.updateStorage();
59                 return exercise;
60             }
61         },
62
63         /**
64          * Remove exercise with a given id
65          * @param {String} exerciseId
66          * @return {Boolean} result of remove
67          */
68         remove: function Model_remove(exerciseId) {
69             // find exercise to remove
70             var exercise = this.find('id', exerciseId)[0], index;
71             // if exercise remove connected alarm
72             if (exercise) {
73                 if (this.alarmHelper.remove(exercise)) {
74                     // if alarm removed update session store
75                     index = this.exercises.indexOf(exercise);
76                     this.exercises.splice(index, 1);
77                     //update localStorage
78                     this.updateStorage();
79                     return true;
80                 }
81             }
82             // error removing
83             return false;
84         },
85
86         /**
87          * Finds list of exercises matching values with given attribute name
88          * @param {String} attr name
89          * @param {*} value of attr
90          * @return {Array} list of exercises
91          */
92         find: function Model_find(attr, value) {
93             var result = this.exercises.filter(
94                 function (el) {
95                     return el[attr] === value.toString();
96                 }
97             );
98             return result;
99         },
100
101         /**
102          * Save exercises in local storage
103          */
104         updateStorage: function () {
105             try {
106                 window.localStorage.setItem(
107                     'exercises',
108                     JSON.stringify(this.exercises)
109                 );
110             } catch (e) {
111                 if (e.code === 22) {
112                     //QuotaExceededError
113                     app.ui.popup(
114                         'Not enough memory to save data.' +
115                             ' Please remove unnecessary files.'
116                     );
117                 }
118             }
119         },
120
121         /**
122          * Returns array of all currently stored exercises
123          * @return {Array} list of exercises
124          */
125         getAll: function Model_getAll() {
126             return this.exercises;
127         }
128
129     };
130 }());