devices.js (2690B)
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 5 "use strict"; 6 7 const { 8 ADD_DEVICE, 9 ADD_DEVICE_TYPE, 10 EDIT_DEVICE, 11 LOAD_DEVICE_LIST_START, 12 LOAD_DEVICE_LIST_ERROR, 13 LOAD_DEVICE_LIST_END, 14 REMOVE_DEVICE, 15 UPDATE_DEVICE_DISPLAYED, 16 UPDATE_DEVICE_MODAL, 17 } = require("resource://devtools/client/responsive/actions/index.js"); 18 19 const Types = require("resource://devtools/client/responsive/types.js"); 20 21 const INITIAL_DEVICES = { 22 isModalOpen: false, 23 listState: Types.loadableState.INITIALIZED, 24 modalOpenedFromViewport: null, 25 types: [], 26 }; 27 28 const reducers = { 29 [ADD_DEVICE](devices, { device, deviceType }) { 30 return { 31 ...devices, 32 [deviceType]: [...devices[deviceType], device], 33 }; 34 }, 35 36 [ADD_DEVICE_TYPE](devices, { deviceType }) { 37 return { 38 ...devices, 39 types: [...devices.types, deviceType], 40 [deviceType]: [], 41 }; 42 }, 43 44 [EDIT_DEVICE](devices, { oldDevice, newDevice, deviceType }) { 45 const index = devices[deviceType].indexOf(oldDevice); 46 if (index < 0) { 47 return devices; 48 } 49 50 devices[deviceType].splice(index, 1, newDevice); 51 52 return { 53 ...devices, 54 [deviceType]: [...devices[deviceType]], 55 }; 56 }, 57 58 [UPDATE_DEVICE_DISPLAYED](devices, { device, deviceType, displayed }) { 59 const newDevices = devices[deviceType].map(d => { 60 if (d == device) { 61 d.displayed = displayed; 62 } 63 64 return d; 65 }); 66 67 return { 68 ...devices, 69 [deviceType]: newDevices, 70 }; 71 }, 72 73 [LOAD_DEVICE_LIST_START](devices) { 74 return { 75 ...devices, 76 listState: Types.loadableState.LOADING, 77 }; 78 }, 79 80 [LOAD_DEVICE_LIST_ERROR](devices) { 81 return { 82 ...devices, 83 listState: Types.loadableState.ERROR, 84 }; 85 }, 86 87 [LOAD_DEVICE_LIST_END](devices) { 88 return { 89 ...devices, 90 listState: Types.loadableState.LOADED, 91 }; 92 }, 93 94 [REMOVE_DEVICE](devices, { device, deviceType }) { 95 const index = devices[deviceType].indexOf(device); 96 if (index < 0) { 97 return devices; 98 } 99 100 const list = [...devices[deviceType]]; 101 list.splice(index, 1); 102 103 return { 104 ...devices, 105 [deviceType]: list, 106 }; 107 }, 108 109 [UPDATE_DEVICE_MODAL](devices, { isOpen, modalOpenedFromViewport }) { 110 return { 111 ...devices, 112 isModalOpen: isOpen, 113 modalOpenedFromViewport, 114 }; 115 }, 116 }; 117 118 module.exports = function (devices = INITIAL_DEVICES, action) { 119 const reducer = reducers[action.type]; 120 if (!reducer) { 121 return devices; 122 } 123 return reducer(devices, action); 124 };