summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/options/chromeos/bluetooth_add_device_overlay.js
blob: 99f96a3464e05bca9436d158eedfe0f99404b004 (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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

cr.define('options', function() {
  /** @const */ var OptionsPage = options.OptionsPage;

  /**
   * Encapsulated handling of the Bluetooth options page.
   * @constructor
   */
  function BluetoothOptions() {
    OptionsPage.call(this,
                     'bluetooth',
                     loadTimeData.getString('bluetoothOptionsPageTabTitle'),
                     'bluetooth-options');
  }

  cr.addSingletonGetter(BluetoothOptions);

  BluetoothOptions.prototype = {
    __proto__: OptionsPage.prototype,

    /**
     * The list of available (unpaired) bluetooth devices.
     * @type {DeletableItemList}
     * @private
     */
    deviceList_: null,

    /** @override */
    initializePage: function() {
      OptionsPage.prototype.initializePage.call(this);
      this.createDeviceList_();

      $('bluetooth-add-device-cancel-button').onclick = function(event) {
        chrome.send('stopBluetoothDeviceDiscovery');
        OptionsPage.closeOverlay();
      };

      var self = this;
      $('bluetooth-add-device-apply-button').onclick = function(event) {
        var device = self.deviceList_.selectedItem;
        var address = device.address;
        chrome.send('stopBluetoothDeviceDiscovery');
        OptionsPage.closeOverlay();
        device.pairing = 'bluetoothStartConnecting';
        options.BluetoothPairing.showDialog(device);
        chrome.send('updateBluetoothDevice', [address, 'connect']);
      };

      $('bluetooth-add-device-apply-button').onmousedown = function(event) {
        // Prevent 'blur' event, which would reset the list selection,
        // thereby disabling the apply button.
        event.preventDefault();
      };

      $('bluetooth-unpaired-devices-list').addEventListener('change',
                                                            function() {
        var item = $('bluetooth-unpaired-devices-list').selectedItem;
        var disabled = !item || item.paired || item.connected;
        $('bluetooth-add-device-apply-button').disabled = disabled;
      });
    },

    /**
     * Creates, decorates and initializes the bluetooth device list.
     * @private
     */
    createDeviceList_: function() {
      this.deviceList_ = $('bluetooth-unpaired-devices-list');
      options.system.bluetooth.BluetoothDeviceList.decorate(this.deviceList_);
      this.deviceList_.autoExpands = true;
    }
  };

  /**
   * Automatically start the device discovery process if the
   * "Add device" dialog is visible.
   */
  BluetoothOptions.updateDiscovery = function() {
    var page = BluetoothOptions.getInstance();
    if (page && page.visible)
      chrome.send('findBluetoothDevices');
  }

  // Export
  return {
    BluetoothOptions: BluetoothOptions
  };
});