1 /*global $, tizen, app, UiEvents, TemplateManager, document, window, setTimeout */
6 function Ui(callback) {
11 (function () { // strict mode wrapper
15 templateManager: null,
18 * UI object for UI events
23 * Timeout for chat scroll.
28 * UI module initialisation
30 init: function Ui_init(callback) {
31 this.templateManager = new TemplateManager();
32 this.uiEvents = new UiEvents(this);
33 $.mobile.tizen.disableSelection(document);
35 $(document).ready(this.domInit.bind(this, callback));
39 * When DOM is ready, initialise it (bind events)
41 domInit: function Ui_domInit(callback) {
53 this.templateManager.loadToCache(templates, this.initPages.bind(this, callback));
56 initPages: function Ui_initPages(callback) {
57 var pages = [], body = $('body');
59 body.append(this.templateManager.get('bye_popup'))
60 .append(this.templateManager.get('message_popup'))
63 pages.push(this.templateManager.get('keyboard_page'));
64 pages.push(this.templateManager.get('chat_page'));
65 pages.push(this.templateManager.get('choose_page'));
66 body.append(pages.join(''));
69 this.fixContentHeight();
73 setContentStartAttributes: function Ui_setContentStartAttributes(callback) {
74 var contentStart, contentStartHeight;
75 contentStart = $('#start-content');
76 if (contentStart.height() > $(window).height()) {
77 contentStartHeight = $(window).height() - $('#start-header').height()
78 - parseInt(contentStart.css('padding-top'), 10) - parseInt(contentStart.css('padding-bottom'), 10);
80 contentStartHeight = contentStart.height();
82 setTimeout(function () { // workaround (setTimeout with 0 delay)
84 .css('height', contentStartHeight + 'px')
85 .css('min-height', 'auto')
86 .css('width', contentStart.width() + 'px');
87 $('#start').css('min-height', 'auto');
92 showChatPage: function Ui_showChatPage(serverName) {
93 if (serverName !== undefined) {
94 $('#chat').data('serverName', serverName);
96 $.mobile.changePage('#chat');
99 showKeyboardPage: function Ui_showKeyboardPage() {
100 $.mobile.changePage('#keyboard');
103 clearChatDialog: function Ui_clearChatDialog() {
104 $('#chat-content .ui-listview').empty();
107 showPowerOnButton: function Ui_showPowerOnButton() {
108 $('#start-monit').hide();
109 $('#serverButton').hide();
110 $('#clientButton').hide();
111 $('#turnOnButton').show();
114 showStartButtons: function Ui_showStartButtons() {
115 $('#start-monit').hide();
116 $('#turnOnButton').hide();
117 $('#serverButton').show();
118 $('#clientButton').show();
121 hideStartButtons: function Ui_hideStartButtons() {
122 $('#serverButton').hide();
123 $('#clientButton').hide();
124 $('#turnOnButton').hide();
125 $('#start-monit').show();
128 addDeviceToList: function Ui_addDeviceToList(device) {
129 var listElement, address, sub2, ul = $('#choose-content ul.ui-listview');
131 listElement = this.templateManager.get('server_row', {
132 'deviceAddress': device.address,
133 'deviceName': device.name
136 ul.append(listElement);
137 ul.listview('refresh');
140 clearListOfServers: function Ui_clearListOfServers() {
141 $('#choose-content ul.ui-listview').empty();
144 showByePopup: function Ui_showByePopup(name) {
145 var mode = app.getApplicationMode(), message = $('#byeMessage');
146 if (mode === 'server') {
147 message.html('Client name "' + name + '" is unavailable.\nYour bluetooth device will be automatically restarted.');
148 } else if (mode === 'client') {
149 message.html('Server name "' + name + '" is unavailable.\nYour bluetooth device will be automatically restarted.');
151 $('#byePopup').popup('open', {'positionTo': 'window'});
154 hideByePopup: function Ui_hideByePopup() {
155 $('#byePopup').popup('close');
158 showMessagePopup: function Ui_showMessagePopup(message) {
159 var messagePopup = $('#messagePopup');
160 messagePopup.find('.ui-popup-text').text(message);
161 messagePopup.popup('open', {'positionTo': 'window'});
164 hideMessagePopup: function Ui_hideMessagePopup() {
165 $('#messagePopup').popup('close');
168 displayReceivedMessage: function Ui_displayReceivedMessage(name, text, ping, bye) {
169 var listElement, span, ul;
170 text = decodeURIComponent(text);
171 name = decodeURIComponent(name);
173 this.showByePopup(name);
175 app.setConnection(true);
176 $('#chat-header-type').append(' - connected with ' + name);
177 this.checkSendButtonState();
179 listElement = this.templateManager.get('left_bubble', {
182 ul = $('#chat-content > .ui-scrollview-view > ul');
183 ul.append(listElement);
184 ul.listview('refresh');
188 enableSendButton: function Ui_enableSendButton() {
190 .css({'pointer-events': 'auto', 'color': '#000'})
191 .removeClass('ui-disabled');
194 disableSendButton: function Ui_disableSendButton() {
196 .css({'pointer-events': 'none', 'color': '#bbb'})
197 .addClass('ui-disabled');
200 checkSendButtonState: function Ui_checkSendButtonState() {
201 if (app.helpers.checkStringLength($('#text').val().trim()) && app.isConnection()) {
202 this.enableSendButton();
204 this.disableSendButton();
208 scrollToBottom: function Ui_scrollToBottom(element) {
209 var bottom = element.children().first().outerHeight(true) - element.height();
210 element.scrollview('scrollTo', 0, -Math.max(0, bottom), 0);
213 displaySentMessage: function Ui_displaySentMessage(message) {
214 var listElement, span, ul, content, self = this;
215 message = decodeURIComponent(message);
216 listElement = this.templateManager.get('right_bubble', {
219 content = $('#chat-content');
220 ul = content.find('ul');
221 ul.append(listElement);
222 ul.listview('refresh');
223 this.checkSendButtonState();
224 this.scrolltimeout = setTimeout(function () {
225 self.scrolltimeout = null;
226 self.scrollToBottom(content);
230 setDiscoveringProgress: function Ui_setDiscoveringProgress(boolean) {
231 $('#discovering').progress('hide', !boolean).progress('running', boolean);
234 fixContentHeight: function Ui_fixContentHeight() {
235 var contentHeight = screen.availHeight - $('div[data-role="header"]').outerHeight() - $('div[data-role="footer"]').outerHeight();
236 $('div[data-role="content"]').css('height', contentHeight);