ui.js (6950B)
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 CLEAR_REQUESTS, 9 OPEN_NETWORK_DETAILS, 10 OPEN_ACTION_BAR, 11 RESIZE_NETWORK_DETAILS, 12 ENABLE_PERSISTENT_LOGS, 13 DISABLE_BROWSER_CACHE, 14 OPEN_STATISTICS, 15 REMOVE_SELECTED_CUSTOM_REQUEST, 16 RESET_COLUMNS, 17 RESPONSE_HEADERS, 18 SELECT_DETAILS_PANEL_TAB, 19 SELECT_ACTION_BAR_TAB, 20 SEND_CUSTOM_REQUEST, 21 SELECT_REQUEST, 22 TOGGLE_COLUMN, 23 WATERFALL_RESIZE, 24 PANELS, 25 MIN_COLUMN_WIDTH, 26 SET_COLUMNS_WIDTH, 27 SET_HEADERS_URL_PREVIEW_EXPANDED, 28 SET_DEFAULT_RAW_RESPONSE, 29 } = require("resource://devtools/client/netmonitor/src/constants.js"); 30 31 const cols = { 32 status: true, 33 method: true, 34 domain: true, 35 file: true, 36 path: false, 37 url: false, 38 protocol: false, 39 scheme: false, 40 remoteip: false, 41 initiator: true, 42 type: true, 43 cookies: false, 44 setCookies: false, 45 transferred: true, 46 contentSize: true, 47 priority: false, 48 startTime: false, 49 endTime: false, 50 responseTime: false, 51 duration: false, 52 latency: false, 53 waterfall: true, 54 }; 55 56 function Columns() { 57 return Object.assign( 58 cols, 59 RESPONSE_HEADERS.reduce( 60 (acc, header) => Object.assign(acc, { [header]: false }), 61 {} 62 ) 63 ); 64 } 65 66 function ColumnsData() { 67 const defaultColumnsData = JSON.parse( 68 Services.prefs 69 .getDefaultBranch(null) 70 .getCharPref("devtools.netmonitor.columnsData") 71 ); 72 return new Map(defaultColumnsData.map(i => [i.name, i])); 73 } 74 75 function UI(initialState = {}) { 76 return { 77 columns: Columns(), 78 columnsData: ColumnsData(), 79 detailsPanelSelectedTab: PANELS.HEADERS, 80 networkDetailsOpen: false, 81 networkDetailsWidth: null, 82 networkDetailsHeight: null, 83 persistentLogsEnabled: Services.prefs.getBoolPref( 84 "devtools.netmonitor.persistlog" 85 ), 86 defaultRawResponse: Services.prefs.getBoolPref( 87 "devtools.netmonitor.ui.default-raw-response", 88 false 89 ), 90 browserCacheDisabled: Services.prefs.getBoolPref("devtools.cache.disabled"), 91 slowLimit: Services.prefs.getIntPref("devtools.netmonitor.audits.slow"), 92 statisticsOpen: false, 93 waterfallWidth: null, 94 networkActionOpen: false, 95 selectedActionBarTabId: null, 96 shouldExpandHeadersUrlPreview: false, 97 ...initialState, 98 }; 99 } 100 101 function resetColumns(state) { 102 return { 103 ...state, 104 columns: Columns(), 105 columnsData: ColumnsData(), 106 }; 107 } 108 109 function resizeWaterfall(state, action) { 110 if (state.waterfallWidth == action.width) { 111 return state; 112 } 113 return { 114 ...state, 115 waterfallWidth: action.width, 116 }; 117 } 118 119 function openNetworkDetails(state, action) { 120 if (state.networkDetailsOpen == action.open) { 121 return state; 122 } 123 return { 124 ...state, 125 networkDetailsOpen: action.open, 126 }; 127 } 128 129 function openNetworkAction(state, action) { 130 if (state.networkActionOpen == action.open) { 131 return state; 132 } 133 return { 134 ...state, 135 networkActionOpen: action.open, 136 }; 137 } 138 139 function resizeNetworkDetails(state, action) { 140 if ( 141 state.networkDetailsWidth == action.width && 142 state.networkDetailsHeight == action.height 143 ) { 144 return state; 145 } 146 return { 147 ...state, 148 networkDetailsWidth: action.width, 149 networkDetailsHeight: action.height, 150 }; 151 } 152 153 function enablePersistentLogs(state, action) { 154 if (action.persistentLogsEnabled == action.enabled) { 155 return state; 156 } 157 return { 158 ...state, 159 persistentLogsEnabled: action.enabled, 160 }; 161 } 162 163 function disableBrowserCache(state, action) { 164 if (state.browserCacheDisabled == action.disabled) { 165 return state; 166 } 167 return { 168 ...state, 169 browserCacheDisabled: action.disabled, 170 }; 171 } 172 173 function openStatistics(state, action) { 174 if (state.statisticsOpen == action.open) { 175 return state; 176 } 177 return { 178 ...state, 179 statisticsOpen: action.open, 180 }; 181 } 182 183 function setDetailsPanelTab(state, action) { 184 if (state.detailsPanelSelectedTab == action.id) { 185 return state; 186 } 187 return { 188 ...state, 189 detailsPanelSelectedTab: action.id, 190 }; 191 } 192 193 function setActionBarTab(state, action) { 194 if (state.selectedActionBarTabId == action.id) { 195 return state; 196 } 197 return { 198 ...state, 199 selectedActionBarTabId: action.id, 200 }; 201 } 202 203 function setHeadersUrlPreviewExpanded(state, action) { 204 if (state.shouldExpandHeadersUrlPreview == action.expanded) { 205 return state; 206 } 207 return { 208 ...state, 209 shouldExpandHeadersUrlPreview: action.expanded, 210 }; 211 } 212 213 function toggleColumn(state, action) { 214 const { column } = action; 215 216 if (!state.columns.hasOwnProperty(column)) { 217 return state; 218 } 219 220 return { 221 ...state, 222 columns: { 223 ...state.columns, 224 [column]: !state.columns[column], 225 }, 226 }; 227 } 228 229 function setColumnsWidth(state, action) { 230 const { widths } = action; 231 const columnsData = new Map(state.columnsData); 232 233 widths.forEach(col => { 234 let data = columnsData.get(col.name); 235 if (!data) { 236 data = { 237 name: col.name, 238 minWidth: MIN_COLUMN_WIDTH, 239 }; 240 } 241 columnsData.set(col.name, { 242 ...data, 243 width: col.width, 244 }); 245 }); 246 247 return { 248 ...state, 249 columnsData, 250 }; 251 } 252 253 function setDefaultRawResponse(state, action) { 254 return { 255 ...state, 256 defaultRawResponse: action.enabled, 257 }; 258 } 259 260 function ui(state = UI(), action) { 261 switch (action.type) { 262 case CLEAR_REQUESTS: 263 return openNetworkDetails(state, { open: false }); 264 case OPEN_NETWORK_DETAILS: 265 return openNetworkDetails(state, action); 266 case RESIZE_NETWORK_DETAILS: 267 return resizeNetworkDetails(state, action); 268 case ENABLE_PERSISTENT_LOGS: 269 return enablePersistentLogs(state, action); 270 case DISABLE_BROWSER_CACHE: 271 return disableBrowserCache(state, action); 272 case OPEN_STATISTICS: 273 return openStatistics(state, action); 274 case RESET_COLUMNS: 275 return resetColumns(state); 276 case REMOVE_SELECTED_CUSTOM_REQUEST: 277 return openNetworkDetails(state, { open: true }); 278 case SEND_CUSTOM_REQUEST: 279 return openNetworkDetails(state, { open: false }); 280 case SELECT_DETAILS_PANEL_TAB: 281 return setDetailsPanelTab(state, action); 282 case SELECT_ACTION_BAR_TAB: 283 return setActionBarTab(state, action); 284 case SELECT_REQUEST: 285 return openNetworkDetails(state, { open: true }); 286 case TOGGLE_COLUMN: 287 return toggleColumn(state, action); 288 case WATERFALL_RESIZE: 289 return resizeWaterfall(state, action); 290 case SET_COLUMNS_WIDTH: 291 return setColumnsWidth(state, action); 292 case OPEN_ACTION_BAR: 293 return openNetworkAction(state, action); 294 case SET_HEADERS_URL_PREVIEW_EXPANDED: 295 return setHeadersUrlPreviewExpanded(state, action); 296 case SET_DEFAULT_RAW_RESPONSE: 297 return setDefaultRawResponse(state, action); 298 default: 299 return state; 300 } 301 } 302 303 module.exports = { 304 Columns, 305 ColumnsData, 306 UI, 307 ui, 308 };