diff options
Diffstat (limited to 'devtools/client/responsive.html/components/dpr-selector.js')
-rw-r--r-- | devtools/client/responsive.html/components/dpr-selector.js | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/devtools/client/responsive.html/components/dpr-selector.js b/devtools/client/responsive.html/components/dpr-selector.js new file mode 100644 index 000000000..31b8db1c2 --- /dev/null +++ b/devtools/client/responsive.html/components/dpr-selector.js @@ -0,0 +1,131 @@ +/* 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 { DOM: dom, createClass, PropTypes, addons } = + require("devtools/client/shared/vendor/react"); + +const Types = require("../types"); +const { getStr, getFormatStr } = require("../utils/l10n"); + +const PIXEL_RATIO_PRESET = [1, 2, 3]; + +const createVisibleOption = value => + dom.option({ + value, + title: value, + key: value, + }, value); + +const createHiddenOption = value => + dom.option({ + value, + title: value, + hidden: true, + disabled: true, + }, value); + +module.exports = createClass({ + displayName: "DPRSelector", + + propTypes: { + devices: PropTypes.shape(Types.devices).isRequired, + displayPixelRatio: Types.pixelRatio.value.isRequired, + selectedDevice: PropTypes.string.isRequired, + selectedPixelRatio: PropTypes.shape(Types.pixelRatio).isRequired, + onChangePixelRatio: PropTypes.func.isRequired, + }, + + mixins: [ addons.PureRenderMixin ], + + getInitialState() { + return { + isFocused: false + }; + }, + + onFocusChange({type}) { + this.setState({ + isFocused: type === "focus" + }); + }, + + onSelectChange({ target }) { + this.props.onChangePixelRatio(+target.value); + }, + + render() { + let { + devices, + displayPixelRatio, + selectedDevice, + selectedPixelRatio, + } = this.props; + + let hiddenOptions = []; + + for (let type of devices.types) { + for (let device of devices[type]) { + if (device.displayed && + !hiddenOptions.includes(device.pixelRatio) && + !PIXEL_RATIO_PRESET.includes(device.pixelRatio)) { + hiddenOptions.push(device.pixelRatio); + } + } + } + + if (!PIXEL_RATIO_PRESET.includes(displayPixelRatio)) { + hiddenOptions.push(displayPixelRatio); + } + + let state = devices.listState; + let isDisabled = (state !== Types.deviceListState.LOADED) || (selectedDevice !== ""); + let selectorClass = ""; + let title; + + if (isDisabled) { + selectorClass += " disabled"; + title = getFormatStr("responsive.autoDPR", selectedDevice); + } else { + title = getStr("responsive.devicePixelRatio"); + + if (selectedPixelRatio.value) { + selectorClass += " selected"; + } + } + + if (this.state.isFocused) { + selectorClass += " focused"; + } + + let listContent = PIXEL_RATIO_PRESET.map(createVisibleOption); + + if (state == Types.deviceListState.LOADED) { + listContent = listContent.concat(hiddenOptions.map(createHiddenOption)); + } + + return dom.label( + { + id: "global-dpr-selector", + className: selectorClass, + title, + }, + "DPR", + dom.select( + { + value: selectedPixelRatio.value || displayPixelRatio, + disabled: isDisabled, + onChange: this.onSelectChange, + onFocus: this.onFocusChange, + onBlur: this.onFocusChange, + }, + ...listContent + ) + ); + }, + +}); |