devices.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. const { getJSON } = require("devtools/client/shared/getjson");
  6. const DEVICES_URL = "devtools.devices.url";
  7. const { LocalizationHelper } = require("devtools/shared/l10n");
  8. const L10N = new LocalizationHelper("devtools/client/locales/device.properties");
  9. /* This is a catalog of common web-enabled devices and their properties,
  10. * intended for (mobile) device emulation.
  11. *
  12. * The properties of a device are:
  13. * - name: brand and model(s).
  14. * - width: viewport width.
  15. * - height: viewport height.
  16. * - pixelRatio: ratio from viewport to physical screen pixels.
  17. * - userAgent: UA string of the device's browser.
  18. * - touch: whether it has a touch screen.
  19. * - firefoxOS: whether Firefox OS is supported.
  20. *
  21. * The device types are:
  22. * ["phones", "tablets", "laptops", "televisions", "consoles", "watches"].
  23. *
  24. * You can easily add more devices to this catalog from your own code (e.g. an
  25. * addon) like so:
  26. *
  27. * var myPhone = { name: "My Phone", ... };
  28. * require("devtools/client/shared/devices").addDevice(myPhone, "phones");
  29. */
  30. // Local devices catalog that addons can add to.
  31. let localDevices = {};
  32. // Add a device to the local catalog.
  33. function addDevice(device, type = "phones") {
  34. let list = localDevices[type];
  35. if (!list) {
  36. list = localDevices[type] = [];
  37. }
  38. list.push(device);
  39. }
  40. exports.addDevice = addDevice;
  41. // Remove a device from the local catalog.
  42. // returns `true` if the device is removed, `false` otherwise.
  43. function removeDevice(device, type = "phones") {
  44. let list = localDevices[type];
  45. if (!list) {
  46. return false;
  47. }
  48. let index = list.findIndex(item => device);
  49. if (index === -1) {
  50. return false;
  51. }
  52. list.splice(index, 1);
  53. return true;
  54. }
  55. exports.removeDevice = removeDevice;
  56. // Get the complete devices catalog.
  57. function getDevices() {
  58. // Fetch common devices from Mozilla's CDN.
  59. return getJSON(DEVICES_URL).then(devices => {
  60. for (let type in localDevices) {
  61. if (!devices[type]) {
  62. devices.TYPES.push(type);
  63. devices[type] = [];
  64. }
  65. devices[type] = localDevices[type].concat(devices[type]);
  66. }
  67. return devices;
  68. });
  69. }
  70. exports.getDevices = getDevices;
  71. // Get the localized string for a device type.
  72. function getDeviceString(deviceType) {
  73. return L10N.getStr("device." + deviceType);
  74. }
  75. exports.getDeviceString = getDeviceString;