929a27e365acfaeb81a59765531d6c9fba319c06
[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                                         app.ui.unlockOptionButtons();
107                                 },
108                                 function (e) {
109                                         console.error('App_makeCall: CALL service failure', e);
110                                         self.ui.showErrorPopup('Call service is unavailable');
111                                         app.ui.unlockOptionButtons();
112                                 },
113                                 {
114                                         onsuccess: function () {
115                                                 app.ui.unlockOptionButtons();
116                                         },
117                                         onfailure: function (er) {
118                                                 console.error('Error during call to ' + phoneNumber, er);
119                                                 app.ui.unlockOptionButtons();
120                                         }
121                                 }
122                         );
123                 },
124
125                 /**
126                  * Launch extension sms service
127                  * @param {string} phoneNumber
128                  */
129                 sendSms: function App_sendSms(phoneNumber) {
130
131                         var self = this,
132                                 appControl = new tizen.ApplicationControl(
133                                         'http://tizen.org/appcontrol/operation/compose',
134                                         'sms:' + phoneNumber
135                                 );
136
137                         tizen.application.launchAppControl(
138                                 appControl,
139                                 null,
140                                 function () {
141                                         app.ui.unlockOptionButtons();
142                                 },
143                                 function (e) {
144                                         console.error('Message launch error: ', e);
145                                         self.ui.showErrorPopup('Message service is unavailable');
146                                         app.ui.unlockOptionButtons();
147                                 },
148                                 {
149                                         onsuccess: function () {
150                                                 app.ui.unlockOptionButtons();
151                                         },
152                                         onfailure: function (er) {
153                                                 console.error('Message service launch error: ', er);
154                                                 self.ui.showErrorPopup('Message service is unavailable');
155                                                 app.ui.unlockOptionButtons();
156                                         }
157                                 }
158                         );
159                 },
160
161                 /**
162                  * Deletes specified log entry
163                  * @param {CallHistoryEntry} entry
164                  */
165                 deleteLog: function App_deleteLogs(entry) {
166                         this.model.deleteLog(entry);
167                 },
168
169                 /**
170                  * App exit
171                  */
172                 exit: function App_exit(event) {
173                         event.preventDefault();
174                         event.stopPropagation();
175                         tizen.application.getCurrentApplication().exit();
176                 }
177         };
178 }());