77acebe3d061f35042554c0a84ea9b5b238282b6
[apps/web/sample/CallLog.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, tizen, setTimeout, navigator, app */
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         this.configData = {};
32     };
33
34     App.prototype = {
35         /**
36         * @type Array
37         */
38         requires: [
39             'js/app.config.js',
40             'js/app.model.js',
41             'js/app.ui.js',
42             'js/app.ui.templateManager.js',
43             'js/app.ui.templateManager.modifiers.js',
44             'js/app.helpers.js'
45         ],
46
47         /**
48         * @type Model
49         */
50         model: null,
51
52         /**
53         * @type Ui
54         */
55         ui: null,
56
57         /**
58         * @type Config
59         */
60         config: null,
61
62         /**
63         * @type {number}
64         */
65         lastViewedCaller: 0,
66
67         /**
68         * Initialisation function
69         */
70         init: function App_init() {
71             // instantiate the libs
72             this.config = new Config();
73             this.model = new Model();
74             this.model.registerChangeListener(this.updateCallLists.bind(this));
75             this.ui = new Ui();
76             this.updateCallLists();
77
78             return this;
79         },
80
81         /**
82         * Updates call history and caller detailed history lists
83         */
84         updateCallLists: function App_updateCallLists() {
85             this.ui.updateCheckboxes();
86             // workaround - time zone update
87             tizen.time.getCurrentDateTime().toLocalTimezone();
88             // workaround;
89             setTimeout(this.showHistoryForCaller(this.lastViewedCaller), 500);
90             this.showCallHistory();
91         },
92
93         /**
94         * Renders call history view
95         */
96         showCallHistory: function App_showCallHistory() {
97             this.model.getCallHistory(this.ui.showCallHistory.bind(this.ui));
98         },
99
100         /**
101         * Renders history for caller view
102         * @param {string} phoneNumber
103         */
104         showHistoryForCaller: function App_showHistoryForCaller(phoneNumber) {
105             this.lastViewedCaller = phoneNumber;
106             this.model.getCallHistoryForCaller(
107                 phoneNumber,
108                 this.ui.showHistoryForCaller.bind(this.ui, phoneNumber)
109             );
110         },
111
112         /**
113         * @param {number} addressBookId
114         * @param {number} contactId
115         * @returns {string} photoURI Photo URI for specified contact
116         */
117         getPhotoURIForContact: function App_getPhotoURIForContact(personId) {
118             return this.model.getPhotoURIForContact(personId);
119         },
120
121         /**
122         * Launch extension call service
123         * @param {string} phoneNumber
124         */
125         makeCall: function App_makeCall(phoneNumber) {
126             var self = this,
127                 appControl = new tizen.ApplicationControl(
128                     'http://tizen.org/appcontrol/operation/call',
129                     'tel:' + phoneNumber
130                 );
131             tizen.application.launchAppControl(
132                 appControl,
133                 null,
134                 function () {
135                     setTimeout(
136                         function () {
137                             self.ui.unlockButtons();
138                         },
139                         1500
140                     );
141                 },
142                 function (e) {
143                     console.error('Call to ' + phoneNumber
144                         + ' failed. Call service is unavailable.', e);
145                     self.ui.showErrorPopup('Call failed. '
146                         + 'Call service is unavailable.');
147                     self.ui.unlockButtons();
148                 },
149                 {
150                     onsuccess: function () {
151                         self.ui.unlockButtons();
152                     },
153                     onfailure: function (e) {
154                         console.log('App_makeCall: Call to ' + phoneNumber
155                             + ' failed. Call service was busy.', e);
156                         self.ui.unlockButtons();
157                         //self.ui.showErrorPopup('Call failed.'
158                         //      + 'Call service was busy.');
159                     }
160                 }
161             );
162         },
163
164         /**
165         * Launch extension sms service
166         * @param {string} phoneNumber
167         */
168         sendSms: function App_sendSms(phoneNumber) {
169
170             var self = this,
171                 appControl = new tizen.ApplicationControl(
172                     'http://tizen.org/appcontrol/operation/compose',
173                     'sms:' + phoneNumber
174                 );
175
176             tizen.application.launchAppControl(
177                 appControl,
178                 null,
179                 null,
180                 function (e) {
181                     console.error('Message launch error: ', e);
182                     self.ui.showErrorPopup('Message service is unavailable');
183                     app.ui.unlockOptionButtons();
184                 },
185                 {
186                     onfailure: function (er) {
187                         console.error('Message service launch error: ', er);
188                         self.ui.showErrorPopup(
189                             'Message service is unavailable'
190                         );
191                         app.ui.unlockOptionButtons();
192                     }
193                 }
194             );
195         },
196
197         /**
198         * Deletes specified log entry
199         * @param {CallHistoryEntry} entry
200         */
201         deleteLog: function App_deleteLogs(entry) {
202             this.model.deleteLog(entry);
203         },
204
205         /**
206         * App exit
207         */
208         exit: function App_exit(event) {
209             event.preventDefault();
210             event.stopPropagation();
211             tizen.application.getCurrentApplication().exit();
212         }
213
214     };
215
216 }());