summaryrefslogtreecommitdiffstats
path: root/devtools/client/responsive/components/Device.js
blob: d4f3c79620be7f69acee1a2391d4b9a895a0c572 (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
/* 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 {
  createFactory,
  PureComponent,
} = require("devtools/client/shared/vendor/react");
const dom = require("devtools/client/shared/vendor/react-dom-factories");
const PropTypes = require("devtools/client/shared/vendor/react-prop-types");

const { getFormatStr } = require("devtools/client/responsive/utils/l10n");
const { parseUserAgent } = require("devtools/client/responsive/utils/ua");
const Types = require("devtools/client/responsive/types");

const DeviceInfo = createFactory(
  require("devtools/client/responsive/components/DeviceInfo")
);

class Device extends PureComponent {
  static get propTypes() {
    return {
      // props.children are the buttons rendered as part of custom device label.
      children: PropTypes.oneOfType([
        PropTypes.arrayOf(PropTypes.node),
        PropTypes.node,
      ]),
      device: PropTypes.shape(Types.devices).isRequired,
      onDeviceCheckboxChange: PropTypes.func.isRequired,
    };
  }

  constructor(props) {
    super(props);

    this.state = {
      // Whether or not the device's input label is checked.
      isChecked: this.props.device.isChecked,
    };

    this.onCheckboxChanged = this.onCheckboxChanged.bind(this);
  }

  onCheckboxChanged(e) {
    this.setState(prevState => {
      return { isChecked: !prevState.isChecked };
    });

    this.props.onDeviceCheckboxChange(e);
  }

  getBrowserOrOsTooltip(agent) {
    if (agent) {
      return agent.name + (agent.version ? ` ${agent.version}` : "");
    }

    return null;
  }

  getTooltip() {
    const { device } = this.props;
    const { browser, os } = parseUserAgent(device.userAgent);

    const browserTitle = this.getBrowserOrOsTooltip(browser);
    const osTitle = this.getBrowserOrOsTooltip(os);
    let browserAndOsTitle = null;
    if (browserTitle && osTitle) {
      browserAndOsTitle = getFormatStr(
        "responsive.deviceDetails.browserAndOS",
        browserTitle,
        osTitle
      );
    } else if (browserTitle || osTitle) {
      browserAndOsTitle = browserTitle || osTitle;
    }

    const sizeTitle = getFormatStr(
      "responsive.deviceDetails.size",
      device.width,
      device.height
    );

    const dprTitle = getFormatStr(
      "responsive.deviceDetails.DPR",
      device.pixelRatio
    );

    const uaTitle = getFormatStr(
      "responsive.deviceDetails.UA",
      device.userAgent
    );

    const touchTitle = getFormatStr(
      "responsive.deviceDetails.touch",
      device.touch
    );

    return (
      `${browserAndOsTitle ? browserAndOsTitle + "\n" : ""}` +
      `${sizeTitle}\n` +
      `${dprTitle}\n` +
      `${uaTitle}\n` +
      `${touchTitle}\n`
    );
  }

  render() {
    const { children, device } = this.props;
    const tooltip = this.getTooltip();

    return dom.label(
      {
        className: "device-label",
        key: device.name,
        title: tooltip,
      },
      dom.input({
        className: "device-input-checkbox",
        name: device.name,
        type: "checkbox",
        value: device.name,
        checked: device.isChecked,
        onChange: this.onCheckboxChanged,
      }),
      DeviceInfo({ device }),
      children
    );
  }
}

module.exports = Device;