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

const ViewportDimension = createFactory(
  require("resource://devtools/client/responsive/components/ViewportDimension.js")
);

const {
  getStr,
} = require("resource://devtools/client/responsive/utils/l10n.js");
const Types = require("resource://devtools/client/responsive/types.js");

class DeviceForm extends PureComponent {
  static get propTypes() {
    return {
      formType: PropTypes.string.isRequired,
      device: PropTypes.shape(Types.device).isRequired,
      devices: PropTypes.shape(Types.devices).isRequired,
      onSave: PropTypes.func.isRequired,
      viewportTemplate: PropTypes.shape(Types.viewport).isRequired,
      onDeviceFormHide: PropTypes.func.isRequired,
    };
  }

  constructor(props) {
    super(props);

    const { height, width } = this.props.viewportTemplate;

    this.state = {
      height,
      width,
    };

    this.nameInputRef = createRef();
    this.pixelRatioInputRef = createRef();
    this.touchInputRef = createRef();
    this.userAgentInputRef = createRef();

    this.onChangeSize = this.onChangeSize.bind(this);
    this.onDeviceFormHide = this.onDeviceFormHide.bind(this);
    this.onDeviceFormSave = this.onDeviceFormSave.bind(this);
    this.onInputFocus = this.onInputFocus.bind(this);
    this.validateNameField = this.validateNameField.bind(this);
  }

  onChangeSize(_, width, height) {
    this.setState({
      width,
      height,
    });
  }

  onDeviceFormSave(e) {
    e.preventDefault();

    if (!this.pixelRatioInputRef.current.checkValidity()) {
      return;
    }

    if (
      !this.validateNameField(
        this.nameInputRef.current.value,
        this.props.device
      )
    ) {
      this.nameInputRef.current.setCustomValidity(
        getStr("responsive.deviceNameAlreadyInUse")
      );
      return;
    }

    this.props.onSave({
      name: this.nameInputRef.current.value.trim(),
      width: this.state.width,
      height: this.state.height,
      pixelRatio: parseFloat(this.pixelRatioInputRef.current.value),
      userAgent: this.userAgentInputRef.current.value,
      touch: this.touchInputRef.current.checked,
    });

    this.onDeviceFormHide();
  }

  onDeviceFormHide() {
    // Ensure that we have onDeviceFormHide before calling it.
    if (this.props.onDeviceFormHide) {
      this.props.onDeviceFormHide();
    }
  }

  onInputFocus(e) {
    // If the formType is "add", select all text in input field when focused.
    if (this.props.formType === "add") {
      e.target.select();
    }
  }

  /**
   * Validates the name field's value.
   *
   * @param  {String} value
   *         The input field value for the device name.
   * @return {Boolean} true if device name is valid, false otherwise.
   */
  validateNameField(value) {
    const nameFieldValue = value.trim();
    let isValidDeviceName = false;

    // If the formType is "add", then we just need to check if a custom device with that
    // same name exists.
    if (this.props.formType === "add") {
      isValidDeviceName = !this.props.devices.custom.find(
        device => device.name == nameFieldValue
      );
    } else {
      // Otherwise, the formType is "edit". We'd have to find another device that
      // already has the same name, but not itself, so:
      // Find the index of the device being edited. Use this index value to distinguish
      // between the device being edited from other devices in the list.
      const deviceIndex = this.props.devices.custom.findIndex(({ name }) => {
        return name === this.props.device.name;
      });

      isValidDeviceName = !this.props.devices.custom.find((d, index) => {
        return d.name === nameFieldValue && index !== deviceIndex;
      });
    }

    return isValidDeviceName;
  }

  render() {
    const { device, formType } = this.props;
    const { width, height } = this.state;

    return dom.form(
      { id: "device-form" },
      dom.label(
        { id: "device-form-name" },
        dom.span(
          { className: "device-form-label" },
          getStr("responsive.deviceAdderName")
        ),
        dom.input({
          defaultValue: device.name,
          ref: this.nameInputRef,
          onFocus: this.onInputFocus,
        })
      ),
      dom.label(
        { id: "device-form-size" },
        dom.span(
          { className: "device-form-label" },
          getStr("responsive.deviceAdderSize")
        ),
        ViewportDimension({
          viewport: { width, height },
          doResizeViewport: this.onChangeSize,
          onRemoveDeviceAssociation: () => {},
        })
      ),
      dom.label(
        { id: "device-form-pixel-ratio" },
        dom.span(
          { className: "device-form-label" },
          getStr("responsive.deviceAdderPixelRatio2")
        ),
        dom.input({
          type: "number",
          step: "any",
          defaultValue: device.pixelRatio,
          ref: this.pixelRatioInputRef,
          onFocus: this.onInputFocus,
        })
      ),
      dom.label(
        { id: "device-form-user-agent" },
        dom.span(
          { className: "device-form-label" },
          getStr("responsive.deviceAdderUserAgent2")
        ),
        dom.input({
          defaultValue: device.userAgent,
          ref: this.userAgentInputRef,
          onFocus: this.onInputFocus,
        })
      ),
      dom.label(
        { id: "device-form-touch" },
        dom.input({
          defaultChecked: device.touch,
          type: "checkbox",
          ref: this.touchInputRef,
        }),
        dom.span(
          { className: "device-form-label" },
          getStr("responsive.deviceAdderTouch2")
        )
      ),
      dom.div(
        { className: "device-form-buttons" },
        dom.button(
          { id: "device-form-save", onClick: this.onDeviceFormSave },
          formType === "add"
            ? getStr("responsive.deviceAdderSave")
            : getStr("responsive.deviceFormUpdate")
        ),
        dom.button(
          { id: "device-form-cancel", onClick: this.onDeviceFormHide },
          getStr("responsive.deviceAdderCancel")
        )
      )
    );
  }
}

module.exports = DeviceForm;