[BluetoothChat]update BluetoothChat(tizen_2.1)
[samples/web/BluetoothChat.git] / js / app.js
1 /*global $, tizen, app, Config, Helpers, Model, Ui, Server, Client */
2 var App = null;
3
4 (function () { // strict mode wrapper
5         'use strict';
6
7         /**
8          * Creates a new application object
9          *
10          * @class Application
11          */
12         App = function App() {};
13
14         App.prototype = {
15                 /**
16                  * @type Array
17                  */
18                 requires: ['js/app.config.js',
19                            'js/app.helpers.js',
20                            'js/app.model.js',
21                            'js/app.ui.js',
22                            'js/app.ui.templateManager.js',
23                            'js/app.ui.events.js',
24                            'js/app.client.js',
25                            'js/app.client.model.js',
26                            'js/app.server.js',
27                            'js/app.server.model.js'
28                            ],
29
30                 /**
31                  * @type Model
32                  */
33                 model: null,
34
35                 /**
36                  * @type Ui
37                  */
38                 ui: null,
39
40                 /**
41                  * @type Config
42                  */
43                 config: null,
44
45                 /**
46                  * @type Helpers
47                  */
48                 helpers: null,
49
50                 /**
51                  * @type Client
52                  */
53                 client: null,
54
55                 /**
56                  * @type Server
57                  */
58                 server: null,
59
60                 /**
61                  * @type String
62                  */
63                 currentName: '',
64
65                 /**
66                  * @type Boolean
67                  */
68                 doNotSendBye: false,
69
70                 /**
71                  * @type Boolean
72                  */
73                 connection: false,
74
75                 /**
76                  * Initialisation function
77                  */
78                 init: function App_init() {
79                         this.config = new Config();
80                         this.helpers = new Helpers();
81                         this.model = new Model();
82                         this.ui = new Ui(this.initModel.bind(this));
83                 },
84
85                 initModel: function App_initModel() {
86                         this.model.init(this.checkPowerState.bind(this));
87                 },
88
89                 /**
90                  * exit application action
91                  */
92                 exit: function App_exit() {
93                         tizen.application.getCurrentApplication().exit();
94                 },
95
96                 isConnection: function App_isConnection() {
97                         return this.connection;
98                 },
99
100                 setConnection: function App_setConnection(bool) {
101                         this.connection = bool;
102                 },
103
104                 getDoNotSendBye: function App_getDoNotSendBye() {
105                         return this.doNotSendBye;
106                 },
107
108                 setDoNotSendBye: function App_setDoNotSendBye(bool) {
109                         this.doNotSendBye = bool;
110                 },
111
112                 getCurrentName: function App_getCurrentName() {
113                         return this.currentName;
114                 },
115
116                 getApplicationMode: function App_getApplicationMode() {
117                         var mode = 'start';
118                         if (this.client !== null) {
119                                 mode = 'client';
120                         } else if (this.server !== null) {
121                                 mode = 'server';
122                         }
123                         return mode;
124                 },
125
126                 resetApplicationMode: function App_resetApplicationMode() {
127                         this.client = null;
128                         this.server = null;
129                 },
130
131                 checkPowerState: function App_checkPowerState() {
132                         this.ui.setContentStartAttributes(
133                                 this.model.checkPowerState.bind(
134                                         this.model,
135                                         this.ui.showPowerOnButton,
136                                         this.ui.showStartButtons
137                                 )
138                         );
139                 },
140
141                 powerOn: function App_powerOn() {
142                         this.model.powerOn(this.ui.showStartButtons);
143                 },
144
145                 powerOff: function App_powerOff() {
146                         this.model.powerOff(this.exit);
147                 },
148
149                 restartBluetooth: function App_restartBluetooth() {
150                         this.model.restartBluetooth(this.powerOn.bind(this));
151                 },
152
153                 startServer: function App_startServer() {
154                         this.server = new Server(this.model.adapter, this.model.serviceUUID);
155                         this.showKeyboardPage();
156                 },
157
158                 startClient: function App_startClient() {
159                         this.client = new Client(this.model.adapter, this.model.serviceUUID);
160                         this.showKeyboardPage();
161                 },
162
163                 showKeyboardPage: function App_showKeyboardPage() {
164                         this.ui.showKeyboardPage();
165                 },
166
167                 setUserName: function App_setUserName(value) {
168                         this.currentName = value;
169                 },
170
171                 setAdapterName: function App_setAdapterName() {
172                         var changeName = false, mode = this.getApplicationMode();
173                         if (this.model.adapter.name !== this.currentName) {
174                                 changeName = true;
175                         }
176                         if (mode === 'server') {
177                                 this.model.setAdapterName(changeName, this.server.registerServer.bind(this.server));
178                         } else if (mode === 'client') {
179                                 this.model.setAdapterName(changeName, this.client.searchServer.bind(this.client));
180                         }
181                 },
182
183                 clearListOfServers: function App_clearListOfServers() {
184                         this.ui.clearListOfServers();
185                 },
186
187                 displaySentMessage: function App_displaySentMessage(message) {
188                         this.ui.displaySentMessage(message);
189                 },
190
191                 sendMessage: function App_sendMessage(message) {
192                         var mode = this.getApplicationMode();
193                         if (mode === 'server') {
194                                 this.server.sendMessage(message, this.displaySentMessage.bind(this));
195                         } else if (mode === 'client') {
196                                 this.client.sendMessage(message, this.displaySentMessage.bind(this));
197                         }
198                 },
199
200                 sendBye: function App_sendBye() {
201                         var mode = this.getApplicationMode();
202                         if (mode === 'server') {
203                                 this.server.sendBye();
204                         } else if (mode === 'client') {
205                                 this.client.sendBye();
206                         }
207                 }
208         };
209 }());