[CallLog] updated CallLog sources
[samples/web/CallLog.git] / js / app.js
1 /*jslint devel: true*/
2 /*global Config, Model, Ui, tizen, setTimeout, navigator */
3
4 var App = null;
5
6 (function () { // strict mode wrapper
7         'use strict';
8
9         /**
10          * Creates a new application object
11          *
12          * @class Application
13          */
14         App = function App() {
15                 this.configData = {};
16         };
17
18         App.prototype = {
19                 /**
20                  * @type Array
21                  */
22                 requires: ['js/app.config.js', 'js/app.model.js', 'js/app.ui.js', 'js/app.ui.templateManager.js', 'js/app.helpers.js'],
23
24                 /**
25                  * @type Model
26                  */
27                 model: null,
28
29                 /**
30                  * @type Ui
31                  */
32                 ui: null,
33
34                 /**
35                  * @type Config
36                  */
37                 config: null,
38
39                 /**
40                  * @type {number}
41                  */
42                 lastViewedCaller: 0,
43
44                 /**
45                  * Initialisation function
46                  */
47                 init: function App_init() {
48                         // instantiate the libs
49                         this.config = new Config();
50                         this.model = new Model();
51                         this.model.registerChangeListener(this.updateCallLists.bind(this));
52
53                         this.ui = new Ui();
54
55                         return this;
56                 },
57
58                 /**
59                  * Updates call history and caller detailed history lists
60                  */
61                 updateCallLists: function App_updateCallLists() {
62                         // workaround;
63                         setTimeout(this.showHistoryForCaller(this.lastViewedCaller), 500);
64                         this.showCallHistory();
65                 },
66
67                 /**
68                  * Renders call history view
69                  */
70                 showCallHistory: function App_showCallHistory() {
71                         this.model.getCallHistory(this.ui.showCallHistory.bind(this.ui));
72                 },
73
74                 /**
75                  * Renders history for caller view
76                  * @param {string} phoneNumber
77                  */
78                 showHistoryForCaller: function App_showHistoryForCaller(phoneNumber) {
79                         this.lastViewedCaller = phoneNumber;
80                         this.model.getCallHistoryForCaller(phoneNumber, this.ui.showHistoryForCaller.bind(this.ui, phoneNumber));
81                 },
82
83                 /**
84                  * @param {number} addressBookId
85                  * @param {number} contactId
86                  * @returns {string} photoURI Photo URI for specified contact
87                  */
88                 getPhotoURIForContact: function App_getPhotoURIForContact(personId) {
89                         return this.model.getPhotoURIForContact(personId);
90                 },
91
92                 /**
93                  * Launch extension call service
94                  * @param {string} phoneNumber
95                  */
96                 makeCall: function App_makeCall(phoneNumber) {
97                         var self = this,
98                                 appControl = new tizen.ApplicationControl(
99                                         'http://tizen.org/appcontrol/operation/call',
100                                         'tel:' + phoneNumber
101                                 );
102                         tizen.application.launchAppControl(
103                                 appControl,
104                                 null,
105                                 function () {
106                                 },
107                                 function (e) {
108                                         console.error('Call to ' + phoneNumber
109                                                 + ' failed. Call service is unavailable.', e);
110                                         self.ui.showErrorPopup('Call failed. '
111                                                 + 'Call service is unavailable.');
112                                 },
113                                 {
114                                         onsuccess: function () {
115                                         },
116                                         onfailure: function (e) {
117                                                 console.log('App_makeCall: Call to ' + phoneNumber
118                                                         + ' failed. Call service was busy.', e);
119                                                 //self.ui.showErrorPopup('Call failed.'
120                                                 //      + 'Call service was busy.');
121                                         }
122                                 }
123                         );
124                 },
125
126                 /**
127                  * Launch extension sms service
128                  * @param {string} phoneNumber
129                  */
130                 sendSms: function App_sendSms(phoneNumber) {
131
132                         var self = this,
133                                 appControl = new tizen.ApplicationControl(
134                                         'http://tizen.org/appcontrol/operation/compose',
135                                         'sms:' + phoneNumber
136                                 );
137
138                         tizen.application.launchAppControl(
139                                 appControl,
140                                 null,
141                                 function () {
142                                         app.ui.unlockOptionButtons();
143                                 },
144                                 function (e) {
145                                         console.error('Message launch error: ', e);
146                                         self.ui.showErrorPopup('Message service is unavailable');
147                                         app.ui.unlockOptionButtons();
148                                 },
149                                 {
150                                         onsuccess: function () {
151                                                 app.ui.unlockOptionButtons();
152                                         },
153                                         onfailure: function (er) {
154                                                 console.error('Message service launch error: ', er);
155                                                 self.ui.showErrorPopup('Message service is unavailable');
156                                                 app.ui.unlockOptionButtons();
157                                         }
158                                 }
159                         );
160                 },
161
162                 /**
163                  * Deletes specified log entry
164                  * @param {CallHistoryEntry} entry
165                  */
166                 deleteLog: function App_deleteLogs(entry) {
167                         this.model.deleteLog(entry);
168                 },
169
170                 /**
171                  * App exit
172                  */
173                 exit: function App_exit(event) {
174                         event.preventDefault();
175                         event.stopPropagation();
176                         tizen.application.getCurrentApplication().exit();
177                 }
178         };
179 }());