[BluetoothChat] updated BluetoothChat sources
[samples/web/BluetoothChat.git] / js / app.ui.events.js
1 /*global $, tizen, app */
2 /**
3  * @class UiEvents
4  */
5 function UiEvents(parent) {
6         'use strict';
7         this.ui = parent;
8 }
9
10 (function () { // strict mode wrapper
11         'use strict';
12         UiEvents.prototype = {
13
14                 /**
15                  * Initialization
16                  */
17                 init: function UiEvents_init() {
18                         this.addPageEvents();
19                 },
20
21                 /**
22                  * Bind events to pages
23                  */
24                 addPageEvents: function UiEvents_addPageEvents() {
25                         var self = this;
26
27                         $('#start-header .ui-btn-back').on('click', function (event) {
28                                 event.preventDefault();
29                                 event.stopPropagation();
30                                 app.powerOff();
31                         });
32
33                         $('#choose-footer').on('click', '.ui-btn-back', function (event) {
34                                 event.preventDefault();
35                                 event.stopPropagation();
36                                 app.setDoNotSendBye(true);
37                                 $.mobile.changePage('#start');
38                         });
39
40                         $('#chat-header').on('click', '.ui-btn-back', function (event) {
41                                 event.preventDefault();
42                                 event.stopPropagation();
43                                 if (!app.isConnection()) {
44                                         app.setDoNotSendBye(true);
45                                 }
46                                 $.mobile.changePage('#start');
47                         });
48
49                         $('#chat-header').on('click', '.ui-btn-back', function (event) {
50                                 event.preventDefault();
51                         });
52
53                         $('#start').on('pagebeforeshow', function () {
54                                 self.ui.hideStartButtons();
55                                 self.ui.clearChatDialog();
56                                 if (!app.getDoNotSendBye()) {
57                                         app.sendBye();
58                                 } else {
59                                         app.setDoNotSendBye(false);
60                                 }
61                                 if (app.getApplicationMode() === 'server') {
62                                         app.server.unregisterChatServer();
63                                 } else if (app.getApplicationMode() === 'client') {
64                                         app.client.destroyBonding();
65                                 }
66                         });
67
68                         $('#turnOnButton').on('click', function (event) {
69                                 self.ui.hideStartButtons();
70                                 app.powerOn();
71                         });
72
73                         $('#serverButton').on('click', function (event) {
74                                 app.resetApplicationMode();
75                                 app.startServer();
76                         });
77
78                         $('#clientButton').on('click', function (event) {
79                                 app.resetApplicationMode();
80                                 app.startClient();
81                         });
82
83                         $('#keyboard').on('pagebeforeshow', function () {
84                                 $('#keyboard-text').val('').attr('placeholder', 'Type ' + app.getApplicationMode() + ' name');
85                                 $('#keyboard-header > h1').html('Type ' + app.getApplicationMode() + ' name');
86                         });
87
88                         $('#keyboard').on('pageshow', function () {
89                                 setTimeout(function () { $('#keyboard-text').focus(); }, 500);
90                         });
91
92                         $('#keyboard-ok-button').on('click', function (event) {
93                                 event.preventDefault();
94                                 var value = $('#keyboard-text').val(), mode;
95                                 if (value.length !== 0) {
96                                         app.setUserName(value);
97                                         mode = app.getApplicationMode();
98                                         if (mode === 'server') {
99                                                 app.setAdapterName();
100                                         } else if (mode === 'client') {
101                                                 $.mobile.changePage('#choose');
102                                         }
103                                 }
104                         });
105
106                         $('#choose').on('pagebeforeshow', function () {
107                                 app.setAdapterName();
108                         });
109
110                         $('#choose').on('pagehide', function () {
111                                 app.clearListOfServers();
112                                 app.client.stopServerSearching();
113                         });
114
115                         $('#choose-content').on('click', 'ul.ui-listview li', function () {
116                                 app.client.stopServerSearching($(this).attr('address'));
117                         });
118
119                         $('#chat').on('pagebeforeshow', function () {
120                                 $('#chat-header-type').html(app.getApplicationMode());
121                                 $('#chat-header-name').html(app.getCurrentName());
122                                 if ($(this).data('serverName') !== undefined) {
123                                         $('#chat-header-type').append(' - connected to ' + $(this).data('serverName'));
124                                         $(this).removeData('serverName');
125                                 }
126                                 self.ui.checkSendButtonState();
127                         });
128
129                         $('#chat').on('pageshow', function () {
130                                 if (app.getApplicationMode() === 'server' && !app.isBluetoothVisible()) {
131                                         setTimeout(function () { app.ui.showMessagePopup('Please make your Bluetooth visible in Settings.'); }, 500);
132                                 }
133                         });
134
135                         $('#chat').on('pagehide', function () {
136                                 $('#text').val('');
137                                 app.setConnection(false);
138                         });
139
140                         $('#text').on('input', function () {
141                                 self.ui.checkSendButtonState();
142                         });
143
144                         $('#text').on('focus', function () {
145                                 var content = $('#chat-content');
146                                 if (self.ui.scrolltimeout !== null) {
147                                         clearTimeout(self.ui.scrolltimeout);
148                                 }
149                                 self.ui.scrolltimeout = setTimeout(function () {
150                                         self.ui.scrolltimeout = null;
151                                         self.ui.scrollToBottom(content);
152                                 }, 1000);
153                         });
154
155                         $('#text').on('blur', function () {
156                                 var content = $('#chat-content');
157                                 if (self.ui.scrolltimeout !== null) {
158                                         clearTimeout(self.ui.scrolltimeout);
159                                 }
160                                 self.ui.scrolltimeout = setTimeout(function () {
161                                         self.ui.scrolltimeout = null;
162                                         self.ui.scrollToBottom(content);
163                                 }, 700);
164                         });
165
166                         $('#ui-mySend').on('click', function (event) {
167                                 event.stopPropagation();
168                                 var message = $('#text').val();
169                                 if (message.length === 0) {
170                                         return;
171                                 }
172                                 $('#text').val('');
173                                 self.ui.disableSendButton();
174                                 app.sendMessage(message);
175                         });
176
177                         $('body').on('click', '#byeOK', function () {
178                                 self.ui.hideByePopup();
179                                 $('#keyboard-back-button').trigger('click');
180                         });
181
182                         $('body').on('touchstart', '#byePopup-screen', function () {
183                                 $('#byeOK').trigger('click');
184                         });
185
186                         $('body').on('click', '#messageOK', function () {
187                                 self.ui.hideMessagePopup();
188                         });
189
190                         $('body').on('touchstart', '#messagePopup-screen', function () {
191                                 $('#messageOK').trigger('click');
192                         });
193
194                         $('#chat-content').on('touchstart', function () {
195                                 if (self.ui.scrolltimeout !== null) {
196                                         clearTimeout(self.ui.scrolltimeout);
197                                         self.ui.scrolltimeout = null;
198                                 }
199                         });
200
201                         window.addEventListener('tizenhwkey', function(e) {
202                                 if (e.keyName == "back") {
203                                         event.preventDefault();
204                                         app.setDoNotSendBye(true);
205                                         if ($.mobile.activePage.attr('id') === 'start') {
206                                                 tizen.application.getCurrentApplication().exit();
207                                         } else if ($.mobile.activePage.attr('id') === 'chat') {
208                                                 $.mobile.changePage('#start');
209                                         } else {
210                                                 history.back();
211                                         }
212                                 }
213                         });
214
215                 }
216         };
217 }());