Updated application sources
[apps/web/sample/ExercisePlanner.git] / project / js / app.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 Config, Model, Ui, app, tizen */
19
20 var App = null;
21
22 (function () { // strict mode wrapper
23     'use strict';
24
25     /**
26      * Creates a new application object
27      *
28      * @class Application
29      */
30     App = function App() {};
31
32     App.prototype = {
33         /**
34          * @type Array
35          */
36         requires: ['js/app.config.js', 'js/app.model.js', 'js/app.alarm.js', 'js/app.ui.js',
37                 'js/app.ui.templateManager.js', 'js/app.ui.templateManager.modifiers.js'],
38         /**
39          * @type Config
40          */
41         config: null,
42         /**
43          * @type Model
44          */
45         model: null,
46         /**
47          * @type Ui
48          */
49         ui: null,
50         /**
51          * @type bool
52          */
53         APP_CONTROL_DATA_KEY: 'http://tizen.org/appcontrol/data/alarm_id',
54         APP_CONTROL_OPERATION_URI: 'http://tizen.org/appcontrol/operation/exercise',
55
56         /**
57          * Initialisation function
58          */
59         init: function init() {
60             // instantiate the libs
61             this.config = new Config();
62             this.model = new Model();
63             this.ui = new Ui();
64
65             // initialise the modules
66             this.model.init(this);
67             this.ui.init(this, this.getRequestedAppControlData());
68
69             return this;
70         },
71
72         /**
73          * Returns this application id
74          * @return {Number} application id
75          */
76         getId: function getId() {
77             return tizen.application.getCurrentApplication().appInfo.id;
78         },
79
80         /**
81          * Parse request AppControl object and retrieve connected exercise info
82          * @return {String} exercise id or undefined
83          */
84         getRequestedAppControlData: function getRequestedAppControlData() {
85             var reqAppControl = tizen
86                 .application
87                 .getCurrentApplication()
88                 .getRequestedAppControl(),
89                 data,
90                 len,
91                 exerciseId;
92
93             if (reqAppControl) {
94                 data = reqAppControl.appControl.data;
95                 len = data.length - 1;
96
97                 while (len >= 0) {
98                     if (data[len].key === this.APP_CONTROL_DATA_KEY) {
99                         exerciseId = data[len].value[0];
100                         break;
101                     }
102                     len -= 1;
103                 }
104
105                 return exerciseId;
106             }
107         },
108
109         /**
110          * Application exit from model
111          */
112         exit: function exit() {
113             tizen.application.getCurrentApplication().exit();
114         },
115
116         /**
117          * Adds exercise to storage
118          * @param {Object} exercise
119          * @param {Function} success callback
120          * @param {Function} failure callback
121          */
122         addExercise: function addExercise(exercise, success, failure) {
123             // if add was successful
124             if (this.model.add(exercise)) {
125                 if (success instanceof Function) {
126                     success();
127                 }
128             } else { // if add fail
129                 console.log('problem with adding');
130                 if (failure instanceof Function) {
131                     failure();
132                 }
133             }
134         },
135
136         /**
137          * Gets all stored exercises
138          * @return {Array} list of exercises
139          */
140         getAllExercises: function getAllExercises() {
141             return this.model.getAll();
142         },
143
144         /**
145          * Single exercise which match value in corresponding key
146          * @param {String} attr name
147          * @param {*} value
148          * @return {Object|undefined} exercise object
149          */
150         getExercise: function getExercise(attr, value) {
151             return this.model.find(attr, value)[0];
152         },
153
154         /**
155          * Adds exercise to storage
156          * @param {String} exerciseId
157          * @param {Function} success callback
158          * @param {Function} failure callback
159          */
160         removeExercise: function removeExercise(exerciseId, success, failure) {
161             // if removed was successfully completed
162             if (this.model.remove(exerciseId)) {
163                 if (success instanceof Function) {
164                     success();
165                 }
166             } else { // if there was problem with removing exercise
167                 console.log('problem with removing');
168                 if (failure instanceof Function) {
169                     failure();
170                 }
171             }
172         }
173
174     };
175 }());