[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                         var contentHeight = screen.availHeight - $('div[data-role="header"]').outerHeight() - $('div[data-role="footer"]').outerHeight();
81                         $('div[data-role="content"]').css('height',contentHeight);
82
83                         this.config = new Config();
84                         this.helpers = new Helpers();
85                         this.model = new Model();
86                         this.ui = new Ui(this.initModel.bind(this));
87                 },
88
89                 initModel: function App_initModel() {
90                         this.model.init(this.checkPowerState.bind(this));
91                 },
92
93                 /**
94                  * exit application action
95                  */
96                 exit: function App_exit() {
97                         tizen.application.getCurrentApplication().exit();
98                 },
99
100                 isConnection: function App_isConnection() {
101                         return this.connection;
102                 },
103
104                 setConnection: function App_setConnection(boolean) {
105                         this.connection = boolean;
106                 },
107
108                 getDoNotSendBye: function App_getDoNotSendBye() {
109                         return this.doNotSendBye;
110                 },
111
112                 setDoNotSendBye: function App_setDoNotSendBye(boolean) {
113                         this.doNotSendBye = boolean;
114                 },
115
116                 getCurrentName: function App_getCurrentName() {
117                         return this.currentName;
118                 },
119
120                 getApplicationMode: function App_getApplicationMode() {
121                         var mode = 'start';
122                         if (this.client !== null) {
123                                 mode = 'client';
124                         } else if (this.server !== null) {
125                                 mode = 'server';
126                         }
127                         return mode;
128                 },
129
130                 resetApplicationMode: function App_resetApplicationMode() {
131                         this.client = null;
132                         this.server = null;
133                 },
134
135                 checkPowerState: function App_checkPowerState() {
136                         this.ui.setContentStartAttributes(
137                                 this.model.checkPowerState.bind(
138                                         this.model, this.ui.showPowerOnButton, this.ui.showStartButtons
139                                 )
140                         );
141                 },
142
143                 powerOn: function App_powerOn() {
144                         this.model.powerOn(this.ui.showStartButtons);
145                 },
146
147                 powerOff: function App_powerOff() {
148                         this.model.powerOff(this.exit);
149                 },
150
151                 restartBluetooth: function App_restartBluetooth() {
152                         this.model.restartBluetooth(this.powerOn.bind(this));
153                 },
154
155                 startServer: function App_startServer() {
156                         this.server = new Server(this.model.adapter, this.model.serviceUUID);
157                         this.showKeyboardPage();
158                 },
159
160                 startClient: function App_startClient() {
161                         this.client = new Client(this.model.adapter, this.model.serviceUUID);
162                         this.showKeyboardPage();
163                 },
164
165                 showKeyboardPage: function App_showKeyboardPage() {
166                         this.ui.showKeyboardPage();
167                 },
168
169                 setUserName: function App_setUserName(value) {
170                         this.currentName = value;
171                 },
172
173                 setAdapterName: function App_setAdapterName() {
174                         var changeName = false, mode = this.getApplicationMode();
175                         if (this.model.adapter.name !== this.currentName) {
176                                 changeName = true;
177                         }
178                         if (mode === 'server') {
179                                 this.model.setAdapterName(changeName, this.server.registerServer.bind(this.server));
180                         } else if (mode === 'client') {
181                                 this.model.setAdapterName(changeName, this.client.searchServer.bind(this.client));
182                         }
183                 },
184
185                 clearListOfServers: function App_clearListOfServers() {
186                         this.ui.clearListOfServers();
187                 },
188
189                 displaySentMessage: function App_displaySentMessage(message) {
190                         this.ui.displaySentMessage(message);
191                 },
192
193                 sendMessage: function App_sendMessage(message) {
194                         var mode = this.getApplicationMode();
195                         if (mode === 'server') {
196                                 this.server.sendMessage(message, this.displaySentMessage.bind(this));
197                         } else if (mode === 'client') {
198                                 this.client.sendMessage(message, this.displaySentMessage.bind(this));
199                         }
200                 },
201
202                 sendBye: function App_sendBye() {
203                         var mode = this.getApplicationMode();
204                         if (mode === 'server') {
205                                 this.server.sendBye();
206                         } else if (mode === 'client') {
207                                 this.client.sendBye();
208                         }
209                 }
210         };
211 }());