summaryrefslogtreecommitdiffstats
path: root/devtools/client/netmonitor/src/components/messages/Toolbar.js
blob: 7b71c9aa061ee99d47db58a1ecd7b2951677e31c (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
/* 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/. */

"use strict";

const {
  Component,
  createFactory,
} = require("resource://devtools/client/shared/vendor/react.js");
const {
  connect,
} = require("resource://devtools/client/shared/redux/visibility-handler-connect.js");
const PropTypes = require("resource://devtools/client/shared/vendor/react-prop-types.js");
const Actions = require("resource://devtools/client/netmonitor/src/actions/index.js");
const {
  CHANNEL_TYPE,
  FILTER_SEARCH_DELAY,
} = require("resource://devtools/client/netmonitor/src/constants.js");
const dom = require("resource://devtools/client/shared/vendor/react-dom-factories.js");
const {
  L10N,
} = require("resource://devtools/client/netmonitor/src/utils/l10n.js");
const { button, span, div } = dom;

// Components
const MessageFilterMenu = createFactory(
  require("resource://devtools/client/netmonitor/src/components/messages/MessageFilterMenu.js")
);
const SearchBox = createFactory(
  require("resource://devtools/client/shared/components/SearchBox.js")
);

// Localization
const MSG_TOOLBAR_CLEAR = L10N.getStr("netmonitor.ws.toolbar.clear");
const MSG_SEARCH_KEY_SHORTCUT = L10N.getStr(
  "netmonitor.ws.toolbar.filterFreetext.key"
);
const MSG_SEARCH_PLACE_HOLDER = L10N.getStr(
  "netmonitor.ws.toolbar.filterFreetext.label"
);

/**
 * MessagesPanel toolbar component.
 *
 * Toolbar contains a set of useful tools that clear the list of
 * existing messages as well as filter content.
 */
class Toolbar extends Component {
  static get propTypes() {
    return {
      searchboxRef: PropTypes.object.isRequired,
      toggleMessageFilterType: PropTypes.func.isRequired,
      toggleControlFrames: PropTypes.func.isRequired,
      clearMessages: PropTypes.func.isRequired,
      setMessageFilterText: PropTypes.func.isRequired,
      messageFilterType: PropTypes.string.isRequired,
      showControlFrames: PropTypes.bool.isRequired,
      channelType: PropTypes.string,
    };
  }

  componentWillUnmount() {
    const { setMessageFilterText } = this.props;
    setMessageFilterText("");
  }

  /**
   * Render a separator.
   */
  renderSeparator() {
    return span({ className: "devtools-separator" });
  }

  /**
   * Render a clear button.
   */
  renderClearButton(clearMessages) {
    return button({
      className:
        "devtools-button devtools-clear-icon message-list-clear-button",
      title: MSG_TOOLBAR_CLEAR,
      onClick: () => {
        clearMessages();
      },
    });
  }

  /**
   * Render the message filter menu button.
   */
  renderMessageFilterMenu() {
    const {
      messageFilterType,
      toggleMessageFilterType,
      showControlFrames,
      toggleControlFrames,
    } = this.props;

    return MessageFilterMenu({
      messageFilterType,
      toggleMessageFilterType,
      showControlFrames,
      toggleControlFrames,
    });
  }

  /**
   * Render filter Searchbox.
   */
  renderFilterBox(setMessageFilterText) {
    return SearchBox({
      delay: FILTER_SEARCH_DELAY,
      keyShortcut: MSG_SEARCH_KEY_SHORTCUT,
      placeholder: MSG_SEARCH_PLACE_HOLDER,
      type: "filter",
      ref: this.props.searchboxRef,
      onChange: setMessageFilterText,
    });
  }

  render() {
    const { clearMessages, setMessageFilterText, channelType } = this.props;
    const isWs = channelType === CHANNEL_TYPE.WEB_SOCKET;
    return div(
      {
        id: "netmonitor-toolbar-container",
        className: "devtools-toolbar devtools-input-toolbar",
      },
      this.renderClearButton(clearMessages),
      isWs ? this.renderSeparator() : null,
      isWs ? this.renderMessageFilterMenu() : null,
      this.renderSeparator(),
      this.renderFilterBox(setMessageFilterText)
    );
  }
}

module.exports = connect(
  state => ({
    messageFilterType: state.messages.messageFilterType,
    showControlFrames: state.messages.showControlFrames,
    channelType: state.messages.currentChannelType,
  }),
  dispatch => ({
    clearMessages: () => dispatch(Actions.clearMessages()),
    toggleMessageFilterType: filter =>
      dispatch(Actions.toggleMessageFilterType(filter)),
    toggleControlFrames: () => dispatch(Actions.toggleControlFrames()),
    setMessageFilterText: text => dispatch(Actions.setMessageFilterText(text)),
  })
)(Toolbar);