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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
/* 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/. */
"use strict";
const {
createFactory,
PureComponent,
} = require("resource://devtools/client/shared/vendor/react.js");
const dom = require("resource://devtools/client/shared/vendor/react-dom-factories.js");
const { hr } = dom;
const PropTypes = require("resource://devtools/client/shared/vendor/react-prop-types.js");
const {
getStr,
} = require("resource://devtools/client/responsive/utils/l10n.js");
const {
parseUserAgent,
} = require("resource://devtools/client/responsive/utils/ua.js");
const Types = require("resource://devtools/client/responsive/types.js");
const MenuButton = createFactory(
require("resource://devtools/client/shared/components/menu/MenuButton.js")
);
loader.lazyGetter(this, "MenuItem", () => {
const menuItemClass = require("resource://devtools/client/shared/components/menu/MenuItem.js");
const menuItem = createFactory(menuItemClass);
menuItem.DUMMY_ICON = menuItemClass.DUMMY_ICON;
return menuItem;
});
loader.lazyGetter(this, "MenuList", () => {
return createFactory(
require("resource://devtools/client/shared/components/menu/MenuList.js")
);
});
class DeviceSelector extends PureComponent {
static get propTypes() {
return {
devices: PropTypes.shape(Types.devices).isRequired,
onChangeDevice: PropTypes.func.isRequired,
onUpdateDeviceModal: PropTypes.func.isRequired,
selectedDevice: PropTypes.string.isRequired,
viewportId: PropTypes.number.isRequired,
};
}
getMenuProps(device) {
if (!device) {
return { icon: null, label: null, tooltip: null };
}
const { browser, os } = parseUserAgent(device.userAgent);
let label = device.name;
if (os) {
label += ` ${os.name}`;
if (os.version) {
label += ` ${os.version}`;
}
}
let icon = null;
let tooltip = label;
if (browser) {
icon = `chrome://devtools/skin/images/browsers/${browser.name.toLowerCase()}.svg`;
tooltip += ` ${browser.name} ${browser.version}`;
}
return { icon, label, tooltip };
}
getSelectedDevice() {
const { devices, selectedDevice } = this.props;
if (!selectedDevice) {
return null;
}
for (const type of devices.types) {
for (const device of devices[type]) {
if (selectedDevice === device.name) {
return device;
}
}
}
return null;
}
renderMenuList() {
const {
devices,
onChangeDevice,
onUpdateDeviceModal,
selectedDevice,
viewportId,
} = this.props;
const menuItems = [];
for (const type of devices.types) {
for (const device of devices[type]) {
if (device.displayed) {
const { icon, label, tooltip } = this.getMenuProps(device);
menuItems.push(
MenuItem({
key: label,
className: "device-selector-item",
checked: selectedDevice === device.name,
label,
icon: icon || MenuItem.DUMMY_ICON,
tooltip,
onClick: () => onChangeDevice(viewportId, device, type),
})
);
}
}
}
menuItems.sort(function (a, b) {
return a.props.label.localeCompare(b.props.label);
});
if (menuItems.length) {
menuItems.push(hr({ key: "separator" }));
}
menuItems.push(
MenuItem({
key: "edit-device",
label: getStr("responsive.editDeviceList2"),
onClick: () => onUpdateDeviceModal(true, viewportId),
})
);
return MenuList({}, menuItems);
}
render() {
const { devices } = this.props;
const selectedDevice = this.getSelectedDevice();
let { icon, label, tooltip } = this.getMenuProps(selectedDevice);
if (!selectedDevice) {
label = getStr("responsive.responsiveMode");
}
// MenuButton is expected to be used in the toolbox document usually,
// but since RDM's frame also loads theme-switching.js, we can create
// MenuButtons (& HTMLTooltips) in the RDM frame document.
const toolboxDoc = window.document;
return MenuButton(
{
id: "device-selector",
menuId: "device-selector-menu",
toolboxDoc,
className: "devtools-button devtools-dropdown-button",
label,
icon,
title: tooltip,
disabled: devices.listState !== Types.loadableState.LOADED,
},
() => this.renderMenuList()
);
}
}
module.exports = DeviceSelector;
|