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