[jslint] Enable js lint and fix the errors.
[platform/framework/web/tizen-extensions-crosswalk.git] / demos / tizen / js / main.js
1 Number.prototype.format = function() {
2   if (this < 10)
3     return '0' + this;
4   return this;
5 };
6
7 var blueApp = {};
8
9
10 /**
11  * Initializes the application data and settings.
12  */
13 blueApp.init = function() {
14   $('#return-btn').click(function(event) {
15     window.close();
16   });
17
18   blueApp.cleanDeviceList();
19
20   $('#paired-device-group').hide();
21   $('#visibility-toggle').click(blueApp.toggleVisibility);
22
23   $('input[type=radio][name=visibility]').change(function() {
24     blueApp.changedVisibility();
25   });
26
27   $('#scan-btn').click(blueApp.scan);
28
29   blueApp.bluetoothLoad();
30
31   $('#bluetooth-toggle').change(blueApp.adapterStatusToggle);
32 };
33
34
35 /**
36  * On power setting success callback.
37  */
38 blueApp.adapterPowerSuccessCb = function() {
39   var status = $('#bluetooth-toggle').val();
40
41   var noti = new tizen.StatusNotification('SIMPLE', 'Bluetooth', {
42     content: 'Successfully powered ' + status + ' bluetooth adapter.'
43   });
44   tizen.notification.post(noti);
45
46   if (blueApp.adapter.powered)
47     blueApp.discoverDevices();
48 };
49
50
51 /**
52  * On power setting error callback.
53  */
54 blueApp.adapterPowerErrCb = function() {
55   var status = $('#bluetooth-toggle').val();
56
57   var noti = new tizen.StatusNotification('SIMPLE', 'Bluetooth', {
58     content: 'Failed to power ' + status + ' bluetooth adapter.'
59   });
60   tizen.notification.post(noti);
61 };
62
63
64 /**
65  * Device buttom has been toggled.
66  *
67  * Powers down the bluetooth device.
68  */
69 blueApp.adapterStatusToggle = function() {
70   var status = $('#bluetooth-toggle').val() == 'on';
71
72   if (!status) {
73     blueApp.cleanDeviceList();
74     blueApp.adapter.stopDiscovery(function() {
75       blueApp.adapter.setPowered(status, blueApp.adapterPowerSuccessCb,
76           blueApp.adapterPowerErrCb);
77     });
78   } else {
79     blueApp.adapter.setPowered(status, blueApp.adapterPowerSuccessCb,
80         blueApp.adapterPowerErrCb);
81   }
82 };
83
84
85 /**
86  * Clean the device list.
87  */
88 blueApp.cleanDeviceList = function() {
89   $('#available-device-list').hide();
90   $('#available-device-list').html('');
91 };
92
93
94 /**
95  * Scan for bluetooth devices.
96  */
97 blueApp.scan = function() {
98   if (!blueApp.adapter) return;
99
100   blueApp.adapter.stopDiscovery(function() {
101     blueApp.cleanDeviceList();
102     blueApp.discoverDevices();
103   });
104 };
105
106 blueApp.newDeviceEntry = function(device, list) {
107   var deviceStyle;
108   var deviceClass = device.deviceClass;
109   var deviceItem = "<li><div id='device-icon' ";
110
111   if (!blueApp.adapter.powered) return;
112
113   switch (deviceClass.major) {
114     case tizen.bluetooth.deviceMajor.COMPUTER:
115       deviceStyle = 'device-icon-computer';
116       break;
117     case tizen.bluetooth.deviceMajor.PHONE:
118       deviceStyle = 'device-icon-telephone';
119       break;
120     case tizen.bluetooth.deviceMajor.IMAGING:
121       if (deviceClass.minor == tizen.bluetooth.deviceMinor.PRINTER) {
122         deviceStyle = 'device-icon-printer';
123       }
124       break;
125   }
126
127   deviceItem += "class='" + deviceStyle + "' />";
128   deviceItem += device.name;
129   deviceItem += '</li>';
130
131   $(list + '-group').show();
132   $(list + '-list').show();
133   $(list + '-list').append(
134       $(deviceItem).attr('class',
135                          'ui-li ui-li-static ui-btn-up-s ui-li-last').attr(
136                            'address', device.address));
137
138   return $(list + "-list li[address='" + device.address + "']");
139 };
140
141 blueApp.pairDevice = function() {
142   var deviceEntry = $(this);
143
144   blueApp.adapter.createBonding($(deviceEntry).attr('address'), function(device) {
145     $(deviceEntry).remove();
146     blueApp.newDeviceEntry(device, '#paired-device').click(blueApp.unpairDevice);
147   }, function(e) {
148     var noti = new tizen.StatusNotification('SIMPLE', 'Bluetooth', {
149       content: 'Failed pairing device.'});
150     tizen.notification.post(noti);
151   });
152 };
153
154 blueApp.unpairDevice = function() {
155   var deviceEntry = $(this);
156   var address = $(this).attr('address');
157
158   blueApp.adapter.destroyBonding(address, function(device) {
159     $(deviceEntry).remove();
160     blueApp.newDeviceEntry(device, '#available-device').click(blueApp.pairDevice);
161
162     if (!$('#paired-device-list li').size())
163       $('#paired-device-group').hide();
164   }, function(e) {
165     var noti = new tizen.StatusNotification('SIMPLE', 'Bluetooth', {
166       content: 'Could not unpair bluetooth device.'
167     });
168     tizen.notification.post(noti);
169   });
170 };
171
172
173 /**
174  * Efectively add new devices to devices list.
175  */
176 blueApp.addDevice = function(device) {
177   var clickCb = blueApp.pairDevice;
178   var deviceList = '#available-device';
179
180   if (device.isBonded) {
181     deviceList = '#paired-device';
182     clickCb = blueApp.unpairDevice;
183   }
184
185   blueApp.newDeviceEntry(device, deviceList).click(clickCb);
186 };
187
188
189 /**
190  * Tizen bluetooth callbacs.
191  */
192 blueApp.discoverDevicesCb = {
193   onstarted: function() {
194     console.log('Discovery has started.');
195   },
196   ondevicefound: blueApp.addDevice,
197   ondevicedisappeared: function(address) {
198     $('#available-device-list li').each(function() {
199       if ($(this).attr('address') == address)
200         $(this).remove();
201     });
202   },
203   onfinished: function(devices) {
204     var noti = new tizen.StatusNotification('SIMPLE', 'Bluetooth', {
205       content: 'Finished bluetooth devices discovery.'
206     });
207     tizen.notification.post(noti);
208   }
209 };
210
211
212 /**
213  * Start the discover call and handle failure.
214  */
215 blueApp.discoverDevices = function() {
216   blueApp.adapter.discoverDevices(blueApp.discoverDevicesCb, function(e) {
217     var noti = new tizen.StatusNotification('SIMPLE', 'Bluetooth', {
218       content: 'Failed to discover bluetooth devices.'
219     });
220     tizen.notification.post(noti);
221   });
222 };
223
224
225 /**
226  * Bluetooth initial data load.
227  *
228  * Gets the current default adapter and start to discover devices if it's
229  * powered.
230  */
231 blueApp.bluetoothLoad = function() {
232   try {
233     blueApp.adapter = tizen.bluetooth.getDefaultAdapter();
234     $('#adapter-name').html(blueApp.adapter.name);
235   } catch (err) {
236     console.log('bluetooth is off');
237   }
238
239   if (blueApp.adapter.visible) {
240     $('#visibility-display').html('On');
241   }
242
243   $('#bluetooth-toggle').val(blueApp.adapter.powered ? 'on' : 'off');
244   $('#bluetooth-toggle').slider('refresh');
245
246   if (blueApp.adapter.powered)
247     blueApp.discoverDevices();
248 };
249
250
251 /**
252  * Timer for setting the UI visible remaining time case a timeout has been set.
253  */
254 blueApp.setRemainingTime = function() {
255   var timeOut;
256   var delta;
257   var display;
258   var currTime = new Date().getTime() / 1000;
259   var visibilityTime = blueApp.visibilityTime;
260   var visibilityTimeout = blueApp.visibilityTimeout;
261
262   timeOut = visibilityTime + visibilityTimeout;
263   delta = timeOut - currTime;
264   display = Math.floor(delta / 60).format() + ':' + Math.floor(delta % 60).format();
265
266   $('#visibility-display').html(display);
267
268   if (currTime < timeOut)
269     setTimeout(blueApp.setRemainingTime, 1000);
270   else
271     $('#visibility-display').html('Off');
272 };
273
274
275 /**
276  * On visibility setting success callback.
277  */
278 blueApp.visibilitySuccessCb = function() {
279   setTimeout(blueApp.setRemainingTime, 1000);
280 };
281
282
283 /**
284  * On visibility setting error callback.
285  */
286 blueApp.visibilityErrorCb = function() {
287   var noti = new tizen.StatusNotification('SIMPLE', 'Bluetooth', {
288     content: 'Error on setting bluetooth visibility.'
289   });
290   tizen.notification.post(noti);
291   $('#visibility-display').html('Off');
292 };
293
294
295 /**
296  * Reset the visibility label.
297  *
298  * Sets the visibility label with the just selected visibility item.
299  */
300 blueApp.changedVisibility = function() {
301   var timeout = parseInt($('input[type=radio][name=visibility]:checked')
302                            .val(), 10);
303   var visibility = $('input[type=radio][name=visibility]:checked').attr(
304       'label');
305   blueApp.toggleVisibility();
306   $('#visibility-display').html(visibility);
307   blueApp.visibilityTime = 0;
308   blueApp.visibilityTimeout = 0;
309
310   if (timeout > 0) {
311     blueApp.visibilityTime = new Date().getTime() / 1000;
312     blueApp.visibilityTimeout = timeout;
313
314     blueApp.adapter.setVisible(true, blueApp.visibilitySuccessCb,
315         blueApp.visibilityErrorCb, timeout);
316   } else if (timeout == 0)
317     blueApp.adapter.setVisible(false);
318   else
319     blueApp.adapter.setVisible(true);
320 };
321
322
323 /**
324  * Toggle the device/adapter visibility.
325  *
326  * Displays or not the visibility options(Off, 1 min, 5 min etc) and toggles the
327  * the item arrow(off/on).
328  */
329 blueApp.toggleVisibility = function() {
330   var vGroup = $('#visibility-group');
331   var display = 'none';
332   var toggle = 'visibility-on';
333
334   if (!blueApp.adapter) return;
335
336   if (vGroup.css('display') == 'none') {
337     display = 'block';
338     toggle = 'visibility-off';
339   }
340
341   vGroup.css('display', display);
342   $('#visibility-toggle-icon').attr('class', toggle);
343 };
344
345 $(document).ready(blueApp.init);