summaryrefslogtreecommitdiff
path: root/project/js/app.ui.js
blob: 9bd9d99a64550f228d57c1a0eb766ed7f7479376 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*global $, tizen, app, UiEvents, TemplateManager, document, window, setTimeout */
/**
 * @class Ui
 */

function Ui(callback) {
	'use strict';
	this.init(callback);
}

(function () { // strict mode wrapper
	'use strict';
	Ui.prototype = {

		templateManager: null,

		/**
		 * UI object for UI events
		 */
		uiEvents: null,

		/**
		 * Timeout for chat scroll.
		 */
		scrolltimeout: null,

		/**
		 * UI module initialisation
		 */
		init: function Ui_init(callback) {
			this.templateManager = new TemplateManager();
			this.uiEvents = new UiEvents(this);
			$.mobile.tizen.disableSelection(document);

			$(document).ready(this.domInit.bind(this, callback));
		},

		/**
		 * When DOM is ready, initialise it (bind events)
		 */
		domInit: function Ui_domInit(callback) {
			var templates = [
				'keyboard_page',
				'chat_page',
				'choose_page',
				'server_row',
				'left_bubble',
				'right_bubble',
				'bye_popup',
				'message_popup'
			];

			this.templateManager.loadToCache(templates, this.initPages.bind(this, callback));
		},

		initPages: function Ui_initPages(callback) {
			var pages = [], body = $('body');

			body.append(this.templateManager.get('bye_popup'))
				.append(this.templateManager.get('message_popup'))
				.trigger('create');

			pages.push(this.templateManager.get('keyboard_page'));
			pages.push(this.templateManager.get('chat_page'));
			pages.push(this.templateManager.get('choose_page'));
			body.append(pages.join(''));

			this.uiEvents.init();
			this.fixContentHeight();
			callback();
		},

		setContentStartAttributes: function Ui_setContentStartAttributes(callback) {
			var contentStart, contentStartHeight;
			contentStart = $('#start-content');
			if (contentStart.height() > $(window).height()) {
				contentStartHeight = $(window).height() - $('#start-header').height()
					- parseInt(contentStart.css('padding-top'), 10) - parseInt(contentStart.css('padding-bottom'), 10);
			} else {
				contentStartHeight = contentStart.height();
			}
			setTimeout(function () { // workaround (setTimeout with 0 delay)
				contentStart
					.css('height', contentStartHeight  + 'px')
					.css('min-height', 'auto')
					.css('width', contentStart.width() + 'px');
				$('#start').css('min-height', 'auto');
				callback();
			}, 0);
		},

		showChatPage: function Ui_showChatPage(serverName) {
			if (serverName !== undefined) {
				$('#chat').data('serverName', serverName);
			}
			$.mobile.changePage('#chat');
		},

		showKeyboardPage: function Ui_showKeyboardPage() {
			$.mobile.changePage('#keyboard');
		},

		clearChatDialog: function Ui_clearChatDialog() {
			$('#chat-content .ui-listview').empty();
		},

		showPowerOnButton: function Ui_showPowerOnButton() {
			$('#start-monit').hide();
			$('#serverButton').hide();
			$('#clientButton').hide();
			$('#turnOnButton').show();
		},

		showStartButtons: function Ui_showStartButtons() {
			$('#start-monit').hide();
			$('#turnOnButton').hide();
			$('#serverButton').show();
			$('#clientButton').show();
		},

		hideStartButtons: function Ui_hideStartButtons() {
			$('#serverButton').hide();
			$('#clientButton').hide();
			$('#turnOnButton').hide();
			$('#start-monit').show();
		},

		addDeviceToList: function Ui_addDeviceToList(device) {
			var listElement, address, sub2, ul = $('#choose-content ul.ui-listview');

			listElement = this.templateManager.get('server_row', {
				'deviceAddress': device.address,
				'deviceName': device.name
			});

			ul.append(listElement);
			ul.listview('refresh');
		},

		clearListOfServers: function Ui_clearListOfServers() {
			$('#choose-content ul.ui-listview').empty();
		},

		showByePopup: function Ui_showByePopup(name) {
			var mode = app.getApplicationMode(), message = $('#byeMessage');
			if (mode === 'server') {
				message.html('Client name "' + name + '" is unavailable.\nYour bluetooth device will be automatically restarted.');
			} else if (mode === 'client') {
				message.html('Server name "' + name + '" is unavailable.\nYour bluetooth device will be automatically restarted.');
			}
			$('#byePopup').popup('open', {'positionTo': 'window'});
		},

		hideByePopup: function Ui_hideByePopup() {
			$('#byePopup').popup('close');
		},

		showMessagePopup: function Ui_showMessagePopup(message) {
			var messagePopup = $('#messagePopup');
			messagePopup.find('.ui-popup-text').text(message);
			messagePopup.popup('open', {'positionTo': 'window'});
		},

		hideMessagePopup: function Ui_hideMessagePopup() {
			$('#messagePopup').popup('close');
		},

		displayReceivedMessage: function Ui_displayReceivedMessage(name, text, ping, bye) {
			var listElement, span, ul;
			text = decodeURIComponent(text);
			name = decodeURIComponent(name);
			if (bye) {
				this.showByePopup(name);
			} else if (ping) {
				app.setConnection(true);
				$('#chat-header-type').append(' - connected with ' + name);
				this.checkSendButtonState();
			} else {
				listElement = this.templateManager.get('left_bubble', {
					'text': text
				});
				ul = $('#chat-content > .ui-scrollview-view > ul');
				ul.append(listElement);
				ul.listview('refresh');
			}
		},

		enableSendButton: function Ui_enableSendButton() {
			$('#ui-mySend')
				.css({'pointer-events': 'auto', 'color': '#000'})
				.removeClass('ui-disabled');
		},

		disableSendButton: function Ui_disableSendButton() {
			setTimeout( function() {
				$('#ui-mySend')
					.css({'pointer-events': 'none', 'color': '#bbb'})
					.addClass('ui-disabled');
				},
				0
			);
		},

		checkSendButtonState: function Ui_checkSendButtonState() {
			if (app.helpers.checkStringLength($('#text').val().trim()) && app.isConnection()) {
				this.enableSendButton();
			} else {
				this.disableSendButton();
			}
		},

		scrollToBottom: function Ui_scrollToBottom(element) {
			var bottom = element.children().first().outerHeight(true) - element.height();
			element.scrollview('scrollTo', 0, -Math.max(0, bottom), 0);
		},

		displaySentMessage: function Ui_displaySentMessage(message) {
			var listElement, span, ul, content, self = this;
			message = decodeURIComponent(message);
			listElement = this.templateManager.get('right_bubble', {
				'text': message
			});
			content = $('#chat-content');
			ul = content.find('ul');
			ul.append(listElement);
			ul.listview('refresh');
			this.checkSendButtonState();
			this.scrolltimeout = setTimeout(function () {
				self.scrolltimeout = null;
				self.scrollToBottom(content);
			}, 700);
		},

		setDiscoveringProgress: function Ui_setDiscoveringProgress(boolean) {
			$('#discovering').progress('hide', !boolean).progress('running', boolean);
		},

		fixContentHeight: function Ui_fixContentHeight() {
			var contentHeight = screen.availHeight - $('div[data-role="header"]').outerHeight() - $('div[data-role="footer"]').outerHeight();
			$('div[data-role="content"]').css('height', contentHeight);
		}

	};

}());