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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
/* 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 Services = require("Services");
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 { connect } = require("devtools/client/shared/vendor/react-redux");
const Toolbar = createFactory(
require("devtools/client/responsive/components/Toolbar")
);
loader.lazyGetter(this, "DeviceModal", () =>
createFactory(require("devtools/client/responsive/components/DeviceModal"))
);
const {
changeNetworkThrottling,
} = require("devtools/client/shared/components/throttling/actions");
const {
addCustomDevice,
editCustomDevice,
removeCustomDevice,
updateDeviceDisplayed,
updateDeviceModal,
updatePreferredDevices,
} = require("devtools/client/responsive/actions/devices");
const {
takeScreenshot,
} = require("devtools/client/responsive/actions/screenshot");
const {
changeUserAgent,
toggleLeftAlignment,
toggleReloadOnTouchSimulation,
toggleReloadOnUserAgent,
toggleTouchSimulation,
toggleUserAgentInput,
} = require("devtools/client/responsive/actions/ui");
const {
changeDevice,
changePixelRatio,
changeViewportAngle,
removeDeviceAssociation,
resizeViewport,
rotateViewport,
} = require("devtools/client/responsive/actions/viewports");
const {
getOrientation,
} = require("devtools/client/responsive/utils/orientation");
const Types = require("devtools/client/responsive/types");
class App extends PureComponent {
static get propTypes() {
return {
devices: PropTypes.shape(Types.devices).isRequired,
dispatch: PropTypes.func.isRequired,
leftAlignmentEnabled: PropTypes.bool.isRequired,
networkThrottling: PropTypes.shape(Types.networkThrottling).isRequired,
screenshot: PropTypes.shape(Types.screenshot).isRequired,
viewports: PropTypes.arrayOf(PropTypes.shape(Types.viewport)).isRequired,
};
}
constructor(props) {
super(props);
this.onAddCustomDevice = this.onAddCustomDevice.bind(this);
this.onBrowserContextMenu = this.onBrowserContextMenu.bind(this);
this.onChangeDevice = this.onChangeDevice.bind(this);
this.onChangeNetworkThrottling = this.onChangeNetworkThrottling.bind(this);
this.onChangePixelRatio = this.onChangePixelRatio.bind(this);
this.onChangeTouchSimulation = this.onChangeTouchSimulation.bind(this);
this.onChangeUserAgent = this.onChangeUserAgent.bind(this);
this.onChangeViewportOrientation = this.onChangeViewportOrientation.bind(
this
);
this.onDeviceListUpdate = this.onDeviceListUpdate.bind(this);
this.onEditCustomDevice = this.onEditCustomDevice.bind(this);
this.onExit = this.onExit.bind(this);
this.onRemoveCustomDevice = this.onRemoveCustomDevice.bind(this);
this.onRemoveDeviceAssociation = this.onRemoveDeviceAssociation.bind(this);
this.doResizeViewport = this.doResizeViewport.bind(this);
this.onRotateViewport = this.onRotateViewport.bind(this);
this.onScreenshot = this.onScreenshot.bind(this);
this.onToggleLeftAlignment = this.onToggleLeftAlignment.bind(this);
this.onToggleReloadOnTouchSimulation = this.onToggleReloadOnTouchSimulation.bind(
this
);
this.onToggleReloadOnUserAgent = this.onToggleReloadOnUserAgent.bind(this);
this.onToggleUserAgentInput = this.onToggleUserAgentInput.bind(this);
this.onUpdateDeviceDisplayed = this.onUpdateDeviceDisplayed.bind(this);
this.onUpdateDeviceModal = this.onUpdateDeviceModal.bind(this);
}
componentWillUnmount() {
this.browser.removeEventListener("contextmenu", this.onContextMenu);
this.browser = null;
}
onAddCustomDevice(device) {
this.props.dispatch(addCustomDevice(device));
}
onBrowserContextMenu() {
// Update the position of remote browser so that makes the context menu to show at
// proper position before showing.
this.browser.frameLoader.requestUpdatePosition();
}
onChangeDevice(id, device, deviceType) {
// Resize the viewport first.
this.doResizeViewport(id, device.width, device.height);
// TODO: Bug 1332754: Move messaging and logic into the action creator so that the
// message is sent from the action creator and device property changes are sent from
// there instead of this function.
window.postMessage(
{
type: "change-device",
device,
viewport: device,
},
"*"
);
const orientation = getOrientation(device, device);
this.props.dispatch(changeViewportAngle(0, orientation.angle));
this.props.dispatch(changeDevice(id, device.name, deviceType));
this.props.dispatch(changePixelRatio(id, device.pixelRatio));
this.props.dispatch(changeUserAgent(device.userAgent));
this.props.dispatch(toggleTouchSimulation(device.touch));
}
onChangeNetworkThrottling(enabled, profile) {
window.postMessage(
{
type: "change-network-throttling",
enabled,
profile,
},
"*"
);
this.props.dispatch(changeNetworkThrottling(enabled, profile));
}
onChangePixelRatio(pixelRatio) {
window.postMessage(
{
type: "change-pixel-ratio",
pixelRatio,
},
"*"
);
this.props.dispatch(changePixelRatio(0, pixelRatio));
}
onChangeTouchSimulation(enabled) {
window.postMessage(
{
type: "change-touch-simulation",
enabled,
},
"*"
);
this.props.dispatch(toggleTouchSimulation(enabled));
}
onChangeUserAgent(userAgent) {
window.postMessage(
{
type: "change-user-agent",
userAgent,
},
"*"
);
this.props.dispatch(changeUserAgent(userAgent));
}
onChangeViewportOrientation(id, type, angle, isViewportRotated = false) {
window.postMessage(
{
type: "viewport-orientation-change",
orientationType: type,
angle,
isViewportRotated,
},
"*"
);
if (isViewportRotated) {
this.props.dispatch(changeViewportAngle(id, angle));
}
}
onDeviceListUpdate(devices) {
updatePreferredDevices(devices);
}
onEditCustomDevice(oldDevice, newDevice) {
// If the edited device is currently selected, then update its original association
// and reset UI state.
let viewport = this.props.viewports.find(
({ device }) => device === oldDevice.name
);
if (viewport) {
viewport = {
...viewport,
device: newDevice.name,
deviceType: "custom",
height: newDevice.height,
width: newDevice.width,
pixelRatio: newDevice.pixelRatio,
touch: newDevice.touch,
userAgent: newDevice.userAgent,
};
}
this.props.dispatch(editCustomDevice(viewport, oldDevice, newDevice));
}
onExit() {
window.postMessage({ type: "exit" }, "*");
}
onRemoveCustomDevice(device) {
// If the custom device is currently selected on any of the viewports,
// remove the device association and reset all the ui state.
for (const viewport of this.props.viewports) {
if (viewport.device === device.name) {
this.onRemoveDeviceAssociation(viewport.id);
}
}
this.props.dispatch(removeCustomDevice(device));
}
onRemoveDeviceAssociation(id) {
// TODO: Bug 1332754: Move messaging and logic into the action creator so that device
// property changes are sent from there instead of this function.
this.props.dispatch(removeDeviceAssociation(id));
this.props.dispatch(toggleTouchSimulation(false));
this.props.dispatch(changePixelRatio(id, 0));
this.props.dispatch(changeUserAgent(""));
}
doResizeViewport(id, width, height) {
// This is the setter function that we pass to Toolbar and Viewports
// so they can modify the viewport.
window.postMessage(
{
type: "viewport-resize",
width,
height,
},
"*"
);
this.props.dispatch(resizeViewport(id, width, height));
}
/**
* Dispatches the rotateViewport action creator. This utilized by the RDM toolbar as
* a prop.
*
* @param {Number} id
* The viewport ID.
*/
onRotateViewport(id) {
let currentDevice;
const viewport = this.props.viewports[id];
for (const type of this.props.devices.types) {
for (const device of this.props.devices[type]) {
if (viewport.device === device.name) {
currentDevice = device;
}
}
}
// If no device is selected, then assume the selected device's primary orientation is
// opposite of the viewport orientation.
if (!currentDevice) {
currentDevice = {
height: viewport.width,
width: viewport.height,
};
}
const currentAngle = Services.prefs.getIntPref(
"devtools.responsive.viewport.angle"
);
const angleToRotateTo = currentAngle === 90 ? 0 : 90;
const { type, angle } = getOrientation(
currentDevice,
viewport,
angleToRotateTo
);
this.onChangeViewportOrientation(id, type, angle, true);
this.props.dispatch(rotateViewport(id));
window.postMessage(
{
type: "viewport-resize",
height: viewport.width,
width: viewport.height,
},
"*"
);
}
onScreenshot() {
this.props.dispatch(takeScreenshot());
}
onToggleLeftAlignment() {
this.props.dispatch(toggleLeftAlignment());
window.postMessage(
{
type: "toggle-left-alignment",
leftAlignmentEnabled: this.props.leftAlignmentEnabled,
},
"*"
);
}
onToggleReloadOnTouchSimulation() {
this.props.dispatch(toggleReloadOnTouchSimulation());
}
onToggleReloadOnUserAgent() {
this.props.dispatch(toggleReloadOnUserAgent());
}
onToggleUserAgentInput() {
this.props.dispatch(toggleUserAgentInput());
}
onUpdateDeviceDisplayed(device, deviceType, displayed) {
this.props.dispatch(updateDeviceDisplayed(device, deviceType, displayed));
}
onUpdateDeviceModal(isOpen, modalOpenedFromViewport) {
this.props.dispatch(updateDeviceModal(isOpen, modalOpenedFromViewport));
window.postMessage({ type: "update-device-modal", isOpen }, "*");
}
render() {
const { devices, networkThrottling, screenshot, viewports } = this.props;
const {
onAddCustomDevice,
onChangeDevice,
onChangeNetworkThrottling,
onChangePixelRatio,
onChangeTouchSimulation,
onChangeUserAgent,
onDeviceListUpdate,
onEditCustomDevice,
onExit,
onRemoveCustomDevice,
onRemoveDeviceAssociation,
doResizeViewport,
onRotateViewport,
onScreenshot,
onToggleLeftAlignment,
onToggleReloadOnTouchSimulation,
onToggleReloadOnUserAgent,
onToggleUserAgentInput,
onUpdateDeviceDisplayed,
onUpdateDeviceModal,
} = this;
if (!viewports.length) {
return null;
}
const selectedDevice = viewports[0].device;
const selectedPixelRatio = viewports[0].pixelRatio;
let deviceAdderViewportTemplate = {};
if (devices.modalOpenedFromViewport !== null) {
deviceAdderViewportTemplate = viewports[devices.modalOpenedFromViewport];
}
return dom.div(
{ id: "app" },
Toolbar({
devices,
networkThrottling,
screenshot,
selectedDevice,
selectedPixelRatio,
viewport: viewports[0],
onChangeDevice,
onChangeNetworkThrottling,
onChangePixelRatio,
onChangeTouchSimulation,
onChangeUserAgent,
onExit,
onRemoveDeviceAssociation,
doResizeViewport,
onRotateViewport,
onScreenshot,
onToggleLeftAlignment,
onToggleReloadOnTouchSimulation,
onToggleReloadOnUserAgent,
onToggleUserAgentInput,
onUpdateDeviceModal,
}),
devices.isModalOpen
? DeviceModal({
deviceAdderViewportTemplate,
devices,
onAddCustomDevice,
onDeviceListUpdate,
onEditCustomDevice,
onRemoveCustomDevice,
onUpdateDeviceDisplayed,
onUpdateDeviceModal,
})
: null
);
}
}
const mapStateToProps = state => {
return {
...state,
leftAlignmentEnabled: state.ui.leftAlignmentEnabled,
};
};
module.exports = connect(mapStateToProps)(App);
|