summaryrefslogtreecommitdiffstats
path: root/devtools/client/aboutdebugging/components/tabs/panel.js
blob: e280ce7f10a71e33042c41750d0b19f8a3737374 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* eslint-env browser */

"use strict";

const { createClass, createFactory, DOM: dom, PropTypes } =
  require("devtools/client/shared/vendor/react");
const Services = require("Services");

const PanelHeader = createFactory(require("../panel-header"));
const TargetList = createFactory(require("../target-list"));
const TabTarget = createFactory(require("./target"));

loader.lazyRequireGetter(this, "DebuggerClient",
  "devtools/shared/client/main", true);

const Strings = Services.strings.createBundle(
  "chrome://devtools/locale/aboutdebugging.properties");

module.exports = createClass({
  displayName: "TabsPanel",

  propTypes: {
    client: PropTypes.instanceOf(DebuggerClient).isRequired,
    id: PropTypes.string.isRequired
  },

  getInitialState() {
    return {
      tabs: []
    };
  },

  componentDidMount() {
    let { client } = this.props;
    client.addListener("tabListChanged", this.update);
    this.update();
  },

  componentWillUnmount() {
    let { client } = this.props;
    client.removeListener("tabListChanged", this.update);
  },

  update() {
    this.props.client.mainRoot.listTabs().then(({ tabs }) => {
      // Filter out closed tabs (represented as `null`).
      tabs = tabs.filter(tab => !!tab);
      tabs.forEach(tab => {
        // FIXME Also try to fetch low-res favicon. But we should use actor
        // support for this to get the high-res one (bug 1061654).
        let url = new URL(tab.url);
        if (url.protocol.startsWith("http")) {
          let prePath = url.origin;
          let idx = url.pathname.lastIndexOf("/");
          if (idx === -1) {
            prePath += url.pathname;
          } else {
            prePath += url.pathname.substr(0, idx);
          }
          tab.icon = prePath + "/favicon.ico";
        } else {
          tab.icon = "chrome://devtools/skin/images/globe.svg";
        }
      });
      this.setState({ tabs });
    });
  },

  render() {
    let { client, id } = this.props;
    let { tabs } = this.state;

    return dom.div({
      id: id + "-panel",
      className: "panel",
      role: "tabpanel",
      "aria-labelledby": id + "-header"
    },
    PanelHeader({
      id: id + "-header",
      name: Strings.GetStringFromName("tabs")
    }),
    dom.div({},
      TargetList({
        client,
        id: "tabs",
        name: Strings.GetStringFromName("tabs"),
        sort: false,
        targetClass: TabTarget,
        targets: tabs
      })
    ));
  }
});