grids.js (2551B)
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 UPDATE_GRID_COLOR, 9 UPDATE_GRID_HIGHLIGHTED, 10 UPDATE_GRIDS, 11 } = require("resource://devtools/client/inspector/grids/actions/index.js"); 12 13 const INITIAL_GRIDS = []; 14 15 const reducers = { 16 [UPDATE_GRID_COLOR](grids, { nodeFront, color }) { 17 const newGrids = grids.map(g => { 18 if (g.nodeFront === nodeFront) { 19 g = Object.assign({}, g, { color }); 20 } 21 22 return g; 23 }); 24 25 return newGrids; 26 }, 27 28 [UPDATE_GRID_HIGHLIGHTED](grids, { nodeFront, highlighted }) { 29 const maxHighlighters = Services.prefs.getIntPref( 30 "devtools.gridinspector.maxHighlighters" 31 ); 32 const highlightedNodeFronts = grids 33 .filter(g => g.highlighted) 34 .map(g => g.nodeFront); 35 let numHighlighted = highlightedNodeFronts.length; 36 37 // Get the total number of highlighted grids including the one that will be 38 // highlighted/unhighlighted. 39 if (!highlightedNodeFronts.includes(nodeFront) && highlighted) { 40 numHighlighted += 1; 41 } else if (highlightedNodeFronts.includes(nodeFront) && !highlighted) { 42 numHighlighted -= 1; 43 } 44 45 return grids.map(g => { 46 if (maxHighlighters === 1) { 47 // When there is only one grid highlighter available, only the given grid 48 // container nodeFront can be highlighted, and all the other grid containers 49 // are unhighlighted. 50 return Object.assign({}, g, { 51 highlighted: g.nodeFront === nodeFront && highlighted, 52 }); 53 } else if ( 54 numHighlighted === maxHighlighters && 55 g.nodeFront !== nodeFront 56 ) { 57 // The maximum number of highlighted grids have been reached. Disable all the 58 // other non-highlighted grids. 59 return Object.assign({}, g, { 60 disabled: !g.highlighted, 61 }); 62 } else if (g.nodeFront === nodeFront) { 63 // This is the provided grid nodeFront to highlight/unhighlight. 64 return Object.assign({}, g, { 65 disabled: false, 66 highlighted, 67 }); 68 } 69 70 return Object.assign({}, g, { 71 disabled: false, 72 }); 73 }); 74 }, 75 76 [UPDATE_GRIDS](_, { grids }) { 77 return grids; 78 }, 79 }; 80 81 module.exports = function (grids = INITIAL_GRIDS, action) { 82 const reducer = reducers[action.type]; 83 if (!reducer) { 84 return grids; 85 } 86 return reducer(grids, action); 87 };